feat(mcp): реализовать серверную часть MCP, адаптеры, сервисы и скрипты приёмки
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7380f18f15
commit
18c0a52171
1565 files changed
+175806
-5772
No files matched your search
@@ -1,5 +1,33 @@
|
|||||||
# PostgreSQL connection string
|
# PostgreSQL connection string
|
||||||
DATABASE_URL=postgres://mmapp:mmapp@127.0.0.1:5432/mmapp
|
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=<operator-approved SHA-256 of request bytes>
|
||||||
|
# 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=<confirmed activation SHA-256>
|
||||||
|
# Optional operator-approved logical schema SHA-256 from db:inspect-schema.
|
||||||
|
# DATABASE_RUNTIME_SCHEMA_SHA256=<approved schema SHA-256>
|
||||||
|
# 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=<operator-approved SHA-256 of approval bytes>
|
||||||
|
# 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=<approved archive SHA-256>
|
||||||
|
# DATABASE_RESTORE_DATA_SHA256=<approved data SHA-256>
|
||||||
|
# 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)
|
# SQLite file used only for one-shot boot ETL (empty PG + existing mikrotik.db)
|
||||||
DATABASE_PATH=./mikrotik.db
|
DATABASE_PATH=./mikrotik.db
|
||||||
@@ -17,5 +45,37 @@ AUTH_REQUIRED=false
|
|||||||
AUTH_JWT_SECRET=dev-secret-change-me
|
AUTH_JWT_SECRET=dev-secret-change-me
|
||||||
AUTH_ISSUER=https://auth.shnt.top
|
AUTH_ISSUER=https://auth.shnt.top
|
||||||
AUTH_PORTAL_URL=http://localhost:5175
|
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.
|
# Optional: enabled by the NetFlow agent Compose installer. Linux Unix socket only.
|
||||||
# FLOW_AGENT_SOCKET=/run/mm-netflow-host/agent.sock
|
# 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
|
||||||
@@ -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/node_modules ./node_modules
|
||||||
COPY --from=build /app/packages/contracts ./packages/contracts
|
COPY --from=build /app/packages/contracts ./packages/contracts
|
||||||
COPY --from=build /app/backend/dist ./backend/dist
|
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/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 --from=build /app/backend/drizzle ./backend/drizzle
|
||||||
COPY deploy/netflow-agent/install.sh ./deploy/netflow-agent/install.sh
|
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
|
# Nested install from lockfile (dotenv etc.) — ESM resolves from /app/backend/dist → ../node_modules
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
+172
-25
@@ -1,48 +1,190 @@
|
|||||||
{
|
{
|
||||||
"name": "mikrotik-manager-backend",
|
"name": "mikrotik-manager-backend",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "MikroTik Manager backend — Fastify + Drizzle + PostgreSQL",
|
"description": "MikroTik Manager backend — Fastify + Drizzle + PostgreSQL",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"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: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",
|
"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: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",
|
"start": "node dist/index.js",
|
||||||
"db:generate": "drizzle-kit generate",
|
"db:generate": "drizzle-kit generate",
|
||||||
"db:migrate": "drizzle-kit migrate",
|
"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:studio": "drizzle-kit studio",
|
||||||
"db:migrate-from-sqlite": "tsx src/scripts/migrate-sqlite-to-pg.ts",
|
"db:migrate-from-sqlite": "tsx src/scripts/migrate-sqlite-to-pg.ts",
|
||||||
"facts:rebuild": "tsx src/scripts/rebuild-flow-facts.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: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:exec": "tsx src/services/mikrotik-logs.test.ts && tsx src/routes/exec.test.ts && tsx src/plugins/auth.exec.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: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: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: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: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-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: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 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: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: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: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/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: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/ros-file-contents.test.ts && tsx src/services/mikrotik-response.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: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: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",
|
"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-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: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": "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: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/routes/traffic-flow-purge.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",
|
"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: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: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: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/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: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: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: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 src/routes/server-secrets-sql.test.ts"
|
"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": {
|
"dependencies": {
|
||||||
"@aws-sdk/client-s3": "^3.888.0",
|
"@aws-sdk/client-s3": "^3.888.0",
|
||||||
@@ -50,6 +192,10 @@
|
|||||||
"@fastify/jwt": "^10.2.2",
|
"@fastify/jwt": "^10.2.2",
|
||||||
"@fastify/type-provider-zod": "^1.0.0",
|
"@fastify/type-provider-zod": "^1.0.0",
|
||||||
"@mmapp/contracts": "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",
|
"acme-client": "^5.4.0",
|
||||||
"better-sqlite3": "^12.9.0",
|
"better-sqlite3": "^12.9.0",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
@@ -66,6 +212,7 @@
|
|||||||
"@types/node": "^22.15.3",
|
"@types/node": "^22.15.3",
|
||||||
"@types/pg": "^8.23.1",
|
"@types/pg": "^8.23.1",
|
||||||
"drizzle-kit": "^0.31.10",
|
"drizzle-kit": "^0.31.10",
|
||||||
|
"embedded-postgres": "18.4.0-beta.17",
|
||||||
"jose": "^6.2.11",
|
"jose": "^6.2.11",
|
||||||
"pino-pretty": "^13.1.3",
|
"pino-pretty": "^13.1.3",
|
||||||
"tsx": "^4.19.3",
|
"tsx": "^4.19.3",
|
||||||
|
|||||||
@@ -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 }));
|
||||||
@@ -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}))
|
||||||
@@ -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 }))
|
||||||
@@ -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}))
|
||||||
@@ -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()
|
||||||
@@ -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
|
||||||
@@ -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()
|
||||||
@@ -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 })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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/<observed-native-id>'], 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -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/);
|
||||||
|
});
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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/);
|
||||||
|
});
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
@@ -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)); }
|
||||||
|
});
|
||||||
@@ -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})
|
||||||
|
}
|
||||||
@@ -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)) }
|
||||||
|
})
|
||||||
@@ -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)}}})
|
||||||
@@ -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))
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
} },
|
||||||
|
] })
|
||||||
|
}
|
||||||
@@ -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')
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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')])
|
||||||
|
})
|
||||||
@@ -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()
|
||||||
@@ -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)
|
||||||
@@ -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()
|
||||||
|
|
||||||
@@ -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)
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -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}))
|
||||||
|
}
|
||||||
@@ -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))
|
||||||
|
})
|
||||||
@@ -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<void>){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<string,unknown>={},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<void>)(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<unknown>)=>{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<string,unknown>={}
|
||||||
|
const dependencies:Record<string,unknown>={'./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<Record<string,unknown>>
|
||||||
|
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<string,unknown>)=>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<string,unknown>;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']")<source.indexOf("const flags="));assert(source.includes("import('./writer-profile/maintenance-resolution.mjs')"))
|
||||||
|
})
|
||||||
|
|
||||||
|
test('existing protectedDirectory owner contract refuses a foreign CLI UID before SQL or writes',()=>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)
|
||||||
|
}))
|
||||||
@@ -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',
|
||||||
|
])
|
||||||
@@ -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<typeof requestSchema>
|
||||||
|
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<string, string>()
|
||||||
|
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 })
|
||||||
|
}
|
||||||
@@ -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 }
|
||||||
|
}
|
||||||
@@ -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}) }
|
||||||
|
})
|
||||||
@@ -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<string,string>()
|
||||||
|
for(let i=1;i<args.length;i+=2){assert(args[i].startsWith('--')&&flags.includes(args[i].slice(2))&&!values.has(args[i].slice(2))&&args[i+1]);values.set(args[i].slice(2),args[i+1])}
|
||||||
|
const get=(name:string)=>values.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<typeof collectCatalogue>[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()<deadlineAt)
|
||||||
|
const {archive,...receipt}=result
|
||||||
|
const base={...receipt.source,archiveSha256:sha(archive),storageSha256:receipt.snapshot.sha256}
|
||||||
|
assert(!legacyCustody||receipt.lineageSnapshot)
|
||||||
|
const body=legacyCustody?{format:'legacy-probe-custody-approval-v1',...base,lineageSha256:receipt.lineageSnapshot.sha256}
|
||||||
|
:{format:'legacy-probe-archive-approval-v1',...base}
|
||||||
|
const domain=legacyCustody?LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN:LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN
|
||||||
|
const signedApproval=sourceBindingBytes({body,signature:sign(null,Buffer.from(domain+sourceBindingCanonical(body)),key).toString('base64url')})
|
||||||
|
const dumpFile='legacy-probe.dump',approvalFile='legacy-probe-approval.json'
|
||||||
|
await writeFile(path.join(archiveDirectory,dumpFile),archive,{flag:'wx',mode:0o600})
|
||||||
|
await writeFile(path.join(archiveDirectory,approvalFile),signedApproval,{flag:'wx',mode:0o600})
|
||||||
|
return {...receipt,event:legacyCustody?'database-legacy-custody':'database-legacy-export',outcome:'exported',dumpFile,approvalFile,archiveSha256:body.archiveSha256,approvalSha256:sha(signedApproval)}
|
||||||
|
})
|
||||||
|
const result=await active()
|
||||||
|
if(writes||legacyInspection||catalogueInspection)return result
|
||||||
|
assert.equal(result.event,'application-writer-profile-preflight')
|
||||||
|
return {...result,event:'application-writer-profile-source-preflight',sourceActivationMatched:true}
|
||||||
|
}
|
||||||
|
if(process.argv[1]&&import.meta.url===pathToFileURL(path.resolve(process.argv[1])).href){
|
||||||
|
try{const result=await main(process.argv.slice(2));console.log(JSON.stringify(result));if(result.cleanupUnconfirmed||result.journalUnconfirmed)process.exitCode=2}
|
||||||
|
catch(error){
|
||||||
|
const sourceCommand=sourceCommands.includes(process.argv[2])
|
||||||
|
const command=process.argv[2], code=(error as NodeJS.ErrnoException|null)?.code
|
||||||
|
const unknown=sourceCommand&&code==='SOURCE_ACTIVATION_TERMINAL_UNKNOWN'||['registry-enroll','restore-profile-enroll'].includes(command)&&code==='WRITER_REGISTRY_TERMINAL_UNKNOWN'||
|
||||||
|
command==='marker-fence-enroll'&&code==='MARKER_FENCE_TERMINAL_UNKNOWN'||command==='legacy-handoff-execute'&&code==='LEGACY_HANDOFF_EXECUTION_UNKNOWN'||
|
||||||
|
command==='legacy-handoff-adopt'&&code==='LEGACY_HANDOFF_ADOPTION_UNKNOWN'||command==='legacy-handoff-release'&&code==='LEGACY_HANDOFF_RELEASE_UNKNOWN'||
|
||||||
|
command==='source58-foundation-enroll'&&code==='SOURCE58_FOUNDATION_UNKNOWN'||command==='source57-admission-enroll'&&code==='SOURCE57_ADMISSION_UNKNOWN'
|
||||||
|
console.error(JSON.stringify({event:sourceCommand?(unknown?'database-source-activation-unknown':'database-source-activation-refused'):
|
||||||
|
command==='registry-inspect'?'application-writer-registry-inspection-refused':
|
||||||
|
command==='registry-enroll'?(unknown?'application-writer-registry-enrollment-unknown':'application-writer-registry-enrollment-refused'):
|
||||||
|
command==='restore-profile-enroll'?(unknown?'application-writer-restore-profile-enrollment-unknown':'application-writer-restore-profile-enrollment-refused'):
|
||||||
|
command==='source-catalogue-inspect'?'application-writer-source-catalogue-inspection-refused':
|
||||||
|
command==='marker-fence-enroll'?(unknown?'application-writer-marker-fence-enrollment-unknown':'application-writer-marker-fence-enrollment-refused'):
|
||||||
|
command==='legacy-export'?'database-legacy-export-refused':
|
||||||
|
command==='legacy-custody'?'database-legacy-custody-refused':
|
||||||
|
command==='legacy-handoff-inspect'?'database-legacy-handoff-inspection-refused':
|
||||||
|
command==='legacy-cold-verify'?'database-legacy-cold-proof-refused':
|
||||||
|
command==='legacy-handoff-execute'?(unknown?'database-legacy-handoff-execution-unknown':'database-legacy-handoff-execution-refused'):
|
||||||
|
command==='legacy-handoff-adopt'?(unknown?'database-legacy-handoff-adoption-unknown':'database-legacy-handoff-adoption-refused'):
|
||||||
|
command==='legacy-handoff-release'?(unknown?'database-legacy-handoff-release-unknown':'database-legacy-handoff-release-refused'):
|
||||||
|
command==='legacy-control-reference'?'database-legacy-control-reference-refused':
|
||||||
|
command==='legacy-source58-preflight'?'database-legacy-source58-preflight-refused':
|
||||||
|
command==='source58-foundation-enroll'?(unknown?'database-source58-foundation-unknown':'database-source58-foundation-refused'):
|
||||||
|
command==='source57-admission-enroll'?(unknown?'database-source57-admission-unknown':'database-source57-admission-refused'):
|
||||||
|
['maintenance-inspect','maintenance-resolve'].includes(process.argv[2])?'database-maintenance-command-refused':process.argv[2]==='legacy-inspect'?'database-legacy-inspection-refused':process.argv[2]==='source-preflight'?'application-writer-profile-source-preflight-refused':'application-writer-profile-publication-refused',automaticReplay:false,inspectPendingBeforeRetry:true,readyAuthorized:false}));process.exitCode=unknown?2:1
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import test from 'node:test'
|
||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import {generateKeyPairSync,createHash,X509Certificate,sign} from 'node:crypto'
|
||||||
|
import {rootCertificates} from 'node:tls'
|
||||||
|
import {createRequire} from 'node:module'
|
||||||
|
import {approval,upgradeApproval,reconciliationApproval,poolOptions,STARTUP_OPTIONS,assertJsDriverEnvironment,REBIND58_APPROVAL_FORMAT} from './writer-profile/operator-contract.mjs'
|
||||||
|
import {bytes,canonical,POLICY_DOMAIN,signedEnvelope} from '../src/db/application-writer-profile-format.ts'
|
||||||
|
const hash=(v:Buffer|string)=>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}))
|
||||||
|
})
|
||||||
@@ -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<unknown>,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<PreflightDiagnostics>
|
||||||
|
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<T>(source:string,modules:Record<string,unknown>):T{
|
||||||
|
const exports:Record<string,unknown>={};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<string,Buffer>()
|
||||||
|
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<unknown>)=>{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<PublisherModule>(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<string,unknown>={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<unknown>)=>{
|
||||||
|
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<unknown>,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<OperatorModule>(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<OperatorModule>(cli,modules).main(args),/Pool cleanup unconfirmed/)
|
||||||
|
failEnd=false;asyncError=true;await assert.rejects(compile<OperatorModule>(cli,modules).main(args),/Publisher terminal unconfirmed/)
|
||||||
|
asyncError=false;result.platformProtectionVerified=false;await assert.rejects(compile<OperatorModule>(cli,modules).main(args),/Publisher terminal unconfirmed/)
|
||||||
|
result.platformProtectionVerified=true;protectedRefused=true;const before=retainedPools.length
|
||||||
|
await assert.rejects(compile<OperatorModule>(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<OperatorModule>(cli,modules).main(commandArgs),/Protected source refused/)
|
||||||
|
assert.equal(retainedPools.length,poolCount)
|
||||||
|
protectedRefused=false
|
||||||
|
events.length=0;reads.length=0
|
||||||
|
const terminal=await compile<OperatorModule>(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<OperatorModule>(cli,modules).main(activeArgs),/Activation refused/)
|
||||||
|
assert.equal(retainedPools.length,beforeActivation)
|
||||||
|
assert.deepEqual(events,['approval','transport','protected-source','activation-admission'])
|
||||||
|
await assert.rejects(compile<OperatorModule>(cli,modules).main(['publish',...args.slice(1)]))
|
||||||
|
await assert.rejects(compile<OperatorModule>(cli,modules).main(activated('publish')), /Activation refused/)
|
||||||
|
assert.equal(retainedPools.length,beforeActivation)
|
||||||
|
activationRefused=false;events.length=0;reads.length=0
|
||||||
|
const active=await compile<OperatorModule>(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<OperatorModule>(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<OperatorModule>(cli,modules).main(legacyArgs.slice(0,-2)))
|
||||||
|
activationRefused=true;await assert.rejects(compile<OperatorModule>(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<OperatorModule>(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<OperatorModule>(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<OperatorModule>(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<OperatorModule>(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<OperatorModule>(cli,modules).main(['reconcile',...args.slice(1)])
|
||||||
|
assert.equal(historical.outcome,'confirmed');assert.equal(historical.readyAuthorized,false)
|
||||||
|
assert.deepEqual(catalogueCalls,[])
|
||||||
|
})
|
||||||
|
|
||||||
@@ -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()})
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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()}
|
||||||
|
}
|
||||||
@@ -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}
|
||||||
|
}
|
||||||
@@ -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}
|
||||||
|
}
|
||||||
@@ -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?.()
|
||||||
|
}
|
||||||
@@ -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<string, Record<string, string>[]> = {
|
||||||
|
'/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<void>(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<ReturnType<typeof buildApp>> | undefined
|
||||||
|
let child: ReturnType<typeof spawn> | undefined
|
||||||
|
let childExit: Promise<number> | 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<number>((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<void>((done, reject) => router.close(error => error ? reject(error) : done())) } }
|
||||||
|
}
|
||||||
@@ -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?.() }
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 }
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
@@ -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<void>(r => listener.listen(0, "127.0.0.1", r)); const port = (listener.address() as { port: number }).port
|
||||||
|
await new Promise<void>(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<ReturnType<typeof buildApp>> | undefined, child: ReturnType<typeof spawn> | undefined
|
||||||
|
const credentials: string[] = []
|
||||||
|
const bridgePids = new Set<number>()
|
||||||
|
let childExit: Promise<number> | undefined
|
||||||
|
let releaseLogRead: (() => void) | undefined
|
||||||
|
let logReadCompleted: Promise<void> | undefined
|
||||||
|
let revocationOwnerId: string | undefined
|
||||||
|
const revokedLogRequests: { connectionId: string; tool: string }[] = []
|
||||||
|
const originalConnect = pool.connect
|
||||||
|
const delay = (ms: number) => new Promise<void>(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<string, unknown>, 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<number>(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<void>(resolve => { completed = resolve })
|
||||||
|
const hold = new Promise<void>(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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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))
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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}
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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}
|
||||||
|
}
|
||||||
@@ -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}
|
||||||
|
}
|
||||||
@@ -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}))
|
||||||
|
}
|
||||||
@@ -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})
|
||||||
|
}
|
||||||
@@ -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 })
|
||||||
@@ -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')
|
||||||
|
}
|
||||||
@@ -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')
|
||||||
@@ -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
|
||||||
@@ -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})}
|
||||||
@@ -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<void>(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<void>(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<unknown[]>}},localRequire=createRequire(import.meta.url)
|
||||||
|
const mocks:Record<string,unknown>={'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')
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
@@ -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=<known script>, 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' : rouLine truncated
|
||||||
|
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)
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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)
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
})
|
||||||
@@ -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])=>a<b?-1:a>b?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')
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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<StreamMetrics>;authority:false}
|
||||||
|
const rank:Record<CatalogueKind,number>={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<string,unknown>{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<CatalogueRecord>{
|
||||||
|
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||part<count-1&&payload.length!==FRAME_LIMITS.chunkBytes)throw Error('Catalogue order/chunk continuity refused')
|
||||||
|
const k=kind as CatalogueKind
|
||||||
|
if(part===0){if(this.previous&&(rank[k]<rank[this.previous.kind]||rank[k]===rank[this.previous.kind]&&Buffer.compare(Buffer.from(identity),Buffer.from(this.previous.identity))<=0))throw Error('Strict catalogue identity order required');this.currentKind=k;this.currentIdentity=identity;this.expectedParts=count}
|
||||||
|
if(k!==this.currentKind||identity!==this.currentIdentity||count!==this.expectedParts)throw Error('Changed record framing refused')
|
||||||
|
this.parts.push(Buffer.from(payload));this.bytes+=payload.length;pageBytes+=payload.length;this.metrics.chunks++;this.metrics.payloadBytes+=payload.length
|
||||||
|
// Text protocol bytea uses hex. DataRow header + six field lengths + actual
|
||||||
|
// other text field sizes; RowDescription/ReadyForQuery/JS heap are separate.
|
||||||
|
this.metrics.estimatedDataRowBytes+=31+Buffer.byteLength(id)+String(part).length+String(count).length+Buffer.byteLength(k)+Buffer.byteLength(identity)+2+2*payload.length
|
||||||
|
if(this.bytes>FRAME_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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<Record<LogicalRole,Readonly<{name:string;signedOid:string}>>>
|
||||||
|
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<Record<LogicalRole,string>> {
|
||||||
|
exact(Object.keys(input).sort(),[...ROLE_KEYS].sort(),'binding roles')
|
||||||
|
const names=new Set<string>(),oids=new Set<string>(),result={} as Record<LogicalRole,string>
|
||||||
|
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<Record<LogicalRole,string>>, template:Readonly<Inventory>):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<Record<LogicalRole,string>>;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<string>();private readonly leaves=new Set<string>();private readonly templates=new Map<string,{table:Row;groups:Inventory}>()
|
||||||
|
private readonly topology:Expanded44TopologyBudgetData;private counts?:Row
|
||||||
|
private readonly policy:ReturnType<typeof catalogueTopologyPolicy>
|
||||||
|
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}}
|
||||||
|
}
|
||||||
@@ -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')
|
||||||
|
}
|
||||||
@@ -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<string,Json>
|
||||||
|
export type Inventory=Record<string,Row[]>
|
||||||
|
export const GROUPS=['columns','triggers','constraints','indexes','roleGrants','inheritance'] as const
|
||||||
|
export type CatalogueEdition = 44 | 56 | 57 | 58
|
||||||
|
export const GLOBALS=['database','effectiveDatabase','effectiveFunctions','effectiveSchemas','effectiveSequences','events','functions','identities','memberships','parameters','roles','schemas','settings','sequences'] as const
|
||||||
|
export const sorted=(rows:Row[])=>rows.slice().sort((a,b)=>canonical(a)<canonical(b)?-1:canonical(a)>canonical(b)?1:0)
|
||||||
|
export const omit=(row:Row,names:readonly string[]):Row=>Object.fromEntries(Object.entries(row).filter(([key])=>!names.includes(key)))
|
||||||
|
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)]))
|
||||||
|
}
|
||||||
@@ -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}}
|
||||||
|
}
|
||||||
@@ -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<string,unknown>
|
||||||
|
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<string,unknown>[]} >}
|
||||||
|
type CatalogueInstallation = {roleBindings:ConstructorParameters<typeof Fixed44CatalogueValueBridgeData>[0]}
|
||||||
|
assert.equal(FIXED44_CATALOGUE_BUILD_SQL.split('LIMIT 104').length,2)
|
||||||
|
const fixed56Sql = FIXED44_CATALOGUE_BUILD_SQL.replace('LIMIT 104','LIMIT 110')
|
||||||
|
/** 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}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)}
|
||||||
@@ -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<typeof GROUPS[number],number>]))
|
||||||
|
const base=Object.freeze(Object.fromEntries(GROUPS.map(group=>[group,source[group].length])) as Record<typeof GROUPS[number],number>)
|
||||||
|
const leafMax=Object.freeze(Object.fromEntries(GROUPS.map(group=>[group,group==='inheritance'?1:Math.max(...parents.map(name=>perRelation.get(name)![group]))])) as Record<typeof GROUPS[number],number>)
|
||||||
|
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<typeof GROUPS[number],number>)
|
||||||
|
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<typeof GROUPS[number],number>]))
|
||||||
|
const source56BaseRows = Object.freeze(Object.fromEntries(GROUPS.map(group => [group, FIXED56_ROLE_TEMPLATE[group].length])) as Record<typeof GROUPS[number],number>)
|
||||||
|
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<typeof GROUPS[number],number>),
|
||||||
|
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<string>();private readonly perParent=new Map<string,number>();private readonly rows=Object.fromEntries(GROUPS.map(g=>[g,0])) as Record<typeof GROUPS[number],number>
|
||||||
|
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<string,ManagedLeafData>();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<string,number>();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]<b[0]?-1:1)).slice(0,3)
|
||||||
|
return Object.freeze({perLeafRows:selected.reduce((sum,[,value])=>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})
|
||||||
|
}
|
||||||
@@ -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<Inventory>={"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":"","attgenerateLine truncated
|
||||||
@@ -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 WLine truncated
|
||||||
@@ -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<Inventory> = 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")
|
||||||
|
}
|
||||||
@@ -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<Record<string, { remove: number[]; add: Row[] }>> = {
|
||||||
|
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<transaction_timestamp()-interval '180 days'\n AND (p.operation_id,p.operation_version) IN (\n ('logs.settings.update',1),('traffic.settings.update',2),('uptime.settings.update',2),\n ('servers.api-ping.settings.update',2),('internet-path.settings.update',1),('geoip.settings.update',1),('backups.schedule.update',1)\n ) AND p.state='completed'\n ) THEN RETURN NEW; END IF;\n RAISE EXCEPTION 'ordinary operation retirement claim cannot be replaced or reopened';\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC","search_path=pg_catalog, public"],"prokind":"f","proname":"mcp_ordinary_retirement_claim_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$\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 JOIN public.mcp_operations source ON source.id=NEW.source_operation_id AND source.connection_id=o.connection_id\n JOIN public.mcp_plans sp ON sp.id=source.plan_id AND sp.connection_id=source.connection_id\n JOIN public.mcp_connections c ON c.id=o.connection_id\n WHERE o.id=NEW.recovery_operation_id AND p.id=NEW.recovery_plan_id\n AND o.connection_id=NEW.connection_id AND c.owner_id=NEW.owner_id\n AND r.version=NEW.profile_version AND o.state='pending' AND p.state='executing'\n AND p.operation_id='operations.recover' AND p.operation_version=8\n AND sp.id=NEW.source_plan_id AND sp.operation_id='routing.community.switch' AND sp.operation_version=7\n AND source.state IN('applied','partial')\n ) THEN RAISE EXCEPTION 'manual inverse charge differs from pending admission'; END IF;\n RETURN NEW;\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$\nBEGIN\n IF NEW.mode='manual' AND NOT pg_try_advisory_xact_lock(hashtextextended('mcp-runner-admission',0))\n THEN RAISE EXCEPTION 'manual intent admission snapshot busy'; END IF;\n IF NEW.mode='manual' AND 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_operation_steps s ON s.operation_id=o.id AND s.step_index=0\n JOIN public.mcp_operations source ON source.id=NEW.source_operation_id AND source.connection_id=o.connection_id\n WHERE o.id=NEW.recovery_operation_id AND o.state='running'\n AND p.state='executing' AND p.operation_id='operations.recover' AND p.operation_version=8\n AND NEW.source_step_index=0 AND s.state='intent'\n AND s.intended_state->>'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_at<transaction_timestamp()-interval '180 days'\n AND p.state='completed' AND p.operation_id='servers.poll' AND p.operation_version=1 AND p.approved_by IS NOT NULL\n AND s.state='verified' AND s.finished_at IS NOT NULL AND t.action_id=p.operation_id\n AND t.target_global AND cardinality(t.target_server_ids)>0\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 FLine truncated
|
||||||
|
{"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<transaction_timestamp()-interval '180 days'\n AND p.operation_id='servers.probes.run' AND p.operation_version=1 AND p.state='completed'\n AND (EXISTS(SELECT 1 FROM public.server_probe_storage_admissions a\n WHERE a.receipt_id=NEW.receipt_id AND a.execution_id=o.id AND a.identity_kind='mcp' AND a.owner_id=c.owner_id\n AND a.claim_token IS NULL AND a.claim_attempted_at IS NOT NULL AND a.cancelled_at IS NULL AND a.purged_at IS NULL)\n OR(EXISTS(SELECT 1 FROM public.server_probe_terminal_proofs f\n WHERE f.operation_id=o.id AND f.receipt_id=NEW.receipt_id AND f.source_proof=NEW.source_proof\n AND f.source->>'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();\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_gc_allowed","prosecdef":false},
|
||||||
|
{"acl":null,"arguments":"id uuid","definition":"CREATE OR REPLACE FUNCTION public.server_probe_storage_gc_allowed(id uuid)\n RETURNS boolean\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\nAS $function$\nDECLARE a server_probe_storage_admissions%ROWTYPE; activity timestamptz; current_retention integer; terminal_at timestamptz;\nBEGIN\n IF current_setting('mm.probe_storage_gc',true) IS DISTINCT FROM 'on' THEN RETURN false; END IF;\n PERFORM pg_advisory_xact_lock(hashtextextended('server-probe-storage-v2',0));\n SELECT * INTO a FROM server_probe_storage_admissions WHERE receipt_id=id FOR UPDATE;\n IF NOT FOUND OR a.gc_not_before>=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'<clock_timestamp();\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC"],"prokind":"f","proname":"server_probe_storage_gc_allowed","prosecdef":false},
|
||||||
|
{"acl":null,"arguments":"target_receipt uuid, current_retention integer","definition":"CREATE OR REPLACE FUNCTION public.server_probe_retired_gc_allowed(target_receipt uuid, current_retention integer)\n RETURNS boolean\n LANGUAGE plpgsql\n SET \"TimeZone\" TO 'UTC'\n SET search_path TO 'pg_catalog', 'public'\nAS $function$\nDECLARE activity timestamptz; retained_days integer; safety integer;\nBEGIN\n IF current_setting('mm.probe_storage_gc',true) IS DISTINCT FROM 'on'\n OR current_setting('mm.probe_retired_gc_receipt',true) IS DISTINCT FROM target_receipt::text THEN RETURN false; END IF;\n SELECT (r.source->>'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_at<clock_timestamp() AND a.gc_not_before<clock_timestamp()\n AND NOT EXISTS(SELECT 1 FROM public.mcp_operations WHERE id=t.operation_id)\n AND NOT EXISTS(SELECT 1 FROM public.mcp_plans WHERE id=t.plan_id)\n AND NOT EXISTS(SELECT 1 FROM public.mcp_operation_steps WHERE operation_id=t.operation_id)\n AND NOT EXISTS(SELECT 1 FROM public.mcp_operation_checkpoints WHERE operation_id=t.operation_id)\n AND NOT EXISTS(SELECT 1 FROM public.mcp_diagnostic_cancellations WHERE operation_id=t.operation_id)\n AND NOT EXISTS(SELECT 1 FROM public.server_probe_execution_quarantines q\n WHERE q.receipt_id=r.receipt_id OR(q.identity_kind='mcp' AND q.execution_id=r.operation_id));\n IF NOT FOUND OR activity IS NULL OR NOT isfinite(activity)\n OR 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_executions j WHERE j.receipt_id=target_receipt\n AND (j.finished_at IS NULL OR j.finished_at>activity))\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'<clock_timestamp();\nEND $function$\n","language":"plpgsql","namespace":"public","owner":"@mm-role:operator@","proconfig":["TimeZone=UTC","search_path=pg_catalog, public"],"prokind":"f","proname":"server_probe_retired_gc_allowed","prosecdef":false},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
indexes: {
|
||||||
|
remove: [],
|
||||||
|
add: [
|
||||||
|
{"definition":"CREATE UNIQUE INDEX mcp_operation_replay_tombston_connection_id_idempotency_dig_key ON public.mcp_operation_replay_tombstones USING btree (connection_id, idempotency_digest)","index_name":"mcp_operation_replay_tombston_connection_id_idempotency_dig_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operation_replay_tombstones"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX mcp_operation_replay_tombstones_pkey ON public.mcp_operation_replay_tombstones USING btree (operation_id)","index_name":"mcp_operation_replay_tombstones_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operation_replay_tombstones"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX mcp_operation_replay_tombstones_plan_id_key ON public.mcp_operation_replay_tombstones USING btree (plan_id)","index_name":"mcp_operation_replay_tombstones_plan_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_operation_replay_tombstones"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX mcp_ordinary_retirement_claims_pkey ON public.mcp_ordinary_retirement_claims USING btree (operation_id)","index_name":"mcp_ordinary_retirement_claims_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_ordinary_retirement_claims"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX mcp_ordinary_retirement_claims_plan_id_key ON public.mcp_ordinary_retirement_claims USING btree (plan_id)","index_name":"mcp_ordinary_retirement_claims_plan_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"mcp_ordinary_retirement_claims"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_poll_retirement_claims_pkey ON public.server_poll_retirement_claims USING btree (receipt_id)","index_name":"server_poll_retirement_claims_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_poll_retirement_claims"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_probe_retired_sources_operation_id_key ON public.server_probe_retired_sources USING btree (operation_id)","index_name":"server_probe_retired_sources_operation_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_retired_sources"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_probe_retired_sources_pkey ON public.server_probe_retired_sources USING btree (receipt_id)","index_name":"server_probe_retired_sources_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_retired_sources"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_probe_retirement_claims_operation_id_key ON public.server_probe_retirement_claims USING btree (operation_id)","index_name":"server_probe_retirement_claims_operation_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_retirement_claims"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_probe_retirement_claims_pkey ON public.server_probe_retirement_claims USING btree (receipt_id)","index_name":"server_probe_retirement_claims_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_retirement_claims"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_probe_retirement_claims_plan_id_key ON public.server_probe_retirement_claims USING btree (plan_id)","index_name":"server_probe_retirement_claims_plan_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_retirement_claims"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_probe_terminal_proofs_pkey ON public.server_probe_terminal_proofs USING btree (operation_id)","index_name":"server_probe_terminal_proofs_pkey","indisprimary":true,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_terminal_proofs"},
|
||||||
|
{"definition":"CREATE UNIQUE INDEX server_probe_terminal_proofs_receipt_id_key ON public.server_probe_terminal_proofs USING btree (receipt_id)","index_name":"server_probe_terminal_proofs_receipt_id_key","indisprimary":false,"indisready":true,"indisunique":true,"indisvalid":true,"relname":"server_probe_terminal_proofs"},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
roleGrants: {
|
||||||
|
remove: [],
|
||||||
|
add: [
|
||||||
|
{"delete":false,"insert":false,"read":false,"relname":"mcp_operation_replay_tombstones","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":false,"relname":"mcp_ordinary_retirement_claims","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":false,"relname":"server_poll_retirement_claims","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":false,"relname":"server_probe_retired_sources","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":false,"relname":"server_probe_retirement_claims","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":false,"relname":"server_probe_terminal_proofs","rolname":"@mm-role:publisher@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":true,"relname":"mcp_operation_replay_tombstones","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":true,"relname":"mcp_ordinary_retirement_claims","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":true,"relname":"server_poll_retirement_claims","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":true,"relname":"server_probe_retired_sources","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":true,"relname":"server_probe_retirement_claims","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":false,"read":true,"relname":"server_probe_terminal_proofs","rolname":"@mm-role:capture@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":true,"read":true,"relname":"mcp_operation_replay_tombstones","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":true,"read":true,"relname":"mcp_ordinary_retirement_claims","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":true,"read":true,"relname":"server_poll_retirement_claims","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":true,"read":true,"relname":"server_probe_retired_sources","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":false,"insert":true,"read":true,"relname":"server_probe_retirement_claims","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},
|
||||||
|
{"delete":true,"insert":true,"read":true,"relname":"mcp_operation_replay_tombstones","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},
|
||||||
|
{"delete":true,"insert":true,"read":true,"relname":"mcp_ordinary_retirement_claims","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},
|
||||||
|
{"delete":true,"insert":true,"read":true,"relname":"server_poll_retirement_claims","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},
|
||||||
|
{"delete":true,"insert":true,"read":true,"relname":"server_probe_retired_sources","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},
|
||||||
|
{"delete":true,"insert":true,"read":true,"relname":"server_probe_retirement_claims","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},
|
||||||
|
{"delete":true,"insert":true,"read":true,"relname":"server_probe_terminal_proofs","rolname":"@mm-role:operator@","trigger":true,"truncate":true,"update":true},
|
||||||
|
{"delete":true,"insert":true,"read":true,"relname":"server_probe_terminal_proofs","rolname":"@mm-role:runtime@","trigger":false,"truncate":false,"update":false},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
tables: {
|
||||||
|
remove: [],
|
||||||
|
add: [
|
||||||
|
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ar/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_operation_replay_tombstones","reloptions":null,"relrowsecurity":false},
|
||||||
|
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ar/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"mcp_ordinary_retirement_claims","reloptions":null,"relrowsecurity":false},
|
||||||
|
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ar/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_poll_retirement_claims","reloptions":null,"relrowsecurity":false},
|
||||||
|
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ar/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_retired_sources","reloptions":null,"relrowsecurity":false},
|
||||||
|
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ar/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_retirement_claims","reloptions":null,"relrowsecurity":false},
|
||||||
|
{"acl":["@mm-role:capture@=r/@mm-role:operator@","@mm-role:operator@=arwdDxtm/@mm-role:operator@","@mm-role:runtime@=ard/@mm-role:operator@"],"namespace":"public","owner":"@mm-role:operator@","partition_bound":null,"partition_key":null,"relforcerowsecurity":false,"relispartition":false,"relkind":"r","relname":"server_probe_terminal_proofs","reloptions":null,"relrowsecurity":false},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
triggers: {
|
||||||
|
remove: [],
|
||||||
|
add: [
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operation_replay_tombstones FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operation_replay_tombstones","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_ordinary_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_ordinary_retirement_claims","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_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_retirement_claims","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_retired_sources FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_retired_sources","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_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_retirement_claims","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_terminal_proofs FOR EACH STATEMENT EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_terminal_proofs","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.mcp_operation_replay_tombstones FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_operation_replay_tombstones","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_ordinary_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"mcp_ordinary_retirement_claims","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_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_poll_retirement_claims","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_retired_sources FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_retired_sources","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_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_retirement_claims","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_terminal_proofs FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()","function":"mmapp_control.candidate_truncate_preadmitted()","parent":false,"relname":"server_probe_terminal_proofs","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_ordinary_retirement_claim_immutable BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_ordinary_retirement_claims FOR EACH ROW EXECUTE FUNCTION mcp_ordinary_retirement_claim_immutable()","function":"public.mcp_ordinary_retirement_claim_immutable()","parent":false,"relname":"mcp_ordinary_retirement_claims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_ordinary_retirement_claim_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER mcp_ordinary_retirement_claim_no_truncate BEFORE TRUNCATE ON public.mcp_ordinary_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION mcp_ordinary_retirement_claim_immutable()","function":"public.mcp_ordinary_retirement_claim_immutable()","parent":false,"relname":"mcp_ordinary_retirement_claims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"mcp_ordinary_retirement_claim_no_truncate","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON public.mcp_operation_replay_tombstones FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_operation_replay_tombstones","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_ordinary_retirement_claims FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"mcp_ordinary_retirement_claims","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_retirement_claims FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_poll_retirement_claims","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_retired_sources FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_retired_sources","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_retirement_claims FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_retirement_claims","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_terminal_proofs FOR EACH ROW EXECUTE FUNCTION server_partition_writer_guard()","function":"public.server_partition_writer_guard()","parent":false,"relname":"server_probe_terminal_proofs","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_retirement_claim_immutable BEFORE INSERT OR DELETE OR UPDATE ON public.server_poll_retirement_claims FOR EACH ROW EXECUTE FUNCTION server_poll_retirement_claim_immutable()","function":"public.server_poll_retirement_claim_immutable()","parent":false,"relname":"server_poll_retirement_claims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_retirement_claim_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_retirement_claim_no_truncate BEFORE TRUNCATE ON public.server_poll_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION server_poll_retirement_claim_immutable()","function":"public.server_poll_retirement_claim_immutable()","parent":false,"relname":"server_poll_retirement_claims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_retirement_claim_no_truncate","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_poll_retirement_requires_claim BEFORE UPDATE ON public.server_poll_archives FOR EACH ROW WHEN (((old.version = 1) AND (new.version = 2))) EXECUTE FUNCTION server_poll_retirement_requires_claim()","function":"public.server_poll_retirement_requires_claim()","parent":false,"relname":"server_poll_archives","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_poll_retirement_requires_claim","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":19,"unconditional":false},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_retired_source_immutable BEFORE DELETE OR UPDATE ON public.server_probe_retired_sources FOR EACH ROW EXECUTE FUNCTION server_probe_retired_source_immutable()","function":"public.server_probe_retired_source_immutable()","parent":false,"relname":"server_probe_retired_sources","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_retired_source_immutable","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":27,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_retired_source_insert_guard BEFORE INSERT ON public.server_probe_retired_sources FOR EACH ROW EXECUTE FUNCTION server_probe_retired_source_insert_guard()","function":"public.server_probe_retired_source_insert_guard()","parent":false,"relname":"server_probe_retired_sources","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_retired_source_insert_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":7,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_retired_source_no_truncate BEFORE TRUNCATE ON public.server_probe_retired_sources FOR EACH STATEMENT EXECUTE FUNCTION server_probe_retired_source_immutable()","function":"public.server_probe_retired_source_immutable()","parent":false,"relname":"server_probe_retired_sources","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_retired_source_no_truncate","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_retirement_claim_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_retirement_claims FOR EACH ROW EXECUTE FUNCTION server_probe_retirement_claim_guard()","function":"public.server_probe_retirement_claim_guard()","parent":false,"relname":"server_probe_retirement_claims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_retirement_claim_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_retirement_claim_no_truncate BEFORE TRUNCATE ON public.server_probe_retirement_claims FOR EACH STATEMENT EXECUTE FUNCTION server_probe_retirement_claim_guard()","function":"public.server_probe_retirement_claim_guard()","parent":false,"relname":"server_probe_retirement_claims","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_retirement_claim_no_truncate","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_terminal_proof_guard BEFORE INSERT OR DELETE OR UPDATE ON public.server_probe_terminal_proofs FOR EACH ROW EXECUTE FUNCTION server_probe_terminal_proof_guard()","function":"public.server_probe_terminal_proof_guard()","parent":false,"relname":"server_probe_terminal_proofs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_terminal_proof_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":31,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":false,"definition":"CREATE TRIGGER server_probe_terminal_proof_no_truncate BEFORE TRUNCATE ON public.server_probe_terminal_proofs FOR EACH STATEMENT EXECUTE FUNCTION server_probe_terminal_proof_guard()","function":"public.server_probe_terminal_proof_guard()","parent":false,"relname":"server_probe_terminal_proofs","tgdeferrable":false,"tgenabled":"O","tginitdeferred":false,"tgisinternal":false,"tgname":"server_probe_terminal_proof_no_truncate","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":34,"unconditional":true},
|
||||||
|
{"arg_bytes":0,"constraint":true,"definition":"CREATE CONSTRAINT TRIGGER \"RI_ConstraintTrigger_a_{oid}\" AFTER DELETE ON public.mcp_connections FROM mcp_operation_replay_tombstones 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_ordinary_retirement_claims 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_operation_replay_tombstones FROM server_probe_retired_sources NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_del\"()","function":"pg_catalog#1654","parent":false,"relname":"mcp_operation_replay_tombstones","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 server_probe_terminal_proofs 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.server_poll_storage_admissions FROM server_poll_retirement_claims NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","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_storage_admissions FROM server_probe_retired_sources NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_cascade_del\"()","function":"pg_catalog#1646","parent":false,"relname":"server_probe_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 UPDATE ON public.mcp_connections FROM mcp_operation_replay_tombstones 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_ordinary_retirement_claims 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_operation_replay_tombstones FROM server_probe_retired_sources NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"mcp_operation_replay_tombstones","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 server_probe_terminal_proofs 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.server_poll_storage_admissions FROM server_poll_retirement_claims 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_storage_admissions FROM server_probe_retired_sources NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_noaction_upd\"()","function":"pg_catalog#1655","parent":false,"relname":"server_probe_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_c_{oid}\" AFTER INSERT ON public.mcp_operation_replay_tombstones FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_operation_replay_tombstones","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_ordinary_retirement_claims FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"mcp_ordinary_retirement_claims","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_retirement_claims 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_retirement_claims","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_retired_sources FROM mcp_operation_replay_tombstones NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_probe_retired_sources","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_retired_sources FROM server_probe_storage_admissions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_probe_retired_sources","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_terminal_proofs FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_ins\"()","function":"pg_catalog#1644","parent":false,"relname":"server_probe_terminal_proofs","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.mcp_operation_replay_tombstones FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_operation_replay_tombstones","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_ordinary_retirement_claims FROM mcp_connections NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"mcp_ordinary_retirement_claims","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_retirement_claims 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_retirement_claims","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_retired_sources FROM mcp_operation_replay_tombstones NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_probe_retired_sources","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_retired_sources FROM server_probe_storage_admissions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_probe_retired_sources","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_terminal_proofs FROM mcp_operations NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION \"RI_FKey_check_upd\"()","function":"pg_catalog#1645","parent":false,"relname":"server_probe_terminal_proofs","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_retirement_final_guard AFTER UPDATE ON public.server_poll_archives DEFERRABLE INITIALLY DEFERRED FOR EACH ROW WHEN (((old.version = 1) AND (new.version = 2))) EXECUTE FUNCTION server_poll_retirement_final_guard()","function":"public.server_poll_retirement_final_guard()","parent":false,"relname":"server_poll_archives","tgdeferrable":true,"tgenabled":"O","tginitdeferred":true,"tgisinternal":false,"tgname":"server_poll_retirement_final_guard","tgnargs":0,"tgnewtable":null,"tgoldtable":null,"tgtype":17,"unconditional":false},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { canonical, sha } from "./application-writer-profile-canonical.js"
|
||||||
|
import { sorted, type Inventory } from "./catalogue-projection.js"
|
||||||
|
import { FIXED56_ROLE_TEMPLATE } from "./fixed56-build-definition.js"
|
||||||
|
|
||||||
|
export const BUILD57_PROVENANCE = Object.freeze({
|
||||||
|
format: "source57-build-candidate-v1",
|
||||||
|
manifestSha256: "7dbd678e59740c14a29594180c86b85b3b2dd53fd5aa7a72da6d14a513760dbc",
|
||||||
|
templateSha256: "8d81bb1954c17dc46d69ce18c5d7636a0d080552812e40c94506bd305c89429e",
|
||||||
|
})
|
||||||
|
|
||||||
|
export const FIXED57_ROLE_TEMPLATE: Readonly<Inventory> = 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")
|
||||||
|
}
|
||||||
@@ -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])
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -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<Inventory> = 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")
|
||||||
|
}
|
||||||
@@ -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<string,string>()
|
||||||
|
for(let i=1;i<args.length;i+=2){const name=args[i].slice(2);assert(args[i].startsWith('--')&&flags.includes(name)&&!values.has(name)&&args[i+1]);values.set(name,args[i+1])}
|
||||||
|
const get=(name:string)=>values.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()<deadlineAt);retained();const result=await client.query(text,values);retained();assert(!failed&&Date.now()<deadlineAt);return result
|
||||||
|
}}
|
||||||
|
await sql.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
|
||||||
|
await sql.query("SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'; SET LOCAL search_path=pg_catalog")
|
||||||
|
const observation=await captureDatabaseSourceObservation(sql,{deadlineAt})
|
||||||
|
assert.equal(sourceBindingCanonical(observation.source),sourceBindingCanonical(cold))
|
||||||
|
const {rows:[actor]}=await sql.query(`SELECT r.oid::text AS oid,
|
||||||
|
NOT (r.rolsuper OR r.rolreplication OR r.rolbypassrls OR r.rolcreatedb OR r.rolcreaterole)
|
||||||
|
AND NOT pg_catalog.has_schema_privilege(r.oid,'public','CREATE')
|
||||||
|
AND NOT EXISTS(SELECT FROM pg_catalog.pg_class c WHERE c.relnamespace='public'::regnamespace AND CASE
|
||||||
|
WHEN c.relkind IN ('r','p','v','m','f') THEN pg_catalog.has_table_privilege(r.oid,c.oid,'INSERT, UPDATE, DELETE, TRUNCATE, TRIGGER')
|
||||||
|
OR pg_catalog.has_any_column_privilege(r.oid,c.oid,'INSERT, UPDATE')
|
||||||
|
WHEN c.relkind='S' THEN pg_catalog.has_sequence_privilege(r.oid,c.oid,'USAGE, UPDATE') ELSE false END) AS readonly
|
||||||
|
FROM pg_catalog.pg_roles r WHERE r.rolname=CURRENT_USER`)
|
||||||
|
assert(actor?.oid===approval.coldRoleOid&&actor.readonly===true,'Cold restore reader must have read-only privileges')
|
||||||
|
const storage=await captureLegacyProbeStorage(sql),lineage=await captureLegacyProbeLineage(sql)
|
||||||
|
assert(storage.rowCount>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()<deadlineAt)
|
||||||
|
await readProtectedDatabaseSourceBindingRegistry(pin,{deadlineAt})
|
||||||
|
const body=legacyColdProofBody.parse({format:'legacy-probe-cold-proof-v1',approvalSha256,custodySha256:custody.approvalSha256,
|
||||||
|
coldArchiveSha256:approval.coldArchiveSha256,...hashes,keySha256:approval.keySha256,tlsSpkiSha256:approval.tls.spkiSha256,
|
||||||
|
coldSource:cold,roleOid:approval.coldRoleOid,observedAt:Date.now(),expiresAt:approval.expiresAt})
|
||||||
|
const receipt=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_PROBE_COLD_PROOF_DOMAIN+sourceBindingCanonical(body)),key).toString('base64url')})
|
||||||
|
const file='legacy-probe-cold-proof.json'
|
||||||
|
await writeFile(path.join(get('output-directory'),file),receipt,{flag:'wx',mode:0o600})
|
||||||
|
return {event:'database-legacy-cold-proof',outcome:'observed',proofFile:file,proofSha256:sha(receipt),conversionAllowed:false,readyAuthorized:false}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
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,protectedDirectory} from './operator-files.mjs'
|
||||||
|
import {createProtectedDatabaseSourceBindingVerifier} from './protected-source-registry.mjs'
|
||||||
|
import {withActiveSource} from './source-activation.mjs'
|
||||||
|
import {mcpStateDirectory} from '../../src/modules/mcp/security.js'
|
||||||
|
import {captureDatabaseSourceObservation} from '../../src/db/database-source-observation.js'
|
||||||
|
import {provisionControlledSchemaReferenceOn,verifyLegacyControlReferenceApproval} from '../../src/db/controlled-schema-reference-core.js'
|
||||||
|
import {inspectApprovedLegacySource58Predecessor,verifyLegacySource58PreflightApproval} from '../../src/db/legacy-source58-transition.js'
|
||||||
|
import {SYSTEM_DATABASE_OPERATION_LOCK,SYSTEM_DATABASE_REQUEST_LOCK} from '../../src/db/operation-lock.js'
|
||||||
|
import type {SchemaInventoryClient} from '../../src/db/migration-schema-inventory.js'
|
||||||
|
|
||||||
|
const parse=(bytes:Buffer)=>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<string,string>()
|
||||||
|
for(let i=1;i<args.length;i+=2){
|
||||||
|
const name=args[i].slice(2)
|
||||||
|
assert(args[i].startsWith('--')&&flags.includes(name)&&!values.has(name)&&args[i+1])
|
||||||
|
values.set(name,args[i+1])
|
||||||
|
}
|
||||||
|
const get=(name:string)=>values.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<T>(inspect?:(sql:SchemaInventoryClient)=>Promise<T>){
|
||||||
|
const client=await source.connect()
|
||||||
|
try{
|
||||||
|
const peer=retain(client),sql={query:async(text:string,parameters?:unknown[])=>{
|
||||||
|
peer();assert(Date.now()<deadlineAt);const result=await client.query(text,parameters);peer();assert(Date.now()<deadlineAt);return result
|
||||||
|
}}
|
||||||
|
if(inspect)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 Source58 drain unavailable')
|
||||||
|
}
|
||||||
|
await sql.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
|
||||||
|
await sql.query("SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'")
|
||||||
|
assert.equal((await sql.query('SELECT oid::text AS oid FROM pg_catalog.pg_roles WHERE rolname=CURRENT_USER AND rolname=SESSION_USER')).rows[0]?.oid,request.roles.publisher.signedOid)
|
||||||
|
await verifier.verify(sql,await captureDatabaseSourceObservation(sql,{deadlineAt}),{deadlineAt})
|
||||||
|
let result:T|undefined
|
||||||
|
if(inspect){
|
||||||
|
await sql.query("SET LOCAL search_path=pg_catalog; SET LOCAL standard_conforming_strings=on; SET LOCAL statement_timeout='5s'")
|
||||||
|
result=await inspect(sql)
|
||||||
|
await verifier.verify(sql,await captureDatabaseSourceObservation(sql,{deadlineAt}),{deadlineAt})
|
||||||
|
}
|
||||||
|
assert.equal((await sql.query('ROLLBACK')).command,'ROLLBACK')
|
||||||
|
return result
|
||||||
|
}finally{client.release(true)}
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
if(transition){
|
||||||
|
const observation=await observe(sql=>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()])}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -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<string,string>()
|
||||||
|
for(let i=1;i<args.length;i+=2){const name=args[i].slice(2);assert(args[i].startsWith('--')&&flags.includes(name)&&!values.has(name)&&args[i+1]);values.set(name,args[i+1])}
|
||||||
|
const get=(name:string)=>values.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()<deadlineAt,'Legacy handoff lifetime ended')
|
||||||
|
const retainedSql=(client:PoolClient)=>{
|
||||||
|
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<typeof retainedSql>,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<T,>(checkSchema:boolean,inspect?:(sql:ReturnType<typeof retainedSql>)=>Promise<T>)=>{
|
||||||
|
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}
|
||||||
|
})
|
||||||
|
}
|
||||||
Loaded 100 of 1565 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user