Files
nixos-core/docs/plans/p4-readiness-plan.md
T
kiskaadee ecacb1cc39 docs(p4): formalize control plane readiness plan and boundary specifications
- Reconcile appctl implementation inventory and caller map
- Define canonical command and error contract with schema v1.0
- Close authorization design with strict token enforcement everywhere
- Specify daemon execution boundaries and fixed subprocess allowlist
- Formalize append-only audit telemetry schema and failure-handling semantics
- Establish P4 acceptance test matrix and definition of done
- Synchronize ROADMAP.md milestone table and todo.txt task tracking
2026-10-14 13:41:17 -05:00

22 KiB
Raw Blame History

📋 Control Plane Milestone P4: Readiness Plan & Boundary Specifications

Status: 🛠️ In Progress (Pre-P4 Architecture & Security Gate)
Target Milestone: Milestone P4: Appctl Control Plane Daemon & API
Governing ADR: homelab-appctl-control-plane-architecture.md


🎯 Purpose & Scope

Before initiating code implementation for homelab-appctl.service, this readiness plan formalizes the operational contracts, execution boundaries, and security invariants that P4 depends upon.

Milestones P0–P3 established the core foundation:

  • P0: Baseline consolidation & Pytest/Nix test harness.
  • P1: Zero-data-loss mail realignment & state isolation (~/var/lib/homelab/snappymail).
  • P2: GitOps repository registry (deployments.yaml), AST security validation, and Systemd sandboxing.
  • P3: Manifest Schema v1.0 (core_manifest.py), two-axis inspection (provenance.py), and promoted authoritative engine (appctl_engine.py).

This plan resolves the six architectural prerequisites necessary to implement P4 as a secure, fail-closed operational control plane rather than discovering boundaries ad-hoc during coding.


📑 Sequence of Deliverables

Deliverable Phase Focus Status
1. Implementation Inventory P0 Verification Baseline truth, callers, dependencies, and migration state ✅ Completed
2. Command & Error Contract P4 Prerequisite (Blocker) Canonical domain operations, typed schemas, and error taxonomy 🎯 In Review
3. Authorization Design Security Gate (Blocker) Token lifecycle, RBAC matrix, and fail-closed evaluation 🎯 In Review
4. Execution & Privilege Boundaries P4 Prerequisite Subprocess allowlist, Docker API capabilities, and filesystem roots ⏳ Pending Gate
5. Audit Telemetry Semantics P4 Prerequisite RFC JSONL audit schema, persistent ownership, and lifecycle ⏳ Pending Gate
6. P4 Acceptance Test Suite Verification Concrete DoD, black-box transport parity, and security regression gates ⏳ Pending Gate

1. Reconcile the Implementation Inventory

A. Current File & Module Roles

File / Component Language Authority Status Functional Responsibilities
scripts/appctl Bash Active CLI Client CLI entry point; secret environment sourcing (homeserver.env, traefik-deployments.env); resolves metadata via engine; directly runs docker compose and git pull subprocesses.
scripts/appctl_engine.py Python 3 Authoritative Engine Domain models (SitesApp, CoreService); schema v1.0 parsing (core_manifest.py); two-axis inspection (provenance.py); SSL cert inspection; Homepage compilation; metadata serialization (resolve).
scripts/appctl_engine_v2.py Python 3 Compatibility Shim Thin backward-compatibility wrapper re-exporting all symbols from appctl_engine.py. No unique domain logic.
scripts/core_manifest.py Python 3 Schema Library Strongly-typed Manifest dataclasses; transport validation (https://, ssh://, git@); ref semantics classification (branch, tag, commit).
scripts/provenance.py Python 3 Provenance Library Two-axis status engine; local checkout inspection (git status, commit SHA); bounded upstream query (git ls-remote with 10s timeout).
scripts/gitops_dispatcher.py Python 3 Independent Service Webhook listener (port 9000); HMAC validation; flock serialization; independent AST validation; executes its own docker compose and git pull subprocesses.

B. Command Inventory & Implementation Division

Command CLI Syntax Handled By Operation Type Current Behavior
list / status appctl list [--fetch] [--core] [--ssl] appctl_engine.py Read-Only Scans ~/Sites, evaluates two-axis checkout and upstream provenance, queries container status, prints formatted table.
info appctl info <service> [--ssl] appctl_engine.py Read-Only Prints detailed metadata, two-axis local checkout vs. upstream provenance, network memberships, and TLS certificate info.
ssl appctl ssl [service] appctl_engine.py Read-Only Connects over TLS socket (port 443), extracts peer X.509 certificate, calculates days until expiration.
resolve appctl resolve <query> appctl_engine.py Read-Only Resolves canonical name / alias; emits JSON string consumed by Bash setup_app_env.
complete appctl complete appctl_engine.py Read-Only Emits space-separated list of known workload names, aliases, and core services for Bash shell autocompletion.
sync appctl sync [--homepage-dir=PATH] appctl_engine.py Mutating (Filesystem) Reads all app.yaml manifests, groups and sorts cards by weight, writes config/homepage/services.yaml.
up appctl up <service|core> scripts/appctl (Bash) Mutating (Containers) Sourced environment variables \to docker compose up -d --remove-orphans \to triggers appctl sync.
down appctl down <service|core> scripts/appctl (Bash) Mutating (Containers) docker compose down (or stop for Core services) \to triggers appctl sync.
restart appctl restart <service|core> scripts/appctl (Bash) Mutating (Containers) Sourced environment variables \to docker compose restart (or down/up) \to triggers appctl sync.
update appctl update <service|core> scripts/appctl (Bash) Mutating (Git + Containers) Verifies clean git working tree \to git pull --ff-only \to docker compose pull \to docker compose up -d \to triggers appctl sync.
pull appctl pull <service|core> scripts/appctl (Bash) Mutating (Images) docker compose pull \to docker compose up -d --remove-orphans.
logs appctl logs <service|core> [-f] scripts/appctl (Bash) Read-Only (Streaming) Dispatches docker compose logs -f.
config appctl config <service|core> scripts/appctl (Bash) Read-Only Injects environment \to executes docker compose config to view fully evaluated compose AST.

C. Active Call Sites Across the Repository

  1. System Path Integration:
    • nixos/configuration.nix: Installs host wrapper /run/current-system/sw/bin/appctl calling ${CORE_DIR:-$HOME/Core}/scripts/appctl "$@".
  2. Internal Script Invocation:
    • scripts/appctl calls python3 "$ENGINE" resolve "$query" to set up the Compose environment.
    • scripts/appctl calls python3 "$ENGINE" sync after mutating actions (up, down, restart, update).
  3. Automated Test Coverage:

D. Architectural Findings & Migration Deductions

  1. V1 Engine is Fully Superseded: scripts/appctl_engine.py is now identical to the modern dataclass implementation. scripts/appctl_engine_v2.py is merely an alias and can be deprecated/retired once the daemon is active.
  2. Duplicated Operational Logic in GitOps: scripts/gitops_dispatcher.py and scripts/appctl maintain separate, duplicate implementations of git pull and docker compose up. This violates Invariant #1 (Single Operational Truth). In P4, GitOps webhook handlers must delegate execution to the control-plane daemon via its internal client.
  3. Environment Injection Vulnerability in Bash: The current Bash CLI constructs shell exports dynamically using eval "$(python3 ...)". The daemon replaces this subprocess string interpolation with in-process, typed dictionary manipulation.

2. Command and Error Contract (P4 Prerequisite)

A. Canonical Domain Operations

Every client (local CLI, remote CLI, GitOps dispatcher, and future Operations UI) invokes one of these domain operations across either transport:

                            ┌─────────────────────────┐
                            │    Client Request       │
                            └────────────┬────────────┘
                                         ▼
                   ┌───────────────────────────────────────────┐
                   │        Operation Dispatch Router          │
                   └─────────────────────┬─────────────────────┘
                                         │
         ┌───────────────────────────────┴───────────────────────────────┐
         ▼                                                               ▼
   [ Read-Only Operations ]                                    [ Mutating Operations ]
   • workload.list                                             • workload.deploy
   • workload.get                                              • workload.stop
   • workload.resolve                                          • workload.restart
   • workload.config                                           • workload.update
   • workload.logs                                             • workload.pull
   • platform.ssl                                              • platform.sync_homepage
   • platform.health

B. Typed Request and Response Models

All requests and responses use strict JSON payloads with explicit schema versioning (version: "1.0"):

// Example: POST /operations/deploy
// Request:
{
  "version": "1.0",
  "operation": "workload.deploy",
  "target": "jellyfin",
  "parameters": {
    "recreate": true,
    "remove_orphans": true
  }
}

// Response (Success):
{
  "version": "1.0",
  "status": "success",
  "operation_id": "op_01JAX71BC98E4",
  "target": "jellyfin",
  "timestamp": "2026-10-13T18:30:00Z",
  "duration_ms": 1420,
  "result": {
    "action": "recreated",
    "containers": ["jellyfin"],
    "health": "running"
  }
}

C. Standardized Error Taxonomy

Failures are strongly typed and distinguished at three distinct architectural levels:

Error Classification HTTP Code Error Code Description / Fail-Closed Action
Validation Error 400 Bad Request ERR_VALIDATION_FAILED Request schema violation, invalid target name, malformed app.yaml, or Compose AST security violation.
Authentication Error 401 Unauthorized ERR_AUTH_REQUIRED Missing, expired, or malformed API token / HMAC signature.
Authorization Error 403 Forbidden ERR_PERMISSION_DENIED Authenticated principal lacks necessary role (homelab-admin vs. homelab-viewer) for the target operation.
Target Not Found 404 Not Found ERR_TARGET_NOT_FOUND Workload target does not exist under ~/Sites or Core services registry.
State Conflict 409 Conflict ERR_CONCURRENT_MUTATION Another mutating operation on the target workload is currently holding the operation lock.
Execution Error 500 Internal Error ERR_EXECUTION_FAILED Docker daemon error, subprocess non-zero exit, or persistent volume failure.

3. Authorization Design (Security Gate)

A. The Core Principle: Host Access Does Not Confer Authority

Entering the host via SSH establishes Unix access, but zero operational control plane privileges. Every mutating request to homelab-appctl.service must present authenticated credentials.

B. Principal Identity & Role Mapping

The platform defines three hierarchical authorization tiers:

Role Permitted Operations Typical Principals
homelab-admin All operations (including Core platform mutation, daemon reload, secret regeneration). Break-glass admin token, human interactive operator (kiskaadee).
homelab-operator Workload mutations (deploy, stop, restart, update, pull, sync_homepage). Cannot mutate Core platform infrastructure. GitOps webhook runner token, automation scripts.
homelab-viewer Read-only operations (list, get, resolve, config, logs, ssl, health). Zero mutations permitted. Homepage dashboard status monitor, read-only diagnostic CLI.

C. Transport Authentication Rules

  1. Unix Domain Socket (/run/homelab/appctl.sock):
    • Socket ownership: root:homelab-operators, file mode 0660.
    • Read-only operations allowed without token if the client UID belongs to homelab-operators.
    • All mutating operations strictly require a valid user-scoped Bearer token passed in the request header (Authorization: Bearer <token>).
  2. HTTP Network Listener (Local LAN / Traefik):
    • External Web: Traefik verifies Authelia session and forwards trusted identity headers (Remote-User, Remote-Groups). Headers are strictly rejected unless incoming connection originates from the trusted Docker proxy-net gateway.
    • Remote CLI / API: Requires user-scoped Bearer token.
  3. GitOps Ingress:
    • Authenticated via HMAC-SHA256 signature (X-Gitea-Signature / X-Hub-Signature-256) against /run/secrets/gitops/webhook_secret.
    • Evaluated as an internal caller with homelab-operator role, constrained strictly to the target repository declared in config/deployments.yaml.

4. Execution & Privilege Boundaries (P4 Prerequisite)

A. Operational Capability Matrix

Each canonical operation is strictly bound to minimal execution permissions, allowable subprocesses, and filesystem boundaries:

Domain Operation Subprocess / Tool Allowed Arguments / API Required Filesystem Roots Authority / Role Required
workload.list In-process Python (provenance.py) Read directory metadata; optional git ls-remote (10s timeout) Read: ~/Sites/, Core/ homelab-viewer
workload.get In-process Python (provenance.py, core_manifest.py) Parse app.yaml, inspect git status Read: ~/Sites/<app>/ homelab-viewer
workload.resolve In-process Python (appctl_engine.py) Name resolution & JSON export Read: ~/Sites/, Core/ homelab-viewer
workload.config /run/current-system/sw/bin/docker compose -f <workload>/docker-compose.yml config Read: ~/Sites/<app>/ homelab-viewer
workload.logs /run/current-system/sw/bin/docker compose -f <workload>/docker-compose.yml logs [--tail=N] [--follow] Read: ~/Sites/<app>/ homelab-viewer
workload.deploy /run/current-system/sw/bin/docker compose -f <workload>/docker-compose.yml up -d --remove-orphans Read/Write: ~/Sites/<app>/, ~/var/lib/homelab/<app>/ homelab-operator
workload.stop /run/current-system/sw/bin/docker compose -f <workload>/docker-compose.yml down Read/Write: ~/Sites/<app>/ homelab-operator
workload.restart /run/current-system/sw/bin/docker compose -f <workload>/docker-compose.yml restart Read/Write: ~/Sites/<app>/ homelab-operator
workload.pull /run/current-system/sw/bin/docker compose -f <workload>/docker-compose.yml pull Read: ~/Sites/<app>/ homelab-operator
workload.update /run/current-system/sw/bin/git
/run/current-system/sw/bin/docker
1. -C <workload> pull --ff-only
2. compose pull
3. compose up -d --remove-orphans
Read/Write: ~/Sites/<app>/, ~/var/lib/homelab/<app>/ homelab-operator
platform.sync_homepage In-process Python (appctl_engine.py) Internal YAML AST generator Read: ~/Sites/
Write: Core/config/homepage/services.yaml
homelab-operator
platform.core_restart /run/current-system/sw/bin/docker compose -f Core/docker-compose.yml restart [service] Read: Core/ homelab-admin

B. Secret Reference Boundary

  1. Daemon Isolation: The control plane daemon does not ingest master decryption keys (key.txt) or decrypt raw SOPS archives.
  2. Ephemeral Secret Projection: The daemon only accesses runtime projections mounted by NixOS/sops-nix under /run/secrets/:
    • /run/secrets/appctl/tokens.json: Hashed token store for API authentication (mode 0400, owned by daemon user).
    • /run/secrets/gitops/webhook_secret: Shared HMAC secret for webhook verification (mode 0400).
    • /run/secrets/rendered/traefik-deployments.env: Projected workload secrets referenced during container startup.
  3. No Secret Material in Logs or Audit: All environment injection is filtered: any environment variable or argument matching *_KEY, *_SECRET, *_PASSWORD, *_TOKEN, or *_HASH is redacted before logging.

C. Systemd Unit Sandboxing (homelab-appctl.service)

Following the pattern established in Milestone P2 for homelab-gitops.service, the daemon runs with systemd sandboxing:

  • ProtectSystem = "strict": Host filesystem is read-only.
  • ProtectHome = "read-only": User home is read-only by default.
  • ReadWritePaths: Explicitly allowlisted writable roots:
    • /home/kiskaadee/Sites (workload definitions and Git checkouts)
    • /home/kiskaadee/var/lib/homelab (workload persistent state roots)
    • /var/log/homelab/appctl or /home/kiskaadee/.local/state/homelab/appctl (audit log directory)
    • /run/homelab (socket runtime directory)
  • PrivateTmp = true: Isolated temporary directories.
  • NoNewPrivileges = true: Disallows privilege escalation.

5. Audit Telemetry Semantics (P4 Prerequisite)

A. Field Schema (RFC-Compliant JSONL)

Every operation lifecycle event is emitted as a structured JSON object to audit.jsonl (mode 0600):

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "audit_version": "1.0",
  "event_id": "evt_01JAX73AB190",
  "timestamp": "2026-10-13T18:32:05.112Z",
  "phase": "COMPLETED",
  "principal": {
    "identity": "kiskaadee",
    "role": "homelab-admin",
    "token_id": "tok_99fa1b",
    "auth_method": "bearer_token"
  },
  "action": "workload.update",
  "target": "docs",
  "transport": "unix_socket",
  "origin": "peer_uid_1000",
  "client": "appctl-cli/2.0",
  "parameters": {
    "recreate": true,
    "remove_orphans": true
  },
  "result": {
    "status": "SUCCESS",
    "exit_code": 0,
    "duration_ms": 3210,
    "error_code": null,
    "message": "Workload updated and reconciled successfully"
  }
}

B. Event Lifecycle States

Phase Description
DISPATCHED Emitted immediately after authentication and validation pass, before invoking Docker/Git.
COMPLETED Emitted when operation terminates cleanly with exit code 0.
FAILED Emitted when validation, Docker, or Git execution raises a non-zero exit or exception.
DENIED Emitted when authentication fails (401) or authorization check fails (403).

C. Failure Handling & Audit Guarantees

  1. Pre-Execution Write: Mutating actions log a DISPATCHED record before calling external binaries. If the daemon crashes or the host loses power mid-operation, the pending operation is permanently preserved in the audit log.
  2. Fail-Closed Disk Guard: If writing to audit.jsonl fails (e.g. out of disk space, permission error), the daemon immediately aborts the requested mutation and returns ERR_EXECUTION_FAILED. Unaudited mutations are strictly prohibited.
  3. Audit Log Location & Service Ownership:
    • Primary: /var/log/homelab/appctl/audit.jsonl (systemd service root) or ~/.local/state/homelab/appctl/audit.jsonl.
    • Permissions: Mode 0600 owned by homelab-appctl service user.

6. P4 Definition of Done & Acceptance Test Matrix

Before marking Milestone P4 complete, the following automated regression suite must pass:

Test Suite Matrix

Test Suite / Area Assertion / Invariant Verified Failure Action
Transport Parity test_transport_dual_equivalence: Compare outputs of appctl list via Unix socket vs HTTP GET /workloads. Assert 100% semantic and data parity. Reject PR
Unauthenticated Rejection test_auth_missing_token_fails_closed: POST /operations/deploy without Authorization header returns 401 Unauthorized and executes zero subprocesses. Block Release
RBAC Authorization Enforcement test_rbac_viewer_mutation_denied: Present valid token with homelab-viewer role against POST /operations/restart. Assert 403 Forbidden and zero container restarts. Block Release
Path Traversal Containment test_path_traversal_rejection: Submit target ../../etc or /var/run. Assert ERR_VALIDATION_FAILED (400) and zero filesystem escape. Block Release
Truthful Error Reporting test_execution_failure_audit: Mock failing Docker compose run (exit 1). Assert daemon returns ERR_EXECUTION_FAILED (500) and records phase: FAILED in audit log. Reject PR
Audit Persistence Gate test_audit_failure_blocks_mutation: Mock read-only audit log. Assert mutating command fails closed with zero Docker invocations. Block Release
Daemon Crash Recovery test_daemon_restart_preserves_state: Kill and restart homelab-appctl.service. Assert running container stacks are undisturbed and desired Git state remains intact. Reject PR
Existing Invariant Regression Run ./scripts/test: All 84 existing unit, security, and nix flake tests pass with 0 regressions. Block Merge