relative links - Update all Homelab documents and engineering journals with core tags (`architecture`, `security`, `operations`, `ADR`). - Replace machine-specific `file:///` URLS and absolute local paths with portable relative Markdown links and code backticks. - Align Homelab project landing page and documentation status with current infrastructure topology
26 KiB
type, status, project, tags
| type | status | project | tags | ||||||
|---|---|---|---|---|---|---|---|---|---|
| plan | active | homelab |
|
Homelab Core — Architecture Consolidation & Hardening Implementation Guide
This guide turns the Architecture Consolidation & Hardening Roadmap v2 into an executable sequence of work.
The objective is not to introduce new infrastructure. It is to make the existing system's trust boundaries, contracts, deployment behavior, and recovery semantics explicit and machine-enforced.
🚦 Implementation Status & Execution Progress
| Phase / Milestone | Status | Deliverables Completed / Commits | Immediate Next Action |
|---|---|---|---|
| P0: GitOps Trust Boundary | 🔒 100% SEALED & FROZEN | Deliverables 1.1–1.5 (29dd186, 01acdb4, fa8054a, c265c3e, 5aae4ef, f8cbe08, 4c9e7a4) |
Full admission pipeline, revision-consistent manifest validation, status tracking, and superseding async execution locked in. |
| Architecture Invariants & CI | ✅ ESTABLISHED | Deliverables 3.1, 3.2, 3.6 (142697a, f4b676b, 3351466) |
35 automated tests passing, ./scripts/test, pure Nix flake check, Gitea Actions CI. |
| P1: Manifest Contract v1 | 🎯 UP NEXT | Pending Deliverables 2.1–2.5 | Author docs/architecture/manifest-specification.md and JSON schema. Address repository identity authority & alias collision rejection. |
| P1: Privilege Boundary | ⏳ PENDING | Deliverables 4.1–4.4 | Dedicated gitops user & systemd confinement. |
| P2: Architecture Docs | ⏳ PENDING | Deliverables 5.1–5.9 | Architecture topologies, ownership matrix, and historical archive. |
| P3/P4: Reliability & Lifecycle | ⏳ PENDING | Deliverables 6.1–7.4 | Preflight checks, container health verification, deployment history, and systemd-supervised durable worker lifecycle. |
Current Repository State:
mainbranch at commit4c9e7a4. All 35 automated tests pass via./scripts/testandnix flake check. Gitea Actions CI enforces zero architectural regressions. P0 is functionally closed and frozen.
0. Working Rules
Before starting the implementation, establish four rules for the entire refactor.
Rule 1 — One architectural change at a time
Each deliverable should have a small, independently reviewable commit or commit series.
Avoid combining:
- GitOps security changes
- manifest redesign
- NixOS privilege changes
- documentation migration
into one large commit.
Rule 2 — Preserve behavior unless the deliverable explicitly changes it
The refactor should distinguish:
- security changes
- contract changes
- behavioral changes
- documentation changes
For example, replacing:
actions:
- git_pull
- compose_up
with:
deployment:
strategy: compose
is intentionally a contract change.
Moving a secret declaration between NixOS modules should not change runtime behavior.
Rule 3 — The repository is the source of truth
Before implementing a deliverable, inspect the actual consumers.
Do not assume a field is unused because it is not documented.
For every migration:
producer → parser → consumer → runtime behavior
must be identified.
Rule 4 — Every deliverable finishes with a proof
Each implementation should end with at least one reproducible verification command.
Examples:
python -m pytest
nix flake check
docker compose config -q
or:
git grep ...
A completed change should be demonstrably correct, not merely plausible.
1. P0 — GitOps Trust Boundary
The first objective is to turn GitOps from a remotely triggerable command executor into a constrained deployment admission pipeline.
The current threat chain is:
repository modification
↓
app.yaml
↓
webhook
↓
dispatcher
↓
shell execution
↓
kiskaadee
↓
Docker / sudo
↓
host
The roadmap explicitly identifies this as the highest-risk vulnerability.
Deliverable 1.1 — Remove arbitrary custom execution
Step 1 — Inventory current custom consumers
Search every application manifest:
find ~/Sites -name app.yaml -print0 |
xargs -0 grep -n "custom:"
Also search the Core repository:
git grep -n "custom"
git grep -n "shell=True"
git grep -n "subprocess.run"
Record every existing consumer before changing the dispatcher.
The roadmap already identifies homelab-magnetflix as a known consumer.
Step 2 — Decide the replacement for each consumer
For every custom command, classify it:
normal deployment operation
→ built-in deployment behavior
database migration
→ explicit lifecycle mechanism
one-off administrative command
→ manual operator action
unsupported behavior
→ remove
Do not create a generic replacement such as:
post_deploy:
command: "..."
That simply recreates the vulnerability with a different name.
Step 3 — Remove custom from the dispatcher
Delete the code path that eventually reaches:
subprocess.run(cmd, shell=True, ...)
Then make unsupported action values fail closed:
unknown action → deployment rejected
Step 4 — Migrate MagnetFlix
Replace its current custom migration mechanism with the explicitly approved mechanism chosen above.
Deploy MagnetFlix manually once to prove the replacement works.
Step 5 — Add a regression test
Create a test proving that a manifest containing:
custom: "..."
cannot result in command execution.
Exit condition
The following must be true:
git grep -n "shell=True" scripts/gitops_dispatcher.py
returns nothing relevant, and no manifest can provide arbitrary shell input.
2. Deliverable 1.2 — Trusted Repository Resolution
The webhook payload must identify a repository by logical identity, not by filesystem path. Core resolves that identity locally.
Step 1 — Define the trusted mapping
Decide what constitutes the canonical repository identity.
For example:
homelab-docs
homelab-gitea
homelab-magnetflix
Map those identities to known directories under:
~/Sites/
The important property is:
payload → name → trusted mapping → local path
never:
payload → path
Step 2 — Implement one resolver
Create one function responsible for:
resolve_repository(name) -> trusted repository
Every deployment must use it.
Do not allow callers to construct paths independently.
Step 3 — Canonicalize the resolved path
After resolution:
realpath(target)
must still be checked against the allowed root.
This remains defense in depth rather than the primary trust mechanism.
Step 4 — Test traversal and ambiguity
Reject:
../../foo
/home/foo
/etc
symlink escapes
unknown repositories
Test both malicious and valid names.
Exit condition
A webhook can select only repositories explicitly recognized by Core.
3. Deliverable 1.3 — Webhook Authentication
The roadmap specifies HMAC-SHA256 using X-Gitea-Signature, with the secret stored via SOPS and projected through /run/secrets/.
Step 1 — Obtain the exact incoming request representation
Before implementing verification, establish exactly what bytes the signature covers:
HTTP body
Do not verify a reconstructed or parsed JSON object.
Signature verification must operate on the original request body.
Step 2 — Store the webhook secret
Add a dedicated secret to SOPS.
Do not place it in:
app.yaml
docker-compose.yml
gitops_dispatcher.py
Project it to the service at runtime.
Step 3 — Implement verification
Verification should:
- read the raw body
- read the signature header
- calculate HMAC-SHA256
- compare using a constant-time comparison
- reject invalid requests before deployment
Conceptually:
request
↓
read raw body
↓
calculate HMAC
↓
constant-time compare
↓
valid → continue
invalid → reject
Step 4 — Reject missing signatures
These are failures:
no signature
malformed signature
wrong signature
empty secret
There should be no anonymous fallback.
Step 5 — Test
Create tests for:
valid signature
invalid signature
missing signature
modified body
modified signature
Exit condition
No unauthenticated request can reach deployment admission.
4. Deliverable 1.4 — Repository, Event, and Branch Admission
Authentication answers:
Who sent this?
It does not answer:
Should this request be deployed?
The admission layer must validate the repository, event/ref, and branch policy before execution.
Step 1 — Define accepted event semantics
Document which webhook events are deployable.
For example:
push → eligible
pull_request → rejected
tag → rejected unless explicitly supported
Use the actual webhook payload structure rather than assuming every event is equivalent.
Step 2 — Validate repository identity
Verify:
payload repository
↓
trusted repository mapping
Reject unknown repositories.
Step 3 — Validate the branch
Determine the branch from the manifest:
deployment:
branch: main
with:
default = main
Then require the incoming ref to match the declared policy.
Step 4 — Validate manifest before execution
The manifest must be parsed and validated before Git or Docker actions occur.
The roadmap explicitly places manifest validation inside the admission pipeline.
Exit condition
An authenticated request still gets rejected if:
wrong repository
wrong event
wrong branch
invalid manifest
unsupported strategy
5. Deliverable 1.5 — Serialized Deployment
The desired behavior is:
A arrives → deploy A
B arrives → queue B
C arrives → replace B
A finishes → deploy C
rather than attempting every intermediate revision.
Step 1 — Identify deployment targets
Serialization should be per application/repository.
Two unrelated applications should be able to deploy independently.
Step 2 — Choose filesystem-level synchronization
Use a simple mechanism such as:
flock
or a systemd-managed worker.
Do not add Redis, RabbitMQ, or another message broker.
Step 3 — Separate admission from execution
The HTTP request should not perform:
git pull
docker compose pull
docker compose up
Instead:
HTTP request
↓
validate
↓
enqueue latest desired revision
↓
return
Then:
worker
↓
take target
↓
deploy
This directly addresses the roadmap's asynchronous-dispatch requirement.
Step 4 — Implement superseding
Maintain at most:
one active deployment
one pending deployment
When a newer valid request arrives:
pending old revision → replaced
pending new revision → retained
Step 5 — Test race behavior
Simulate:
A starts
B arrives
C arrives
A finishes
and verify that:
B is skipped
C is deployed
P0 exit condition
The complete flow is now:
webhook
↓
cryptographic verification
↓
repository admission
↓
event/ref admission
↓
manifest validation
↓
deployment scheduling
↓
serialized executor
↓
closed deployment strategy
This is the first major milestone.
6. P1 — Manifest Contract v1
The manifest currently functions as an informal configuration format. The goal is to turn it into a strict API contract.
Do this only after the P0 trust boundary is established.
Deliverable 2.1 — Write the Manifest Specification First
Do not begin by writing JSON Schema.
Begin with a human specification.
Create:
docs/architecture/manifest-specification.md
Define:
Required fields
For example:
schemaVersion
name
domain
Optional fields
For example:
title
description
aliases
visible
auth
networks
env
deployment
homepage
Types
Explicitly define:
name → string
aliases → array[string]
visible → boolean
networks → array[string]
env → mapping[string,string]
Defaults
Document defaults explicitly.
Allowed values
For example:
deployment.strategy:
compose
Repository Identity Authority & Collision Policies
Define authoritative vs presentation metadata to resolve the identity authority boundary:
- Determine whether Core's central registry is authoritative or if application manifests register identities.
- Enforce strict collision rejection:
duplicate canonical names→ validation error / rejected.duplicate aliasesacross applications → validation error / rejected.alias / canonical name collisions→ validation error / rejected.
Cross-field constraints
Examples:
auth=true may require the proxy network
deployment.strategy must be supported
domain must be unique
name must be unique
Security constraints
Explicitly prohibit:
arbitrary commands
filesystem paths
Docker socket access
privileged deployment directives
The manifest must describe application intent, not host capabilities.
7. Deliverable 2.2 — Create JSON Schema v1
Create:
schemas/app-v1.schema.json
Start from the written specification.
Include:
$schema
$id
title
type
required
properties
additionalProperties
Prefer rejecting unknown fields rather than silently accepting them.
The roadmap deliberately makes JSON Schema the normative external contract and Pydantic the runtime enforcement mechanism.
Step-by-step
- encode primitive types
- encode required fields
- encode defaults
- encode enumerations
- encode nested objects
- add security restrictions
- add cross-field constraints where supported
- write valid examples
- write invalid examples
Create fixtures such as:
tests/fixtures/manifests/valid/
tests/fixtures/manifests/invalid/
8. Deliverable 2.3 — Introduce schemaVersion
Every application manifest becomes:
schemaVersion: 1
Core must reject:
schemaVersion: 2
and missing schemaVersion unless a future compatibility policy explicitly says otherwise.
This prevents silent interpretation changes.
9. Deliverable 2.4 — Replace Imperative Deployment Actions
Current:
deployment:
branch: main
actions:
- git_pull
- compose_up
Target:
deployment:
strategy: compose
branch: main
verify: healthcheck
The important principle is:
application declares intent
↓
Core owns execution
not:
application tells Core which commands to run
The roadmap explicitly adopts this intent-over-imperative model.
Migration procedure
For each application:
- inspect current actions
- determine the intended strategy
- convert to
deployment.strategy - remove
actions - validate
- deploy manually
- confirm equivalent behavior
- commit migration
10. Deliverable 2.5 — Replace the Custom YAML Parser
The existing parser should stop being the source of truth.
Choose a real YAML parser compatible with the project constraints.
Then construct a typed model representing the v1 manifest.
The runtime flow becomes:
app.yaml
↓
YAML parser
↓
Pydantic model
↓
validated manifest object
↓
appctl / GitOps
Do not let every consumer parse YAML independently.
11. Deliverable 2.6 — Desired State vs Observed State
Make the distinction explicit.
app.yaml
↓
desired state
while:
Git / Docker / TLS / container health
↓
observed state
appctl reconciles and presents the two rather than storing observed runtime facts in app.yaml.
Practical test
Ask of every field:
Could this value be determined from runtime inspection instead?
If yes, it probably does not belong in the manifest.
12. Deliverable 2.7 — Preflight Validation
Both consumers must use the same validation layer:
appctl
GitOps dispatcher
Create one reusable validation interface.
For example:
load_manifest(path)
validate_manifest(manifest)
The CLI and dispatcher should not implement different validation rules.
Exit condition
Every application manifest in ~/Sites is:
schemaVersion: 1
valid
intent-based
strictly parsed
13. P1 — Architecture Invariant Tests
The objective here is to convert AGENTS.md architectural rules into executable constraints. The roadmap defines five categories.
Create:
tests/
├── structural/
├── security/
├── manifests/
├── operational/
└── documentation/
14. Deliverable 3.1 — Structural Tests
Verify nixos/, scripts/, docs/, docker-compose.yml, flake.nix, AGENTS.md, UNLICENSE exist.
Also reject deprecated structures such as infra/core/, ./up.sh, ./down.sh.
The test should inspect the filesystem rather than relying on documentation claims.
15. Deliverable 3.2 — Security Tests
Create assertions for:
no direct docker.sock mounts
socket-proxy POST=0
socket-proxy DELETE=0
no privileged containers
no forbidden host paths
no shell=True in GitOps
Where possible, parse Compose/YAML rather than using fragile grep-only checks.
16. Deliverable 3.3 — Manifest Tests
Scan every ~/Sites/*/app.yaml and validate:
schemaVersion
schema validity
unique names
unique domains
allowed networks
allowed deployment strategies
This creates a repository-wide contract test rather than validating only the application currently being deployed.
17. Deliverable 3.4 — Operational Tests
Run:
docker compose config -q # for Core
nix flake check # for NixOS configuration
Then run equivalent Compose validation for each application stack.
18. Deliverable 3.5 — Documentation Tests
Build simple checks that identify commands, paths, and deprecated paths referenced in docs.
Focus on known failure modes rather than building a natural-language parser.
The immediate reason for this is existing documentation drift.
19. Deliverable 3.6 — CI Integration
Add a Gitea Actions workflow.
On every Core change, run at minimum:
structural
security
manifest
The desired property is:
architectural regression → CI failure
20. P1 — Privilege Boundary
After arbitrary command execution is removed, reduce the remaining GitOps authority.
The roadmap proposes a dedicated Unix user and systemd hardening.
21. Deliverable 4.1 — Create the gitops User
Add a dedicated NixOS user gitops with only the permissions necessary for deployment. No wheel. Docker access only.
22. Deliverable 4.2 — Move GitOps Runtime to gitops
Change User=kiskaadee to the dedicated identity.
Then inspect every dependency: Sites permissions, Docker permissions, secret access, configuration files, working directory, logs, temporary files.
Do not assume the service will continue working simply because the user belongs to docker.
23. Deliverable 4.3 — Apply systemd Hardening
Apply the planned directives:
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=...
PrivateTmp=yes
NoNewPrivileges=yes
CapabilityBoundingSet=
After each restriction:
sudo systemctl daemon-reload
sudo systemctl restart homelab-gitops
journalctl -u homelab-gitops
This incremental process is important because systemd hardening often exposes previously implicit filesystem assumptions.
24. Deliverable 4.4 — Prove the New Boundary
Perform an explicit capability review:
Can GitOps write outside Sites?
Can GitOps access unrelated secrets?
Can GitOps invoke sudo?
Can GitOps modify NixOS?
Can GitOps execute arbitrary shell?
Can GitOps access the Docker daemon?
The goal is not merely "It runs." The goal is "Its authority is intentionally bounded."
25. P2 — Architecture Documentation
Do this after P0/P1 behavior has stabilized.
The documentation tree proposed by the roadmap is:
docs/
├── architecture/
│ ├── system-topology.md
│ ├── ownership-matrix.md
│ ├── gitops-protocol-v1.md
│ └── manifest-specification.md
├── security/
│ ├── trust-model.md
│ ├── secret-management.md
│ └── socket-proxy-policy.md
├── operations/
│ ├── nixos-management.md
│ └── disaster-recovery.md
├── adr/
└── archive/
Principle: docs/ = current truth; docs/archive/ = historical truth.
26–34. Documentation Deliverables (5.1–5.9)
Create each document after the implementation it describes is stable:
- 5.1
docs/architecture/system-topology.md— three-layer diagram (use the approved Mermaid flowchart from the roadmap), orchestration as cross-cutting - 5.2
docs/architecture/ownership-matrix.md— owns / must not own per layer - 5.3
docs/architecture/gitops-protocol-v1.md— webhook format, auth, resolution, branch policy, serialization, failure semantics - 5.4
docs/architecture/manifest-specification.md— human-readable companion to JSON Schema - 5.5
docs/security/trust-model.md— full trust chain and component authority - 5.6
docs/security/secret-management.md— SOPS lifecycle, projection, ownership vs. consumption - 5.7
docs/operations/disaster-recovery.md— split declarative recovery vs. state recovery - 5.8 Historical migration — ADRs to
docs/adr/, obsolete docs todocs/archive/ - 5.9 Audit active docs — correct ports, paths, commands, TLS/Watchtower/DDNS claims
35. P3 — Deployment Verification
Deliverable 6.1 — Preflight Validation
Before changing runtime state, run schema validation, branch validation, and docker compose config -q.
Deliverable 6.2 — Implement Deployment Lifecycle
Standardize the sequence:
capture current revision → fetch target → validate manifest
→ validate Compose → pull images → compose up → verify → record result
Keep execution deterministic. Do not allow manifests to change the sequence arbitrarily.
Deliverable 6.3 — Health Verification
Implement container state + health status checks. Optionally HTTP probe when a domain is declared.
A health verification failure should not automatically imply rollback in v1.
Deliverable 6.4 — Explicit Failure Semantics
Preflight failure → reject + log reason
Git failure → FAILED, stop
Compose failure → FAILED, stop
Health failure → DEGRADED, log warning
No automatic rollback in v1.
Deliverable 6.5 — Structured Deployment Logging
Produce JSON logs with: repository, revision, stage, timestamp, result, error.
36. P4 — Deployment State & Observability
Deliverable 7.1 — Define Deployment Record Schema
{
"id": "deploy-docs-20260912-001422",
"repository": "homelab-docs",
"commit": "9ac3f7b2",
"previous_commit": "87b2e1a4",
"branch": "main",
"timestamp": "2026-09-12T00:14:22-05:00",
"duration_seconds": 22,
"result": "SUCCESS",
"failure_reason": null,
"triggered_by": "webhook"
}
Deliverable 7.2 — Implement Append-Only Storage
Use JSON Lines. One record per line. Decide per-repo file vs. global log based on querying needs.
Deliverable 7.3 — Track Previous Revision
Capture previous commit at deployment time for rollback context.
Deliverable 7.4 — Add appctl deployment
appctl deployment <app>
Shows last and previous deployment: commit, time, duration, result.
46. Final Integration Pass
After all deliverables are implemented, confirm the five acceptance questions from the roadmap can be answered from the repository alone:
- What does NixOS own?
- What does Core own?
- What does an application repository own?
- What can GitOps execute?
- What happens when deployment fails?
Suggested Commit Sequence
1. refactor(gitops): remove arbitrary custom execution
2. feat(gitops): add trusted repository resolution
3. feat(gitops): add webhook authentication
4. feat(gitops): enforce repository/event/branch admission
5. feat(gitops): add serialized asynchronous deployment
6. migrate(apps): convert custom deployment consumers
7. docs(manifest): define app.yaml v1 specification
8. feat(manifest): add JSON Schema v1
9. feat(manifest): add typed runtime validation
10. migrate(apps): add schemaVersion and intent-based deployment
11. test(architecture): add structural invariants
12. test(architecture): add security invariants
13. test(manifests): add manifest invariants
14. test(architecture): add operational invariants
15. test(docs): add documentation invariants
16. ci: run architecture validation in Gitea Actions
17. feat(security): create dedicated gitops user
18. harden(systemd): restrict GitOps service
19. docs(architecture): add system topology
20. docs(architecture): add ownership matrix
21. docs(gitops): document protocol v1
22. docs(manifest): add specification reference
23. docs(security): document trust and secrets
24. docs(operations): document recovery
25. docs: archive historical material
26. feat(gitops): add deployment preflight
27. feat(gitops): add deployment verification
28. feat(gitops): define failure semantics
29. feat(gitops): add deployment records
30. feat(appctl): add deployment history
Definition of Done
The consolidation is complete when:
[x] GitOps cannot execute arbitrary repository-supplied shell
[x] webhook requests are authenticated
[x] repositories are resolved through trusted identity
[x] branch/event policy is enforced
[x] deployment execution is asynchronous and serialized
[ ] app.yaml v1 is formally specified
[ ] JSON Schema is normative
[ ] runtime validation is typed
[ ] all manifests use schemaVersion
[ ] deployment configuration expresses intent
[x] architectural invariants are tested automatically
[x] CI enforces those invariants
[ ] GitOps runs under a restricted identity
[ ] systemd limits GitOps filesystem authority
[ ] current architecture is documented
[ ] historical documentation is separated
[ ] deployment preflight exists
[ ] deployment health is verified
[ ] failure semantics are explicit
[ ] deployment history is persisted
[ ] appctl can report deployment history
The resulting system should be more constrained than the current one, not merely more documented. Its architecture should be understandable from the repository, while its most important boundaries are enforced even when a future contributor does not remember them.