Files
nixos-core/docs/secrets-and-state.md
T
kiskaadee abf8d8d7e0
Core Architecture & Invariant CI / validate (push) Successful in 9m42s
docs(platform): establish implementation-backed documentation baseline
Replace legacy documentation with a precise, implementation-derived
operator manual that accurately reflects current system boundaries.

- Define strict orthogonal boundaries between ownership (Core vs Sites),
  runtime topology, and cross-cutting orchestration
- Document the precise GitOps pipeline, webhook admission gates, and
  flock serialization mechanics
- Provide safe operational runbooks for NixOS, systemd, and appctl
- Map accurate persistent state boundaries and SOPS secret projections
- Remove speculative schemas and destructive operational defaults
2026-09-16 12:25:49 -05:00

121 lines
6.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/credentials/ddclient.service/ddclient.conf │
│ -> Consumed by systemd ddclient service │
│ │
│ 5. /run/secrets/pdf_decrypt_password │
│ -> Consumed by shell pdf_dc utility │
└────────────────────────────────────────────────────────────────────────┘
```
---
## 3. Projected Secret Templates
### A. Core Platform Environment (`/run/secrets/homeserver.env`)
Generated by `nixos/modules/homeserver.nix`. Supplies credentials to the Core Compose stack:
* `DOMAIN`: Apex domain (`roadtotech.me`).
* `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` sources when orchestrating stacks in `~/Sites` (such as database passwords, API keys, and runner tokens).
---
## 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: "..."
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 ~/Core#server
```
---
## 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).
* **Dynamic User Directory**: User accounts, credentials, and group memberships are managed live in **LLDAP** (`https://users.roadtotech.me`). 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 files |
| **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 |
| **Webmail Storage** | `~/Core/config/snappymail/data/` | ❌ No | SnappyMail configuration and sessions |
| **Container Volumes** | Docker volume `portainer_data` | ❌ No | Portainer internal metadata |
| **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 |