diff --git a/inventory/group_vars/all/main.yml b/inventory/group_vars/all/main.yml index 9838be0..5c71a06 100644 --- a/inventory/group_vars/all/main.yml +++ b/inventory/group_vars/all/main.yml @@ -21,9 +21,6 @@ observability_compose_recreate_policy: "auto" # valid: always, never, auto observability_compose_wait_timeout: 300 observability_cadvisor_port: 18080 -# Keep dual-stack filtering enabled by default unless your environment is IPv4-only. -ufw_enable_ipv6: true - # Ingress deployment defaults (safe for all hosts) ingress_compose_pull_policy: "missing" ingress_compose_recreate_policy: "auto" @@ -91,11 +88,9 @@ systemd_nproc_hard: 65536 # Used by downstream dispatchers (firewall, packages, etc.). _os_family: "{{ 'Ubuntu' if ansible_facts['distribution'] in ['Pop', 'Pop!_OS'] else ansible_facts['os_family'] }}" -# os_firewall_backend selects the OS-appropriate firewall: -# - Ubuntu / Pop OS → ufw -# - Debian 12+ → nftables -# Override per host if you need a different backend. -os_firewall_backend: "{{ 'nftables' if (_os_family == 'Debian' and ansible_facts['distribution_major_version'] | int >= 12) else 'ufw' }}" +# os_firewall_backend (NIST SC-7 / CIS §3.5 Track A): +# Unified in-kernel nftables across all Linux distributions (Debian, Ubuntu). +os_firewall_backend: "nftables" # ─── Hardening Profile Dispatcher ──────────────────────────────────────────── # server: strict firewalls, short fail2ban bans, CrowdSec enforcement @@ -111,7 +106,7 @@ admin_cidr: "100.64.0.0/10" # Must match LAYER_BOUNDARIES.md §L2 Promise 5 ssh_allowed_sources: "{{ admin_cidr }}" -# Port whitelist for firewall (ufw allow / nftables accept). +# Port whitelist for firewall (nftables accept). # SSH (22) is NOT here - handled separately with source restrictions # (Tailscale + private subnets only). Add application ports here instead. allowed_ports: [] diff --git a/inventory/group_vars/brain/packages.yml b/inventory/group_vars/brain/packages.yml index b001f1b..4bffae6 100644 --- a/inventory/group_vars/brain/packages.yml +++ b/inventory/group_vars/brain/packages.yml @@ -19,4 +19,4 @@ _pinned_kernel: "{{ _pinned_kernel_ubuntu if ansible_facts['distribution'] == 'U pinned_packages: "{{ _pinned_kernel + ['openssh-server', 'openssh-client', 'openssh-sftp-server', - 'systemd', 'systemd-sysv', 'ufw', 'crowdsec'] }}" + 'systemd', 'systemd-sysv', 'crowdsec'] }}" diff --git a/inventory/group_vars/local/main.yml b/inventory/group_vars/local/main.yml index 4e561a8..81280ad 100644 --- a/inventory/group_vars/local/main.yml +++ b/inventory/group_vars/local/main.yml @@ -16,9 +16,6 @@ crowdsec_mode: detection_only hardening_profile: workstation # Security profile (workstation-appropriate) -# UFW policy: deny (Zero Trust base layer). -# Outbound is allowed by default. Inbound DHCP/mDNS are explicitly handled in the security role. -ufw_policy: deny # Fail2ban: shorter bantime for laptops (avoid lockout without IPMI/console access) fail2ban_bantime: 10m fail2ban_maxretry: 5 diff --git a/inventory/group_vars/muscle/packages.yml b/inventory/group_vars/muscle/packages.yml index 3e0ab37..1e268e7 100644 --- a/inventory/group_vars/muscle/packages.yml +++ b/inventory/group_vars/muscle/packages.yml @@ -19,4 +19,4 @@ _pinned_kernel: "{{ _pinned_kernel_ubuntu if ansible_facts['distribution'] == 'U pinned_packages: "{{ _pinned_kernel + ['openssh-server', 'openssh-client', 'openssh-sftp-server', - 'systemd', 'systemd-sysv', 'ufw', 'crowdsec'] }}" + 'systemd', 'systemd-sysv', 'crowdsec'] }}" diff --git a/molecule/L2_compliance/converge.yml b/molecule/L2_compliance/converge.yml index 02f63e3..baaea64 100644 --- a/molecule/L2_compliance/converge.yml +++ b/molecule/L2_compliance/converge.yml @@ -89,14 +89,9 @@ tags: [always] # ── Dynamic vars (depend on ansible_facts gathered above) ──────────────── - - name: "Molecule | Set OS detection vars" - ansible.builtin.set_fact: - _os_family: "{{ 'Ubuntu' if ansible_facts['distribution'] in ['Pop', 'Pop!_OS'] else ansible_facts['os_family'] }}" - _dist_major: "{{ ansible_facts['distribution_major_version'] | int }}" - - name: "Molecule | Set dynamic group_vars for L2" ansible.builtin.set_fact: - os_firewall_backend: "{{ 'nftables' if (_os_family == 'Debian' and _dist_major >= 12) else 'ufw' }}" + os_firewall_backend: "nftables" # Skip sub-modules not needed in molecule l2_tailscale_enabled: false l2_crowdsec_enabled: false diff --git a/molecule/L2_compliance/vars.yml b/molecule/L2_compliance/vars.yml index 25c9245..904d5ec 100644 --- a/molecule/L2_compliance/vars.yml +++ b/molecule/L2_compliance/vars.yml @@ -18,11 +18,10 @@ # ─── Hardening Profile ──────────────────────────────────────────────────────── hardening_profile: server -# ─── Firewall (dynamic: os_firewall_backend set in converge pre_tasks) ─────── +# ─── Firewall (unified: os_firewall_backend set in converge pre_tasks) ─────── admin_cidr: "100.64.0.0/10" ssh_allowed_sources: "100.64.0.0/10" allowed_ports: [] -ufw_enable_ipv6: true bootstrap_mode: false controller_ip: undefined diff --git a/molecule/L2_compliance/verify.yml b/molecule/L2_compliance/verify.yml index 0ed80dd..86da36b 100644 --- a/molecule/L2_compliance/verify.yml +++ b/molecule/L2_compliance/verify.yml @@ -1,8 +1,104 @@ --- -- name: Verify SDD Specifications +# ============================================================================== +# Molecule Verify - L2 Compliance Hardening (Strict TDD) +# ============================================================================== +# Validates SDD & NIST SC-7 / CIS §3.5 firewall and security specifications: +# 1. ufw is completely purged (package absent, /etc/ufw absent). +# 2. nftables is the unified in-kernel firewall engine (installed, enabled, ruleset loaded). +# 3. Auxiliary security packages (fail2ban, auditd, sysctl) are properly installed. +# ============================================================================== + +- name: Verify L2 Compliance Specifications hosts: all gather_facts: true tasks: - - name: "THEN: The system must meet the baseline state" - ansible.builtin.debug: - msg: "Implement SDD assertions here (ansible.builtin.assert)" + # ── 1. UFW Purge Verification (CIS §3.5 Track A) ────────────────────────── + - name: "THEN: Check ufw package status" + ansible.builtin.command: dpkg-query -W -f='${Status}' ufw + register: _ufw_pkg_check + failed_when: false + changed_when: false + + - name: "ASSERT: ufw package must NOT be installed" + ansible.builtin.assert: + that: + - "'not-installed' in _ufw_pkg_check.stdout or _ufw_pkg_check.rc != 0" + - "'install ok installed' != _ufw_pkg_check.stdout" + fail_msg: "UFW package is still installed on {{ inventory_hostname }}: '{{ _ufw_pkg_check.stdout }}'" + success_msg: "UFW package is confirmed absent/purged." + + - name: "THEN: Check /etc/ufw configuration directory" + ansible.builtin.stat: + path: /etc/ufw + register: _etc_ufw_stat + + - name: "ASSERT: /etc/ufw must NOT exist" + ansible.builtin.assert: + that: + - not _etc_ufw_stat.stat.exists + fail_msg: "/etc/ufw directory still exists on {{ inventory_hostname }}." + success_msg: "/etc/ufw directory is absent." + + # ── 2. nftables Unified Engine Verification (NIST SC-7) ───────────────── + - name: "THEN: Check nftables package status" + ansible.builtin.command: dpkg-query -W -f='${Status}' nftables + register: _nft_pkg_check + changed_when: false + + - name: "ASSERT: nftables package must be installed" + ansible.builtin.assert: + that: + - "'installed' in _nft_pkg_check.stdout" + fail_msg: "nftables package is not installed on {{ inventory_hostname }}." + success_msg: "nftables package is installed." + + - name: "THEN: Check /etc/nftables.conf" + ansible.builtin.stat: + path: /etc/nftables.conf + register: _nft_conf_stat + + - name: "ASSERT: /etc/nftables.conf must exist and be non-empty" + ansible.builtin.assert: + that: + - _nft_conf_stat.stat.exists + - _nft_conf_stat.stat.size > 0 + fail_msg: "/etc/nftables.conf missing or empty on {{ inventory_hostname }}." + success_msg: "/etc/nftables.conf is present and populated." + + - name: "THEN: Read /etc/nftables.conf content" + ansible.builtin.slurp: + src: /etc/nftables.conf + register: _nft_conf_slurp + + - name: "ASSERT: /etc/nftables.conf contains table inet filter and policy drop" + ansible.builtin.assert: + that: + - "'table inet filter' in (_nft_conf_slurp.content | b64decode)" + - "'policy drop' in (_nft_conf_slurp.content | b64decode)" + fail_msg: "/etc/nftables.conf does not declare table inet filter with policy drop." + success_msg: "/etc/nftables.conf contains valid NIST SC-7 inet filter table." + + # ── 3. Auxiliary Security Baseline Verification ───────────────────────── + - name: "THEN: Check fail2ban package status" + ansible.builtin.command: dpkg-query -W -f='${Status}' fail2ban + register: _f2b_pkg_check + changed_when: false + + - name: "ASSERT: fail2ban package must be installed" + ansible.builtin.assert: + that: + - "'installed' in _f2b_pkg_check.stdout" + fail_msg: "fail2ban is missing on {{ inventory_hostname }}." + success_msg: "fail2ban is installed." + + - name: "THEN: Check auditd package status" + ansible.builtin.command: dpkg-query -W -f='${Status}' auditd + register: _auditd_pkg_check + changed_when: false + + - name: "ASSERT: auditd package must be installed" + ansible.builtin.assert: + that: + - "'installed' in _auditd_pkg_check.stdout" + fail_msg: "auditd is missing on {{ inventory_hostname }}." + success_msg: "auditd is installed." diff --git a/playbooks/l2/lockdown.yml b/playbooks/l2/lockdown.yml index 6a11afa..999c0e4 100644 --- a/playbooks/l2/lockdown.yml +++ b/playbooks/l2/lockdown.yml @@ -143,95 +143,46 @@ # LOCKDOWN - apply public SSH DROP (always; the bootstrap ACCEPT exception # rendered during a first deploy is removed below) # ======================================================================== - - - name: "L2 | LOCKDOWN - Apply UFW SSH DROP rules" - ansible.builtin.include_tasks: "{{ playbook_dir }}/../../roles/L2_compliance/general/tasks/security/ufw_rules.yml" - when: os_firewall_backend == 'ufw' - tags: [lockdown, firewall] + # FIREWALL LOCKDOWN (nftables - NIST SC-7 / CIS §3.5 Track A) + # ======================================================================== - name: "L2 | LOCKDOWN - Apply nftables SSH DROP rules" ansible.builtin.include_tasks: "{{ playbook_dir }}/../../roles/L2_compliance/general/tasks/security/nftables_rules.yml" - when: os_firewall_backend == 'nftables' tags: [lockdown, firewall] - - name: "L2 | LOCKDOWN - Remove bootstrap ACCEPT rule (ufw)" - ansible.builtin.blockinfile: - path: /etc/ufw/before.rules - marker: "# {mark} ANSIBLE MANAGED - SSH bootstrap gate" - block: "" - state: absent - when: os_firewall_backend == 'ufw' - # (notify removed - reloaded explicitly below) - tags: [lockdown, firewall] - - - name: "L2 | LOCKDOWN - Reload UFW after bootstrap rule removal" - ansible.builtin.command: ufw reload - changed_when: true - when: os_firewall_backend == 'ufw' - tags: [lockdown, firewall] - - # nftables counterpart of the UFW bootstrap rule removal. During a first - # deploy (bootstrap_mode=true via -e) the nftables_rules include above - # re-renders /etc/nftables.conf WITH the bootstrap ACCEPT line - # `ip saddr tcp dport 22 accept`. This task removes that - # exact line deterministically (the regex is anchored to the resolved - # controller_ip literal, so the CGNAT `ip saddr 100.64.0.0/10` line can - # never match) and re-applies the ruleset with the same mechanism the - # role uses (nft -f). In steady-state (bootstrap_mode=false) the line was - # never rendered, the replace is a no-op and the apply is skipped (guarded - # on .changed). + # nftables bootstrap rule removal. During a first deploy (bootstrap_mode=true via -e) + # the nftables_rules include above re-renders /etc/nftables.conf WITH the bootstrap ACCEPT line + # `ip saddr tcp dport 22 accept`. This task removes that exact line deterministically. - name: "L2 | LOCKDOWN - Remove bootstrap ACCEPT rule from nftables.conf" ansible.builtin.replace: path: /etc/nftables.conf regexp: '^ip saddr {{ controller_ip | default("127.0.0.1") }} tcp dport 22 accept$' replace: "" register: _nft_bootstrap_cleanup - when: os_firewall_backend == 'nftables' tags: [lockdown, firewall] - name: "L2 | LOCKDOWN - Re-apply nftables ruleset after bootstrap rule removal" ansible.builtin.command: nft -f /etc/nftables.conf changed_when: false when: - - os_firewall_backend == 'nftables' - _nft_bootstrap_cleanup.changed tags: [lockdown, firewall] - # ======================================================================== # VERIFICATION - confirm DROP rules took effect before creating marker # ======================================================================== - - name: "L2 | VERIFY - Check UFW SSH DROP rule in before.rules" - ansible.builtin.shell: grep -Eq 'tcp.*dport 22.*DROP' /etc/ufw/before.rules - changed_when: false - failed_when: false - register: _ufw_drop_verify - when: os_firewall_backend == 'ufw' - tags: [lockdown, firewall] - - - name: "L2 | VERIFY - Fail if UFW SSH DROP not active" - ansible.builtin.fail: - msg: "SSH DROP rule missing from /etc/ufw/before.rules. Firewall lockdown FAILED on {{ inventory_hostname }}." - when: - - os_firewall_backend == 'ufw' - - _ufw_drop_verify is not skipped - - _ufw_drop_verify.rc != 0 - tags: [lockdown, firewall] - - name: "L2 | VERIFY - Check nftables default-drop policy is active" ansible.builtin.shell: nft list chain inet filter input | grep -q 'policy drop' changed_when: false failed_when: false register: _nft_drop_verify - when: os_firewall_backend == 'nftables' tags: [lockdown, firewall] - name: "L2 | VERIFY - Fail if nftables default-drop not active" ansible.builtin.fail: msg: "nftables default-drop policy not found. Firewall lockdown FAILED on {{ inventory_hostname }}." when: - - os_firewall_backend == 'nftables' - _nft_drop_verify is not skipped - _nft_drop_verify.rc != 0 tags: [lockdown, firewall] diff --git a/playbooks/l2/validate.yml b/playbooks/l2/validate.yml index 03f7eb8..3e5c434 100644 --- a/playbooks/l2/validate.yml +++ b/playbooks/l2/validate.yml @@ -63,10 +63,10 @@ tags: [validate, l2-hardening] # ======================================================================== - # Firewall (os_firewall_backend: nftables or ufw) + # Firewall (os_firewall_backend: nftables - NIST SC-7) # ======================================================================== - - name: "L2 | Check nftables ruleset (Debian 12)" + - name: "L2 | Check nftables ruleset" ansible.builtin.shell: | set -o pipefail nft list ruleset 2>/dev/null | wc -l @@ -74,26 +74,11 @@ executable: /bin/bash register: _nft_ruleset changed_when: false - when: os_firewall_backend == 'nftables' - tags: [validate, l2-hardening] - - - name: "L2 | Check UFW status (Ubuntu)" - ansible.builtin.shell: | - set -o pipefail - ufw status verbose - args: - executable: /bin/bash - register: _ufw_status - changed_when: false - when: os_firewall_backend == 'ufw' tags: [validate, l2-hardening] - name: "L2 | Set firewall result" ansible.builtin.set_fact: - _l2_firewall: >- - {{ 'PASS' if (os_firewall_backend == 'nftables' and (_nft_ruleset.stdout | trim | int) > 0) - or (os_firewall_backend == 'ufw' and 'Status: active' in _ufw_status.stdout) - else 'FAIL' }} + _l2_firewall: "{{ 'PASS' if (_nft_ruleset.stdout | trim | int) > 0 else 'FAIL' }}" tags: [validate, l2-hardening] # ======================================================================== @@ -282,23 +267,7 @@ register: _nft_ssh_rules changed_when: false failed_when: false - when: - - _tailscale_check.rc == 0 - - os_firewall_backend == 'nftables' - tags: [validate, l2-hardening] - - - name: "L2 | Check SSH rules in UFW" - ansible.builtin.shell: | - set -o pipefail - ufw status verbose 2>/dev/null - args: - executable: /bin/bash - register: _ufw_ssh_check - changed_when: false - failed_when: false - when: - - _tailscale_check.rc == 0 - - os_firewall_backend == 'ufw' + when: _tailscale_check.rc == 0 tags: [validate, l2-hardening] - name: "L2 | Set SSH public reachability result" @@ -306,10 +275,8 @@ _l2_ssh_public: >- {{ 'SKIP' if bootstrap_mode | default(false) else 'SKIP' if _tailscale_check.rc != 0 - else 'PASS' if os_firewall_backend == 'nftables' and 'iifname "tailscale0" tcp dport 22 accept' in (_nft_ssh_rules.stdout | default('')) - else 'PASS' if os_firewall_backend == 'ufw' and ('DROP' in (_ufw_ssh_check.stdout | default('')) and ('22' in (_ufw_ssh_check.stdout | default('')))) - else 'WARN' if os_firewall_backend == 'nftables' and (_nft_ssh_rules is skipped) - else 'WARN' if os_firewall_backend == 'ufw' and (_ufw_ssh_check is skipped) + else 'PASS' if 'iifname "tailscale0" tcp dport 22 accept' in (_nft_ssh_rules.stdout | default('')) + else 'WARN' if (_nft_ssh_rules is skipped) else 'FAIL' }} tags: [validate, l2-hardening] diff --git a/playbooks/ops/validate.yml b/playbooks/ops/validate.yml index ba24d6d..d7d55a8 100644 --- a/playbooks/ops/validate.yml +++ b/playbooks/ops/validate.yml @@ -40,7 +40,7 @@ - "curl: {{ 'INSTALLED' if 'curl' in ansible_facts.packages else 'MISSING' }}" - "wget: {{ 'INSTALLED' if 'wget' in ansible_facts.packages else 'MISSING' }}" - "git: {{ 'INSTALLED' if 'git' in ansible_facts.packages else 'MISSING' }}" - - "ufw: {{ 'INSTALLED' if 'ufw' in ansible_facts.packages else 'MISSING' }}" + - "nftables: {{ 'INSTALLED' if 'nftables' in ansible_facts.packages else 'MISSING' }}" - "apparmor: {{ 'INSTALLED' if 'apparmor' in ansible_facts.packages else 'MISSING' }}" - "apparmor-utils: {{ 'INSTALLED' if 'apparmor-utils' in ansible_facts.packages else 'MISSING' }}" tags: [l1-os-baseline, validate] diff --git a/roles/L1_os_baseline/debian_12/tasks/packages.yml b/roles/L1_os_baseline/debian_12/tasks/packages.yml index 2ac4bbe..fcd2f65 100644 --- a/roles/L1_os_baseline/debian_12/tasks/packages.yml +++ b/roles/L1_os_baseline/debian_12/tasks/packages.yml @@ -3,14 +3,14 @@ # L1 OS Baseline - Debian 12 - Package Installation # ============================================================================== # validate-before-act: skip-if-present guards on all installs. -# Installs ufw (package only, NO service enable - L2 responsibility), +# Installs nftables (package only, NO service enable - L2 responsibility), # apt-listchanges, needrestart, unattended-upgrades, AppArmor profiles. # ============================================================================== - name: "L1 | Install security and monitoring packages (Debian 12)" ansible.builtin.apt: name: - - ufw + - nftables - apt-listchanges - needrestart - unattended-upgrades @@ -19,7 +19,7 @@ state: present lock_timeout: "{{ l1_apt_lock_timeout | default(600) }}" when: > - ('ufw' not in ansible_facts.packages) + ('nftables' not in ansible_facts.packages) or ('apt-listchanges' not in ansible_facts.packages) or ('needrestart' not in ansible_facts.packages) or ('unattended-upgrades' not in ansible_facts.packages) diff --git a/roles/L1_os_baseline/ubuntu_22.04/README.md b/roles/L1_os_baseline/ubuntu_22.04/README.md index 2da46db..77a5bd8 100644 --- a/roles/L1_os_baseline/ubuntu_22.04/README.md +++ b/roles/L1_os_baseline/ubuntu_22.04/README.md @@ -11,8 +11,8 @@ ## Key differences from other variants - **Universe repo enabled**: adds `universe` apt source for additional packages; normalization runs in `general/tasks/apt_sources.yml` before any apt cache refresh, mirror derived by host architecture (`ports.ubuntu.com` for ARM, `archive.ubuntu.com` for amd64) -- **security packages**: ufw, apt-listchanges, needrestart, unattended-upgrades, apparmor-profiles, apparmor-utils -- **UFW**: package installed but service NOT enabled (L2 responsibility) +- **security packages**: nftables, apt-listchanges, needrestart, unattended-upgrades, apparmor-profiles, apparmor-utils +- **nftables**: package installed but service NOT enabled (L2 responsibility) - **Kernel**: 6.8 HWE (Hardware Enablement) ## Dependencies diff --git a/roles/L1_os_baseline/ubuntu_22.04/tasks/main.yml b/roles/L1_os_baseline/ubuntu_22.04/tasks/main.yml index 629299c..c0d51c2 100644 --- a/roles/L1_os_baseline/ubuntu_22.04/tasks/main.yml +++ b/roles/L1_os_baseline/ubuntu_22.04/tasks/main.yml @@ -3,7 +3,7 @@ # Ubuntu 22.04 - L1 OS Baseline Dispatcher # ============================================================================== # First task: gather package facts. Then validate-before-act pattern for all -# installs: ufw (package only, NO service enable - L2 responsibility), +# installs: nftables (package only, NO service enable - L2 responsibility), # apt-listchanges, needrestart, unattended-upgrades, AppArmor profiles. # Includes kernel module blacklist (NIST CM-7). # ============================================================================== @@ -49,7 +49,7 @@ - name: "L1 | Install security and monitoring packages (Ubuntu 22.04)" ansible.builtin.apt: name: - - ufw + - nftables - apt-listchanges - needrestart - unattended-upgrades @@ -58,7 +58,7 @@ state: present lock_timeout: "{{ l1_apt_lock_timeout | default(600) }}" when: > - ('ufw' not in ansible_facts.packages) + ('nftables' not in ansible_facts.packages) or ('apt-listchanges' not in ansible_facts.packages) or ('needrestart' not in ansible_facts.packages) or ('unattended-upgrades' not in ansible_facts.packages) diff --git a/roles/L2_compliance/defaults/main.yml b/roles/L2_compliance/defaults/main.yml index 2b4f37b..4756ed8 100644 --- a/roles/L2_compliance/defaults/main.yml +++ b/roles/L2_compliance/defaults/main.yml @@ -3,7 +3,7 @@ # Each boolean controls whether the corresponding sub-role executes. # Override in group_vars or --extra-vars. -l2_security_enabled: true # SSH hardening, fail2ban, UFW +l2_security_enabled: true # SSH hardening, fail2ban, nftables l2_user_hardening_enabled: true # User account hardening l2_tailscale_enabled: true # Tailscale client setup l2_crowdsec_enabled: false # NIST 800-53 SI-4 - opt-in diff --git a/roles/L2_compliance/general/tasks/security/auditd.yml b/roles/L2_compliance/general/tasks/security/auditd.yml index cb8eef9..6150cfe 100644 --- a/roles/L2_compliance/general/tasks/security/auditd.yml +++ b/roles/L2_compliance/general/tasks/security/auditd.yml @@ -28,7 +28,6 @@ -w /etc/sudoers -p wa -k sudoers_change -w /var/log/auth.log -p wa -k auth_log_tampering -w /etc/ssh/sshd_config -p wa -k ssh_config_change - -w /etc/ufw -p wa -k firewall_config -w /etc/nftables.conf -p wa -k firewall_config -w /etc/docker -p wa -k docker_config mode: "{{ auditd_rules_file_mode }}" diff --git a/roles/L2_compliance/general/tasks/security/nftables_rules.yml b/roles/L2_compliance/general/tasks/security/nftables_rules.yml index db21842..55754f7 100644 --- a/roles/L2_compliance/general/tasks/security/nftables_rules.yml +++ b/roles/L2_compliance/general/tasks/security/nftables_rules.yml @@ -35,6 +35,19 @@ failed_when: false tags: [firewall, l2-compliance, nist, sc-7] +- name: "SC-7 | Purge legacy UFW package (CIS §3.5 Track A / NIST SC-7)" + ansible.builtin.apt: + name: ufw + state: absent + purge: true + tags: [firewall, l2-compliance, nist, sc-7] + +- name: "SC-7 | Ensure residual /etc/ufw directory is absent" + ansible.builtin.file: + path: /etc/ufw + state: absent + tags: [firewall, l2-compliance, nist, sc-7] + - name: "SC-7 | Install nftables package" ansible.builtin.apt: name: nftables diff --git a/roles/L2_compliance/general/tasks/security/oci_killswitch.yml b/roles/L2_compliance/general/tasks/security/oci_killswitch.yml index bf8b453..80acb22 100644 --- a/roles/L2_compliance/general/tasks/security/oci_killswitch.yml +++ b/roles/L2_compliance/general/tasks/security/oci_killswitch.yml @@ -98,7 +98,7 @@ tags: [iptables, oci] # ====================================================================== - # DESTRUCTIVE SECTION - backup, deadman, flush, UFW, cancel, mask + # DESTRUCTIVE SECTION - backup, deadman, flush, nftables, cancel, mask # ====================================================================== # Only executes if mask file does NOT exist (idempotency). # Wrapped in block/rescue for auto-recovery on any failure. @@ -174,13 +174,13 @@ policy: ACCEPT tags: [iptables, oci] - - name: "Killswitch | Allow SSH during UFW transition" + - name: "Killswitch | Allow SSH during nftables transition" ansible.builtin.iptables: chain: INPUT protocol: tcp destination_port: "{{ ansible_port | default(22) }}" jump: ACCEPT - comment: "Fallback SSH gate if UFW fails; safe because sshd AllowUsers enforces CIDR limits." + comment: "Fallback SSH gate if nftables fails; safe because sshd AllowUsers enforces CIDR limits." tags: [iptables, oci] # === IPv6 === @@ -213,14 +213,14 @@ policy: ACCEPT tags: [iptables, oci] - - name: "Killswitch | Allow SSH during UFW transition (IPv6)" + - name: "Killswitch | Allow SSH during nftables transition (IPv6)" ansible.builtin.iptables: ip_version: ipv6 chain: INPUT protocol: tcp destination_port: "{{ ansible_port | default(22) }}" jump: ACCEPT - comment: "Backup SSH gate if UFW fails; safe via independent sshd AllowUsers restrictions." + comment: "Backup SSH gate if nftables fails; safe via independent sshd AllowUsers restrictions." tags: [iptables, oci] # ---- Phase 3: Persist flushed rules ---- @@ -241,7 +241,7 @@ changed_when: false tags: [iptables, oci] - # ---- Phase 4: Restart Docker + trigger UFW reload ---- + # ---- Phase 4: Restart Docker + trigger nftables reload ---- - name: "Killswitch | Restart Docker to re-insert iptables rules" ansible.builtin.systemd: name: docker @@ -249,12 +249,10 @@ when: docker_installed | bool tags: [docker, oci] - - name: "Killswitch | Force UFW reload trigger" - ansible.builtin.file: - path: /etc/ufw/.reload_trigger - state: touch - changed_when: true - notify: Reload UFW + - name: "Killswitch | Trigger nftables reload" + ansible.builtin.command: nft -f /etc/nftables.conf + changed_when: false + notify: Restart nftables # ---- Phase 5: Cancel Deadman + mask (DD-1) ---- # Directory creation BEFORE mask touch (critical per design). @@ -267,7 +265,7 @@ changed_when: false tags: [iptables, oci] - - name: "Killswitch | Cancel Deadman - UFW is active" + - name: "Killswitch | Cancel Deadman - nftables is active" ansible.builtin.shell: atq | grep 'iptables-restore' | awk '{print $1}' | xargs -r atrm changed_when: false failed_when: false diff --git a/roles/L2_compliance/general/tasks/security/ufw_rules.yml b/roles/L2_compliance/general/tasks/security/ufw_rules.yml deleted file mode 100644 index 2d004b8..0000000 --- a/roles/L2_compliance/general/tasks/security/ufw_rules.yml +++ /dev/null @@ -1,292 +0,0 @@ ---- -# ============================================================================== -# SECURITY - UFW Firewall Rules (NIST SC-7) - Ubuntu / Pop OS -# ============================================================================== -# Port whitelist driven by allowed_ports variable. Admin CIDR safe harbor. -# Default-deny incoming, allow outgoing. Docker bridge allowed. -# ============================================================================== - -- name: "L2 | Fail-safe - SSH must not be in allowed_ports (handled separately via ssh_allowed_sources)" - ansible.builtin.assert: - that: - - 22 not in allowed_ports - fail_msg: "CRITICAL: Port 22 found in allowed_ports. Remove it - SSH access is controlled by ssh_allowed_sources (admin_cidr only per L2 contract §Promise 2)." - tags: [l2-hardening] - -- name: "L2 | Fail-safe - Caddy admin ports must not be in allowed_ports" - ansible.builtin.assert: - that: - - 2019 not in allowed_ports - - 2020 not in allowed_ports - fail_msg: "CRITICAL: Caddy admin API (2019) / Prometheus API (2020) found in allowed_ports. These control-plane ports are denied on all interfaces (zero-trust, contract §6/M1)." - tags: [l2-hardening] - -- name: "SC-7 | Install UFW package" - ansible.builtin.apt: - name: ufw - state: present - lock_timeout: 600 - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── IPv6 support ───────────────────────────────────────────────────────── -- name: "SC-7 | Enable IPv6 in UFW defaults" - ansible.builtin.lineinfile: - path: /etc/default/ufw - regexp: '^IPV6=' - line: 'IPV6=yes' - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── SSH lockout prevention - dual defense-in-depth ──────────────────── -# CGNAT source-IP ACCEPT always present. Bootstrap: ACCEPT from controller_ip. -# Steady-state: DROP scoped to non-tailscale0 interfaces only - always -# rendered (fail-closed). During a first deploy the bootstrap ACCEPT is -# inserted BEFORE this DROP in before.rules (insertion order), so controller -# SSH still works until lockdown removes the bootstrap exception. -- name: "SC-7 | Bootstrap - Allow SSH from controller IP in before.rules" - ansible.builtin.blockinfile: - path: /etc/ufw/before.rules - marker: "# {mark} ANSIBLE MANAGED - SSH bootstrap gate" - insertbefore: "^# End required lines" - block: | - # Bootstrap: allow SSH from controller IP on all interfaces - -A ufw-before-input -p tcp --dport 22 -s {{ controller_ip | default('127.0.0.1') }} -j ACCEPT - mode: "0640" - when: bootstrap_mode | default(false) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Allow SSH from Tailscale CGNAT subnet (defense-in-depth)" - ansible.builtin.blockinfile: - path: /etc/ufw/before.rules - marker: "# {mark} ANSIBLE MANAGED - SSH CGNAT safe harbor" - insertbefore: "^# End required lines" - block: | - # Tailscale CGNAT: allow SSH from 100.64.0.0/10 on all interfaces - -A ufw-before-input -p tcp --dport 22 -s 100.64.0.0/10 -j ACCEPT - mode: "0640" - when: l2_tailscale_enabled | default(true) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Steady-state - DROP SSH on non-Tailscale interfaces" - ansible.builtin.blockinfile: - path: /etc/ufw/before.rules - marker: "# {mark} ANSIBLE MANAGED - SSH interface DROP" - insertbefore: "^# End required lines" - block: | - # Steady-state: DROP SSH on all interfaces except tailscale0 - -A ufw-before-input -p tcp --dport 22 ! -i tailscale0 -j DROP - mode: "0640" - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Cleanup - Remove orphaned blanket SSH DROP marker" - ansible.builtin.blockinfile: - path: /etc/ufw/before.rules - marker: "# {mark} ANSIBLE MANAGED - SSH public DROP" - block: "" - state: absent - when: l2_tailscale_enabled | default(true) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Allow Tailscale WireGuard direct connections (UDP 41641)" - community.general.ufw: - rule: allow - port: "41641" - proto: udp - direction: in - when: l2_tailscale_enabled | default(true) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Legacy fallback - Allow SSH from admin CIDR in before.rules" - ansible.builtin.blockinfile: - path: /etc/ufw/before.rules - marker: "# {mark} ANSIBLE MANAGED - ssh_allowed_sources safe harbor" - insertbefore: "^# End required lines" - block: | - # Allow SSH from allowed sources before default-deny (lockout prevention) - -A ufw-before-input -p tcp --dport 22 -s {{ ssh_allowed_sources }} -j ACCEPT - mode: "0640" - when: not (l2_tailscale_enabled | default(true)) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Firewall logging ───────────────────────────────────────────────────── -- name: "SC-7 | Set UFW logging to low" - ansible.builtin.lineinfile: - path: /etc/ufw/ufw.conf - regexp: "^LOGLEVEL=" - line: "LOGLEVEL=low" - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Port whitelist from allowed_ports ──────────────────────────────────── -- name: "SC-7 | Open whitelisted ports" - community.general.ufw: - rule: allow - port: "{{ item | string }}" - proto: "{{ 'udp' if item | int == 41641 else 'tcp' }}" - loop: "{{ allowed_ports }}" - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Deny Caddy admin API on ALL interfaces (zero-trust) ───────────────── -# Closes the container -> host path: the Docker bridge allow below -# (172.16.0.0/12) would otherwise permit any container to reach a host-bound -# 0.0.0.0:2019 admin API (systemd/bare-metal deployments). The admin API -# accepts config POSTs, so it is denied everywhere except loopback -# (contract §6/M1). -- name: "SC-7 | Deny Caddy admin API on all interfaces (2019)" - community.general.ufw: - rule: deny - port: "2019" - proto: tcp - direction: in - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Deny Caddy Prometheus API on all interfaces (2020)" - community.general.ufw: - rule: deny - port: "2020" - proto: tcp - direction: in - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Docker bridge internal networking ──────────────────────────────────── -- name: "SC-7 | Allow Docker internal networking (bridge)" - community.general.ufw: - rule: allow - src: 172.16.0.0/12 - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Workstation exceptions ─────────────────────────────────────────────── -- name: "SC-7 | Allow DHCP client (workstation)" - community.general.ufw: - rule: allow - port: "68" - proto: udp - when: hardening_profile == 'workstation' - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Allow mDNS (workstation)" - community.general.ufw: - rule: allow - port: "5353" - proto: udp - when: hardening_profile == 'workstation' - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Metrics exporters on Tailscale ─────────────────────────────────────── -- name: "SC-7 | Allow Node Exporter on tailscale0" - community.general.ufw: - rule: allow - port: "9100" - proto: tcp - interface: tailscale0 - direction: in - when: enable_metrics_exporters | default(false) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Allow cAdvisor on tailscale0" - community.general.ufw: - rule: allow - port: "18080" - proto: tcp - interface: tailscale0 - direction: in - when: enable_metrics_exporters | default(false) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Tailscale Serve (caddy-waf-ui control plane) ───────────────────────── -- name: "SC-7 | Allow Tailscale Serve on tailscale0" - community.general.ufw: - rule: allow - port: "{{ tailscale_serve_port | default('8443') }}" - proto: tcp - interface: tailscale0 - direction: in - when: tailscale_serve_enabled | default(false) - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Caddy HTTP/HTTPS (ingress nodes) ──────────────────────────────────── -- name: "SC-7 | Allow Caddy HTTP (ingress nodes)" - community.general.ufw: - rule: allow - port: "80" - proto: tcp - when: "'ingress' in group_names or 'brain' in group_names" - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Allow Caddy HTTPS (ingress nodes)" - community.general.ufw: - rule: allow - port: "443" - proto: tcp - when: "'ingress' in group_names or 'brain' in group_names" - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Allow Caddy HTTP/3 QUIC (ingress nodes)" - community.general.ufw: - rule: allow - port: "443" - proto: udp - when: "'ingress' in group_names or 'brain' in group_names" - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Deny Caddy admin + Prometheus API on Tailscale ────────────────────── -- name: "SC-7 | Deny Caddy admin API on tailscale0 (2019)" - community.general.ufw: - rule: deny - port: "2019" - proto: tcp - interface: tailscale0 - direction: in - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Deny Caddy Prometheus API on tailscale0 (2020)" - community.general.ufw: - rule: deny - port: "2020" - proto: tcp - interface: tailscale0 - direction: in - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] - -# ─── Enable UFW ─────────────────────────────────────────────────────────── -- name: "SC-7 | Enable UFW with default deny incoming" - community.general.ufw: - state: enabled - policy: deny - direction: incoming - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Set UFW default allow outgoing" - community.general.ufw: - state: enabled - policy: allow - direction: outgoing - tags: [firewall, l2-compliance, nist, sc-7] - -- name: "SC-7 | Enable packet forwarding in UFW (Docker + VPN)" - ansible.builtin.lineinfile: - path: /etc/default/ufw - regexp: "^DEFAULT_FORWARD_POLICY=" - line: 'DEFAULT_FORWARD_POLICY="ACCEPT"' - notify: Reload UFW - tags: [firewall, l2-compliance, nist, sc-7] diff --git a/roles/L2_compliance/general/templates/tailscale/tailscale-acls.json.j2 b/roles/L2_compliance/general/templates/tailscale/tailscale-acls.json.j2 index 330354b..761fc3b 100644 --- a/roles/L2_compliance/general/templates/tailscale/tailscale-acls.json.j2 +++ b/roles/L2_compliance/general/templates/tailscale/tailscale-acls.json.j2 @@ -41,7 +41,7 @@ %} {# Tailscale Serve: operator sources reach the serve listener on muscle (caddy-waf-ui control plane). Rendered only when global ACL serve policy - is enabled; the host firewall (ufw/nftables) opens this port on tailscale0 + is enabled; the host firewall (nftables) opens this port on tailscale0 locally when tailscale_serve_enabled is true on the node. #} {% if (tailscale_acl_serve_enabled | default(tailscale_serve_enabled | default(false))) | bool %} {% set acl_serve_sources = ((tailscale_acl_serve_sources | default(acl_admin_ui_sources)) + (tailscale_acl_serve_sources_extra | default([]))) | unique %} diff --git a/roles/L2_compliance/handlers/main.yml b/roles/L2_compliance/handlers/main.yml index b7e66c7..aa70c13 100644 --- a/roles/L2_compliance/handlers/main.yml +++ b/roles/L2_compliance/handlers/main.yml @@ -8,11 +8,6 @@ # handlers context doesn't resolve for notify: directives. # ============================================================================== -- name: Reload UFW - community.general.ufw: - state: reloaded - throttle: 1 - - name: Restart fail2ban ansible.builtin.systemd: name: fail2ban diff --git a/roles/L2_compliance/nist_800_53/tasks/evidence_sc7.yml b/roles/L2_compliance/nist_800_53/tasks/evidence_sc7.yml index bad9964..4f1866f 100644 --- a/roles/L2_compliance/nist_800_53/tasks/evidence_sc7.yml +++ b/roles/L2_compliance/nist_800_53/tasks/evidence_sc7.yml @@ -19,26 +19,17 @@ mode: "0755" tags: [compliance, l2-compliance, nist, sc-7] -- name: "SC-7 | Collect firewall status (ufw)" - ansible.builtin.command: ufw status verbose - register: _sc7_ufw - changed_when: false - failed_when: false - when: os_firewall_backend == 'ufw' - tags: [compliance, l2-compliance, nist, sc-7] - - name: "SC-7 | Collect firewall status (nftables)" ansible.builtin.command: nft list ruleset register: _sc7_nft changed_when: false failed_when: false - when: os_firewall_backend == 'nftables' tags: [compliance, l2-compliance, nist, sc-7] - name: "SC-7 | Write firewall status evidence" ansible.builtin.copy: dest: /srv/evidence/nist/sc-7/firewall_status - content: "{{ _sc7_ufw.stdout if os_firewall_backend == 'ufw' else _sc7_nft.stdout }}" + content: "{{ _sc7_nft.stdout | default('nftables ruleset empty or unavailable') }}" mode: "0640" tags: [compliance, l2-compliance, nist, sc-7] diff --git a/roles/L2_compliance/tasks/main.yml b/roles/L2_compliance/tasks/main.yml index bf4fbd1..0c1147f 100644 --- a/roles/L2_compliance/tasks/main.yml +++ b/roles/L2_compliance/tasks/main.yml @@ -3,7 +3,7 @@ # Each sub-role is gated by its enabled flag from defaults/main.yml. # Tags preserved at include level for selective execution. -- name: "L2 | Include security hardening (SSH, fail2ban, UFW)" +- name: "L2 | Include security hardening (SSH, fail2ban, nftables)" ansible.builtin.include_tasks: "{{ role_path }}/general/tasks/security/main.yml" when: l2_security_enabled | default(true) tags: [l2-compliance, security]