diff --git a/config/deployments.yaml b/config/deployments.yaml index d7b760e..35b7016 100644 --- a/config/deployments.yaml +++ b/config/deployments.yaml @@ -1,48 +1,48 @@ # Core Authoritative Repository Identity Registry # Governs admitted continuous deployment targets for GitOps webhook execution. # Owns: repository identity, canonical filesystem path, and aliases. -# Enforces: strict path containment within ~/Sites. +# Enforces: strict path containment within ~/Workloads. schemaVersion: "1.0" repositories: snappymail: - path: "~/Sites/snappymail" + path: "~/Workloads/snappymail" aliases: - "homelab-snappymail" - "mail" - "webmail" gitea: - path: "~/Sites/homelab-gitea" + path: "~/Workloads/homelab-gitea" aliases: - "homelab-gitea" - "git" jellyfin: - path: "~/Sites/homelab-jellyfin" + path: "~/Workloads/homelab-jellyfin" aliases: - "homelab-jellyfin" - "media" courses: - path: "~/Sites/courses" + path: "~/Workloads/courses" aliases: - "homelab-courses" bdinvite: - path: "~/Sites/homelab-bdinvite" + path: "~/Workloads/homelab-bdinvite" aliases: - "homelab-bdinvite" - "birthday" landing: - path: "~/Sites/homelab-landing" + path: "~/Workloads/homelab-landing" aliases: - "homelab-landing" mermaid: - path: "~/Sites/homelab-mermaid" + path: "~/Workloads/homelab-mermaid" aliases: - "homelab-mermaid" excalidraw: - path: "~/Sites/homelab-excalidraw" + path: "~/Workloads/homelab-excalidraw" aliases: - "homelab-excalidraw" houston: - path: "~/Sites/homelab-houston" + path: "~/Workloads/homelab-houston" aliases: - "homelab-houston" diff --git a/nixos/configuration.nix b/nixos/configuration.nix index 76b1a5f..d9cefec 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -23,6 +23,8 @@ enable = true; config.safe.directory = [ "/home/${config.homelab.operatorUser}/Core" + "/home/${config.homelab.operatorUser}/Workloads" + "/home/${config.homelab.operatorUser}/Workloads/*" "/home/${config.homelab.operatorUser}/Sites/*" ]; }; diff --git a/nixos/modules/homeserver.nix b/nixos/modules/homeserver.nix index 640ff99..9180412 100644 --- a/nixos/modules/homeserver.nix +++ b/nixos/modules/homeserver.nix @@ -23,8 +23,8 @@ }; sitesDir = lib.mkOption { type = lib.types.str; - default = "/home/${config.homelab.operatorUser}/Sites"; - description = "Path to Sites directory"; + default = "/home/${config.homelab.operatorUser}/Workloads"; + description = "Path to Workloads directory (legacy option name: sitesDir)"; }; }; @@ -124,6 +124,8 @@ GITOPS_APPCTL_TOKEN_FILE = config.sops.secrets."gitops/appctl_token".path; DOCKER_CONFIG = "/home/${config.homelab.operatorUser}/.local/state/homelab/docker"; HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab"; + WORKLOADS_DIR = config.homelab.sitesDir; + SITES_DIR = config.homelab.sitesDir; }; serviceConfig = { @@ -198,6 +200,7 @@ APPCTL_AUDIT_LOG = "/var/log/homelab/appctl/audit.jsonl"; APPCTL_LOCK_DIR = "/var/lib/homelab/appctl/locks"; APPCTL_TOKENS_FILE = config.sops.secrets."appctl/tokens_json".path; + WORKLOADS_DIR = config.homelab.sitesDir; SITES_DIR = config.homelab.sitesDir; CORE_DIR = config.homelab.coreDir; HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab"; diff --git a/scripts/appctl_daemon.py b/scripts/appctl_daemon.py index bba84e9..8d544d9 100755 --- a/scripts/appctl_daemon.py +++ b/scripts/appctl_daemon.py @@ -57,7 +57,12 @@ from pydantic import ValidationError logger = logging.getLogger("appctl_daemon") CORE_DIR = Path(os.environ.get("CORE_DIR", Path(__file__).resolve().parent.parent)) -SITES_DIR = Path(os.environ.get("SITES_DIR", os.path.expanduser("~/Sites"))) +WORKLOADS_DIR = Path( + os.environ.get("WORKLOADS_DIR") + or os.environ.get("SITES_DIR") + or os.path.expanduser("~/Workloads") +) +SITES_DIR = WORKLOADS_DIR DEPLOYMENTS_FILE = Path( os.environ.get("HOMELAB_DEPLOYMENTS_FILE", CORE_DIR / "config" / "deployments.yaml") ) diff --git a/scripts/appctl_engine.py b/scripts/appctl_engine.py index af16215..7aef9c5 100755 --- a/scripts/appctl_engine.py +++ b/scripts/appctl_engine.py @@ -37,12 +37,19 @@ CORE_DIR = os.environ.get( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), ) -SITES_DIR = os.environ.get( - "SITES_DIR", - os.path.join(os.path.dirname(CORE_DIR), "Sites") - if os.path.isdir(os.path.join(os.path.dirname(CORE_DIR), "Sites")) - else os.path.expanduser("~/Sites"), -) +def _resolve_default_workloads_dir() -> str: + """Resolve workloads root honoring WORKLOADS_DIR, SITES_DIR, sibling Workloads, or ~/Workloads.""" + env_dir = os.environ.get("WORKLOADS_DIR") or os.environ.get("SITES_DIR") + if env_dir: + return env_dir + sibling_workloads = os.path.join(os.path.dirname(CORE_DIR), "Workloads") + if os.path.isdir(sibling_workloads): + return sibling_workloads + return os.path.expanduser("~/Workloads") + + +WORKLOADS_DIR = _resolve_default_workloads_dir() +SITES_DIR = WORKLOADS_DIR HOMELAB_DOMAIN = ( os.environ.get("HOMELAB_DOMAIN") diff --git a/scripts/gitops_dispatcher.py b/scripts/gitops_dispatcher.py index 239948d..ca347cf 100755 --- a/scripts/gitops_dispatcher.py +++ b/scripts/gitops_dispatcher.py @@ -36,7 +36,12 @@ try: except ImportError: yaml = None -SITES_DIR = Path(os.environ.get("SITES_DIR", os.path.expanduser("~/Sites"))) +WORKLOADS_DIR = Path( + os.environ.get("WORKLOADS_DIR") + or os.environ.get("SITES_DIR") + or os.path.expanduser("~/Workloads") +) +SITES_DIR = WORKLOADS_DIR CORE_DIR = Path(os.environ.get("CORE_DIR", Path(__file__).resolve().parent.parent)) CONFIG_DIR = Path( os.environ.get("HOMELAB_CONFIG_DIR", os.path.expanduser("~/.config/homelab")) @@ -287,7 +292,10 @@ def get_trusted_repository_mapping() -> dict[str, Path]: raw_path = entry.get("path") if not raw_path: continue - if raw_path.startswith("~/Sites/") or raw_path == "~/Sites": + if raw_path.startswith("~/Workloads/") or raw_path == "~/Workloads": + sub = raw_path[len("~/Workloads") :].lstrip("/") + p = (SITES_DIR / sub).resolve() if sub else SITES_DIR.resolve() + elif raw_path.startswith("~/Sites/") or raw_path == "~/Sites": sub = raw_path[len("~/Sites") :].lstrip("/") p = (SITES_DIR / sub).resolve() if sub else SITES_DIR.resolve() else: diff --git a/tests/security/test_gitops_security.py b/tests/security/test_gitops_security.py index 097053a..f40e626 100644 --- a/tests/security/test_gitops_security.py +++ b/tests/security/test_gitops_security.py @@ -228,6 +228,34 @@ def test_missing_or_corrupt_registry_fails_closed(tmp_path: Path, monkeypatch): # Missing registry must fail closed: empty mapping, no resolution assert gitops_dispatcher.get_trusted_repository_mapping() == {} + + +def test_registry_resolves_workloads_and_sites_tilde_prefixes( + tmp_path: Path, monkeypatch +): + """Ensure ~/Workloads/ and legacy ~/Sites/ paths resolve cleanly inside SITES_DIR.""" + workloads_dir = tmp_path / "Workloads" + app_a = workloads_dir / "snappymail" + app_b = workloads_dir / "legacy-app" + app_a.mkdir(parents=True) + app_b.mkdir(parents=True) + + registry = tmp_path / "deployments.yaml" + registry.write_text( + 'schemaVersion: "1.0"\n' + "repositories:\n" + " snappymail:\n" + ' path: "~/Workloads/snappymail"\n' + " legacy:\n" + ' path: "~/Sites/legacy-app"\n', + encoding="utf-8", + ) + + monkeypatch.setattr(gitops_dispatcher, "SITES_DIR", workloads_dir) + monkeypatch.setattr(gitops_dispatcher, "DEPLOYMENTS_FILE", registry) + + assert gitops_dispatcher.resolve_repository("snappymail") == app_a.resolve() + assert gitops_dispatcher.resolve_repository("legacy") == app_b.resolve() assert gitops_dispatcher.resolve_repository("valid") is None # Corrupt YAML registry must also fail closed diff --git a/tests/security/test_security_invariants.py b/tests/security/test_security_invariants.py index 4720d80..393f89f 100644 --- a/tests/security/test_security_invariants.py +++ b/tests/security/test_security_invariants.py @@ -485,12 +485,18 @@ def test_nixos_switch_alias_uses_path_and_impure(): def test_nixos_safe_directory_includes_core_and_sites(): - """Ensure programs.git.config.safe.directory includes Core and Sites to prevent dubious ownership errors.""" + """Ensure programs.git.config.safe.directory includes Core, Workloads, and Sites to prevent dubious ownership errors.""" config_nix = CORE_ROOT / "nixos" / "configuration.nix" content = config_nix.read_text(encoding="utf-8") assert '"/home/${config.homelab.operatorUser}/Core"' in content, ( "nixos/configuration.nix must include Core in git safe.directory" ) + assert '"/home/${config.homelab.operatorUser}/Workloads"' in content, ( + "nixos/configuration.nix must include Workloads in git safe.directory" + ) + assert '"/home/${config.homelab.operatorUser}/Workloads/*"' in content, ( + "nixos/configuration.nix must include Workloads/* in git safe.directory" + ) assert '"/home/${config.homelab.operatorUser}/Sites/*"' in content, ( "nixos/configuration.nix must include Sites/* in git safe.directory" ) @@ -521,9 +527,31 @@ def test_nixos_safe_directory_includes_core_and_sites(): assert "/home/operator/Core" in safe_dirs, ( f"Expected '/home/operator/Core' in git safe.directory, got {safe_dirs}" ) + assert "/home/operator/Workloads" in safe_dirs, ( + f"Expected '/home/operator/Workloads' in git safe.directory, got {safe_dirs}" + ) + assert "/home/operator/Workloads/*" in safe_dirs, ( + f"Expected '/home/operator/Workloads/*' in git safe.directory, got {safe_dirs}" + ) assert "/home/operator/Sites/*" in safe_dirs, ( f"Expected '/home/operator/Sites/*' in git safe.directory, got {safe_dirs}" ) +def test_nixos_homeserver_defaults_to_workloads_dir(): + """Ensure homeserver.nix defaults sitesDir to ~/Workloads and exports WORKLOADS_DIR + SITES_DIR (#33).""" + homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix" + content = homeserver_nix.read_text(encoding="utf-8") + + assert 'default = "/home/${config.homelab.operatorUser}/Workloads";' in content, ( + "homelab.sitesDir must default to /home/${config.homelab.operatorUser}/Workloads" + ) + assert "WORKLOADS_DIR = config.homelab.sitesDir;" in content, ( + "homeserver.nix must export WORKLOADS_DIR" + ) + assert "SITES_DIR = config.homelab.sitesDir;" in content, ( + "homeserver.nix must export SITES_DIR for backward compatibility" + ) + + diff --git a/tests/unit/test_appctl_engine.py b/tests/unit/test_appctl_engine.py index 2721061..2555614 100644 --- a/tests/unit/test_appctl_engine.py +++ b/tests/unit/test_appctl_engine.py @@ -554,3 +554,56 @@ def test_cmd_list_flags_manifest_error( engine.cmd_list([]) captured = capsys.readouterr() assert "Manifest Error" in captured.out + + +def test_workloads_dir_env_precedence_and_fallback( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +): + """Ensure WORKLOADS_DIR > SITES_DIR > ~/Workloads fallback across core modules (#33).""" + import importlib + + import appctl_daemon + import gitops_dispatcher + + workloads_override = tmp_path / "custom-workloads" + sites_override = tmp_path / "legacy-sites" + fake_home = tmp_path / "home" + fake_core = tmp_path / "repo" / "core" + fake_core.mkdir(parents=True) + + try: + # 1. WORKLOADS_DIR takes precedence over SITES_DIR + monkeypatch.setenv("CORE_DIR", str(fake_core)) + monkeypatch.setenv("WORKLOADS_DIR", str(workloads_override)) + monkeypatch.setenv("SITES_DIR", str(sites_override)) + importlib.reload(engine) + importlib.reload(appctl_daemon) + importlib.reload(gitops_dispatcher) + assert engine.SITES_DIR == str(workloads_override) + assert appctl_daemon.SITES_DIR == workloads_override + assert gitops_dispatcher.SITES_DIR == workloads_override + + # 2. SITES_DIR acts as fallback when WORKLOADS_DIR is unset + monkeypatch.delenv("WORKLOADS_DIR", raising=False) + importlib.reload(engine) + importlib.reload(appctl_daemon) + importlib.reload(gitops_dispatcher) + assert engine.SITES_DIR == str(sites_override) + assert appctl_daemon.SITES_DIR == sites_override + assert gitops_dispatcher.SITES_DIR == sites_override + + # 3. Default resolves to ~/Workloads when neither env var is set + monkeypatch.delenv("SITES_DIR", raising=False) + monkeypatch.setenv("HOME", str(fake_home)) + importlib.reload(engine) + importlib.reload(appctl_daemon) + importlib.reload(gitops_dispatcher) + expected_default = fake_home / "Workloads" + assert engine.SITES_DIR == str(expected_default) + assert appctl_daemon.SITES_DIR == expected_default + assert gitops_dispatcher.SITES_DIR == expected_default + finally: + monkeypatch.undo() + importlib.reload(engine) + importlib.reload(appctl_daemon) + importlib.reload(gitops_dispatcher)