Files
nixos-core/docs/secrets-and-state.md
kiskaadee 03c8fdcf8e
Core Architecture & Invariant CI / validate (pull_request) Failing after 1m1s
docs(core): reconcile README and SECURITY with Milestone P4 control plane daemon architecture
- Update README.md to describe homelab-appctl.service operational control plane daemon architecture over UDS and loopback HTTP
- Update Repository Structure table to include bootstrap_token.py and appctl_*.py daemon modules
- Update Host System Management rebuild commands across docs to use path:$HOME/Core#server --impure
- Document control plane transport isolation, SO_PEERCRED viewer auth, SHA-256 tokens, 3-tier RBAC, per-target flocking, and fail-closed audit logging in SECURITY.md
2026-10-30 13:46:22 -05:00

138 lines
8.4 KiB
Markdown

# 🔒 Homelab Core — Secrets Management & Persistent State
This document explains the secrets distribution architecture, runtime projection mechanisms, user directory model, and persistent state boundaries for **Homelab Core**.
---
## 1. Secrets Management Policy
* **No Plaintext Secrets in Git**: All sensitive credentials, API keys, tokens, and private keys are encrypted at rest using **Mozilla SOPS** with **Age keys** derived from the server's SSH host key.
* **In-Memory Projections**: Secret files are decrypted at boot by **`sops-nix`** into an ephemeral in-memory filesystem (`/run/secrets/` and `/run/credentials/`) and are not committed to unencrypted persistent storage.
---
## 2. Secrets Lifecycle & Projection Architecture
```text
[nixos/secrets.yaml] (Encrypted with Age / SOPS)
│
▼ (Decrypted at boot by sops-nix using /etc/ssh/ssh_host_ed25519_key)
┌──────┴─────────────────────────────────────────────────────────────────┐
│ In-Memory Projections (/run/secrets/ & /run/credentials/) │
├────────────────────────────────────────────────────────────────────────┤
│ 1. /run/secrets/homeserver.env │
│ -> Consumed by homeserver-core.service & Core docker-compose.yml │
│ │
│ 2. /run/secrets/traefik-deployments.env │
│ -> Consumed by appctl to inject secrets into Sites workloads │
│ │
│ 3. /run/secrets/gitops/webhook_secret │
│ -> Consumed by homelab-gitops.service for HMAC verification │
│ │
│ 4. /run/secrets/appctl/tokens.json (mode 0400, homelab-appctl) │
│ -> Consumed by homelab-appctl.service for SHA-256 Bearer auth │
│ │
│ 5. /run/secrets/gitops/appctl_token (mode 0400, operatorUser) │
│ -> Consumed by homelab-gitops.service worker for UDS daemon auth │
│ │
│ 6. /run/credentials/ddclient.service/ddclient.conf │
│ -> Consumed by systemd ddclient service │
│ │
│ 7. /run/secrets/pdf_decrypt_password │
│ -> Consumed by shell pdf_dc utility │
└────────────────────────────────────────────────────────────────────────┘
```
---
## 3. Projected Secret Templates & Token Store
### A. Core Platform Environment (`/run/secrets/homeserver.env`)
Generated by `nixos/modules/homeserver.nix`. Supplies credentials to the Core Compose stack:
* `DOMAIN`: Apex domain (e.g., `example.com`).
* `DYNU_API_KEY`: API key for Dynu DNS-01 ACME Let's Encrypt validation.
* `ACME_EMAIL`: Account email for Let's Encrypt certificates.
* `AUTHELIA_SESSION_SECRET`: Authelia cookie encryption secret.
* `AUTHELIA_STORAGE_ENCRYPTION_KEY`: Database encryption key for Authelia SQLite storage.
* `AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET`: Reset password token secret.
* `LLDAP_JWT_SECRET` & `LLDAP_KEY_SEED`: Cryptographic tokens for LLDAP.
* `LLDAP_LDAP_USER_PASS`: Admin password for LLDAP and Authelia LDAP binding.
* `SMTP_SERVER`, `SMTP_PORT`, `SMTP_LOGIN`, `SMTP_KEY`: Outbound relay credentials (Brevo) for Stalwart mail transport.
### B. Workload Deployments Environment (`/run/secrets/traefik-deployments.env`)
Generated by `nixos/modules/traefik-deployments.nix`. Projects shared infrastructure secrets and workload credentials that `appctl` parses inertly in pure Python when orchestrating stacks in `~/Sites` (such as database passwords, API keys, and runner tokens).
### C. Control Plane Token Store (`/run/secrets/appctl/tokens.json` & `/run/secrets/gitops/appctl_token`)
Provisioned from `nixos/secrets.yaml` (`appctl/tokens_json` and `gitops/appctl_token`):
* `/run/secrets/appctl/tokens.json`: Owned by `homelab-appctl:homelab-operators` with strict mode `0400` (enforced fail-closed by `TokenStore._verify_permissions()`, rejecting any write bit or group/world readability). Stores a JSON array of SHA-256 token descriptors (`token_id`, `identity`, `role`, and 64-hex `sha256`) defined under `appctl/tokens_json` in `nixos/secrets.yaml`.
* `/run/secrets/gitops/appctl_token`: Owned by `config.homelab.operatorUser` (mode `0400`). Supplies the `homelab-operator` Bearer secret to `homelab-gitops.service` when delegating `workload.update` to `homelab-appctl.service`.
* `~/.config/homelab/token`: Local operator Bearer token written with strict mode `0600` (in a mode `0700` directory) by `scripts/bootstrap_token.py`. Automatically discovered and resolved by `appctl_cli.py` for authenticated operator interactions without needing `--token` flags or environment variables.
---
## 4. Secret Modification and Rotation Workflow
To edit or rotate secrets:
### 1. Open the Encrypted Secrets File
```bash
cd ~/Core
sops nixos/secrets.yaml
```
SOPS decrypts the file using your host SSH private key and opens it in your editor.
### 2. Modify Secret Keys
```yaml
dynu:
api_key: "..."
authelia:
session_secret: "..."
jwt_secret: "..."
lldap:
admin_password: "..."
gitops:
webhook_secret: "..."
appctl_token: "..."
appctl:
tokens_json: "[...]"
brevo:
smtp_login: "..."
smtp_key: "..."
```
### 3. Save and Apply Changes
Saving the file in your editor automatically re-encrypts it with Age. Apply the updated templates to running services:
```bash
sudo nixos-rebuild switch --flake path:$HOME/Core#server --impure
```
---
## 5. User Directory vs. Static Secrets
Homelab Core separates **user identity** from **static platform secrets**:
* **Static Secrets**: Stored in `nixos/secrets.yaml` (system API keys, JWT secrets, database passwords, control-plane token hashes).
* **Dynamic User Directory**: User accounts, credentials, and group memberships are managed live in **LLDAP** (`https://users.${HOMELAB_DOMAIN}`). User account updates do not require editing `secrets.yaml` or rebuilding NixOS.
* **Authelia Integration**: Authelia queries LLDAP over LDAP (`ldap://lldap:3890`) at runtime using credentials projected from `/run/secrets/homeserver.env`.
---
## 6. Persistent State Map
| Category | Storage Location | Stored in Git? | Description |
| :--- | :--- | :--- | :--- |
| **Declarative Config** | `~/Core/nixos/*.nix`, `docker-compose.yml` | ✅ Yes | System and container definitions |
| **Encrypted Secrets** | `~/Core/nixos/secrets.yaml` | ✅ Yes (Encrypted) | SOPS Age-encrypted secret source |
| **Runtime Secrets** | `/run/secrets/*` | ❌ No (RAM tmpfs) | Ephemeral decrypted environment & token files |
| **Control Plane Audit** | `/var/log/homelab/appctl/audit.jsonl` | ❌ No | Append-only JSONL security & mutation log (`0600`) |
| **Control Plane Locks** | `/var/lib/homelab/appctl/locks/` | ❌ No | Per-target `flock` files & `_homepage_sync.lock` |
| **SSL Certificates** | `~/Core/config/letsencrypt/acme.json` | ❌ No | Active Let's Encrypt certificates (mode 0600) |
| **User Directory** | `~/Core/config/lldap/data/` | ❌ No | LLDAP SQLite database (`users.db`) |
| **Auth Database** | `~/Core/config/authelia/db.sqlite3` | ❌ No | Authelia session database |
| **Mail Data & Keys** | `~/Core/config/stalwart/` | ❌ No | Stalwart mail storage, DKIM keys, mailboxes |
| **Workload State Root** | `~/var/lib/homelab/<app>/` | ❌ No | Per-workload persistent runtime state (e.g., `snappymail`) |
| **Container Volumes** | Docker volume `dozzle_data` | ❌ No | Dozzle UI preferences and state |
| **DDNS History** | `/var/lib/dynu/ip_history.jsonl` | ❌ No | Historical WAN IP transitions log |
| **GitOps State** | `~/.local/state/homelab/gitops/` | ❌ No | Deployment locks, pending JSONs, status logs |