3 Commits
2 changed files with 65 additions and 15 deletions
+29 -15
View File
@@ -53,6 +53,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 +172,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,36 @@
#!/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.
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)"
/usr/bin/cscli --error hub update >/dev/null
/usr/bin/cscli --error hub upgrade --force >/dev/null
{% for scenario in crowdsec.disabled_scenarios.values() | reject('none') | unique | sort %}
if /usr/bin/cscli scenarios list -o raw | cut -d, -f1 | grep -qxF '{{ scenario }}'; then
/usr/bin/cscli --error scenarios remove '{{ scenario }}' --force >/dev/null
fi
{% endfor %}
if [ "$before" != "$(fingerprint)" ]; then
if [ -n "${NO_RELOAD:-}" ]; then
echo changed
else
systemctl reload crowdsec.service
fi
fi