Skip to content
📦 ARCHIVED SNAPSHOT (v1.0) You are viewing historical business logic and documentation for Converters (IoT & Cloud) v1.0 (frozen on 2026-11-23, commit 71e82be). Switch to Latest →

9. Public Ingress Deployment, Local Staging, Diagnostics, Mechanical, QA Tooling & Docs/Mockups

Repository: Converters (planovi-converter) • Rule Prefix: BR-OPS • Documented Rules: 5 • Open [TEAM_WHY_NEEDED] Questions: 4

1. Business Purpose & Actors

Provides hardened production ingress containers (nginx:1.29-alpine + mtls-gateway), a deterministic local multi-container staging environment (PostgreSQL 17, Mosquitto 2, FastAPI, Nginx mTLS, transactional migration runner), VPS health monitoring and SSH allowlist safety tooling, 12 standalone RP2350 hardware/lab bench firmware targets, an OpenSCAD 35 mm DIN-rail enclosure model, repository integrity/secret/hardware-soak test suites, and the active Astro Starlight local documentation portal.

  • Actors: DevOps / Release Engineers (deploy/, scripts/deploy_public_ingress.ps1, deploy/update_allowed_ip.sh), Hardware QA / Lab Engineers (diagnostics/, staging/, tools/*regression*, tools/run_cloud_e2e.py, tools/run_staging_mtls_matrix.py), Mechanical Engineers (mechanical/enclosure_din.scad), Technical Writers (docs/, tools/build_*_document.py).

2. All Features & End-to-End Flows

  • Public mTLS Ingress Stack (deploy/docker-compose.public-ingress.yml, deploy/Dockerfile.mtls-gateway, deploy/nginx.api.planovi.conf, scripts/deploy_public_ingress.ps1):
    • Deployment: scripts/deploy_public_ingress.ps1 validates secret lengths (>= 32 chars, HMAC secret != Edge token), URL shapes, PEM files, and SUPABASE_PROJECT_REF, optionally deploys Supabase Edge Function ingest-telemetry, runs docker compose up -d --build, and verifies fail-closed mTLS via tools/validate_public_ingress.py (passes ONLY if unauthenticated request fails TLS handshake or returns Nginx 400/495/496 "certificate").
    • Hardening: Both mtls-gateway (UID 10001:10001) and nginx run read_only: true, no-new-privileges:true, cap_drop: [ALL], tmpfs mounts, and mtls-gateway healthcheck (GET /healthz every 30 s). Nginx listens with proxy_protocol, ssl_verify_client on, client_max_body_size 4k, strips client-supplied X-Planovi-Client-CN/X-Planovi-Gateway-*, and returns 404 for all paths other than POST /functions/v1/ingest-telemetry.
  • Admin Reverse Proxy & VPS Operations (deploy/nginx.admin.planovi.conf, deploy/check-health.sh, deploy/update_allowed_ip.sh):
    • nginx.admin.planovi.conf serves the Flutter SPA from /var/www/planovi-converter-admin and proxies /api/v1/ -> 127.0.0.1:8000/api/v1/ behind IP allowlist (allow <ip>; deny all;).
    • check-health.sh (cron */5 * * * *) inspects 6 Docker containers and POSTs status to /api/v1/infra/report-status.
    • update_allowed_ip.sh restricts UFW port 22/tcp with an automatic 300 s (ROLLBACK_TIMEOUT) background rollback unless --confirm is executed.
  • Local Staging Stack & Migration Runner (staging/, tools/setup_local_staging.py, scripts/staging.ps1):
    • setup_local_staging.py generates a local ECDSA P-256 CA (730 d), server cert with LAN IP SAN, admin/ingress/device certs (LAB-0001, LAB-0002), CRL, C headers (planovi_ca_pem.h, staging_lab_credentials*.h), and .staging-runtime/staging.env.
    • staging/migrate_and_seed.py acquires PostgreSQL advisory lock 0x41534C414E4449 ("ASLANDI"), verifies SHA-256 checksums of all previously applied migrations in planovi_schema_migrations, enforces exactly one BEGIN;..COMMIT; per SQL file, applies pending migrations atomically, seeds PLANOVI-STAGING facility + LAB-0001/LAB-0002 converters, and deactivates legacy fictitious meter 'LEM-60 staging'.
    • staging/nginx.conf (:8443, ssl_verify_client on) rewrites /functions/v1/ingest-telemetry directly to http://backend:8000/api/v1/telemetry/record to exercise the device HTTPS contract locally without Supabase.
  • Standalone Hardware Diagnostics (diagnostics/ — 12 EXCLUDE_FROM_ALL targets):
    • Non-destructive bench probes (hardware_diag, lem60_probe, rtc_sd_probe, wifi_dhcp_probe), 50-slave RS-485 simulator (modbus_50_slave_sim), FAT CSV soak loggers (lem60_rtc_sd_logger, lem60_rtc_sd_soak), destructive one-shot SD crypto test (sd_crypto_prepare_test, requires PLANOVI_SD_CRYPTO_ERASE_AUTHORIZED=1), and reversible lab firmware targets (installer_wifi_lab, otp_lab_mock, otp_bootloader_lab_mock, installer_lab_identity.h).
  • Mechanical, QA Validators, Regression Suites, Docs & Mockups:
    • mechanical/enclosure_din.scad: Preliminary parametric 54 × 90 × 37 mm 35 mm DIN-rail enclosure (part = "base"|"lid"|"assembly", 4× M3 heat-set bosses, 2× PG7 glands, antenna clearance board_y = 17, external metal DIN clip required).
    • tools/validate_repository.py (UTF-8/conflict check, CMake source parity, 4 MiB flash contiguity, OTP row parity between otp_keys.c and provision_otp.py, contiguous migration numbering) & tools/scan_repository_secrets.py (redacted SHA-256[:12] secret scanner across worktree, binaries, .zip/.docx, and git history).
    • Test suites: 10 compiled host Unity C unit tests (tests/CMakeLists.txt; test_fifo_buffer.c excluded pending block mock), 26 Python contract/policy tests (tests/test_*.py), 19 Cloud pytest suites (cloud/tests/), 9-case mTLS negative matrix (tools/run_staging_mtls_matrix.py), and disposable full-stack E2E (tools/run_cloud_e2e.py).
    • docs/: ACTIVE Astro ^4.16.1 + Starlight ^0.28.2 documentation portal (docs/src/content/docs/*, test cases T00–T07, dated hardware test results, DOCX/manual pipeline tools/build_installation_manual.py / build_project_document.py).
    • mockups/planovi-converters/: LEGACY / REFERENCE ONLY — static offline HTML/CSS/JS UI prototype plus an unused site-creator-vinext-starter (next/react/vinext/drizzle-orm) scaffold with zero backend or hardware integration.

3. Hard Business Rules, Validations, Calculations & Edge Cases

  • BR-OPS-01 (Fail-Closed Public Ingress Verification): tools/validate_public_ingress.py passes only if an unauthenticated HTTPS POST without a client cert fails TLS handshake or receives Nginx 400/495/496 mentioning "certificate" and "nginx".
  • BR-OPS-02 (Migration Immutability & Transactionality): staging/migrate_and_seed.py locks via advisory lock 0x41534C414E4449, rejects any applied migration whose file disappeared or whose SHA-256 changed in planovi_schema_migrations, and rejects any SQL file lacking a single outer BEGIN;..COMMIT; block. (Note: migration 019_fix_converter_sync_and_telemetry_rpc.sql intentionally places ALTER TYPE public.meter_model ADD VALUE IF NOT EXISTS 'chint_dtsu666' outside BEGIN;..COMMIT; because PostgreSQL forbids using a newly added enum value in the same transaction — verify runner compatibility before running 019 through staging/migrate_and_seed.py!)
  • BR-OPS-03 (Lab/Diagnostic Compile Isolation): All 12 files in diagnostics/ are EXCLUDE_FROM_ALL targets in CMakeLists.txt; OTP lab mocks fail compile without explicit PLANOVI_INSTALLER_LAB or PLANOVI_BOOTLOADER_QUALIFICATION_LAB; destructive SD erase test requires PLANOVI_SD_CRYPTO_ERASE_AUTHORIZED=1.
  • BR-OPS-04 (OTP & Flash Single-Source-of-Truth Gate): tools/validate_repository.py parses both src/security/otp_keys.c and tools/provision_otp.py to enforce exact OTP row/size/overlap parity and checks src/bootloader/flash_layout.h sums to 0x00400000.
  • BR-OPS-05 (SSH Lockout Safety Rollback): deploy/update_allowed_ip.sh spawns a 300 s background rollback timer restoring /tmp/ssh_ip_previous UFW rules unless --confirm writes /tmp/ssh_ip_confirmed.

4. State Machines & Data Lifecycle

  • Staging Migration Runner Lifecycle (staging/migrate_and_seed.py): ACQUIRE_LOCK -> CHECK_LEDGER (if empty & 008-complete -> ADOPT_LEGACY_000_008) -> VERIFY_CHECKSUMS -> APPLY_PENDING_IN_TX -> SEED_STAGING_DEVICES -> RELEASE_LOCK.
  • UFW SSH Allowlist Lifecycle (deploy/update_allowed_ip.sh): PREVIOUS_IP -> APPLIED_UNCONFIRMED (300s timer) -> CONFIRMED (--confirm) OR AUTO_ROLLED_BACK (timeout).

5. Technical Contract (Inputs -> Processing -> Side Effects)

  • Containers: mtls-nginx (:443, 172.30.0.11), mtls-gateway (:8001, 172.30.0.10), staging postgres:17-alpine (127.0.0.1:55432), staging mqtt (:8883), staging backend (:8000), staging mtls-proxy (:8443), firmware-builder CLI image.
  • Files / Scripts: deploy/*, staging/*, scripts/*, diagnostics/*, mechanical/*, tools/*, tests/*, cloud/tests/*, docs/*, mockups/planovi-converters/*.

6. Cross-Repository Dependencies

  • scripts/sync-brand.sh -> PA-IDEAS/planovi-docs | gh CLI git clone | Syncs BRANDING_GUIDE.md & assets/brand/*.
  • scripts/deploy_public_ingress.ps1 -> Supabase Cloud CLI | SUPABASE_ACCESS_TOKEN + SUPABASE_PROJECT_REF | Deploys ingest-telemetry Edge Function & secrets.
  • deploy/docker-compose.public-ingress.yml -> External VPS Docker network vps-network (shared with vps-proxy, converters-api-dev, supabase).

7. Why It Works This Way (Decisions, Trade-offs & Workarounds)

  • Read-Only Hardened Edge Containers: Minimizes blast radius on the public :443 mTLS entry point by dropping all Linux capabilities and mounting certs read-only under /run/secrets/.
  • Confirmed Bugs & Platform Coupling:
    1. staging/verify_schema.py:10-12: .replace("postgresql+asyncpg://", "postgresql://", 1) applies only to ASLANDI_DATABASE_URL, NOT PLANOVI_DATABASE_URL (operator precedence bug -> crashes asyncpg.connect() when PLANOVI_DATABASE_URL uses SQLAlchemy +asyncpg scheme).
    2. deploy/check-health.sh:13 & deploy/cron-setup.md:34: Hardcoded default STATUS_AUTH_TOKEN fallback committed in repo; cron-setup.md:10 also contains a broken local Windows file path (C:\Users\Silverxis\...).
    3. deploy/nginx.admin.planovi.conf:30: Placeholder allow 1.2.3.4; deny all;.
    4. tools/setup_local_staging.py:27-57 & staging/Dockerfile:9-11: Unconditionally invokes Windows ssl.enum_certificates looking specifically for "Norton Web/Mail Shield Root" — fails on macOS, Linux, or Windows machines without Norton.
    5. deploy/Dockerfile.firmware-builder:28-29: Clones FreeRTOS-Kernel from unpinned -b main despite comment stating V11.1.0.
    6. deploy/docker-compose.public-ingress.yml:25-29: Hardcodes extra_hosts mapping 4 planovi.app domains to 191.218.165.149; lines 14-15 & 20-21 set divergent default TRUSTED_PROXY_CIDRS (0.0.0.0/0 vs 172.30.0.0/24,...).
    7. Hardcoded developer/lab specifics: C:\Users\dawid\... in scripts/staging.ps1:36,52, tools/run_staging_mtls_matrix.py:29, tools/run_cloud_e2e.py:42; lab IP 192.168.0.150 in scripts/t03_8_lab2_network_isolation.ps1:19.
    8. Migration 019 ALTER TYPE ... ADD VALUE outside BEGIN;..COMMIT; vs staging/migrate_and_seed.py’s single-outer-transaction rule (migration_transaction_body).
  • [TEAM_WHY_NEEDED: Why does deploy/docker-compose.public-ingress.yml:25-29 hardcode extra_hosts -> 191.218.165.149 and why do PLANOVI_TRUSTED_PROXY_CIDRS and ASLANDI_TRUSTED_PROXY_CIDRS have conflicting defaults (0.0.0.0/0 vs specific CIDRs)?]
  • [TEAM_WHY_NEEDED: Should tools/setup_local_staging.py make the Windows Norton TLS-inspection CA export optional so macOS/Linux developers can run local staging?]
  • [TEAM_WHY_NEEDED: Should deploy/Dockerfile.firmware-builder pin FreeRTOS-Kernel to a specific release tag (e.g., V11.1.0) instead of -b main for reproducible firmware builds?]
  • [TEAM_WHY_NEEDED: Can mockups/planovi-converters/ (legacy static prototype + unused Next.js/Vinext starter) be archived or removed?]

8. Debugging, Logs & Common Failure Modes

Symptom / Error MessageRoot Cause in CodeHow to Verify / Exact CommandFix
setup_local_staging.py fails on macOS/Linux or non-Norton Windowsexport_windows_tls_inspection_ca() requires Windows ssl.enum_certificates + "Norton Web/Mail Shield Root" (tools/setup_local_staging.py:27-57)Run python3 tools/setup_local_staging.pyGuard Norton CA export behind OS/optional flag
staging/migrate_and_seed.py rejects migration 019migration_transaction_body() requires entire file inside a single BEGIN;..COMMIT;, whereas 019 puts ALTER TYPE ... ADD VALUE before BEGIN;Run pytest tests/test_migration_runner_contract.py or staging/migrate_and_seed.pyUpdate migrate_and_seed.py to support pre-transaction ALTER TYPE ... ADD VALUE statements
staging/verify_schema.py crashes with invalid scheme postgresql+asyncpgPrecedence bug on PLANOVI_DATABASE_URL or ASLANDI_DATABASE_URL.replace(...) (verify_schema.py:10-12)Run PLANOVI_DATABASE_URL=postgresql+asyncpg://... python3 staging/verify_schema.pyParenthesize (os.getenv(...) or ...).replace(...)
validate_public_ingress.py failsUnauthenticated request was not rejected at TLS/Nginx layerpython3 tools/validate_public_ingress.py --url https://api.planovi.app/functions/v1/ingest-telemetryVerify ssl_verify_client on; and /run/secrets/planovi-device-ca.pem in nginx.api.planovi.conf