Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions inventory/group_vars/all/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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: []
Expand Down
2 changes: 1 addition & 1 deletion inventory/group_vars/brain/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }}"
3 changes: 0 additions & 3 deletions inventory/group_vars/local/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion inventory/group_vars/muscle/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }}"
7 changes: 1 addition & 6 deletions molecule/L2_compliance/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions molecule/L2_compliance/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
104 changes: 100 additions & 4 deletions molecule/L2_compliance/verify.yml
Original file line number Diff line number Diff line change
@@ -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."
59 changes: 5 additions & 54 deletions playbooks/l2/lockdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <controller_ip> 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 <controller_ip> 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]
Expand Down
45 changes: 6 additions & 39 deletions playbooks/l2/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,22 @@
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
args:
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]

# ========================================================================
Expand Down Expand Up @@ -282,34 +267,16 @@
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"
ansible.builtin.set_fact:
_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]
Expand Down
2 changes: 1 addition & 1 deletion playbooks/ops/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading
Loading