diff --git a/nixos/modules/homeserver.nix b/nixos/modules/homeserver.nix index 09cc54d..ae955ee 100644 --- a/nixos/modules/homeserver.nix +++ b/nixos/modules/homeserver.nix @@ -103,8 +103,8 @@ # Declarative Dynamic GitOps Webhook Service (Internal Port 9000) systemd.services.homelab-gitops = { description = "Dynamic Homelab GitOps Webhook Dispatcher"; - after = [ "network-online.target" "docker.service" ]; - wants = [ "network-online.target" ]; + after = [ "network-online.target" "docker.service" "homelab-appctl.service" ]; + wants = [ "network-online.target" "homelab-appctl.service" ]; wantedBy = [ "multi-user.target" ]; path = with pkgs; [ @@ -154,7 +154,7 @@ ReadWritePaths = [ config.homelab.sitesDir "/home/${config.homelab.operatorUser}/.local/state/homelab" - "/run/homelab" + "-/run/homelab" ]; }; }; @@ -226,11 +226,11 @@ ProtectControlGroups = true; ReadWritePaths = [ config.homelab.sitesDir - "/home/${config.homelab.operatorUser}/var/lib/homelab" - "${config.homelab.coreDir}/config/homepage" + "-/home/${config.homelab.operatorUser}/var/lib/homelab" + "-${config.homelab.coreDir}/config/homepage" "/var/log/homelab/appctl" - "/var/lib/homelab/appctl" - "/run/homelab" + "-/var/lib/homelab/appctl" + "-/run/homelab" ]; }; }; @@ -242,6 +242,7 @@ "d ${config.homelab.sitesDir} 0755 ${config.homelab.operatorUser} users -" "d /home/${config.homelab.operatorUser}/.local/state/homelab/gitops 0755 ${config.homelab.operatorUser} users -" "d /home/${config.homelab.operatorUser}/.local/state/homelab/docker 0755 ${config.homelab.operatorUser} users -" + "d /home/${config.homelab.operatorUser}/var/lib/homelab 0755 ${config.homelab.operatorUser} users -" # POSIX ACLs granting homelab-appctl traverse and access to operator sites & homepage config "a+ /home/${config.homelab.operatorUser} - - - - u:homelab-appctl:x" "a+ ${config.homelab.coreDir} - - - - u:homelab-appctl:x" diff --git a/tests/security/test_security_invariants.py b/tests/security/test_security_invariants.py index b03178c..4720d80 100644 --- a/tests/security/test_security_invariants.py +++ b/tests/security/test_security_invariants.py @@ -2,6 +2,7 @@ Security invariant tests for Docker Compose and container isolation. """ +import json import os import shutil import subprocess @@ -291,6 +292,61 @@ def test_nixos_tmpfiles_rules_use_recursive_acls(): ) +def test_nixos_tmpfiles_rules_creates_operator_var_lib_homelab(): + """Ensure tmpfiles.rules creates operator var/lib/homelab state dir before ACL assignment (#22).""" + homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix" + content = homeserver_nix.read_text(encoding="utf-8") + + expected_create = '"d /home/${config.homelab.operatorUser}/var/lib/homelab 0755 ${config.homelab.operatorUser} users -"' + expected_acl = '"A+ /home/${config.homelab.operatorUser}/var/lib/homelab - - - - d:u:homelab-appctl:rwx,u:homelab-appctl:rwx"' + + assert expected_create in content, ( + "tmpfiles.rules must declare creation of operator var/lib/homelab directory" + ) + assert expected_acl in content, ( + "tmpfiles.rules must declare ACLs for operator var/lib/homelab directory" + ) + assert content.index(expected_create) < content.index(expected_acl), ( + "operator var/lib/homelab directory creation must precede its ACL rule" + ) + + +def test_nixos_appctl_readwritepaths_optional_prefixes(): + """Ensure appctl service ReadWritePaths uses '-' prefix for optional runtime paths (#22).""" + homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix" + content = homeserver_nix.read_text(encoding="utf-8") + + assert '"-/home/${config.homelab.operatorUser}/var/lib/homelab"' in content + assert '"-${config.homelab.coreDir}/config/homepage"' in content + assert '"-/var/lib/homelab/appctl"' in content + assert '"-/run/homelab"' in content + + +def test_nixos_gitops_orders_after_appctl(): + """Ensure homelab-gitops service declares after and wants dependency on homelab-appctl.service (#22).""" + homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix" + content = homeserver_nix.read_text(encoding="utf-8") + + assert '"homelab-appctl.service"' in content + + nix_bin = shutil.which("nix") + if nix_bin and not os.environ.get("NIX_BUILD_TOP"): + for attr in ["after", "wants"]: + cmd = [ + nix_bin, + "eval", + f".#nixosConfigurations.server.config.systemd.services.homelab-gitops.{attr}", + "--json", + ] + res = subprocess.run( + cmd, cwd=CORE_ROOT, capture_output=True, text=True, check=True + ) + data = json.loads(res.stdout) + assert "homelab-appctl.service" in data, ( + f"homelab-gitops {attr} must include homelab-appctl.service" + ) + + def test_default_homelab_domain_evaluates_to_example_test(): """Assert default homelab.domain in NixOS config evaluates to 'example.test'.""" homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix"