Compare commits
54
Commits
206cd3949f
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3aaf9062d | ||
|
|
f32e6930f5 | ||
|
|
704a5d9389 | ||
|
|
a6e1657363 | ||
|
|
e5a56b61d9 | ||
|
|
08078324c9 | ||
|
|
c2a2a1ea0e | ||
|
|
27f6635cc1 | ||
|
|
e8eee3be55 | ||
|
|
37fc48275d | ||
|
|
cc9cc6d34f | ||
|
|
975beccf50 | ||
|
|
3290d843cc | ||
|
|
4abfea352c | ||
|
|
b74598d265 | ||
|
|
483f3890e5 | ||
|
|
79943e0389 | ||
|
|
252c703afd | ||
|
|
1d10f2be39 | ||
|
|
6a1e7b77e7 | ||
|
|
ce851f59c6 | ||
|
|
6334820750 | ||
|
|
f60a2c7e1f | ||
|
|
b23466f588 | ||
|
|
74a0af2d32 | ||
|
|
c30b3d1f0a | ||
|
|
e5922692f6 | ||
|
|
bacb16b7b2 | ||
|
|
c7c9f6d5d1 | ||
|
|
0267db247e | ||
|
|
add568a242 | ||
|
|
ca34bd8363 | ||
|
|
f92b36ceaa | ||
|
|
32651402c6 | ||
|
|
0abc9fb1c3 | ||
|
|
c1c71194c6 | ||
|
|
66657f3917 | ||
|
|
e1b5639d65 | ||
|
|
7a260afe39 | ||
|
|
78a370dbd1 | ||
|
|
f67edd7a07 | ||
|
|
7464f4da0a | ||
|
|
923cd85020 | ||
|
|
61db0ed55e | ||
|
|
03fc22daf7 | ||
|
|
7604bc4dac | ||
|
|
46fd6a4c40 | ||
|
|
ec64ace8f4 | ||
|
|
5318997b73 | ||
|
|
4fbc4ad317 | ||
|
|
470d856332 | ||
|
|
589f899bae | ||
|
|
c2c8934ec9 | ||
|
|
5c0783cf86 |
@@ -0,0 +1,2 @@
|
||||
# Directory the node exporter reads textfile metrics from (the Debian default).
|
||||
apt_updates_textfile_dir: /var/lib/prometheus/node-exporter
|
||||
@@ -0,0 +1,80 @@
|
||||
# The metrics run after apt-daily (update) and apt-daily-upgrade (upgrade), so they show the state apt
|
||||
# just reached without a timer of their own. The "-" lets a failing metrics run leave apt's units green.
|
||||
- name: Create the textfile directory
|
||||
ansible.builtin.file:
|
||||
path: "{{ apt_updates_textfile_dir }}"
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
- name: Install the apt metrics script
|
||||
ansible.builtin.template:
|
||||
src: apt-updates-metrics.j2
|
||||
dest: /usr/local/sbin/apt-updates-metrics
|
||||
mode: "0755"
|
||||
register: apt_updates_script
|
||||
|
||||
- name: Create the apt timers' drop-in directories
|
||||
ansible.builtin.file:
|
||||
path: /etc/systemd/system/{{ item }}.service.d
|
||||
state: directory
|
||||
mode: "0755"
|
||||
loop:
|
||||
- apt-daily
|
||||
- apt-daily-upgrade
|
||||
|
||||
- name: Run the apt metrics after the apt timers' services
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/{{ item }}.service.d/apt-updates-metrics.conf
|
||||
mode: "0644"
|
||||
content: |
|
||||
[Service]
|
||||
ExecStartPost=-/usr/local/sbin/apt-updates-metrics
|
||||
loop:
|
||||
- apt-daily
|
||||
- apt-daily-upgrade
|
||||
register: apt_updates_dropins
|
||||
|
||||
- name: Reload systemd after the apt metrics drop-ins
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
when: apt_updates_dropins.changed
|
||||
# Check mode does not write the drop-ins.
|
||||
ignore_errors: "{{ ansible_check_mode }}"
|
||||
|
||||
# Debian's prometheus-node-exporter-apt collector writes apt.prom with the same metric families as this role under
|
||||
# other HELP texts; node_exporter drops the rows of both files then. This role owns the apt metrics, so the
|
||||
# collector stays off and its file, also a stale leftover of a removed collector, goes away.
|
||||
- name: Look for the Debian apt metrics collector
|
||||
ansible.builtin.stat:
|
||||
path: /lib/systemd/system/prometheus-node-exporter-apt.timer
|
||||
register: apt_updates_debian_collector
|
||||
|
||||
- name: Stop and disable the Debian apt metrics collector timer
|
||||
ansible.builtin.systemd:
|
||||
name: prometheus-node-exporter-apt.timer
|
||||
enabled: false
|
||||
state: stopped
|
||||
when: apt_updates_debian_collector.stat.exists
|
||||
|
||||
- name: Stop the Debian apt metrics collector service
|
||||
ansible.builtin.systemd:
|
||||
name: prometheus-node-exporter-apt.service
|
||||
state: stopped
|
||||
when: apt_updates_debian_collector.stat.exists
|
||||
|
||||
- name: Remove the Debian apt metrics collector output
|
||||
ansible.builtin.file:
|
||||
path: "{{ apt_updates_textfile_dir }}/apt.prom"
|
||||
state: absent
|
||||
|
||||
# The timers' drop-ins write the metrics after the next apt run; this gives a new host and a changed script
|
||||
# current metrics at once.
|
||||
- name: Look for the apt metrics file
|
||||
ansible.builtin.stat:
|
||||
path: "{{ apt_updates_textfile_dir }}/apt_updates.prom"
|
||||
register: apt_updates_prom
|
||||
|
||||
- name: Write the apt metrics
|
||||
ansible.builtin.command: /usr/local/sbin/apt-updates-metrics
|
||||
when: apt_updates_script.changed or not apt_updates_prom.stat.exists
|
||||
changed_when: true
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/bin/bash
|
||||
# Pending upgrades, held packages, reboot flag and age of the package lists as node exporter textfile
|
||||
# metrics. apt-daily and apt-daily-upgrade run it after their update and upgrade (drop-ins).
|
||||
set -euo pipefail
|
||||
|
||||
host={{ inventory_hostname }}
|
||||
textfile_dir={{ apt_updates_textfile_dir }}
|
||||
out=$textfile_dir/apt_updates.prom
|
||||
tmp=$(mktemp "$textfile_dir/.apt_updates.XXXXXX")
|
||||
trap 'rm -f "$tmp"' EXIT
|
||||
|
||||
# Architectures a pending package can have: the native and foreign ones, plus "all" for architecture-independent
|
||||
# packages. Each gets a row per kind even at 0, so a resolved backlog reads as 0 and not as a vanished series.
|
||||
archs="$(dpkg --print-architecture) $(dpkg --print-foreign-architectures | tr '\n' ' ') all"
|
||||
|
||||
# The simulated upgrade lists "Inst <package> [<installed>] (<candidate> <origins> [<arch>])" per package. The candidate
|
||||
# is the newest version, which can come from regular updates while an older, still newer-than-installed version came
|
||||
# from the security archive. So a package counts as security when any version above the installed one in its
|
||||
# "apt-cache policy" table (newest first, installed marked with ***) has a security origin, everything else as regular.
|
||||
sim=$(apt-get -s upgrade)
|
||||
names=$(awk '/^Inst / { print $2 }' <<< "$sim")
|
||||
security=""
|
||||
if [[ -n $names ]]; then
|
||||
# shellcheck disable=SC2086
|
||||
security=$(apt-cache policy $names | awk '
|
||||
/^[^ ]/ { pkg = $1; sub(/:$/, "", pkg); newer = 1; next }
|
||||
/^ \*\*\* / { newer = 0; next }
|
||||
/^ [0-9]+ / { if (newer && tolower($0) ~ /security/) print pkg }
|
||||
')
|
||||
fi
|
||||
|
||||
pending=$(SECURITY="$security" awk -v host="$host" -v archs="$archs" '
|
||||
BEGIN {
|
||||
n = split(ENVIRON["SECURITY"], s, "\n")
|
||||
for (i = 1; i <= n; i++) sec[s[i]] = 1
|
||||
}
|
||||
/^Inst / {
|
||||
line = $0
|
||||
sub(/^[^(]*\(/, "", line)
|
||||
sub(/\).*$/, "", line)
|
||||
n = split(line, f, " ")
|
||||
pkg_arch = f[n]
|
||||
gsub(/\[|\]/, "", pkg_arch)
|
||||
kind = ($2 in sec) ? "security" : "regular"
|
||||
count[kind, pkg_arch]++
|
||||
}
|
||||
END {
|
||||
split(archs, a, " ")
|
||||
for (i in a) {
|
||||
if (!(("security", a[i]) in count)) count["security", a[i]] = 0
|
||||
if (!(("regular", a[i]) in count)) count["regular", a[i]] = 0
|
||||
}
|
||||
for (key in count) {
|
||||
split(key, k, SUBSEP)
|
||||
printf "apt_upgrades_pending{host=\"%s\",origin=\"%s\",arch=\"%s\"} %d\n", host, k[1], k[2], count[key]
|
||||
}
|
||||
}' <<< "$sim")
|
||||
|
||||
held=$(apt-mark showhold | wc -l)
|
||||
|
||||
reboot=0
|
||||
if [[ -e /run/reboot-required ]]; then
|
||||
reboot=1
|
||||
fi
|
||||
|
||||
# Debian writes update-stamp, Ubuntu update-success-stamp; both are touched by a successful update. Both are missing
|
||||
# until apt.systemd.daily has run an update, then the package cache stands in.
|
||||
stamp=/var/lib/apt/periodic/update-success-stamp
|
||||
if [[ ! -e $stamp ]]; then
|
||||
stamp=/var/lib/apt/periodic/update-stamp
|
||||
fi
|
||||
if [[ ! -e $stamp ]]; then
|
||||
stamp=/var/cache/apt/pkgcache.bin
|
||||
fi
|
||||
cache=$(stat -c %Y "$stamp")
|
||||
|
||||
{
|
||||
echo '# TYPE apt_upgrades_pending gauge'
|
||||
echo "$pending"
|
||||
echo '# TYPE apt_upgrades_held gauge'
|
||||
echo "apt_upgrades_held{host=\"$host\"} $held"
|
||||
echo '# TYPE node_reboot_required gauge'
|
||||
echo "node_reboot_required{host=\"$host\"} $reboot"
|
||||
echo '# TYPE apt_package_cache_timestamp_seconds gauge'
|
||||
echo "apt_package_cache_timestamp_seconds{host=\"$host\"} $cache"
|
||||
} > "$tmp"
|
||||
|
||||
chmod 0644 "$tmp"
|
||||
mv -f "$tmp" "$out"
|
||||
@@ -8,6 +8,8 @@ ipv6_accept_ra_interfaces: []
|
||||
disk_maintenance:
|
||||
# systemd OnCalendar for fstrim.timer; unset keeps the distribution schedule.
|
||||
fstrim_on_calendar:
|
||||
# Mountpoints fstrim.service trims; unset trims every fstab filesystem.
|
||||
fstrim_mountpoints:
|
||||
# Cron "minute hour" of the monthly ZFS trim and scrub from zfsutils-linux; unset keeps 00:24.
|
||||
zfs_cron_time:
|
||||
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# Only the files of this role that exist: in a container the distribution files hold keys that
|
||||
# are not writable there, and sysctl --system fails on them; -e skips keys a container lacks.
|
||||
- name: reload_sysctl
|
||||
command: sysctl --system
|
||||
shell: >-
|
||||
cd /etc/sysctl.d && sysctl -e -p $(ls
|
||||
ip-forwarding.conf
|
||||
ipv6-slaac-address-generation.conf
|
||||
ipv6-slaac-override.conf
|
||||
tcp-bbr.conf 2>/dev/null)
|
||||
|
||||
- name: restart_journald
|
||||
systemd:
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# With manage_etc_hosts in the user-data (Proxmox sets it) cloud-init rewrites /etc/hosts from its
|
||||
# template on every boot and drops the blocks other roles add; user-data outranks cloud.cfg.d, so
|
||||
# the module is taken out of the boot stage instead.
|
||||
- name: Read the cloud-init configuration
|
||||
slurp:
|
||||
path: /etc/cloud/cloud.cfg
|
||||
register: cloud_cfg
|
||||
failed_when: false
|
||||
|
||||
- name: Keep cloud-init from rewriting /etc/hosts
|
||||
when: cloud_cfg.content is defined
|
||||
copy:
|
||||
dest: /etc/cloud/cloud.cfg.d/99-ansible-etc-hosts.cfg
|
||||
content: |
|
||||
cloud_init_modules: {{ (cloud_cfg.content | b64decode | from_yaml).cloud_init_modules | reject('equalto', 'update_etc_hosts') | list | to_json }}
|
||||
@@ -25,6 +25,36 @@
|
||||
daemon_reload: yes
|
||||
state: restarted
|
||||
|
||||
- name: Create the fstrim service drop-in directory
|
||||
when: disk_maintenance.fstrim_mountpoints | default(none) is not none
|
||||
file:
|
||||
path: /etc/systemd/system/fstrim.service.d
|
||||
state: directory
|
||||
|
||||
- name: Set the fstrim mountpoints
|
||||
when: disk_maintenance.fstrim_mountpoints | default(none) is not none
|
||||
copy:
|
||||
dest: /etc/systemd/system/fstrim.service.d/mountpoints.conf
|
||||
content: |
|
||||
[Service]
|
||||
ExecStart=
|
||||
{% for m in disk_maintenance.fstrim_mountpoints %}
|
||||
ExecStart=/sbin/fstrim --verbose --quiet-unsupported {{ m }}
|
||||
{% endfor %}
|
||||
register: fstrim_mountpoints
|
||||
|
||||
- name: Trim every fstab filesystem
|
||||
when: disk_maintenance.fstrim_mountpoints | default(none) is none
|
||||
file:
|
||||
path: /etc/systemd/system/fstrim.service.d/mountpoints.conf
|
||||
state: absent
|
||||
register: fstrim_mountpoints_removed
|
||||
|
||||
- name: Reload systemd for the fstrim mountpoints
|
||||
when: fstrim_mountpoints.changed or fstrim_mountpoints_removed.changed
|
||||
systemd:
|
||||
daemon_reload: yes
|
||||
|
||||
- name: Schedule the monthly ZFS trim and scrub
|
||||
when: disk_maintenance.zfs_cron_time | default(none) is not none
|
||||
lineinfile:
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
- import_tasks: ./timesync.yml
|
||||
|
||||
- import_tasks: ./cloud_init.yml
|
||||
|
||||
- import_tasks: ./aliases.yml
|
||||
|
||||
- import_tasks: ./networking.yml
|
||||
|
||||
@@ -23,19 +23,16 @@
|
||||
|
||||
- name: Generate random IPv6 stable-secret
|
||||
set_fact:
|
||||
ipv6_stable_secret: "{{ 'fd' + (lookup('password', '/dev/null length=30 chars=hex')).split('.')[0][:7] + '::' + (lookup('password', '/dev/null length=4 chars=hex')) }}"
|
||||
ipv6_stable_secret: "{{ lookup('password', '/dev/null length=32 chars=hexdigits') | lower | regex_replace('(.{4})(?!$)', '\\1:') }}"
|
||||
|
||||
# The secret goes first: addr_gen_mode 2 is rejected while the interface has no secret.
|
||||
- name: Set sysctl settings for ipv6-address-generation
|
||||
copy:
|
||||
dest: "/etc/sysctl.d/ipv6-slaac-address-generation.conf"
|
||||
content: |
|
||||
{% if ansible_facts.default_ipv6.interface is defined %}
|
||||
net.ipv6.conf.{{ ansible_facts.default_ipv6.interface }}.addr_gen_mode = 2
|
||||
net.ipv6.conf.{{ ansible_facts.default_ipv6.interface }}.stable_secret = {{ ipv6_stable_secret }}
|
||||
{% else %}
|
||||
net.ipv6.conf.{{ ansible_facts.default_ipv4.interface }}.addr_gen_mode = 2
|
||||
net.ipv6.conf.{{ ansible_facts.default_ipv4.interface }}.stable_secret = {{ ipv6_stable_secret }}
|
||||
{% endif %}
|
||||
{% set iface = ansible_facts.default_ipv6.interface | default(ansible_facts.default_ipv4.interface) %}
|
||||
net.ipv6.conf.{{ iface }}.stable_secret = {{ ipv6_stable_secret }}
|
||||
net.ipv6.conf.{{ iface }}.addr_gen_mode = 2
|
||||
notify: reload_sysctl
|
||||
when:
|
||||
- ansible_facts.default_ipv6 is defined
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
- name: Check if SSH daemon is active
|
||||
systemd:
|
||||
name: sshd
|
||||
name: ssh
|
||||
state: started
|
||||
register: sshd_status
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
- name: Restart SSH daemon
|
||||
service:
|
||||
name: sshd
|
||||
name: ssh
|
||||
state: restarted
|
||||
when: sshd_config.changed
|
||||
when: sshd_status.status.ActiveState == 'active'
|
||||
|
||||
+38
-15
@@ -19,6 +19,14 @@
|
||||
dest: /etc/apt/keyrings/crowdsec.asc
|
||||
mode: "0644"
|
||||
|
||||
# A deb822 crowdsec.sources (written by the package or by hand) already
|
||||
# defines the repository; a second crowdsec.list makes apt warn about
|
||||
# duplicate targets.
|
||||
- name: Check for an existing deb822 CrowdSec apt source
|
||||
ansible.builtin.stat:
|
||||
path: /etc/apt/sources.list.d/crowdsec.sources
|
||||
register: crowdsec_sources
|
||||
|
||||
- name: Add the CrowdSec apt repository
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/apt/sources.list.d/crowdsec.list
|
||||
@@ -28,6 +36,7 @@
|
||||
https://packagecloud.io/crowdsec/crowdsec/{{ ansible_distribution | lower }}/
|
||||
{{ crowdsec.apt_suite_map[ansible_distribution_release] | default(ansible_distribution_release) }} main
|
||||
register: crowdsec_repo
|
||||
when: not crowdsec_sources.stat.exists
|
||||
|
||||
# The postinst asks these; without them it registers a local machine for a
|
||||
# local LAPI and enrols in the central CAPI.
|
||||
@@ -53,6 +62,7 @@
|
||||
- /etc/crowdsec/bouncers
|
||||
- /etc/crowdsec/parsers/s02-enrich
|
||||
- /etc/systemd/system/crowdsec.service.d
|
||||
- /etc/systemd/system/crowdsec-hubupdate.service.d
|
||||
- /etc/systemd/system/crowdsec-firewall-bouncer.service.d
|
||||
- /etc/systemd/system/nftables.service.d
|
||||
|
||||
@@ -171,22 +181,35 @@
|
||||
when: crowdsec_missing | length > 0
|
||||
notify: Restart crowdsec
|
||||
|
||||
# Read again: the collections installed above bring their scenarios along.
|
||||
- name: List the installed hub scenarios
|
||||
ansible.builtin.command: cscli hub list -o json
|
||||
register: crowdsec_scenarios_json
|
||||
changed_when: false
|
||||
failed_when: crowdsec_scenarios_json.rc != 0 and not ansible_check_mode
|
||||
check_mode: false
|
||||
# The script upgrades every hub item with --force (a scenario removed with --force taints its collection, and a plain upgrade skips
|
||||
# tainted collections) and removes the disabled scenarios again. It runs here once and daily as crowdsec-hubupdate.service.
|
||||
- name: Install the hub update script
|
||||
ansible.builtin.template:
|
||||
src: crowdsec-hubupdate.sh.j2
|
||||
dest: /usr/local/sbin/crowdsec-hubupdate
|
||||
mode: "0755"
|
||||
|
||||
# --force also removes items that belong to an installed collection. A plain "cscli hub upgrade" keeps them removed;
|
||||
# "cscli hub upgrade --force" would bring them back.
|
||||
- name: Remove the hub scenarios replaced by local ones
|
||||
ansible.builtin.command: "cscli scenarios remove {{ item.value }} --force"
|
||||
loop: "{{ crowdsec.disabled_scenarios | dict2items | rejectattr('value', 'none') }}"
|
||||
loop_control:
|
||||
label: "{{ item.value }}"
|
||||
when: item.value in ((crowdsec_scenarios_json.stdout | default('{}', true) | from_json).scenarios | default([], true) | map(attribute='name'))
|
||||
- name: Run the hub update script instead of the package one
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/crowdsec-hubupdate.service.d/override.conf
|
||||
mode: "0644"
|
||||
content: |
|
||||
[Service]
|
||||
ExecStart=
|
||||
ExecStart=/usr/local/sbin/crowdsec-hubupdate
|
||||
register: crowdsec_hubupdate_dropin
|
||||
|
||||
- name: Reload systemd for the hub update unit
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
when: crowdsec_hubupdate_dropin is changed
|
||||
|
||||
- name: Upgrade the hub items and remove the disabled scenarios
|
||||
ansible.builtin.command: /usr/local/sbin/crowdsec-hubupdate
|
||||
environment:
|
||||
NO_RELOAD: "1"
|
||||
register: crowdsec_hubupdate
|
||||
changed_when: "'changed' in crowdsec_hubupdate.stdout"
|
||||
notify: Restart crowdsec
|
||||
|
||||
- name: Install the local parsers and scenarios
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/bin/sh
|
||||
# Managed by Ansible. crowdsec-hubupdate.service runs this script instead of /usr/libexec/crowdsec/hubupdate.sh.
|
||||
#
|
||||
# "cscli scenarios remove --force" taints the collection that holds the scenario, and a plain "cscli hub upgrade" skips tainted
|
||||
# collections for good. The upgrade therefore runs with --force, which also brings the removed scenarios back, and the removal is
|
||||
# applied again. crowdsec is reloaded only when the installed hub files or the data files differ afterwards; with NO_RELOAD=1
|
||||
# the script prints "changed" instead, for the role, which restarts crowdsec once at the end of its run. A failed step does not skip
|
||||
# the later ones, so the removal of the disabled scenarios always runs; the script then exits non-zero.
|
||||
set -eu
|
||||
|
||||
test -x /usr/bin/cscli || exit 0
|
||||
|
||||
fingerprint() {
|
||||
{
|
||||
find -L /etc/crowdsec -path /etc/crowdsec/hub -prune -o -type f -exec sha256sum {} +
|
||||
find /var/lib/crowdsec/data -maxdepth 1 -type f ! -name 'crowdsec.db*' ! -name trace -exec sha256sum {} +
|
||||
} 2>/dev/null | sort -k2 | sha256sum
|
||||
}
|
||||
|
||||
before="$(fingerprint)"
|
||||
|
||||
failed=0
|
||||
|
||||
# A failed list command does not skip the removal: the scenario is then removed without knowing whether it is installed.
|
||||
remove_scenario() {
|
||||
if list="$(/usr/bin/cscli scenarios list -o raw)"; then
|
||||
printf '%s\n' "$list" | cut -d, -f1 | grep -qxF "$1" || return 0
|
||||
else
|
||||
failed=1
|
||||
fi
|
||||
/usr/bin/cscli --error scenarios remove "$1" --force >/dev/null || failed=1
|
||||
}
|
||||
|
||||
/usr/bin/cscli --error hub update >/dev/null || failed=1
|
||||
/usr/bin/cscli --error hub upgrade --force >/dev/null || failed=1
|
||||
|
||||
{% for scenario in crowdsec.disabled_scenarios.values() | reject('none') | unique | sort %}
|
||||
remove_scenario '{{ scenario }}'
|
||||
{% endfor %}
|
||||
|
||||
if [ "$before" != "$(fingerprint)" ]; then
|
||||
if [ -n "${NO_RELOAD:-}" ]; then
|
||||
echo changed
|
||||
else
|
||||
systemctl reload crowdsec.service
|
||||
fi
|
||||
fi
|
||||
|
||||
exit "$failed"
|
||||
@@ -20,6 +20,8 @@ STATE_DIR = os.environ.get("STATE_DIRECTORY", "/var/lib/crowdsec-range-bouncer")
|
||||
HASH_FILE = os.path.join(STATE_DIR, "last.sha256")
|
||||
NFT = "/usr/sbin/nft"
|
||||
TABLE = "crowdsec_ranges"
|
||||
# A prefix this wide is a poisoned decision, not a bad network.
|
||||
MIN_DECISION_PREFIXLEN = {4: 8, 6: 16}
|
||||
|
||||
|
||||
def fetch_ranges(lapi_url, api_key):
|
||||
@@ -35,11 +37,16 @@ def fetch_ranges(lapi_url, api_key):
|
||||
return []
|
||||
if not isinstance(decisions, list):
|
||||
raise ValueError("unexpected LAPI answer: %s" % type(decisions).__name__)
|
||||
return [
|
||||
ipaddress.ip_network(d["value"], strict=False)
|
||||
for d in decisions
|
||||
if d["type"] == "ban" and d["scope"].lower() == "range"
|
||||
]
|
||||
networks = []
|
||||
for d in decisions:
|
||||
if d["type"] != "ban" or d["scope"].lower() != "range":
|
||||
continue
|
||||
network = ipaddress.ip_network(d["value"], strict=False)
|
||||
if network.prefixlen < MIN_DECISION_PREFIXLEN[network.version]:
|
||||
print("skipping suspiciously wide range decision %s" % network)
|
||||
continue
|
||||
networks.append(network)
|
||||
return networks
|
||||
|
||||
|
||||
def collapse(networks, version):
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
docker:
|
||||
# Merged into /etc/docker/daemon.json; keys not listed here keep their host value.
|
||||
daemon:
|
||||
# Containers keep running while dockerd restarts or upgrades.
|
||||
live-restore: true
|
||||
@@ -2,3 +2,9 @@
|
||||
service:
|
||||
name: "docker"
|
||||
state: restarted
|
||||
|
||||
# SIGHUP applies the reloadable daemon.json options without stopping containers.
|
||||
- name: reload_docker
|
||||
service:
|
||||
name: "docker"
|
||||
state: reloaded
|
||||
|
||||
+24
-1
@@ -59,11 +59,34 @@
|
||||
- pass
|
||||
state: latest
|
||||
|
||||
- name: Read daemon.json
|
||||
ansible.builtin.slurp:
|
||||
path: /etc/docker/daemon.json
|
||||
register: docker_daemon_json
|
||||
failed_when: false
|
||||
tags: docker_daemon
|
||||
|
||||
- name: Merge daemon options into daemon.json
|
||||
vars:
|
||||
docker_daemon_current: "{{ (docker_daemon_json.content | b64decode | from_json) if docker_daemon_json.content is defined else {} }}"
|
||||
docker_daemon_merged: "{{ docker_daemon_current | combine(docker.daemon) }}"
|
||||
ansible.builtin.copy:
|
||||
content: "{{ docker_daemon_merged | to_nice_json(indent=4, sort_keys=False) }}\n"
|
||||
dest: /etc/docker/daemon.json
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
validate: dockerd --validate --config-file=%s
|
||||
# Rewritten only on a value change, so hand-written formatting survives otherwise.
|
||||
when: docker_daemon_merged != docker_daemon_current
|
||||
notify: reload_docker
|
||||
tags: docker_daemon
|
||||
|
||||
- name: Deploy docker-compose command to new docker compose plugin
|
||||
ansible.builtin.copy:
|
||||
src: docker-compose
|
||||
dest: "/usr/local/bin/docker-compose"
|
||||
mode: preserve
|
||||
mode: "0755"
|
||||
|
||||
- name: Check for docker binary
|
||||
ansible.builtin.stat:
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
---
|
||||
# Address the node exporter binds to; empty = all addresses. For hosts without host_firewall: the NetBird
|
||||
# address keeps the port off the LAN, and the mesh ACL decides who reaches it.
|
||||
host_exporters_listen_address: ""
|
||||
|
||||
# Adds prometheus-smartctl-exporter (trixie-backports) next to the node exporter
|
||||
# on hosts with physical disks.
|
||||
host_exporters_smart: false
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
---
|
||||
# Both exporters listen on all addresses; host_firewall limits who reaches them.
|
||||
# Both exporters listen on all addresses (the node exporter on host_exporters_listen_address if set); host_firewall
|
||||
# limits who reaches them.
|
||||
# Its recommends (smartmontools, nvme-cli, ipmitool) start units that fail on hosts without such hardware.
|
||||
- name: Install the node exporter
|
||||
ansible.builtin.apt:
|
||||
@@ -7,11 +8,53 @@
|
||||
install_recommends: false
|
||||
state: present
|
||||
|
||||
# Left over from installs with recommends; its init script fails at boot without a BMC.
|
||||
# Purge also removes the init script a plain removal keeps.
|
||||
- name: Purge OpenIPMI on hosts without a BMC
|
||||
ansible.builtin.apt:
|
||||
name: openipmi
|
||||
state: absent
|
||||
purge: true
|
||||
when: not host_exporters_ipmi
|
||||
|
||||
- name: Bind the node exporter to one address
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/default/prometheus-node-exporter
|
||||
regexp: '^ARGS='
|
||||
line: 'ARGS="--web.listen-address={{ host_exporters_listen_address }}:9100"'
|
||||
when: host_exporters_listen_address | length > 0
|
||||
register: host_exporters_listen
|
||||
# Check mode does not install the package.
|
||||
ignore_errors: "{{ ansible_check_mode }}"
|
||||
|
||||
- name: Create the node exporter unit drop-in directory
|
||||
ansible.builtin.file:
|
||||
path: /etc/systemd/system/prometheus-node-exporter.service.d
|
||||
state: directory
|
||||
mode: "0755"
|
||||
when: host_exporters_listen_address | length > 0
|
||||
|
||||
# The NetBird address exists only after the daemon is up, and a failed bind must not exhaust the start limit.
|
||||
- name: Start the bound node exporter after NetBird and retry
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/prometheus-node-exporter.service.d/listen-address.conf
|
||||
mode: "0644"
|
||||
content: |
|
||||
[Unit]
|
||||
After=netbird.service
|
||||
StartLimitIntervalSec=0
|
||||
|
||||
[Service]
|
||||
RestartSec=5s
|
||||
when: host_exporters_listen_address | length > 0
|
||||
register: host_exporters_listen_unit
|
||||
|
||||
- name: Enable the node exporter
|
||||
ansible.builtin.systemd:
|
||||
name: prometheus-node-exporter
|
||||
enabled: true
|
||||
state: started
|
||||
state: "{{ 'restarted' if (host_exporters_listen.changed or host_exporters_listen_unit.changed) else 'started' }}"
|
||||
daemon_reload: "{{ host_exporters_listen_unit.changed }}"
|
||||
# Check mode does not install the package.
|
||||
ignore_errors: "{{ ansible_check_mode }}"
|
||||
|
||||
|
||||
@@ -34,6 +34,16 @@ firewall:
|
||||
ifaces: {}
|
||||
v4: {}
|
||||
v6: {}
|
||||
# Hosts outside the ring that reach a NodePort service (CrowdSec LAPI) over wt0, and the port of its pods: Cilium
|
||||
# translates the NodePort before netfilter, so the flows arrive as client -> pod:port.
|
||||
lapi:
|
||||
port: null
|
||||
clients: {}
|
||||
# Hosts outside the ring that upload their journal through Traefik over wt0, and the Traefik container port; as
|
||||
# with lapi, the flows arrive as client -> pod:port.
|
||||
journal:
|
||||
port: null
|
||||
clients: {}
|
||||
# IPv4 port forwards: name -> {daddr, to, tcp: ports, udp: ports}, ports as in
|
||||
# the maps above. Forwarded traffic passes the forward hook, not input.
|
||||
dnat: {}
|
||||
@@ -46,8 +56,13 @@ firewall:
|
||||
forward_guard: {}
|
||||
# Limits on new inbound TCP connections per service, applied to the public and geo ports the host serves (input) and to
|
||||
# DNATed traffic (forward: port forwards and Docker-published ports), after the admin, cluster, VPN, guest and internal
|
||||
# sources and the public_ips of every inventory host (IPv4 per address, IPv6 per /64) are exempted. Each service has a tier
|
||||
# per source (IPv4 /32, IPv6 /64) and a tier per range (IPv4 /24, IPv6 /48) at rate * range_factor. The excess drops with the log prefix fw-ratelimit: (log.ratelimit_rate).
|
||||
# sources and the public_ips of every inventory host (IPv4 per address, IPv6 per /64) are exempted. The rate and burst
|
||||
# below are the service limit, enforced by the handshake meter per source: it counts the packet that completes a TCP
|
||||
# handshake, which a spoofed source cannot send, and logs the excess with fw-rl-hs: and resets the connection. Each service
|
||||
# also has a SYN meter at twice the rate and burst, as a flood cap above the service limit so that a real client reaches the
|
||||
# handshake meter first: a tier per source (IPv4 /32, IPv6 /64) and a tier per range (IPv4 /24, IPv6 /48) at
|
||||
# rate * range_factor. Its excess drops with the log prefix fw-rl-syn-addr: (source tier) or fw-rl-syn-range: (range tier);
|
||||
# a spoofed source can trigger both, so they are evidence only. Log limit: log.ratelimit_rate.
|
||||
# name -> {ports: port or list, rate: connections per `per`, per: second|minute (minute), burst: connections (2 * rate),
|
||||
# range_factor_v4, range_factor_v6: override the factors below}; null drops a service, a service is idle unless a port is served.
|
||||
# Values are 3x the highest legitimate rate seen, with a floor of 12 per minute (3x four connections per minute) where
|
||||
@@ -67,27 +82,29 @@ firewall:
|
||||
web:
|
||||
ports: [80, 443]
|
||||
rate: 180
|
||||
# Kernel log of dropped inbound packets (fw-in-drop: ), rate-limited connections (fw-ratelimit: ) and of rejected
|
||||
# Kernel log of dropped inbound packets (fw-in-drop: ), rate-limited connections (fw-rl-syn-addr: , fw-rl-syn-range: , fw-rl-hs: ) and of rejected
|
||||
# outbound mail (fw-out-reject: ), read from the journal by CrowdSec. Each
|
||||
# rate is one limit shared by all matching packets.
|
||||
log:
|
||||
in_rate: "5/second burst 30 packets"
|
||||
# Shared by all rate-limited connections (fw-ratelimit: ), one limit.
|
||||
# One limit each for fw-rl-syn-addr: , fw-rl-syn-range: and fw-rl-hs: , shared by all services.
|
||||
ratelimit_rate: "5/second burst 30 packets"
|
||||
out_rate: "6/minute burst 5 packets"
|
||||
# Lets the kernel log the nft log statements of container network
|
||||
# namespaces (LXC guests) into the host kernel log.
|
||||
all_netns: false
|
||||
# Shared by the Spamhaus (fw-spamhaus-in: , fw-spamhaus-out: ), FireHOL (fw-firehol-in: , fw-firehol-out: ) and scanner (fw-scanners-in: ) chains, one limit each.
|
||||
# Shared by the Spamhaus (fw-spamhaus-in: , fw-spamhaus-out: ), FireHOL level1 (fw-firehol1-in: , fw-firehol1-out: ),
|
||||
# FireHOL level2 (fw-firehol2-in: , fw-firehol2-out: ) and scanner (fw-scanners-in: ) chains, one limit each.
|
||||
spamhaus_rate: "6/minute burst 10 packets"
|
||||
# Spamhaus DROP, DROPv6 and ASN-DROP: traffic from the listed networks drops
|
||||
# (input, forward), traffic to them is rejected (output, forward). The sets
|
||||
# are refreshed daily by nft-host-spamhaus.
|
||||
spamhaus: true
|
||||
# FireHOL level1 (IPv4): treated like the Spamhaus lists, with the prefixes
|
||||
# fw-firehol-in: and fw-firehol-out: and the same log limit. Entries in
|
||||
# private or reserved space and ones holding an address of the host are left
|
||||
# out. Refreshed daily by nft-host-firehol.
|
||||
# FireHOL lists (IPv4 only, FireHOL has no IPv6 lists), from github.com/firehol/blocklist-ipsets:
|
||||
# level1 and level2 (level2 includes GreenSnow): treated like the Spamhaus lists, with the prefixes fw-firehol1-in: ,
|
||||
# fw-firehol1-out: (level1) and fw-firehol2-in: , fw-firehol2-out: (level2) and the same log limit.
|
||||
# Entries in private or reserved space and ones holding an address of the host are left out. Both sets are refreshed
|
||||
# daily by nft-host-firehol.
|
||||
firehol: true
|
||||
# Internet-wide scanners (Censys, Shodan, ONYPHE, Shadowserver, ...), IPv4
|
||||
# and IPv6: the cidr files of github.com/OpenFilters/internet-scanners
|
||||
@@ -99,6 +116,28 @@ firewall:
|
||||
# of any host, and ones holding an address of the host are left out
|
||||
# (/etc/nft-host-scanners.allowlist). Refreshed daily by nft-host-scanners.
|
||||
scanners: true
|
||||
# Uplink interfaces (name -> interface). New forwarded connections that arrive on one are dropped unless they were
|
||||
# DNATed here or a forward_guard zone admits the destination. The datacenter gateway routes packets for foreign
|
||||
# addresses of the subnet to the host; with ip_forward they would be forwarded and masqueraded with the host address.
|
||||
# Not for hosts whose forwarded inbound traffic is not DNATed in netfilter (Cilium NodePorts are DNATed in BPF).
|
||||
uplinks: {}
|
||||
# Guest bridges (name -> interface) that IPv6 traffic from an uplink may be routed out of; IPv6 has no DNAT. New forwarded
|
||||
# IPv6 connections that arrive on an uplink pass the uplink drop only when they leave through one of them, so the host
|
||||
# never routes foreign destinations back out of the uplink or into the mesh and other interfaces. The kernel routes the
|
||||
# prefix of the guest bridge, so no address is listed. The guest firewalls decide about the ports. IPv4 stays DNAT only.
|
||||
routed_v6: {}
|
||||
# Interfaces (name -> interface) whose forwarded traffic must not leave through the interface it arrived on, added to the
|
||||
# interfaces of the default routes (IPv4 and IPv6) that every host gets. New connections that would be reflected drop;
|
||||
# forwarding hosts (Kubernetes with Cilium, Docker, routing peers) otherwise route packets for foreign destinations that
|
||||
# the datacenter sends to them back out. An uplink (see above) is already covered and left out.
|
||||
no_reflect: {}
|
||||
# Forward chain default. drop: forwarded traffic that no rule admits (replies, DNATed connections, trusted interfaces:
|
||||
# wt0, cluster, internal and guest ifaces, routed_v6 bridges, forward_ifaces, pod interfaces) is logged with fw-fwd-drop:
|
||||
# and dropped. log: the same, but nothing is dropped. accept: no tail, forwarded traffic passes unless a rule above drops it.
|
||||
forward_policy: drop
|
||||
# Interfaces (name -> interface, wildcards allowed) whose forwarded traffic is accepted without trusting their input,
|
||||
# e.g. a transit bridge to a guest that only needs routing.
|
||||
forward_ifaces: {}
|
||||
# Reject outbound TCP 25; the mail group turns it off.
|
||||
block_smtp_out: true
|
||||
# EU-27 plus CH, NO, GB, IS, LI (ipdeny country codes). Empty leaves the geo
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#!/usr/bin/python3
|
||||
# Managed by Ansible (role host_firewall), do not edit.
|
||||
"""Rebuilds the FireHOL level1 set firehol_v4 of table inet host.
|
||||
"""Rebuilds the FireHOL sets of table inet host (IPv4 only, FireHOL has no IPv6 lists).
|
||||
|
||||
A failed download or sanity check exits non-zero before anything changes, so
|
||||
the previous set stays in place.
|
||||
Every set is built from one or more lists of the FireHOL blocklist-ipsets
|
||||
(single addresses or CIDR networks, comment lines start with #). All sets go
|
||||
into one file, so one reload replaces them together. A failed download or
|
||||
sanity check of any list exits non-zero before anything changes, so the
|
||||
previous sets stay in place.
|
||||
"""
|
||||
import ipaddress
|
||||
import json
|
||||
@@ -16,11 +19,16 @@ import time
|
||||
import urllib.request
|
||||
|
||||
PERSISTED = "/etc/nftables.d/host-firehol.nft"
|
||||
URL = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset"
|
||||
BASE_URL = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/"
|
||||
USER_AGENT = "ruekov-host-firewall (nft-host-firehol)"
|
||||
|
||||
# Lower bound that catches an empty or truncated answer.
|
||||
MIN_ENTRIES = 1000
|
||||
# set name -> (lists merged into it, lower bound for their entries together that
|
||||
# catches an empty or truncated answer; the lists hold about 4700 (level1) and
|
||||
# 18700 (level2)).
|
||||
SETS = {
|
||||
"firehol_v4": (["firehol_level1.netset"], 1000),
|
||||
"firehol_level2_v4": (["firehol_level2.netset"], 5000),
|
||||
}
|
||||
# A prefix this wide is a parse error, not a bad network.
|
||||
MIN_PREFIXLEN = 8
|
||||
# Private, CGNAT (NetBird), loopback, link-local and multicast/reserved space:
|
||||
@@ -36,17 +44,18 @@ def fail(message):
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def fetch():
|
||||
def fetch(name):
|
||||
url = BASE_URL + name
|
||||
error = None
|
||||
for attempt in range(3):
|
||||
try:
|
||||
request = urllib.request.Request(URL, headers={"User-Agent": USER_AGENT})
|
||||
request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
|
||||
with urllib.request.urlopen(request, timeout=120) as response:
|
||||
return response.read().decode("utf-8")
|
||||
except OSError as e:
|
||||
error = e
|
||||
time.sleep(5 * (attempt + 1))
|
||||
fail(f"download of {URL} failed: {error}")
|
||||
fail(f"download of {url} failed: {error}")
|
||||
|
||||
|
||||
def own_addresses():
|
||||
@@ -57,20 +66,20 @@ def own_addresses():
|
||||
for a in link.get("addr_info", []) if a.get("family") == "inet"]
|
||||
|
||||
|
||||
def main():
|
||||
def build_set(lists, minimum, own):
|
||||
networks = []
|
||||
for line in fetch().splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
try:
|
||||
networks.append(ipaddress.IPv4Network(line))
|
||||
except ValueError:
|
||||
fail(f"unparsable line: {line!r}")
|
||||
if len(networks) < MIN_ENTRIES:
|
||||
fail(f"only {len(networks)} entries")
|
||||
for name in lists:
|
||||
for line in fetch(name).splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
try:
|
||||
networks.append(ipaddress.IPv4Network(line))
|
||||
except ValueError:
|
||||
fail(f"unparsable line in {name}: {line!r}")
|
||||
if len(networks) < minimum:
|
||||
fail(f"only {len(networks)} entries in {', '.join(lists)}")
|
||||
|
||||
own = own_addresses()
|
||||
kept, excluded, own_skipped = [], 0, 0
|
||||
for network in networks:
|
||||
if any(network.overlaps(e) for e in EXCLUDED):
|
||||
@@ -82,21 +91,28 @@ def main():
|
||||
fail(f"suspiciously wide prefix {network}")
|
||||
kept.append(network)
|
||||
merged = list(ipaddress.collapse_addresses(kept))
|
||||
print(f"firehol_v4: {len(networks)} entries, {excluded} in private or reserved space and "
|
||||
f"{own_skipped} holding an own address skipped -> {len(merged)} merged")
|
||||
return networks, excluded, own_skipped, merged
|
||||
|
||||
elements = ",\n\t\t".join(str(n) for n in merged) + ","
|
||||
|
||||
def main():
|
||||
own = own_addresses()
|
||||
definitions = []
|
||||
for set_name, (lists, minimum) in SETS.items():
|
||||
networks, excluded, own_skipped, merged = build_set(lists, minimum, own)
|
||||
print(f"{set_name}: {len(networks)} entries, {excluded} in private or reserved space and "
|
||||
f"{own_skipped} holding an own address skipped -> {len(merged)} merged")
|
||||
elements = ",\n\t\t".join(str(n) for n in merged) + ","
|
||||
definitions.append(f"\tset {set_name} {{ type ipv4_addr; flags interval; auto-merge; elements = {{\n"
|
||||
f"\t\t{elements}\n\t}} }}\n")
|
||||
persisted = ("# Generated by nft-host-firehol, do not edit.\n"
|
||||
"table inet host {\n"
|
||||
"\tset firehol_v4 { type ipv4_addr; flags interval; auto-merge; elements = {\n"
|
||||
f"\t\t{elements}\n\t}} }}\n}}\n")
|
||||
"table inet host {\n" + "".join(definitions) + "}\n")
|
||||
|
||||
# Checked under another table name: a dry run against the live table
|
||||
# name changes the live sets for its duration and unlisted addresses match.
|
||||
check_rules = persisted.replace("table inet host {", "table inet host_firehol_check {")
|
||||
checked = subprocess.run(["nft", "-c", "-f", "-"], input=check_rules, capture_output=True, text=True)
|
||||
if checked.returncode != 0:
|
||||
fail(f"nft rejects the set: {checked.stderr.strip()}")
|
||||
fail(f"nft rejects the sets: {checked.stderr.strip()}")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
previous = None
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[Unit]
|
||||
Description=Refresh the FireHOL set of the host firewall
|
||||
Description=Refresh the FireHOL sets of the host firewall
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[Unit]
|
||||
Description=Daily refresh of the host firewall FireHOL set
|
||||
Description=Daily refresh of the host firewall FireHOL sets
|
||||
|
||||
[Timer]
|
||||
OnCalendar=daily
|
||||
|
||||
@@ -10,6 +10,11 @@
|
||||
fail_msg: "wt0 address {{ ansible_wt0.ipv4.address }} is missing from firewall.cluster.v4"
|
||||
when: firewall.cluster.v4 | default({}, true) | length > 0 and ansible_wt0.ipv4.address is defined
|
||||
|
||||
- name: Check the forward policy
|
||||
ansible.builtin.assert:
|
||||
that: firewall.forward_policy | default('drop', true) in ['accept', 'log', 'drop']
|
||||
fail_msg: "firewall.forward_policy is {{ firewall.forward_policy }}, expected accept, log or drop"
|
||||
|
||||
- name: Remove the include block of the old nftables role
|
||||
ansible.builtin.blockinfile:
|
||||
path: /etc/nftables.conf
|
||||
@@ -38,6 +43,22 @@
|
||||
reload: false
|
||||
when: firewall.log.all_netns
|
||||
|
||||
# The handshake meters match `ct original packets 2`, which counts only with conntrack accounting (off by default).
|
||||
# The module is loaded at boot so that systemd-sysctl finds the key.
|
||||
- name: Load nf_conntrack at boot
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/modules-load.d/nf_conntrack.conf
|
||||
content: "nf_conntrack\n"
|
||||
mode: "0644"
|
||||
|
||||
- name: Count packets per connection in conntrack
|
||||
ansible.posix.sysctl:
|
||||
name: net.netfilter.nf_conntrack_acct
|
||||
value: "1"
|
||||
sysctl_file: /etc/sysctl.d/90-nf-conntrack-acct.conf
|
||||
sysctl_set: true
|
||||
reload: false
|
||||
|
||||
- name: Install the systemd units
|
||||
ansible.builtin.copy:
|
||||
src: "{{ item }}"
|
||||
@@ -158,7 +179,7 @@
|
||||
path: /etc/nftables.d/host-spamhaus.nft
|
||||
state: absent
|
||||
|
||||
- name: FireHOL set
|
||||
- name: FireHOL sets
|
||||
when: firewall.firehol
|
||||
block:
|
||||
- name: Install the FireHOL set updater
|
||||
@@ -167,25 +188,40 @@
|
||||
dest: /usr/local/sbin/nft-host-firehol
|
||||
mode: "0755"
|
||||
|
||||
# The rule set includes this file, so a missing one would fail the load at boot.
|
||||
- name: Build the FireHOL set on first run
|
||||
ansible.builtin.command: /usr/local/sbin/nft-host-firehol
|
||||
args:
|
||||
creates: /etc/nftables.d/host-firehol.nft
|
||||
# The rule set includes this file and uses both sets, so a missing file or set would fail the load at boot.
|
||||
- name: Look for the FireHOL sets
|
||||
ansible.builtin.command: grep -cE 'set (firehol_v4|firehol_level2_v4) ' /etc/nftables.d/host-firehol.nft
|
||||
register: firewall_firehol_sets
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
check_mode: false
|
||||
|
||||
- name: Read the FireHOL set file
|
||||
# Sets of removed lists are unused by the new rule set, which nft accepts, but the deployed one still uses them:
|
||||
# a rebuild before the new rule set is confirmed would fail its reload. The rebuild at the end of the role drops them.
|
||||
- name: Look for sets of removed FireHOL lists
|
||||
ansible.builtin.command: grep -cE 'set (bruteforceblocker_v4|malware_v4) ' /etc/nftables.d/host-firehol.nft
|
||||
register: firewall_firehol_stale
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
check_mode: false
|
||||
|
||||
- name: Build the FireHOL sets when missing
|
||||
ansible.builtin.command: /usr/local/sbin/nft-host-firehol
|
||||
when: firewall_firehol_sets.stdout != '2'
|
||||
|
||||
- name: Read the FireHOL sets file
|
||||
ansible.builtin.stat:
|
||||
path: /etc/nftables.d/host-firehol.nft
|
||||
register: firewall_firehol
|
||||
|
||||
- name: Require non-empty FireHOL set
|
||||
- name: Require non-empty FireHOL sets
|
||||
ansible.builtin.assert:
|
||||
that: firewall_firehol.stat.exists and firewall_firehol.stat.size > 0
|
||||
fail_msg: /etc/nftables.d/host-firehol.nft is missing or empty, refusing to apply the firewall
|
||||
# Check mode neither installs the updater nor builds the sets.
|
||||
when: not ansible_check_mode
|
||||
|
||||
- name: Enable the FireHOL set timer
|
||||
- name: Enable the FireHOL sets timer
|
||||
ansible.builtin.systemd:
|
||||
name: nft-host-firehol.timer
|
||||
enabled: true
|
||||
@@ -194,10 +230,10 @@
|
||||
# Check mode does not install the unit.
|
||||
ignore_errors: "{{ ansible_check_mode }}"
|
||||
|
||||
- name: Stop the FireHOL set refresh
|
||||
- name: Stop the FireHOL sets refresh
|
||||
when: not firewall.firehol
|
||||
block:
|
||||
- name: Disable the FireHOL set timer
|
||||
- name: Disable the FireHOL sets timer
|
||||
ansible.builtin.systemd:
|
||||
name: nft-host-firehol.timer
|
||||
enabled: false
|
||||
@@ -206,7 +242,7 @@
|
||||
# Check mode does not install the unit.
|
||||
ignore_errors: "{{ ansible_check_mode }}"
|
||||
|
||||
- name: Remove the FireHOL set file
|
||||
- name: Remove the FireHOL sets file
|
||||
ansible.builtin.file:
|
||||
path: /etc/nftables.d/host-firehol.nft
|
||||
state: absent
|
||||
@@ -345,3 +381,45 @@
|
||||
dest: /etc/nftables.d/host.nft.good
|
||||
remote_src: true
|
||||
mode: "0644"
|
||||
|
||||
# GreenSnow is part of FireHOL level2. The rule file goes last: the last confirmed rule set (host.nft.good) and a
|
||||
# reload by a refresh service still include it until the new rule set is confirmed. The set itself leaves the live
|
||||
# table with the reload of the new rule set.
|
||||
- name: Look for the GreenSnow timer
|
||||
ansible.builtin.stat:
|
||||
path: /etc/systemd/system/nft-host-greensnow.timer
|
||||
register: firewall_greensnow_timer
|
||||
|
||||
- name: Stop the GreenSnow refresh
|
||||
ansible.builtin.systemd:
|
||||
name: nft-host-greensnow.timer
|
||||
enabled: false
|
||||
state: stopped
|
||||
when: firewall_greensnow_timer.stat.exists
|
||||
# Check mode does not stop the timer.
|
||||
ignore_errors: "{{ ansible_check_mode }}"
|
||||
|
||||
- name: Remove the GreenSnow files
|
||||
ansible.builtin.file:
|
||||
path: "{{ item }}"
|
||||
state: absent
|
||||
loop:
|
||||
- /etc/systemd/system/nft-host-greensnow.service
|
||||
- /etc/systemd/system/nft-host-greensnow.timer
|
||||
- /usr/local/sbin/nft-host-greensnow
|
||||
- /etc/nftables.d/host-greensnow.nft
|
||||
register: firewall_greensnow_files
|
||||
|
||||
- name: Reload systemd after removing the GreenSnow units
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
when: firewall_greensnow_files is changed
|
||||
|
||||
# The sets file keeps sets of removed FireHOL lists until the rule set without them is confirmed (see above).
|
||||
- name: Drop sets of removed FireHOL lists
|
||||
ansible.builtin.command: /usr/local/sbin/nft-host-firehol
|
||||
when:
|
||||
- firewall.firehol
|
||||
- firewall_firehol_sets.stdout == '2'
|
||||
- (firewall_firehol_stale.stdout | default('0') | int) > 0
|
||||
- not ansible_check_mode
|
||||
|
||||
@@ -30,8 +30,15 @@ set {{ name }} {
|
||||
{% set guest_match = 'iifname { "' ~ (guest_ifaces | join('", "')) ~ '" }' %}
|
||||
{% set dnat = firewall.dnat | default({}, true) | dict2items | rejectattr('value', 'none') | map(attribute='value') | list %}
|
||||
{% set masquerade = firewall.masquerade | default({}, true) | dict2items | rejectattr('value', 'none') | map(attribute='value') | unique | list %}
|
||||
{% set lapi = cluster.lapi | default({}, true) %}
|
||||
{% set lapi_rule = list(lapi.clients) and lapi.port %}
|
||||
{% set journal = cluster.journal | default({}, true) %}
|
||||
{% set journal_rule = list(journal.clients) and journal.port %}
|
||||
{% set scrapers = firewall.scrapers | default({}, true) %}
|
||||
{% set forward_guard = firewall.forward_guard | default({}, true) | dict2items | rejectattr('value', 'none') | list %}
|
||||
{% set routed_v6 = firewall.routed_v6 | default({}, true) | dict2items | rejectattr('value', 'none') | map(attribute='value') | unique | list %}
|
||||
{% set uplink_ifaces = firewall.uplinks | default({}, true) | dict2items | rejectattr('value', 'none') | map(attribute='value') | list %}
|
||||
{% set no_reflect = ((firewall.no_reflect | default({}, true) | dict2items | rejectattr('value', 'none') | map(attribute='value') | list) + ([ansible_default_ipv4.interface | default(none), ansible_default_ipv6.interface | default(none)] | select | list)) | unique | difference(uplink_ifaces) | list %}
|
||||
{% set blocklists = firewall.spamhaus or firewall.firehol %}
|
||||
{% macro port_values(map) -%}
|
||||
{{ map | default({}, true) | dict2items | rejectattr('value', 'none') | map(attribute='value') | map('string') | unique | join(' ') }}
|
||||
@@ -51,6 +58,8 @@ set {{ name }} {
|
||||
'ports': ports | join(', '),
|
||||
'rate': svc.value.rate,
|
||||
'burst': burst,
|
||||
'syn_rate': svc.value.rate * 2,
|
||||
'syn_burst': burst * 2,
|
||||
'per': per,
|
||||
'f4': svc.value.range_factor_v4 | default(rl_factor.v4 | default(6), true),
|
||||
'f6': svc.value.range_factor_v6 | default(rl_factor.v6 | default(3), true),
|
||||
@@ -86,6 +95,12 @@ table inet host {
|
||||
{{ addr_set('admin_v6', 'ipv6_addr', firewall.admin.v6) }}
|
||||
{{ addr_set('cluster_v4', 'ipv4_addr', cluster.v4) }}
|
||||
{{ addr_set('cluster_v6', 'ipv6_addr', cluster.v6) }}
|
||||
{% if lapi_rule %}
|
||||
{{ addr_set('lapi_clients_v4', 'ipv4_addr', lapi.clients) }}
|
||||
{% endif %}
|
||||
{% if journal_rule %}
|
||||
{{ addr_set('journal_clients_v4', 'ipv4_addr', journal.clients) }}
|
||||
{% endif %}
|
||||
{{ addr_set('scrapers_v4', 'ipv4_addr', scrapers.v4) }}
|
||||
{{ addr_set('scrapers_v6', 'ipv6_addr', scrapers.v6) }}
|
||||
{{ addr_list_set('rl_exempt_v4', 'ipv4_addr', exempt_v4) }}
|
||||
@@ -103,8 +118,49 @@ table inet host {
|
||||
set rl_{{ svc.name }}_v6r {
|
||||
type ipv6_addr; flags dynamic; timeout 10m; size 16384;
|
||||
}
|
||||
set rl_hs_{{ svc.name }}_v4 {
|
||||
type ipv4_addr; flags dynamic; timeout 10m; size 65535;
|
||||
}
|
||||
set rl_hs_{{ svc.name }}_v6 {
|
||||
type ipv6_addr; flags dynamic; timeout 10m; size 65535;
|
||||
}
|
||||
{% endfor %}
|
||||
{% if rl_input or rl_forward %}
|
||||
# Flows the handshake meter rejected: the client sends its first data segment right behind the ACK, which reaches the
|
||||
# host before the reset reaches the client and would complete the handshake there.
|
||||
set rl_hs_rejected_v4 {
|
||||
type ipv4_addr . inet_service . ipv4_addr . inet_service; flags dynamic,timeout; timeout 30s; size 65535;
|
||||
}
|
||||
set rl_hs_rejected_v6 {
|
||||
type ipv6_addr . inet_service . ipv6_addr . inet_service; flags dynamic,timeout; timeout 30s; size 65535;
|
||||
}
|
||||
{% endif %}
|
||||
|
||||
{% if cluster.v4 is defined and cluster.v4.pods is defined %}
|
||||
# NetBird marks new flows in prerouting (priority mangle, -150) and masquerades marked ones in postrouting, which
|
||||
# rewrites the source of cross-node pod traffic and breaks Cilium identity and network policy when pod traffic passes
|
||||
# the host stack (veth with legacy host routing). This chain runs right after. Flows from the cluster ring (pods,
|
||||
# node NetBird addresses) into the pod CIDR arriving on wt0 get mark 0x1bd20: not masqueraded, and accepted by the
|
||||
# NetBird forward filter, which drops forwarded wt0 traffic that no route group allows. Other pod to pod flows
|
||||
# lose their marks. The same mark lets the LAPI clients (@lapi_clients_v4) reach the pod port of the NodePort service
|
||||
# with their own address: Cilium has translated the NodePort before this point, so NetBird would masquerade the flow
|
||||
# to the cilium_host address and the network policy would see world.
|
||||
# The journal uploaders (@journal_clients_v4) get the same mark on the Traefik pod port: the NetBird allowlist of
|
||||
# the ingest route checks their own address.
|
||||
chain pod_identity {
|
||||
type filter hook prerouting priority -140; policy accept;
|
||||
iifname "wt0" ct state new ip saddr @cluster_v4 ip daddr {{ cluster.v4.pods }} meta mark set 0x0001bd20
|
||||
{% if lapi_rule %}
|
||||
iifname "wt0" ct state new ip saddr @lapi_clients_v4 ip daddr {{ cluster.v4.pods }} tcp dport {{ lapi.port }} meta mark set 0x0001bd20
|
||||
{% endif %}
|
||||
{% if journal_rule %}
|
||||
iifname "wt0" ct state new ip saddr @journal_clients_v4 ip daddr {{ cluster.v4.pods }} tcp dport {{ journal.port }} meta mark set 0x0001bd20
|
||||
{% endif %}
|
||||
ct state new ip saddr {{ cluster.v4.pods }} ip daddr {{ cluster.v4.pods }} meta mark 0x0001bd21 meta mark set 0
|
||||
ct state new ip saddr {{ cluster.v4.pods }} ip daddr {{ cluster.v4.pods }} meta mark 0x0001bd22 meta mark set 0
|
||||
}
|
||||
|
||||
{% endif %}
|
||||
chain input {
|
||||
type filter hook input priority filter; policy drop;
|
||||
{% if blocklists %}
|
||||
@@ -116,9 +172,19 @@ table inet host {
|
||||
ip6 saddr @spamhaus_v6 jump spamhaus_in
|
||||
{% endif %}
|
||||
{% if firewall.firehol %}
|
||||
ip saddr @firehol_v4 jump firehol_in
|
||||
ip saddr @firehol_v4 jump firehol1_in
|
||||
ip saddr @firehol_level2_v4 jump firehol2_in
|
||||
{% endif %}
|
||||
|
||||
{% endif %}
|
||||
{% if rl_input %}
|
||||
# Before the established rule. Packets of rejected flows get a reset (not on RST, so that the half-open socket of the
|
||||
# client side reset is cleaned up; a reset per packet is cheap, the client sends only a few before it sees the first).
|
||||
# The packet that completes a handshake is a bare ACK and the second original-direction packet (needs
|
||||
# net.netfilter.nf_conntrack_acct); an RST or FIN as second packet is no handshake, and a blind spoofer can send it.
|
||||
meta l4proto tcp tcp flags & rst == 0 ip saddr . tcp sport . ip daddr . tcp dport @rl_hs_rejected_v4 counter reject with tcp reset
|
||||
meta l4proto tcp tcp flags & rst == 0 ip6 saddr . tcp sport . ip6 daddr . tcp dport @rl_hs_rejected_v6 counter reject with tcp reset
|
||||
meta l4proto tcp tcp flags & (fin | syn | rst | ack) == ack ct state established ct direction original ct original packets 2 jump ratelimit_hs_in
|
||||
{% endif %}
|
||||
ct state established,related accept
|
||||
{% if firewall.scanners %}
|
||||
@@ -182,7 +248,12 @@ table inet host {
|
||||
# range (IPv4 /24, IPv6 /48; the per-source rate times range_factor). Admin, cluster, VPN, guest and internal sources
|
||||
# and the public addresses of all inventory hosts (rl_exempt_v4, rl_exempt_v6) are exempt: cluster nodes reach other hosts
|
||||
# with their public address (ArgoCD git fetches, mail submission). The public set is checked only here, no other chain
|
||||
# trusts it. The excess drops with its own log prefix, which CrowdSec reads as a ratelimit event and not as a port scan.
|
||||
# trusts it. The handshake meters carry the service limit (rate and burst of firewall.ratelimit): they count the packet that
|
||||
# completes a handshake (the second original-direction packet of an established connection), which a spoofed source cannot
|
||||
# send, so a real client above the limit is logged with fw-rl-hs: and reset. Only the address tier exists there; ranges are
|
||||
# left to CrowdSec. The SYN meters are a looser flood cap at twice the rate and burst: they drop the excess with the log
|
||||
# prefixes fw-rl-syn-addr: (per address) and fw-rl-syn-range: (per range), which a spoofed source can trigger, so they are
|
||||
# evidence only. Their cap sits above the service limit so that a real client reaches the handshake meter first.
|
||||
{% macro rl_exempt() %}
|
||||
iifname "wt0" return
|
||||
{% for iface in cluster_ifaces + internal_ifaces + guest_ifaces %}
|
||||
@@ -204,19 +275,50 @@ table inet host {
|
||||
}
|
||||
|
||||
{% endfor %}
|
||||
{% for svc in rl_services if svc.input or svc.forward %}
|
||||
chain rl_{{ svc.name }} {
|
||||
update @rl_{{ svc.name }}_v4 { ip saddr limit rate over {{ svc.rate }}/{{ svc.per }} burst {{ svc.burst }} packets } counter jump rl_drop
|
||||
update @rl_{{ svc.name }}_v6 { ip6 saddr & ffff:ffff:ffff:ffff:: limit rate over {{ svc.rate }}/{{ svc.per }} burst {{ svc.burst }} packets } counter jump rl_drop
|
||||
update @rl_{{ svc.name }}_v4r { ip saddr & 255.255.255.0 limit rate over {{ svc.rate * svc.f4 }}/{{ svc.per }} burst {{ svc.burst * svc.f4 }} packets } counter jump rl_drop
|
||||
update @rl_{{ svc.name }}_v6r { ip6 saddr & ffff:ffff:ffff:: limit rate over {{ svc.rate * svc.f6 }}/{{ svc.per }} burst {{ svc.burst * svc.f6 }} packets } counter jump rl_drop
|
||||
{% for hook, svcs in [('in', rl_input), ('fwd', rl_forward)] if svcs %}
|
||||
chain ratelimit_hs_{{ hook }} {
|
||||
{{ rl_exempt() }}
|
||||
{% for svc in svcs %}
|
||||
meta l4proto tcp ct original proto-dst { {{ svc.ports }} } counter jump rl_hs_{{ svc.name }}
|
||||
{% endfor %}
|
||||
}
|
||||
|
||||
{% endfor %}
|
||||
chain rl_drop {
|
||||
limit rate {{ firewall.log.ratelimit_rate }} log prefix "fw-ratelimit: "
|
||||
{% for svc in rl_services if svc.input or svc.forward %}
|
||||
chain rl_{{ svc.name }} {
|
||||
update @rl_{{ svc.name }}_v4 { ip saddr limit rate over {{ svc.syn_rate }}/{{ svc.per }} burst {{ svc.syn_burst }} packets } counter jump rl_drop_addr
|
||||
update @rl_{{ svc.name }}_v6 { ip6 saddr & ffff:ffff:ffff:ffff:: limit rate over {{ svc.syn_rate }}/{{ svc.per }} burst {{ svc.syn_burst }} packets } counter jump rl_drop_addr
|
||||
update @rl_{{ svc.name }}_v4r { ip saddr & 255.255.255.0 limit rate over {{ svc.syn_rate * svc.f4 }}/{{ svc.per }} burst {{ svc.syn_burst * svc.f4 }} packets } counter jump rl_drop_range
|
||||
update @rl_{{ svc.name }}_v6r { ip6 saddr & ffff:ffff:ffff:: limit rate over {{ svc.syn_rate * svc.f6 }}/{{ svc.per }} burst {{ svc.syn_burst * svc.f6 }} packets } counter jump rl_drop_range
|
||||
}
|
||||
|
||||
{% endfor %}
|
||||
{% for svc in rl_services if svc.input or svc.forward %}
|
||||
chain rl_hs_{{ svc.name }} {
|
||||
update @rl_hs_{{ svc.name }}_v4 { ip saddr limit rate over {{ svc.rate }}/{{ svc.per }} burst {{ svc.burst }} packets } counter jump rl_hs_reject
|
||||
update @rl_hs_{{ svc.name }}_v6 { ip6 saddr & ffff:ffff:ffff:ffff:: limit rate over {{ svc.rate }}/{{ svc.per }} burst {{ svc.burst }} packets } counter jump rl_hs_reject
|
||||
}
|
||||
|
||||
{% endfor %}
|
||||
# One log limit per prefix, so a flood of spoofed SYNs cannot use up the budget of the handshake evidence.
|
||||
chain rl_drop_addr {
|
||||
limit rate {{ firewall.log.ratelimit_rate }} log prefix "fw-rl-syn-addr: "
|
||||
drop
|
||||
}
|
||||
|
||||
chain rl_drop_range {
|
||||
limit rate {{ firewall.log.ratelimit_rate }} log prefix "fw-rl-syn-range: "
|
||||
drop
|
||||
}
|
||||
|
||||
# The handshake is complete, so the connection has to be ended actively; the flow is remembered so that its later
|
||||
# packets are reset without a log line (one fw-rl-hs: line per rejected connection).
|
||||
chain rl_hs_reject {
|
||||
meta nfproto ipv4 add @rl_hs_rejected_v4 { ip saddr . tcp sport . ip daddr . tcp dport }
|
||||
meta nfproto ipv6 add @rl_hs_rejected_v6 { ip6 saddr . tcp sport . ip6 daddr . tcp dport }
|
||||
limit rate {{ firewall.log.ratelimit_rate }} log prefix "fw-rl-hs: "
|
||||
reject with tcp reset
|
||||
}
|
||||
{% endif %}
|
||||
{% if firewall.spamhaus %}
|
||||
|
||||
@@ -234,13 +336,23 @@ table inet host {
|
||||
{% endif %}
|
||||
{% if firewall.firehol %}
|
||||
|
||||
chain firehol_in {
|
||||
limit rate {{ firewall.log.spamhaus_rate }} log prefix "fw-firehol-in: "
|
||||
chain firehol1_in {
|
||||
limit rate {{ firewall.log.spamhaus_rate }} log prefix "fw-firehol1-in: "
|
||||
drop
|
||||
}
|
||||
|
||||
chain firehol_out {
|
||||
limit rate {{ firewall.log.spamhaus_rate }} log prefix "fw-firehol-out: "
|
||||
chain firehol1_out {
|
||||
limit rate {{ firewall.log.spamhaus_rate }} log prefix "fw-firehol1-out: "
|
||||
reject
|
||||
}
|
||||
|
||||
chain firehol2_in {
|
||||
limit rate {{ firewall.log.spamhaus_rate }} log prefix "fw-firehol2-in: "
|
||||
drop
|
||||
}
|
||||
|
||||
chain firehol2_out {
|
||||
limit rate {{ firewall.log.spamhaus_rate }} log prefix "fw-firehol2-out: "
|
||||
reject
|
||||
}
|
||||
{% endif %}
|
||||
@@ -267,7 +379,8 @@ table inet host {
|
||||
{% endif %}
|
||||
{% if firewall.firehol %}
|
||||
|
||||
ip daddr @firehol_v4 jump firehol_out
|
||||
ip daddr @firehol_v4 jump firehol1_out
|
||||
ip daddr @firehol_level2_v4 jump firehol2_out
|
||||
{% endif %}
|
||||
{% if firewall.block_smtp_out %}
|
||||
|
||||
@@ -331,14 +444,26 @@ table inet host {
|
||||
ip6 saddr @geo_v6 accept
|
||||
{% endif %}
|
||||
}
|
||||
{% if forward_guard or blocklists or firewall.scanners or rl_forward %}
|
||||
{% set forward_policy = firewall.forward_policy | default('drop', true) %}
|
||||
{% set forward_ifaces = firewall.forward_ifaces | default({}, true) | dict2items | rejectattr('value', 'none') | map(attribute='value') | unique | list %}
|
||||
{% set forward_trusted = (([] if 'external' in group_names else ['wt0']) + cluster_ifaces + internal_ifaces + guest_ifaces + forward_ifaces) | unique | list %}
|
||||
{% if forward_guard or blocklists or firewall.scanners or rl_forward or firewall.block_smtp_out or firewall.uplinks or no_reflect or forward_policy != 'accept' %}
|
||||
|
||||
chain forward {
|
||||
type filter hook forward priority filter; policy accept;
|
||||
type filter hook forward priority filter; policy {{ 'drop' if forward_policy == 'drop' else 'accept' }};
|
||||
{% if no_reflect %}
|
||||
|
||||
# A packet that arrives on the interface of the default route and would leave through it again is a foreign
|
||||
# destination the datacenter routed to this host: it is not forwarded, or the host acts as a reflector.
|
||||
iifname { "{{ no_reflect | join('", "') }}" } oifname { "{{ no_reflect | join('", "') }}" } ct state new counter drop
|
||||
{% endif %}
|
||||
{% if rl_forward %}
|
||||
|
||||
# Docker-published ports and port forwards arrive here after DNAT; pod and guest egress is not DNATed.
|
||||
ct status dnat ct state new jump ratelimit_fwd
|
||||
meta l4proto tcp ct status dnat tcp flags & rst == 0 ip saddr . tcp sport . ip daddr . tcp dport @rl_hs_rejected_v4 counter reject with tcp reset
|
||||
meta l4proto tcp ct status dnat tcp flags & rst == 0 ip6 saddr . tcp sport . ip6 daddr . tcp dport @rl_hs_rejected_v6 counter reject with tcp reset
|
||||
meta l4proto tcp tcp flags & (fin | syn | rst | ack) == ack ct status dnat ct state established ct direction original ct original packets 2 jump ratelimit_hs_fwd
|
||||
{% endif %}
|
||||
{% if firewall.spamhaus %}
|
||||
|
||||
@@ -349,8 +474,10 @@ table inet host {
|
||||
{% endif %}
|
||||
{% if firewall.firehol %}
|
||||
|
||||
ip saddr @firehol_v4 jump firehol_in
|
||||
ip daddr @firehol_v4 jump firehol_out
|
||||
ip saddr @firehol_v4 jump firehol1_in
|
||||
ip daddr @firehol_v4 jump firehol1_out
|
||||
ip saddr @firehol_level2_v4 jump firehol2_in
|
||||
ip daddr @firehol_level2_v4 jump firehol2_out
|
||||
{% endif %}
|
||||
{% if firewall.scanners %}
|
||||
|
||||
@@ -377,6 +504,48 @@ table inet host {
|
||||
{{ match }} jump drop_log
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% for name, iface in firewall.uplinks.items() if iface is not none %}
|
||||
|
||||
{% if routed_v6 %}
|
||||
# {{ name }}: from outside only what was DNATed here, admitted by a forward_guard zone above or, for IPv6, routed out of a guest bridge.
|
||||
iifname "{{ iface }}" ct state new ct status & dnat == 0 meta nfproto ipv4 counter drop
|
||||
iifname "{{ iface }}" ct state new ct status & dnat == 0 meta nfproto ipv6 oifname != { "{{ routed_v6 | join('", "') }}" } counter drop
|
||||
{% else %}
|
||||
# {{ name }}: from outside only what was DNATed here or admitted by a forward_guard zone above.
|
||||
iifname "{{ iface }}" ct state new ct status & dnat == 0 counter drop
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if firewall.block_smtp_out %}
|
||||
{% set smtp_oif = ('oifname { "' ~ (uplink_ifaces | join('", "')) ~ '" } ') if uplink_ifaces else '' %}
|
||||
|
||||
# Pod, container and guest egress to tcp/25 is rejected like the host's own (chain output). Connections DNATed to
|
||||
# a mail host (ct status dnat) and traffic inside the cluster ring (nodes, pods) pass. With uplinks configured
|
||||
# only packets leaving through one are egress: a scan forwarded from the uplink to a guest is not.
|
||||
ct status & dnat == 0 {{ smtp_oif }}ip daddr != @cluster_v4 tcp dport 25 limit rate {{ firewall.log.out_rate }} log prefix "fw-out-reject: "
|
||||
ct status & dnat == 0 {{ smtp_oif }}ip daddr != @cluster_v4 tcp dport 25 reject with tcp reset
|
||||
ct status & dnat == 0 {{ smtp_oif }}ip6 daddr != @cluster_v6 tcp dport 25 limit rate {{ firewall.log.out_rate }} log prefix "fw-out-reject: "
|
||||
ct status & dnat == 0 {{ smtp_oif }}ip6 daddr != @cluster_v6 tcp dport 25 reject with tcp reset
|
||||
{% endif %}
|
||||
{% if forward_policy != 'accept' %}
|
||||
|
||||
# What is forwarded on purpose: replies, connections DNATed here, traffic that arrives on a trusted interface (mesh,
|
||||
# cluster, internal and guest interfaces) and traffic to the pods (Cilium translates services before netfilter, so
|
||||
# connections from outside arrive as client -> pod without ct status dnat). One rule per interface: a set that mixes
|
||||
# wildcards and plain names did not match wt0. Everything else is logged (fw-fwd-drop:, the first place to look when a Cilium or network change breaks forwarding), counted and, with forward_policy drop, dropped. Reflected packets drop above, silently.
|
||||
ct state established,related accept
|
||||
ct status dnat accept
|
||||
{% for iface in forward_trusted %}
|
||||
iifname "{{ iface }}" accept
|
||||
{% endfor %}
|
||||
{% for iface in cluster_ifaces %}
|
||||
oifname "{{ iface }}" accept
|
||||
{% endfor %}
|
||||
{% if routed_v6 %}
|
||||
meta nfproto ipv6 oifname { "{{ routed_v6 | join('", "') }}" } accept
|
||||
{% endif %}
|
||||
limit rate {{ firewall.log.in_rate }} log prefix "fw-fwd-drop: "
|
||||
counter
|
||||
{% endif %}
|
||||
}
|
||||
{% endif %}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ dump() { # file, container, command run inside the container
|
||||
dump {{ d.file | quote }} {{ d.container | quote }} {{ d.command | quote }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
# Global policy of the repository, inherited by every source; the repository is shared by all hosts of the bucket
|
||||
kopia policy set --global --compression=zstd >/dev/null 2>&1 ||
|
||||
{ echo "setting the global compression failed" >&2; failed=1; }
|
||||
{% set ignores = kopia_backup.ignore | dict2items | rejectattr('value', 'none') | list %}
|
||||
{% if ignores %}
|
||||
|
||||
@@ -50,6 +54,24 @@ ignore() {
|
||||
ignore {{ i.key | quote }} {{ i.value | map('quote') | join(' ') }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% set retentions = kopia_backup.retention | default({}) | dict2items | rejectattr('value', 'none') | list %}
|
||||
{% if retentions %}
|
||||
|
||||
# Per-source retention overrides, applied as a whole. A keep flag of 0 means "inherit",
|
||||
# so setting all four flags explicitly fully defines the retention for the path.
|
||||
retention() {
|
||||
local path=$1; shift
|
||||
kopia policy set "$path" "$@" >/dev/null 2>&1 ||
|
||||
{ echo "setting the retention of $path failed" >&2; failed=1; }
|
||||
}
|
||||
{% for r in retentions %}
|
||||
retention {{ r.key | quote }} \
|
||||
--keep-daily={{ r.value.keep_daily | default(0) }} \
|
||||
--keep-weekly={{ r.value.keep_weekly | default(0) }} \
|
||||
--keep-monthly={{ r.value.keep_monthly | default(0) }} \
|
||||
--keep-annual={{ r.value.keep_annual | default(0) }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
for p in {{ kopia_backup.sources | map('quote') | join(' ') }}{{ ' "$etcd"' if kopia_backup.etcd_snapshot }}{{ ' "$dumps"' if kopia_backup.dumps }}; do
|
||||
[ -d "$p" ] && sources+=("$p")
|
||||
|
||||
@@ -19,10 +19,16 @@ kubernetes:
|
||||
token: <shared token for nodes to join>
|
||||
|
||||
network:
|
||||
# Copies the current /etc/resolv.conf whilst only keeping "nameserver" lines
|
||||
# can be used to remove filter
|
||||
# Writes /etc/resolv-k3s.conf (k3s resolv-conf, CoreDNS upstreams) from
|
||||
# resolv_conf_nameservers
|
||||
clean_resolv_conf: false
|
||||
|
||||
# Fixed upstreams, not copied from the node: a copied snapshot goes stale
|
||||
# when the node's DNS changes and can leave CoreDNS with a single upstream
|
||||
resolv_conf_nameservers:
|
||||
- 1.1.1.1
|
||||
- 9.9.9.9
|
||||
|
||||
# One of [none, flannel, calico, cilium]
|
||||
plugin: calico
|
||||
|
||||
|
||||
@@ -45,17 +45,15 @@
|
||||
else (ansible_default_ipv6.address | default('')) }}
|
||||
|
||||
- block:
|
||||
- name: Read resolv.conf
|
||||
shell: "grep '^nameserver' /etc/resolv.conf"
|
||||
register: nameserver_line
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
|
||||
- name: Write /etc/resolv-k3s.conf with only nameservers
|
||||
copy:
|
||||
content: "{{ nameserver_line.stdout }}"
|
||||
- name: Write /etc/resolv-k3s.conf with the fixed nameservers
|
||||
template:
|
||||
src: resolv-k3s.conf.jinja2
|
||||
dest: /etc/resolv-k3s.conf
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
when: kubernetes.network.clean_resolv_conf is true
|
||||
tags: resolv_conf
|
||||
|
||||
- name: Run handlers to reload configurations
|
||||
meta: flush_handlers
|
||||
|
||||
@@ -23,12 +23,14 @@ spec:
|
||||
hostNetwork: true
|
||||
containers:
|
||||
- name: routingtabletowg
|
||||
image: "ruakij/routingtabletowg:0.2.0"
|
||||
image: "ghcr.io/ruakij/routingtabletowg:0.3.0@sha256:a2c505e0f716dbbfdda1a535e2631e5a73b64236cd68d459eb407beab2e1032e"
|
||||
env:
|
||||
- name: INTERFACE
|
||||
value: {{ kubernetes.ipPool.nodeIp_interface }}
|
||||
- name: FILTER_PROTOCOL
|
||||
value: bird
|
||||
- name: FILTER_TABLE
|
||||
value: main
|
||||
- name: PERIODIC_SYNC
|
||||
value: '300'
|
||||
securityContext:
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
# Managed by Ansible (role kubernetes). Fixed upstreams, not copied from the node.
|
||||
{% for nameserver in kubernetes.network.resolv_conf_nameservers %}
|
||||
nameserver {{ nameserver }}
|
||||
{% endfor %}
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
# Every host sends its mail through the mailcow submission port with its own mailbox
|
||||
# (doc/host-mail-relay.md in the kubernetes repo). The password is set in host_vars.
|
||||
# (doc/security-perimeter.md in the kubernetes repo). The password is set in host_vars.
|
||||
mail_relay:
|
||||
host: tranio.ruekov.eu
|
||||
port: 587
|
||||
|
||||
@@ -9,3 +9,5 @@ netbird_client:
|
||||
iface_blacklist_extra: []
|
||||
# Overrides the --log-level of the netbird service when set
|
||||
log_level:
|
||||
# MTU of wt0, written to /var/lib/netbird/default.json (netbird restarts on change); leave empty to keep the config as is
|
||||
mtu:
|
||||
|
||||
@@ -36,6 +36,44 @@
|
||||
cfg: "{{ netbird_config_stopped.content | b64decode | from_json }}"
|
||||
when: netbird_stopped is changed
|
||||
|
||||
- name: Read netbird config for the MTU
|
||||
slurp:
|
||||
src: /var/lib/netbird/default.json
|
||||
register: netbird_config_mtu_file
|
||||
when: netbird_client.mtu | default('') | string | length > 0
|
||||
tags: netbird_client_config
|
||||
|
||||
- name: Set the wt0 MTU in the netbird config
|
||||
when: netbird_config_mtu_file.content is defined
|
||||
vars:
|
||||
netbird_config_mtu: "{{ netbird_config_mtu_file.content | b64decode | from_json }}"
|
||||
netbird_mtu_differs: "{{ (netbird_config_mtu.MTU | default(0)) != (netbird_client.mtu | int) }}"
|
||||
tags: netbird_client_config
|
||||
block:
|
||||
# netbird writes its in-memory config back on exit, so edit only while it is stopped
|
||||
- name: Stop netbird to edit its MTU
|
||||
systemd:
|
||||
name: netbird
|
||||
state: stopped
|
||||
when: netbird_mtu_differs
|
||||
notify: restart_netbird
|
||||
|
||||
- name: Re-read netbird config after stop
|
||||
slurp:
|
||||
src: /var/lib/netbird/default.json
|
||||
register: netbird_config_mtu_stopped
|
||||
when: netbird_mtu_differs
|
||||
|
||||
- name: Write MTU
|
||||
copy:
|
||||
dest: /var/lib/netbird/default.json
|
||||
content: "{{ cfg | combine({'MTU': netbird_client.mtu | int}) | to_nice_json(indent=4, sort_keys=False) }}\n"
|
||||
mode: "0600"
|
||||
vars:
|
||||
cfg: "{{ netbird_config_mtu_stopped.content | b64decode | from_json }}"
|
||||
when: netbird_mtu_differs
|
||||
notify: restart_netbird
|
||||
|
||||
- name: Set netbird log level
|
||||
copy:
|
||||
dest: /etc/systemd/system/netbird.service.d/20-log-level.conf
|
||||
|
||||
@@ -5,19 +5,33 @@
|
||||
- curl
|
||||
- gnupg
|
||||
|
||||
# Same keyring path the hand-installed hosts use, so the repository line is identical.
|
||||
# Same keyring path the hand-installed hosts use, so the repository file is identical.
|
||||
- name: Install the netbird repository keyring
|
||||
ansible.builtin.shell:
|
||||
cmd: set -o pipefail && curl -fsSL https://pkgs.netbird.io/debian/public.key | gpg --dearmor -o /usr/share/keyrings/netbird-archive-keyring.gpg
|
||||
executable: /bin/bash
|
||||
creates: /usr/share/keyrings/netbird-archive-keyring.gpg
|
||||
|
||||
- name: Remove one-line netbird-repository
|
||||
file:
|
||||
path: /etc/apt/sources.list.d/netbird.list
|
||||
state: absent
|
||||
register: netbird_list
|
||||
|
||||
- name: Add netbird-repository
|
||||
apt_repository:
|
||||
repo: "deb [signed-by=/usr/share/keyrings/netbird-archive-keyring.gpg] https://pkgs.netbird.io/debian stable main"
|
||||
state: present
|
||||
filename: netbird
|
||||
deb822_repository:
|
||||
name: netbird
|
||||
types: deb
|
||||
uris: https://pkgs.netbird.io/debian
|
||||
suites: stable
|
||||
components: main
|
||||
signed_by: /usr/share/keyrings/netbird-archive-keyring.gpg
|
||||
register: netbird_repo
|
||||
|
||||
- name: Update apt cache after netbird-repository change
|
||||
apt:
|
||||
update_cache: yes
|
||||
when: netbird_list.changed or netbird_repo.changed
|
||||
|
||||
# present, not latest: an upgrade restarts the daemon and drops the tunnels of the node.
|
||||
- name: Install wireguard & netbird
|
||||
|
||||
@@ -23,6 +23,36 @@
|
||||
dest: /etc/apt/apt.conf.d/52unattended-upgrades-local
|
||||
mode: "0644"
|
||||
|
||||
# needrestart (Ubuntu default) restarts every service that uses an upgraded library, containerd and netbird included:
|
||||
# after a libc upgrade the dead FUSE mounts hung systemd-resolved on antius and the node went NotReady (2026-11-14).
|
||||
# List only; the services pick up the library at the next reboot, which HostRebootRequired tracks.
|
||||
- name: Check for needrestart
|
||||
ansible.builtin.stat:
|
||||
path: /etc/needrestart/conf.d
|
||||
register: unattended_upgrades_needrestart
|
||||
|
||||
- name: Let needrestart list the services instead of restarting them
|
||||
ansible.builtin.template:
|
||||
src: needrestart-list-only.conf.j2
|
||||
dest: /etc/needrestart/conf.d/90-list-only.conf
|
||||
mode: "0644"
|
||||
when: unattended_upgrades_needrestart.stat.exists
|
||||
|
||||
# Debian installs apt-listchanges with email_address=root, which mails the NEWS of every
|
||||
# unattended upgrade through the mail relay.
|
||||
- name: Check for the apt-listchanges config
|
||||
ansible.builtin.stat:
|
||||
path: /etc/apt/listchanges.conf
|
||||
register: unattended_upgrades_listchanges
|
||||
|
||||
- name: Disable the apt-listchanges mails
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/apt/listchanges.conf
|
||||
regexp: '^email_address='
|
||||
line: email_address=none
|
||||
insertafter: '^\[apt\]'
|
||||
when: unattended_upgrades_listchanges.stat.exists
|
||||
|
||||
- name: Enable the apt timers
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ item }}"
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# {{ ansible_managed }}
|
||||
$nrconf{restart} = 'l';
|
||||
@@ -5,7 +5,10 @@
|
||||
|
||||
- name: Download binary
|
||||
get_url:
|
||||
url: https://github.com/Ruakij/wg-ipv6-converter/releases/{{ versionUri }}/wg-ipv6-converter_{{ arch }}
|
||||
url: https://github.com/Ruakij/wg-ipv6-converter/releases/{{ versionUri }}/wg-ipv6-converter-linux-{{ arch }}
|
||||
dest: /usr/local/bin/wg-ipv6-converter
|
||||
mode: "744"
|
||||
# Replaces a differing binary (get_url keeps an existing dest without a
|
||||
# checksum) and verifies the download.
|
||||
checksum: "sha256:https://github.com/Ruakij/wg-ipv6-converter/releases/{{ versionUri }}/SHA256SUMS"
|
||||
register: deployDownload
|
||||
|
||||
@@ -3,5 +3,10 @@
|
||||
# interface without its IPv6 addresses. Start it again whenever
|
||||
# {{ wireguard_ipv6_converter.service.bindTo }} starts. --no-block because the converter orders
|
||||
# itself After={{ wireguard_ipv6_converter.service.bindTo }}.
|
||||
# Upholds= additionally restarts the converter whenever it stops while
|
||||
# {{ wireguard_ipv6_converter.service.bindTo }} is active, e.g. after it exhausted its start limit.
|
||||
[Unit]
|
||||
Upholds=wg-ipv6-converter_{{ wireguard_ipv6_converter.setup.interface }}.service
|
||||
|
||||
[Service]
|
||||
ExecStartPost=/bin/systemctl --no-block start wg-ipv6-converter_{{ wireguard_ipv6_converter.setup.interface }}.service
|
||||
|
||||
@@ -16,7 +16,8 @@ RestartSec=30
|
||||
|
||||
Environment="INTERFACE={{ wireguard_ipv6_converter.setup.interface }}"
|
||||
{% if wireguard_ipv6_converter.setup.ipv6_format is defined %}
|
||||
Environment="IPV6_FORMAT={{ wireguard_ipv6_converter.setup.ipv6_format }}"
|
||||
{# systemd expands specifiers in Environment=, so every % must be doubled. #}
|
||||
Environment="IPV6_FORMAT={{ wireguard_ipv6_converter.setup.ipv6_format | replace('%', '%%') }}"
|
||||
{% endif %}
|
||||
{% if wireguard_ipv6_converter.setup.filter_prefix is defined %}
|
||||
Environment="FILTER_PREFIX={{ wireguard_ipv6_converter.setup.filter_prefix }}"
|
||||
|
||||
Reference in New Issue
Block a user