diff --git a/scripts/appctl_contracts.py b/scripts/appctl_contracts.py index 1a0eaa7..9b10297 100644 --- a/scripts/appctl_contracts.py +++ b/scripts/appctl_contracts.py @@ -16,6 +16,7 @@ from enum import StrEnum from typing import Annotated, Any, Literal from pydantic import ( + AliasChoices, BaseModel, BeforeValidator, ConfigDict, @@ -243,9 +244,14 @@ class OperationRequest(StrictModel): model_config = ConfigDict(extra="forbid", strict=True, populate_by_name=True) version: Literal["1.0"] = "1.0" - operation: FlexibleOperationType = Field(alias="action") + operation: FlexibleOperationType = Field( + validation_alias=AliasChoices("operation", "action") + ) target: Annotated[StrictStr, Field(min_length=1)] | None = None - parameters: dict[str, Any] = Field(default_factory=dict, alias="params") + parameters: dict[str, Any] = Field( + default_factory=dict, + validation_alias=AliasChoices("parameters", "params"), + ) @property def action(self) -> OperationType: @@ -285,7 +291,9 @@ class OperationResponse(StrictModel): success: StrictBool | None = None status: Literal["success", "error"] | None = None operation_id: StrictStr = Field(min_length=1) - operation: FlexibleOperationType = Field(alias="action") + operation: FlexibleOperationType = Field( + validation_alias=AliasChoices("operation", "action") + ) target: StrictStr | None = None output: StrictStr = "" result: dict[str, Any] | None = None diff --git a/scripts/appctl_router.py b/scripts/appctl_router.py new file mode 100644 index 0000000..67e25be --- /dev/null +++ b/scripts/appctl_router.py @@ -0,0 +1,840 @@ +""" +appctl_router.py — Operational Router & Unified Execution Engine (P4 Slice 4) + +Implements the single command execution pipeline for homelab-appctl.service: +- Pure-Python inert .env loader (enforcing ENV_KEY_RE, RESERVED_ENV_KEYS, zero shell evaluation) +- Canonical domain precedence resolution (HOMELAB_DOMAIN -> DOMAIN_SUFFIX -> DOMAIN) +- Mandatory pre-flight validation (Path containment, parse_manifest, validate_compose_security) +- Bounded subprocess execution engine (fixed argv, shell=False, 256 KiB output cap, hard timeouts) +- Revision-consistent workload.update (clean tree check, git pull, post-pull validation, declared action execution) +- Synchronous atomic Homepage compilation under _homepage_sync.lock +""" + +from __future__ import annotations + +import os +import subprocess +import time +from pathlib import Path +from typing import Any + +from appctl_audit import ( + AuditLogger, + AuditPersistenceError, + ConcurrentMutationError, + PrincipalAudit, + TargetLockManager, + current_iso_timestamp, + generate_operation_id, + redact_sensitive_data, +) +from appctl_auth import ( + AuthorizationError, + Principal, + Role, + enforce_rbac, + load_admitted_workloads, +) +from appctl_contracts import ( + MUTATING_OPERATIONS, + SENSITIVE_READ_OPERATIONS, + TARGET_REQUIRED_OPERATIONS, + DeployParams, + ErrorCode, + GetParams, + ListParams, + LogsParams, + OperationError, + OperationRequest, + OperationResponse, + OperationType, + UpdateParams, +) +from appctl_engine import ( + CORE_DIR as ENGINE_CORE_DIR, +) +from appctl_engine import ( + HOMELAB_DOMAIN as ENGINE_HOMELAB_DOMAIN, +) +from appctl_engine import ( + SITES_DIR as ENGINE_SITES_DIR, +) +from appctl_engine import ( + check_ssl_cert, + cmd_sync_homepage, + get_sites_apps, + resolve_app, +) +from core_manifest import ( + ENV_KEY_RE, + RESERVED_ENV_KEYS, + Manifest, + ManifestValidationError, + parse_manifest, +) +from gitops_dispatcher import validate_compose_security + +MAX_OUTPUT_BYTES = 256 * 1024 # 256 KiB cap per invocation + + +class RouterValidationError(Exception): + """Raised when request payload or pre-flight validation fails (400 Bad Request).""" + + def __init__(self, message: str) -> None: + super().__init__(message) + self.code = ErrorCode.ERR_VALIDATION_FAILED + self.http_status = 400 + + +class RouterExecutionError(Exception): + """Raised when a subprocess or internal operation fails (500 Internal Error).""" + + def __init__(self, message: str, exit_code: int = 1) -> None: + super().__init__(message) + self.code = ErrorCode.ERR_EXECUTION_FAILED + self.exit_code = exit_code + self.http_status = 500 + + +def parse_dotenv(path: Path, validate_keys: bool = True) -> dict[str, str]: + """ + Pure-Python inert KEY=VALUE parser. + Never invokes a shell or evaluates variables. Enforces ENV_KEY_RE and RESERVED_ENV_KEYS. + """ + if not path.is_file(): + return {} + + env: dict[str, str] = {} + content = path.read_text(encoding="utf-8", errors="replace") + for line_num, raw_line in enumerate(content.splitlines(), start=1): + line = raw_line.strip() + if not line or line.startswith("#"): + continue + if "=" not in line: + continue + key, val = line.split("=", 1) + key = key.strip() + val = val.strip() + + # Strip optional surrounding single or double quotes + if len(val) >= 2 and ( + (val.startswith('"') and val.endswith('"')) + or (val.startswith("'") and val.endswith("'")) + ): + val = val[1:-1] + + if validate_keys: + if not ENV_KEY_RE.match(key): + raise RouterValidationError( + f"Prohibited environment key syntax in {path.name}:{line_num}: '{key}'" + ) + if key in RESERVED_ENV_KEYS: + raise RouterValidationError( + f"Prohibited reserved environment key in {path.name}:{line_num}: '{key}'" + ) + env[key] = val + return env + + +def resolve_canonical_domain() -> str: + """Resolve canonical platform domain: HOMELAB_DOMAIN -> DOMAIN_SUFFIX -> DOMAIN (fails closed).""" + domain = ( + os.environ.get("HOMELAB_DOMAIN") + or os.environ.get("DOMAIN_SUFFIX") + or os.environ.get("DOMAIN") + or ENGINE_HOMELAB_DOMAIN + ) + if not domain or not domain.strip(): + raise RouterValidationError( + "Canonical domain is unset: HOMELAB_DOMAIN, DOMAIN_SUFFIX, or DOMAIN must be configured." + ) + return domain.strip() + + +def assemble_workload_env( + app_dir: Path, + manifest: Manifest | None, + app_name: str, +) -> dict[str, str]: + """ + Assemble the 6-layer in-memory execution environment for Docker Compose subprocesses: + 1. Base PATH and DOCKER_CONFIG + 2. Global rendered secrets (homeserver.env, traefik-deployments.env) + 3. Canonical domain lock + 4. Infrastructure defaults (PROXY_NETWORK, CERT_RESOLVER, SERVICE_NAME, DOMAIN) + 5. Validated manifest.env from app.yaml + 6. Workload .env and app.env files parsed inertly + """ + env: dict[str, str] = { + "PATH": os.environ.get("PATH", "/run/current-system/sw/bin:/usr/bin:/bin"), + } + if "DOCKER_CONFIG" in os.environ: + env["DOCKER_CONFIG"] = os.environ["DOCKER_CONFIG"] + + # Layer 2: Global secrets from /run/secrets/rendered/ or /run/secrets/ + secrets_candidates = [ + Path("/run/secrets/rendered/homeserver.env"), + Path("/run/secrets/homeserver.env"), + Path("/run/secrets/rendered/traefik-deployments.env"), + Path("/run/secrets/traefik-deployments.env"), + ] + if "HOMESERVER_ENV_FILE" in os.environ: + secrets_candidates.insert(0, Path(os.environ["HOMESERVER_ENV_FILE"])) + + for sec_path in secrets_candidates: + if sec_path.is_file(): + env.update(parse_dotenv(sec_path, validate_keys=False)) + + # Layer 3 & 4: Domain & Infrastructure defaults + domain = resolve_canonical_domain() + env["HOMELAB_DOMAIN"] = domain + env["DOMAIN_SUFFIX"] = domain + env["DOMAIN"] = domain + env["PROXY_NETWORK"] = os.environ.get("PROXY_NETWORK", "proxy-net") + env["CERT_RESOLVER"] = os.environ.get("CERT_RESOLVER", "myresolver") + env["SERVICE_NAME"] = app_name + env["CONTAINER_NAME"] = app_name + env["SERVICE_DOMAIN"] = f"{app_name}.{domain}" + + # Layer 5: Validated manifest env + if manifest and manifest.env: + env.update(manifest.env) + + # Layer 6: Workload-scoped .env and app.env + for local_env in [app_dir / ".env", app_dir / "app.env"]: + if local_env.is_file(): + env.update(parse_dotenv(local_env, validate_keys=True)) + + return env + + +def run_bounded_subprocess( + cmd: list[str], + cwd: Path | str | None = None, + env: dict[str, str] | None = None, + timeout: float = 300.0, +) -> tuple[int, str, str]: + """ + Execute subprocess with fixed argv (shell=False), hard timeout, and 256 KiB output cap. + Returns (exit_code, stdout_str, stderr_str). + """ + proc = subprocess.Popen( + cmd, + cwd=str(cwd) if cwd else None, + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=False, + ) + try: + raw_stdout, raw_stderr = proc.communicate(timeout=timeout) + except subprocess.TimeoutExpired as exc: + proc.kill() + proc.communicate() + raise RouterExecutionError( + f"Command '{' '.join(cmd)}' timed out after {timeout} seconds", + exit_code=124, + ) from exc + + # Enforce 256 KiB cap on output + stdout_str = raw_stdout[:MAX_OUTPUT_BYTES].decode("utf-8", errors="replace") + if len(raw_stdout) > MAX_OUTPUT_BYTES: + stdout_str += "\n[TRUNCATED: Output exceeded 256 KiB limit]" + + stderr_str = raw_stderr[:MAX_OUTPUT_BYTES].decode("utf-8", errors="replace") + if len(raw_stderr) > MAX_OUTPUT_BYTES: + stderr_str += "\n[TRUNCATED: Stderr exceeded 256 KiB limit]" + + return proc.returncode, stdout_str, stderr_str + + +def validate_workload_preflight( + app_dir: Path, + allowed_root: Path, + app_name: str | None = None, +) -> tuple[Manifest, Path]: + """ + Enforce strict pre-flight gate: + 1. Target directory must be contained within allowed_root. + 2. app.yaml must parse cleanly via core_manifest.parse_manifest(). + 3. docker-compose.yml must pass validate_compose_security(). + """ + app_dir_resolved = app_dir.resolve() + allowed_root_resolved = allowed_root.resolve() + if not ( + app_dir_resolved == allowed_root_resolved + or app_dir_resolved.is_relative_to(allowed_root_resolved) + ): + raise RouterValidationError( + f"Path traversal rejected: '{app_dir}' is outside '{allowed_root}'" + ) + + manifest_file = app_dir / "app.yaml" + if not manifest_file.is_file(): + raise RouterValidationError(f"Workload manifest not found at '{manifest_file}'") + + try: + manifest = parse_manifest(manifest_file) + except ManifestValidationError as err: + raise RouterValidationError(f"Workload manifest validation failed: {err}") from err + + effective_name = app_name or manifest.name + compose_file = app_dir / "docker-compose.yml" + if compose_file.exists(): + ok, reason = validate_compose_security( + compose_file=compose_file, + allowed_root=app_dir, + app_name=effective_name, + ) + if not ok: + raise RouterValidationError( + f"Compose AST security validation failed for '{effective_name}': {reason}" + ) + + return manifest, compose_file + + +class OperationalRouter: + """Executes validated domain operations according to P4 boundaries and contracts.""" + + def __init__( + self, + audit_logger: AuditLogger | None = None, + lock_manager: TargetLockManager | None = None, + sites_dir: Path | str | None = None, + core_dir: Path | str | None = None, + ) -> None: + self.audit_logger = audit_logger or AuditLogger() + self.lock_manager = lock_manager or TargetLockManager() + self.sites_dir = Path(sites_dir or ENGINE_SITES_DIR) + self.core_dir = Path(core_dir or ENGINE_CORE_DIR) + + def dispatch( + self, + request: OperationRequest, + principal: Principal, + transport: str = "unix_socket", + origin: str = "peer_uid_1000", + client: str = "appctl-cli/2.0", + ) -> OperationResponse: + """Single entrypoint dispatching all 13 canonical domain operations.""" + op_id = generate_operation_id() + start_time = time.perf_counter() + target = request.target + operation = request.operation + admitted = load_admitted_workloads() + principal_audit = PrincipalAudit( + identity=principal.identity, + role=principal.role.value, + token_id=principal.token_id, + auth_method=principal.auth_method, + ) + + # 1. RBAC Gate + try: + enforce_rbac( + principal=principal, + operation=operation, + target=target, + admitted_workloads=admitted, + ) + except AuthorizationError as err: + self.audit_logger.log_denied( + operation_id=op_id, + principal=principal_audit, + action=operation.value, + target=target, + transport=transport, + origin=origin, + client=client, + parameters=request.parameters, + error_code=err.code.value, + message=str(err), + ) + return OperationResponse( + version="1.0", + success=False, + operation_id=op_id, + operation=operation, + target=target, + error=OperationError(code=err.code, message=str(err)), + ) + + # 2. Target Resolution (for operations requiring target) + resolved_app = None + if operation in TARGET_REQUIRED_OPERATIONS: + if not target: + return OperationResponse( + version="1.0", + success=False, + operation_id=op_id, + operation=operation, + target=target, + error=OperationError( + code=ErrorCode.ERR_VALIDATION_FAILED, + message=f"Operation '{operation.value}' requires a non-empty target", + ), + ) + resolved_app = resolve_app(target) + if not resolved_app: + return OperationResponse( + version="1.0", + success=False, + operation_id=op_id, + operation=operation, + target=target, + error=OperationError( + code=ErrorCode.ERR_TARGET_NOT_FOUND, + message=f"Workload or core target '{target}' not found", + ), + ) + + # 3. Concurrency Lock & Pre-Execution Audit (Mutating Operations) + if operation in MUTATING_OPERATIONS: + lock_ctx = ( + self.lock_manager.homepage_sync_lock() + if operation == OperationType.PLATFORM_SYNC_HOMEPAGE + else self.lock_manager.lock_target(target or "_unknown") + ) + try: + with lock_ctx: + # Fail-closed pre-execution DISPATCHED audit guard + try: + self.audit_logger.log_dispatched( + operation_id=op_id, + principal=principal_audit, + action=operation.value, + target=target, + transport=transport, + origin=origin, + client=client, + parameters=request.parameters, + ) + except AuditPersistenceError as audit_err: + return OperationResponse( + version="1.0", + success=False, + operation_id=op_id, + operation=operation, + target=target, + error=OperationError( + code=ErrorCode.ERR_EXECUTION_FAILED, + message=f"Pre-execution audit guard failed: {audit_err}", + ), + ) + + # Execute Mutation + return self._execute_operation( + request=request, + principal=principal, + principal_audit=principal_audit, + resolved_app=resolved_app, + op_id=op_id, + start_time=start_time, + transport=transport, + origin=origin, + client=client, + ) + except ConcurrentMutationError as lock_err: + self.audit_logger.log_denied( + operation_id=op_id, + principal=principal_audit, + action=operation.value, + target=target, + transport=transport, + origin=origin, + client=client, + parameters=request.parameters, + error_code=ErrorCode.ERR_CONCURRENT_MUTATION.value, + message=str(lock_err), + ) + return OperationResponse( + version="1.0", + success=False, + operation_id=op_id, + operation=operation, + target=target, + error=OperationError( + code=ErrorCode.ERR_CONCURRENT_MUTATION, + message=str(lock_err), + ), + ) + + # 4. Read-Only Operations (no concurrency lock required) + return self._execute_operation( + request=request, + principal=principal, + principal_audit=principal_audit, + resolved_app=resolved_app, + op_id=op_id, + start_time=start_time, + transport=transport, + origin=origin, + client=client, + ) + + def _execute_operation( + self, + request: OperationRequest, + principal: Principal, + principal_audit: PrincipalAudit, + resolved_app: Any, + op_id: str, + start_time: float, + transport: str, + origin: str, + client: str, + ) -> OperationResponse: + """Route to specific operation handler with post-execution audit logging.""" + operation = request.operation + target = request.target + commit_sha: str | None = None + duration_ms = 0 + + try: + if operation == OperationType.WORKLOAD_LIST: + result_data = self._op_workload_list(request) + elif operation == OperationType.WORKLOAD_GET: + result_data = self._op_workload_get(request, resolved_app) + elif operation == OperationType.WORKLOAD_RESOLVE: + result_data = self._op_workload_resolve(resolved_app) + elif operation == OperationType.PLATFORM_SSL: + result_data = self._op_platform_ssl(target) + elif operation == OperationType.PLATFORM_HEALTH: + result_data = self._op_platform_health() + elif operation == OperationType.WORKLOAD_CONFIG: + result_data = self._op_workload_config(resolved_app, principal) + elif operation == OperationType.WORKLOAD_LOGS: + result_data = self._op_workload_logs(request, resolved_app) + elif operation == OperationType.WORKLOAD_DEPLOY: + result_data = self._op_workload_deploy(request, resolved_app) + elif operation == OperationType.WORKLOAD_STOP: + result_data = self._op_workload_stop(resolved_app) + elif operation == OperationType.WORKLOAD_RESTART: + result_data = self._op_workload_restart(resolved_app) + elif operation == OperationType.WORKLOAD_PULL: + result_data = self._op_workload_pull(resolved_app) + elif operation == OperationType.WORKLOAD_UPDATE: + result_data, commit_sha = self._op_workload_update(request, resolved_app) + elif operation == OperationType.PLATFORM_SYNC_HOMEPAGE: + result_data = self._op_platform_sync_homepage() + else: + raise RouterValidationError(f"Unsupported operation: {operation.value}") + + duration_ms = int((time.perf_counter() - start_time) * 1000) + + # Audit COMPLETED for mutating ops and sensitive reads + if operation in MUTATING_OPERATIONS or operation in SENSITIVE_READ_OPERATIONS: + self.audit_logger.log_completed( + operation_id=op_id, + principal=principal_audit, + action=operation.value, + target=target, + transport=transport, + origin=origin, + client=client, + parameters=request.parameters, + duration_ms=duration_ms, + commit_sha=commit_sha, + message=result_data.get("message", "Operation completed successfully"), + ) + + return OperationResponse( + version="1.0", + success=True, + operation_id=op_id, + operation=operation, + target=target, + result=result_data, + output=result_data.get("output", ""), + duration_ms=duration_ms, + timestamp=current_iso_timestamp(), + ) + + except (RouterValidationError, RouterExecutionError) as err: + duration_ms = int((time.perf_counter() - start_time) * 1000) + if operation in MUTATING_OPERATIONS or operation in SENSITIVE_READ_OPERATIONS: + self.audit_logger.log_failed( + operation_id=op_id, + principal=principal_audit, + action=operation.value, + target=target, + transport=transport, + origin=origin, + client=client, + parameters=request.parameters, + duration_ms=duration_ms, + exit_code=getattr(err, "exit_code", 1), + error_code=err.code.value, + message=str(err), + ) + return OperationResponse( + version="1.0", + success=False, + operation_id=op_id, + operation=operation, + target=target, + duration_ms=duration_ms, + error=OperationError(code=err.code, message=str(err)), + ) + + # ----------------------------------------------------------------------- + # Domain Operation Implementations + # ----------------------------------------------------------------------- + + def _op_workload_list(self, request: OperationRequest) -> dict[str, Any]: + params = request.parse_parameters() + assert isinstance(params, ListParams) + apps = get_sites_apps(sites_dir=str(self.sites_dir)) + apps_data = [a.to_dict() for a in apps] + return { + "total": len(apps_data), + "workloads": apps_data, + "message": f"Found {len(apps_data)} workloads", + } + + def _op_workload_get(self, request: OperationRequest, app: Any) -> dict[str, Any]: + params = request.parse_parameters() + assert isinstance(params, GetParams) + data = app.to_dict() + return {"workload": data, "message": f"Retrieved metadata for {app.name}"} + + def _op_workload_resolve(self, app: Any) -> dict[str, Any]: + return {"workload": app.to_dict(), "message": f"Resolved target to {app.name}"} + + def _op_platform_ssl(self, target: str | None) -> dict[str, Any]: + domain = target or resolve_canonical_domain() + try: + cert_info = check_ssl_cert(domain) + return {"ssl": cert_info, "message": f"SSL certificate inspected for {domain}"} + except Exception as err: + raise RouterExecutionError(f"SSL inspection failed for '{domain}': {err}") from err + + def _op_platform_health(self) -> dict[str, Any]: + return { + "status": "healthy", + "audit_log": str(self.audit_logger.log_path), + "lock_dir": str(self.lock_manager.lock_dir), + "message": "Control plane daemon healthy", + } + + def _op_workload_config(self, app: Any, principal: Principal) -> dict[str, Any]: + app_dir = Path(app.dir_path) + manifest, compose_file = validate_workload_preflight(app_dir, self.sites_dir, app.name) + env = assemble_workload_env(app_dir, manifest, app.name) + + cmd = ["docker", "compose", "-f", str(compose_file), "config"] + code, stdout, stderr = run_bounded_subprocess(cmd, cwd=app_dir, env=env, timeout=15.0) + if code != 0: + raise RouterExecutionError(f"docker compose config failed: {stderr}", exit_code=code) + + config_output = stdout + if principal.role != Role.ADMIN: + # Redact secrets for non-admin callers + config_output = redact_sensitive_data(config_output) + + return {"output": config_output, "message": "Evaluated compose configuration"} + + def _op_workload_logs(self, request: OperationRequest, app: Any) -> dict[str, Any]: + params = request.parse_parameters() + assert isinstance(params, LogsParams) + app_dir = Path(app.dir_path) + manifest, compose_file = validate_workload_preflight(app_dir, self.sites_dir, app.name) + env = assemble_workload_env(app_dir, manifest, app.name) + + cmd = [ + "docker", + "compose", + "-f", + str(compose_file), + "logs", + "--no-color", + "--tail", + str(params.tail), + ] + code, stdout, stderr = run_bounded_subprocess(cmd, cwd=app_dir, env=env, timeout=15.0) + if code != 0: + raise RouterExecutionError(f"docker compose logs failed: {stderr}", exit_code=code) + + return {"output": stdout, "tail": params.tail, "message": f"Tail {params.tail} logs"} + + def _op_workload_deploy(self, request: OperationRequest, app: Any) -> dict[str, Any]: + params = request.parse_parameters() + assert isinstance(params, DeployParams) + app_dir = Path(app.dir_path) + manifest, compose_file = validate_workload_preflight(app_dir, self.sites_dir, app.name) + env = assemble_workload_env(app_dir, manifest, app.name) + + # Optional build + if params.build: + build_cmd = ["docker", "compose", "-f", str(compose_file), "build"] + code, out, err = run_bounded_subprocess(build_cmd, cwd=app_dir, env=env, timeout=300.0) + if code != 0: + raise RouterExecutionError(f"docker compose build failed: {err}", exit_code=code) + + # Deploy up + up_cmd = ["docker", "compose", "-f", str(compose_file), "up", "-d"] + if params.remove_orphans: + up_cmd.append("--remove-orphans") + + code, out, err = run_bounded_subprocess(up_cmd, cwd=app_dir, env=env, timeout=300.0) + if code != 0: + raise RouterExecutionError(f"docker compose up failed: {err}", exit_code=code) + + # Trigger Homepage sync + self._trigger_homepage_sync() + + return { + "output": out, + "action": "deployed", + "message": f"Workload '{app.name}' deployed successfully", + } + + def _op_workload_stop(self, app: Any) -> dict[str, Any]: + app_dir = Path(app.dir_path) + manifest, compose_file = validate_workload_preflight(app_dir, self.sites_dir, app.name) + env = assemble_workload_env(app_dir, manifest, app.name) + + cmd = ["docker", "compose", "-f", str(compose_file), "down"] + code, out, err = run_bounded_subprocess(cmd, cwd=app_dir, env=env, timeout=300.0) + if code != 0: + raise RouterExecutionError(f"docker compose down failed: {err}", exit_code=code) + + self._trigger_homepage_sync() + return {"output": out, "action": "stopped", "message": f"Workload '{app.name}' stopped"} + + def _op_workload_restart(self, app: Any) -> dict[str, Any]: + app_dir = Path(app.dir_path) + manifest, compose_file = validate_workload_preflight(app_dir, self.sites_dir, app.name) + env = assemble_workload_env(app_dir, manifest, app.name) + + cmd = ["docker", "compose", "-f", str(compose_file), "restart"] + code, out, err = run_bounded_subprocess(cmd, cwd=app_dir, env=env, timeout=300.0) + if code != 0: + raise RouterExecutionError(f"docker compose restart failed: {err}", exit_code=code) + + self._trigger_homepage_sync() + return {"output": out, "action": "restarted", "message": f"Workload '{app.name}' restarted"} + + def _op_workload_pull(self, app: Any) -> dict[str, Any]: + app_dir = Path(app.dir_path) + manifest, compose_file = validate_workload_preflight(app_dir, self.sites_dir, app.name) + env = assemble_workload_env(app_dir, manifest, app.name) + + pull_cmd = ["docker", "compose", "-f", str(compose_file), "pull"] + code, _, err = run_bounded_subprocess(pull_cmd, cwd=app_dir, env=env, timeout=300.0) + if code != 0: + raise RouterExecutionError(f"docker compose pull failed: {err}", exit_code=code) + + up_cmd = ["docker", "compose", "-f", str(compose_file), "up", "-d", "--remove-orphans"] + code, out_up, err_up = run_bounded_subprocess(up_cmd, cwd=app_dir, env=env, timeout=300.0) + if code != 0: + raise RouterExecutionError(f"docker compose up failed after pull: {err_up}", exit_code=code) + + self._trigger_homepage_sync() + return {"output": out_up, "action": "pulled", "message": f"Workload '{app.name}' pulled and updated"} + + def _op_workload_update( + self, request: OperationRequest, app: Any + ) -> tuple[dict[str, Any], str | None]: + """ + Revision-consistent workload update pipeline (DEC-04): + 1. Clean working tree check (git status --porcelain, 5s) + 2. git pull --ff-only (60s) + 3. Post-pull re-validation of app.yaml & expected_branch & compose security + 4. Execute declared actions (compose_pull, compose_build, compose_up, compose_restart) + 5. Homepage sync + """ + params = request.parse_parameters() + assert isinstance(params, UpdateParams) + app_dir = Path(app.dir_path) + + # 1. Clean working tree check + code, status_out, _ = run_bounded_subprocess( + ["git", "-C", str(app_dir), "status", "--porcelain"], + cwd=app_dir, + timeout=5.0, + ) + if code != 0 or status_out.strip(): + raise RouterValidationError( + f"Workload '{app.name}' has uncommitted changes in git working tree; update aborted." + ) + + # 2. git pull --ff-only + pull_cmd = ["git", "-C", str(app_dir), "pull", "--ff-only"] + if params.expected_branch: + pull_cmd.extend(["origin", params.expected_branch]) + code, pull_out, pull_err = run_bounded_subprocess( + pull_cmd, cwd=app_dir, timeout=60.0 + ) + if code != 0: + raise RouterExecutionError(f"git pull failed for '{app.name}': {pull_err}", exit_code=code) + + # 3. Post-pull re-validation of manifest and expected_branch + manifest, compose_file = validate_workload_preflight(app_dir, self.sites_dir, app.name) + if ( + params.expected_branch + and manifest.source + and manifest.source.ref != params.expected_branch + ): + raise RouterValidationError( + f"Post-pull branch mismatch for '{app.name}': expected '{params.expected_branch}', " + f"manifest declared '{manifest.source.ref}'" + ) + + # 4. Execute declared actions from manifest + env = assemble_workload_env(app_dir, manifest, app.name) + declared_actions = manifest.deployment.actions or ["compose_up"] + output_log = [f"git: {pull_out.strip()}"] + + for action in declared_actions: + if action == "git_pull": + continue # already executed in step 2 + elif action == "compose_pull": + cmd = ["docker", "compose", "-f", str(compose_file), "pull"] + elif action == "compose_build" or (params.build and action == "compose_up"): + cmd = ["docker", "compose", "-f", str(compose_file), "build"] + elif action == "compose_up": + cmd = ["docker", "compose", "-f", str(compose_file), "up", "-d", "--remove-orphans"] + elif action == "compose_restart": + cmd = ["docker", "compose", "-f", str(compose_file), "restart"] + else: + continue + + code, act_out, act_err = run_bounded_subprocess(cmd, cwd=app_dir, env=env, timeout=300.0) + if code != 0: + raise RouterExecutionError( + f"Action '{action}' failed during workload.update for '{app.name}': {act_err}", + exit_code=code, + ) + output_log.append(f"{action}: {act_out.strip()}") + + # 5. Extract current commit SHA + _, sha_out, _ = run_bounded_subprocess( + ["git", "-C", str(app_dir), "rev-parse", "--short", "HEAD"], + cwd=app_dir, + timeout=5.0, + ) + commit_sha = sha_out.strip() or None + + # 6. Trigger Homepage sync + self._trigger_homepage_sync() + + return { + "output": "\n".join(output_log), + "commit_sha": commit_sha, + "actions": declared_actions, + "message": f"Workload '{app.name}' updated and reconciled successfully", + }, commit_sha + + def _op_platform_sync_homepage(self) -> dict[str, Any]: + self._trigger_homepage_sync() + return {"action": "sync_homepage", "message": "Homepage dashboard synchronized successfully"} + + def _trigger_homepage_sync(self) -> None: + """Atomic compile of Homepage dashboard services.yaml under _homepage_sync.lock.""" + try: + cmd_sync_homepage() + except (OSError, ValueError, RuntimeError) as exc: + import sys + print(f"Warning: Homepage sync non-fatal error: {exc}", file=sys.stderr) diff --git a/tests/unit/test_appctl_router.py b/tests/unit/test_appctl_router.py new file mode 100644 index 0000000..4bec44f --- /dev/null +++ b/tests/unit/test_appctl_router.py @@ -0,0 +1,511 @@ +""" +tests/unit/test_appctl_router.py — Unit Tests for Operational Router & Execution Engine (P4 Slice 4) + +Behavior-driven test naming convention: +test___ +""" + +from __future__ import annotations + +from pathlib import Path +from unittest.mock import MagicMock, patch + +import pytest +from appctl_audit import AuditLogger, TargetLockManager +from appctl_auth import Principal, Role +from appctl_contracts import ErrorCode, OperationRequest +from appctl_router import ( + MAX_OUTPUT_BYTES, + OperationalRouter, + RouterExecutionError, + RouterValidationError, + assemble_workload_env, + parse_dotenv, + resolve_canonical_domain, + run_bounded_subprocess, + validate_workload_preflight, +) +from core_manifest import parse_manifest + + +@pytest.fixture(autouse=True) +def set_test_domain(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("HOMELAB_DOMAIN", "example.test") + + +@pytest.fixture +def mock_sites_dir(tmp_path: Path) -> Path: + sites = tmp_path / "Sites" + sites.mkdir(parents=True, exist_ok=True) + return sites + + +@pytest.fixture +def mock_core_dir(tmp_path: Path) -> Path: + core = tmp_path / "Core" + core.mkdir(parents=True, exist_ok=True) + return core + + +@pytest.fixture +def mock_workload_dir(mock_sites_dir: Path) -> Path: + app_dir = mock_sites_dir / "snappymail" + app_dir.mkdir(parents=True, exist_ok=True) + + manifest_content = """ +schema_version: "1.0" +name: "snappymail" +source: + type: "git" + url: "https://github.com/example/snappymail.git" + ref: "main" +deployment: + strategy: "compose" + actions: + - "git_pull" + - "compose_pull" + - "compose_up" +env: + WORKLOAD_CUSTOM_VAR: "custom_value" +""" + (app_dir / "app.yaml").write_text(manifest_content.strip(), encoding="utf-8") + + compose_content = """ +services: + webmail: + image: "snappymail:latest" + container_name: "snappymail" + restart: "unless-stopped" +""" + (app_dir / "docker-compose.yml").write_text(compose_content.strip(), encoding="utf-8") + return app_dir + + +@pytest.fixture +def admin_principal() -> Principal: + return Principal(identity="operator", role=Role.ADMIN, auth_method="bearer_token") + + +@pytest.fixture +def operator_principal() -> Principal: + return Principal(identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token") + + +@pytest.fixture +def viewer_principal() -> Principal: + return Principal(identity="unix_uid_1000", role=Role.VIEWER, auth_method="unix_peercred") + + +# --------------------------------------------------------------------------- +# Pure-Python Inert Dotenv Parser +# --------------------------------------------------------------------------- + + +def test_parse_dotenv_parses_valid_key_values(tmp_path: Path): + env_file = tmp_path / ".env" + env_file.write_text(""" +# Comment line +APP_PORT=8080 +APP_HOST="127.0.0.1" +SINGLE_QUOTE='single_val' +EMPTY_VAL= +WITH_EQUALS=key=value=more +""", encoding="utf-8") + + parsed = parse_dotenv(env_file) + assert parsed["APP_PORT"] == "8080" + assert parsed["APP_HOST"] == "127.0.0.1" + assert parsed["SINGLE_QUOTE"] == "single_val" + assert parsed["EMPTY_VAL"] == "" + assert parsed["WITH_EQUALS"] == "key=value=more" + + +def test_parse_dotenv_preserves_shell_injection_as_inert_literals(tmp_path: Path): + env_file = tmp_path / ".env" + env_file.write_text(""" +INJECTION_ONE=$(touch /tmp/pwned) +INJECTION_TWO=`cat /etc/passwd` +VARIABLE_REF=$HOMELAB_DOMAIN +""", encoding="utf-8") + + parsed = parse_dotenv(env_file) + assert parsed["INJECTION_ONE"] == "$(touch /tmp/pwned)" + assert parsed["INJECTION_TWO"] == "`cat /etc/passwd`" + assert parsed["VARIABLE_REF"] == "$HOMELAB_DOMAIN" + assert not Path("/tmp/pwned").exists() + + +def test_parse_dotenv_rejects_invalid_key_syntax(tmp_path: Path): + bad_keys = ["APP-PORT=80", "123NUM=val", "KEY WITH SPACES=val", "@INVALID=1"] + for bad in bad_keys: + env_file = tmp_path / ".env" + env_file.write_text(bad, encoding="utf-8") + with pytest.raises(RouterValidationError) as exc_info: + parse_dotenv(env_file) + assert "Prohibited environment key syntax" in str(exc_info.value) + + +def test_parse_dotenv_rejects_reserved_environment_keys(tmp_path: Path): + for reserved in ["PATH=foo", "LD_PRELOAD=bar", "SHELL=/bin/bash", "USER=root"]: + env_file = tmp_path / ".env" + env_file.write_text(reserved, encoding="utf-8") + with pytest.raises(RouterValidationError) as exc_info: + parse_dotenv(env_file) + assert "Prohibited reserved environment key" in str(exc_info.value) + + +# --------------------------------------------------------------------------- +# Canonical Domain Resolution & Environment Assembly +# --------------------------------------------------------------------------- + + +def test_resolve_canonical_domain_precedence(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("HOMELAB_DOMAIN", "domain-a.test") + monkeypatch.setenv("DOMAIN_SUFFIX", "domain-b.test") + monkeypatch.setenv("DOMAIN", "domain-c.test") + assert resolve_canonical_domain() == "domain-a.test" + + monkeypatch.delenv("HOMELAB_DOMAIN") + assert resolve_canonical_domain() == "domain-b.test" + + monkeypatch.delenv("DOMAIN_SUFFIX") + assert resolve_canonical_domain() == "domain-c.test" + + +def test_assemble_workload_env_constructs_6_layer_environment( + mock_workload_dir: Path, monkeypatch: pytest.MonkeyPatch +): + monkeypatch.setenv("HOMELAB_DOMAIN", "example.test") + manifest = parse_manifest(mock_workload_dir / "app.yaml") + + # Add local .env + (mock_workload_dir / ".env").write_text("LOCAL_ENV_VAR=local_val\n", encoding="utf-8") + + env = assemble_workload_env(mock_workload_dir, manifest, "snappymail") + + # Layer 1 + assert "PATH" in env + # Layer 3 & 4 + assert env["HOMELAB_DOMAIN"] == "example.test" + assert env["SERVICE_NAME"] == "snappymail" + assert env["SERVICE_DOMAIN"] == "snappymail.example.test" + assert env["PROXY_NETWORK"] == "proxy-net" + # Layer 5 (manifest env) + assert env["WORKLOAD_CUSTOM_VAR"] == "custom_value" + # Layer 6 (.env) + assert env["LOCAL_ENV_VAR"] == "local_val" + + +# --------------------------------------------------------------------------- +# Bounded Subprocess Execution Engine +# --------------------------------------------------------------------------- + + +def test_run_bounded_subprocess_executes_valid_command(): + code, out, err = run_bounded_subprocess(["echo", "hello world"]) + assert code == 0 + assert out.strip() == "hello world" + assert err == "" + + +def test_run_bounded_subprocess_enforces_256_kib_cap(): + # Produce > 256 KiB of output + python_cmd = [ + "python3", + "-c", + f"import sys; sys.stdout.write('A' * ({MAX_OUTPUT_BYTES} + 1024))", + ] + code, out, _ = run_bounded_subprocess(python_cmd) + assert code == 0 + assert len(out.encode("utf-8")) <= MAX_OUTPUT_BYTES + 200 + assert "[TRUNCATED: Output exceeded 256 KiB limit]" in out + + +def test_run_bounded_subprocess_times_out(): + cmd = ["sleep", "2"] + with pytest.raises(RouterExecutionError) as exc_info: + run_bounded_subprocess(cmd, timeout=0.1) + assert "timed out after 0.1 seconds" in str(exc_info.value) + assert exc_info.value.exit_code == 124 + + +# --------------------------------------------------------------------------- +# Workload Pre-Flight Gate +# --------------------------------------------------------------------------- + + +def test_validate_workload_preflight_accepts_valid_workload( + mock_workload_dir: Path, mock_sites_dir: Path +): + manifest, compose_file = validate_workload_preflight( + mock_workload_dir, mock_sites_dir, "snappymail" + ) + assert manifest.name == "snappymail" + assert compose_file.exists() + + +def test_validate_workload_preflight_rejects_path_traversal( + mock_sites_dir: Path, tmp_path: Path +): + outside_dir = tmp_path / "outside_dir" + outside_dir.mkdir(parents=True, exist_ok=True) + with pytest.raises(RouterValidationError) as exc_info: + validate_workload_preflight(outside_dir, mock_sites_dir) + assert "Path traversal rejected" in str(exc_info.value) + + +def test_validate_workload_preflight_rejects_prohibited_compose_security( + mock_workload_dir: Path, mock_sites_dir: Path +): + # Inject privileged: true into docker-compose.yml + bad_compose = """ +services: + webmail: + image: "snappymail:latest" + privileged: true +""" + (mock_workload_dir / "docker-compose.yml").write_text(bad_compose, encoding="utf-8") + + with pytest.raises(RouterValidationError) as exc_info: + validate_workload_preflight(mock_workload_dir, mock_sites_dir, "snappymail") + assert "Compose AST security validation failed" in str(exc_info.value) + + +# --------------------------------------------------------------------------- +# Operational Router Dispatch Pipeline +# --------------------------------------------------------------------------- + + +def test_operational_router_dispatches_platform_health(tmp_path: Path, admin_principal: Principal): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + ) + req = OperationRequest.model_validate({"operation": "platform.health"}) + resp = router.dispatch(req, admin_principal) + assert resp.success is True + assert resp.result is not None + assert resp.result["status"] == "healthy" + + +def test_operational_router_dispatches_platform_sync_homepage( + tmp_path: Path, admin_principal: Principal +): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + ) + req = OperationRequest.model_validate({"operation": "platform.sync_homepage"}) + with patch("appctl_router.cmd_sync_homepage") as mock_sync: + resp = router.dispatch(req, admin_principal) + assert resp.success is True + mock_sync.assert_called_once() + + +def test_operational_router_enforces_rbac_denial( + tmp_path: Path, viewer_principal: Principal +): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + ) + req = OperationRequest.model_validate({ + "operation": "workload.deploy", + "target": "snappymail", + }) + resp = router.dispatch(req, viewer_principal) + assert resp.success is False + assert resp.error is not None + assert resp.error.code == ErrorCode.ERR_PERMISSION_DENIED + + +def test_operational_router_rejects_non_existent_target( + tmp_path: Path, admin_principal: Principal +): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + ) + req = OperationRequest.model_validate({ + "operation": "workload.get", + "target": "nonexistent_workload_999", + }) + resp = router.dispatch(req, admin_principal) + assert resp.success is False + assert resp.error is not None + assert resp.error.code == ErrorCode.ERR_TARGET_NOT_FOUND + + +def test_operational_router_rejects_concurrent_mutation( + tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path +): + lock_mgr = TargetLockManager(tmp_path / "locks") + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=lock_mgr, + ) + req = OperationRequest.model_validate({ + "operation": "workload.deploy", + "target": "snappymail", + }) + + mock_app = MagicMock() + mock_app.name = "snappymail" + mock_app.dir_path = str(mock_workload_dir) + + # Hold the lock on snappymail + with ( + patch("appctl_router.resolve_app", return_value=mock_app), + lock_mgr.lock_target("snappymail"), + ): + resp = router.dispatch(req, admin_principal) + assert resp.success is False + assert resp.error is not None + assert resp.error.code == ErrorCode.ERR_CONCURRENT_MUTATION + + +def test_operational_router_dispatches_workload_deploy( + tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path +): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + sites_dir=mock_workload_dir.parent, + ) + req = OperationRequest.model_validate({ + "operation": "workload.deploy", + "target": "snappymail", + "parameters": {"recreate": True, "remove_orphans": True, "build": False}, + }) + + mock_app = MagicMock() + mock_app.name = "snappymail" + mock_app.dir_path = str(mock_workload_dir) + + with ( + patch("appctl_router.resolve_app", return_value=mock_app), + patch("appctl_router.run_bounded_subprocess", return_value=(0, "Container started", "")) as mock_run, + patch("appctl_router.cmd_sync_homepage"), + ): + resp = router.dispatch(req, admin_principal) + assert resp.success is True + assert resp.result is not None + assert resp.result["action"] == "deployed" + mock_run.assert_called_once() + assert "up" in mock_run.call_args[0][0] + + +def test_operational_router_workload_update_rejects_dirty_git_tree( + tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path +): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + sites_dir=mock_workload_dir.parent, + ) + req = OperationRequest.model_validate({ + "operation": "workload.update", + "target": "snappymail", + }) + + mock_app = MagicMock() + mock_app.name = "snappymail" + mock_app.dir_path = str(mock_workload_dir) + + # Simulate dirty working tree from git status --porcelain + def mock_subproc(cmd: list[str], **kwargs): + if "status" in cmd: + return 0, " M app.yaml\n", "" + return 0, "", "" + + with ( + patch("appctl_router.resolve_app", return_value=mock_app), + patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc), + ): + resp = router.dispatch(req, admin_principal) + assert resp.success is False + assert resp.error is not None + assert resp.error.code == ErrorCode.ERR_VALIDATION_FAILED + assert "uncommitted changes" in resp.error.message + + +def test_operational_router_workload_update_rejects_branch_mismatch( + tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path +): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + sites_dir=mock_workload_dir.parent, + ) + req = OperationRequest.model_validate({ + "operation": "workload.update", + "target": "snappymail", + "parameters": {"expected_branch": "feature/branch"}, + }) + + mock_app = MagicMock() + mock_app.name = "snappymail" + mock_app.dir_path = str(mock_workload_dir) + + def mock_subproc(cmd: list[str], **kwargs): + if "status" in cmd: + return 0, "", "" + if "pull" in cmd: + return 0, "Fast-forward", "" + if "rev-parse" in cmd: + return 0, "1234567", "" + return 0, "", "" + + with ( + patch("appctl_router.resolve_app", return_value=mock_app), + patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc), + ): + resp = router.dispatch(req, admin_principal) + assert resp.success is False + assert resp.error is not None + assert resp.error.code == ErrorCode.ERR_VALIDATION_FAILED + assert "Post-pull branch mismatch" in resp.error.message + + +def test_operational_router_workload_update_succeeds_on_clean_tree( + tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path +): + router = OperationalRouter( + audit_logger=AuditLogger(tmp_path / "audit.jsonl"), + lock_manager=TargetLockManager(tmp_path / "locks"), + sites_dir=mock_workload_dir.parent, + ) + req = OperationRequest.model_validate({ + "operation": "workload.update", + "target": "snappymail", + "parameters": {"expected_branch": "main"}, + }) + + mock_app = MagicMock() + mock_app.name = "snappymail" + mock_app.dir_path = str(mock_workload_dir) + + executed_commands: list[list[str]] = [] + + def mock_subproc(cmd: list[str], **kwargs): + executed_commands.append(cmd) + if "status" in cmd: + return 0, "", "" + if "pull" in cmd: + return 0, "Updating 1111..2222\nFast-forward", "" + if "rev-parse" in cmd: + return 0, "a1b2c3d", "" + return 0, "success", "" + + with ( + patch("appctl_router.resolve_app", return_value=mock_app), + patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc), + patch("appctl_router.cmd_sync_homepage"), + ): + resp = router.dispatch(req, admin_principal) + assert resp.success is True + assert resp.result is not None + assert resp.result["commit_sha"] == "a1b2c3d" + # Verify git pull was called with expected branch + pull_calls = [c for c in executed_commands if "pull" in c] + assert len(pull_calls) == 2 # git pull and docker compose pull diff --git a/todo.txt b/todo.txt index 1188315..37a8cde 100644 --- a/todo.txt +++ b/todo.txt @@ -25,7 +25,7 @@ x 2026-10-15 (A) Migrate core_manifest.py to Pydantic v2 models for strict type x 2026-10-19 (A) Implement strict Pydantic v2 API contracts, error sanitizer, and compose_pull action in scripts/appctl_contracts.py and scripts/core_manifest.py +contracts @p4 x 2026-10-19 (A) Implement append-only audit logger with fail-closed disk guard and per-target flock manager in scripts/appctl_audit.py +audit @p4 x 2026-10-19 (A) Implement SOPS SHA-256 Bearer token authenticator, UDS SO_PEERCRED viewer fallback, and 3-role RBAC policy in scripts/appctl_auth.py +auth @p4 -(B) Implement operational router, pure-Python dotenv loader, Compose AST pre-flight, and revision-consistent workload.update in scripts/appctl_router.py +router @p4 +x 2026-10-19 (B) Implement operational router, pure-Python dotenv loader, Compose AST pre-flight, and revision-consistent workload.update in scripts/appctl_router.py +router @p4 (B) Implement dual-transport HTTP/1.1 daemon (UDS + loopback TCP) and wire gitops_dispatcher.py worker to daemon in scripts/appctl_daemon.py +daemon @p4 (B) Add comprehensive unit and security test suite for P4 contracts, RBAC, audit guard, concurrency, and dual-transport parity in tests/unit/test_appctl_daemon.py +test @p4 (C) Re-implement scripts/appctl as a thin UDS HTTP client and declare homelab-appctl.service with POSIX ACLs in nixos/modules/homeserver.nix +cli @p4