From 8da559381fece567bf9e519eb486e86de2c574e2 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Tue, 27 Oct 2026 00:16:28 -0500 Subject: [PATCH] feat(nixos): decouple operator identity, derive LDAP base DN, and enforce recursive ACLs - Migrate default administrative user from 'kiskaadee' to generic 'operator' in nixos/configuration.nix. - Configure programs.git.config.safe.directory to allow the homelab-appctl daemon to execute Git commands against operator Sites repositories. - Derive LLDAP_LDAP_BASE_DN dynamically from config.homelab.domain in nixos/modules/homeserver.nix and project it to both lldap and authelia in docker-compose.yml. - Upgrade systemd.tmpfiles.rules for Sites, var/lib, and homepage config to recursive POSIX ACLs ('A+') to ensure automatic permission inheritance for newly created directories and files. - Add security invariant tests asserting LLDAP/Authelia env wiring, dynamic base DN derivation, and recursive ACL rule syntax. --- docker-compose.yml | 1 + nixos/configuration.nix | 13 ++++- nixos/modules/homeserver.nix | 12 +++-- tests/security/test_security_invariants.py | 58 ++++++++++++++++++++++ 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 28c134f..1557b8e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -142,6 +142,7 @@ services: - ./config/authelia:/config environment: - DOMAIN=${DOMAIN} + - LLDAP_LDAP_BASE_DN=${LLDAP_LDAP_BASE_DN} - AUTHELIA_SESSION_SECRET=${AUTHELIA_SESSION_SECRET} - AUTHELIA_STORAGE_ENCRYPTION_KEY=${AUTHELIA_STORAGE_ENCRYPTION_KEY} - AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET=${AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET} diff --git a/nixos/configuration.nix b/nixos/configuration.nix index a2ac096..19f184c 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -15,7 +15,15 @@ ]; # ── Homelab platform settings ──────────────────────────────────────────────── - homelab.operatorUser = "kiskaadee"; + homelab.operatorUser = "operator"; + + # ── Host Git Safe Directory ────────────────────────────────────────────────── + programs.git = { + enable = true; + config.safe.directory = [ + "/home/${config.homelab.operatorUser}/Sites/*" + ]; + }; # ── SOPS secrets configuration ────────────────────────────────────────────── # Set once here to avoid duplicate-option conflicts across submodules @@ -57,8 +65,9 @@ networking.networkmanager.enable = true; # ── User account ───────────────────────────────────────────────────────────── - users.users.kiskaadee = { + users.users.${config.homelab.operatorUser} = { isNormalUser = true; + description = "Primary server administrator"; openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICAVPPMk+WApUd/fo78+lsRGnLKSlY4GDZiNGKwBAifD" ]; diff --git a/nixos/modules/homeserver.nix b/nixos/modules/homeserver.nix index 95970e8..09cc54d 100644 --- a/nixos/modules/homeserver.nix +++ b/nixos/modules/homeserver.nix @@ -28,7 +28,10 @@ }; }; - config = { + config = let + domainParts = lib.splitString "." config.homelab.domain; + ldapBaseDn = lib.concatStringsSep "," (map (part: "dc=${part}") domainParts); + in { # Define the keys to decrypt from secrets.yaml sops.secrets = (lib.genAttrs [ "dynu/api_key" @@ -62,6 +65,7 @@ owner = config.homelab.operatorUser; content = lib.generators.toKeyValue {} { DOMAIN = config.homelab.domain; + LLDAP_LDAP_BASE_DN = ldapBaseDn; DOCKER_API_VERSION = "1.40"; DYNU_API_KEY = config.sops.placeholder."dynu/api_key"; ACME_EMAIL = config.sops.placeholder."traefik/acme_email"; @@ -241,9 +245,9 @@ # 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" - "a+ ${config.homelab.sitesDir} - - - - d:u:homelab-appctl:rwx,u:homelab-appctl:rwx" - "a+ /home/${config.homelab.operatorUser}/var/lib/homelab - - - - d:u:homelab-appctl:rwx,u:homelab-appctl:rwx" - "a+ ${config.homelab.coreDir}/config/homepage - - - - d:u:homelab-appctl:rwx,u:homelab-appctl:rwx" + "A+ ${config.homelab.sitesDir} - - - - d:u:homelab-appctl:rwx,u:homelab-appctl:rwx" + "A+ /home/${config.homelab.operatorUser}/var/lib/homelab - - - - d:u:homelab-appctl:rwx,u:homelab-appctl:rwx" + "A+ ${config.homelab.coreDir}/config/homepage - - - - d:u:homelab-appctl:rwx,u:homelab-appctl:rwx" ]; # Open ports in the firewall for Traefik, Gitea SSH, GitOps Webhook Receiver, and Mail (SMTP, Submission, SMTPS, IMAPS, ManageSieve) diff --git a/tests/security/test_security_invariants.py b/tests/security/test_security_invariants.py index f186b90..477759a 100644 --- a/tests/security/test_security_invariants.py +++ b/tests/security/test_security_invariants.py @@ -194,6 +194,7 @@ def test_platform_genericism_no_hardcoded_personal_domains_in_scripts_and_tests( CORE_ROOT / "SECURITY.md", CORE_ROOT / "flake.nix", CORE_ROOT / "docker-compose.yml", + CORE_ROOT / "nixos" / "configuration.nix", ] violations = [] @@ -218,3 +219,60 @@ def test_platform_genericism_no_hardcoded_personal_domains_in_scripts_and_tests( assert not violations, "Platform genericism violations detected:\n" + "\n".join( violations ) + + +def test_docker_compose_authelia_and_lldap_base_dn(): + """Ensure both lldap and authelia services receive LLDAP_LDAP_BASE_DN in docker-compose.yml.""" + compose_path = CORE_ROOT / "docker-compose.yml" + with open(compose_path, encoding="utf-8") as f: + compose = yaml.safe_load(f) + + services = compose.get("services", {}) + assert "lldap" in services, "lldap service must exist in docker-compose.yml" + assert "authelia" in services, "authelia service must exist in docker-compose.yml" + + def get_env_map(svc_def: dict) -> dict[str, str]: + env_items = svc_def.get("environment", []) + res = {} + for item in env_items: + if isinstance(item, str) and "=" in item: + k, v = item.split("=", 1) + res[k.strip()] = v.strip() + return res + + lldap_env = get_env_map(services["lldap"]) + authelia_env = get_env_map(services["authelia"]) + + assert "LLDAP_LDAP_BASE_DN" in lldap_env, ( + "LLDAP_LDAP_BASE_DN must be declared in lldap environment" + ) + assert "LLDAP_LDAP_BASE_DN" in authelia_env, ( + "LLDAP_LDAP_BASE_DN must be passed to authelia environment for LDAP template expansion" + ) + + +def test_nixos_homeserver_env_template_defines_dynamic_base_dn(): + """Ensure homeserver.nix derives LLDAP_LDAP_BASE_DN dynamically from homelab.domain.""" + homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix" + content = homeserver_nix.read_text(encoding="utf-8") + + assert "LLDAP_LDAP_BASE_DN" in content, ( + "homeserver.nix must define LLDAP_LDAP_BASE_DN in homeserver.env template" + ) + assert 'lib.splitString "."' in content or "splitString" in content, ( + "homeserver.nix must compute LDAP base DN dynamically from domain components" + ) + + +def test_nixos_tmpfiles_rules_use_recursive_acls(): + """Ensure tmpfiles.rules uses uppercase A+ for recursive ACL inheritance on sites and homepage.""" + homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix" + content = homeserver_nix.read_text(encoding="utf-8") + + assert '"A+ ${config.homelab.sitesDir}' in content, ( + "tmpfiles.rules must use uppercase 'A+' for recursive ACL inheritance on sitesDir" + ) + assert '"A+ ${config.homelab.coreDir}/config/homepage' in content, ( + "tmpfiles.rules must use uppercase 'A+' for recursive ACL inheritance on homepage config" + ) +