Skip to content

Commit

Permalink
Moved install helper to a library, shorter code (crowdsecurity#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Apr 11, 2023
1 parent 16e3800 commit 5e2c977
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 357 deletions.
88 changes: 0 additions & 88 deletions config/helper.sh

This file was deleted.

4 changes: 4 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ Description: Firewall bouncer for Crowdsec (iptables+ipset)
Depends: iptables, ipset, gettext-base
Replaces: crowdsec-firewall-bouncer
Conflicts: crowdsec-firewall-bouncer-nftables
Section: admin
Priority: optional

Package: crowdsec-firewall-bouncer-nftables
Architecture: any
Description: Firewall bouncer for Crowdsec (nftables)
Depends: nftables, gettext-base
Replaces: crowdsec-firewall-bouncer
Conflicts: crowdsec-firewall-bouncer-iptables
Section: admin
Priority: optional
12 changes: 6 additions & 6 deletions debian/crowdsec-firewall-bouncer-iptables.postinst
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
systemctl daemon-reload

BOUNCER="crowdsec-firewall-bouncer"
CONFIG="/etc/crowdsec/bouncers/$BOUNCER.yaml"
SERVICE="$BOUNCER.service"
BOUNCER_PREFIX="FirewallBouncer"

helper="/usr/lib/$DPKG_MAINTSCRIPT_PACKAGE/helper.sh"
#shellcheck source=./scripts/_bouncer.sh
. "/usr/lib/$DPKG_MAINTSCRIPT_PACKAGE/_bouncer.sh"
START=1

if [ "$1" = "configure" ]; then
if $helper need-api-key "$CONFIG"; then
if ! $helper set-api-key "$CONFIG" "FirewallBouncer"; then
if need_api_key; then
if ! set_api_key; then
START=0
fi
fi
fi

systemctl --quiet is-enabled "$SERVICE" || systemctl unmask "$SERVICE" && systemctl enable "$SERVICE"

$helper set-local-port "$CONFIG"
set_local_port

if [ "$START" -eq 0 ]; then
echo "no api key was generated, you can generate one on your LAPI server by running 'cscli bouncers add <bouncer_name>' and add it to '$CONFIG'" >&2
Expand Down
14 changes: 0 additions & 14 deletions debian/crowdsec-firewall-bouncer-iptables.postrm

This file was deleted.

15 changes: 13 additions & 2 deletions debian/crowdsec-firewall-bouncer-iptables.prerm
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#!/bin/sh

systemctl stop crowdsec-firewall-bouncer || echo "cannot stop service"
systemctl disable crowdsec-firewall-bouncer || echo "cannot disable service"
set -eu

BOUNCER="crowdsec-firewall-bouncer"

#shellcheck source=./scripts/_bouncer.sh
. "/usr/lib/$DPKG_MAINTSCRIPT_PACKAGE/_bouncer.sh"

systemctl stop "$SERVICE" || echo "cannot stop service"
systemctl disable "$SERVICE" || echo "cannot disable service"

if [ "$1" = "purge" ]; then
delete_bouncer
fi
12 changes: 6 additions & 6 deletions debian/crowdsec-firewall-bouncer-nftables.postinst
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
systemctl daemon-reload

BOUNCER="crowdsec-firewall-bouncer"
CONFIG="/etc/crowdsec/bouncers/$BOUNCER.yaml"
SERVICE="$BOUNCER.service"
BOUNCER_PREFIX="FirewallBouncer"

helper="/usr/lib/$DPKG_MAINTSCRIPT_PACKAGE/helper.sh"
#shellcheck source=./scripts/_bouncer.sh
. "/usr/lib/$DPKG_MAINTSCRIPT_PACKAGE/_bouncer.sh"
START=1

if [ "$1" = "configure" ]; then
if $helper need-api-key "$CONFIG"; then
if ! $helper set-api-key "$CONFIG" "FirewallBouncer"; then
if need_api_key; then
if ! set_api_key; then
START=0
fi
fi
fi

systemctl --quiet is-enabled "$SERVICE" || systemctl unmask "$SERVICE" && systemctl enable "$SERVICE"

$helper set-local-port "$CONFIG"
set_local_port

if [ "$START" -eq 0 ]; then
echo "no api key was generated, you can generate one on your LAPI server by running 'cscli bouncers add <bouncer_name>' and add it to '$CONFIG'" >&2
Expand Down
14 changes: 0 additions & 14 deletions debian/crowdsec-firewall-bouncer-nftables.postrm

This file was deleted.

15 changes: 13 additions & 2 deletions debian/crowdsec-firewall-bouncer-nftables.prerm
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#!/bin/sh

systemctl stop crowdsec-firewall-bouncer || echo "cannot stop service"
systemctl disable crowdsec-firewall-bouncer || echo "cannot disable service"
set -eu

BOUNCER="crowdsec-firewall-bouncer"

#shellcheck source=./scripts/_bouncer.sh
. "/usr/lib/$DPKG_MAINTSCRIPT_PACKAGE/_bouncer.sh"

systemctl stop "$SERVICE" || echo "cannot stop service"
systemctl disable "$SERVICE" || echo "cannot disable service"

if [ "$1" = "purge" ]; then
delete_bouncer
fi
2 changes: 1 addition & 1 deletion debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ override_dh_auto_install:
for BACKEND in iptables nftables; do \
PKG="$$BOUNCER-$$BACKEND"; \
install -D -m 0755 $$BOUNCER -t "debian/$$PKG/usr/bin/"; \
install -D -m 0700 config/helper.sh -t "debian/$$PKG/usr/lib/$$PKG/"; \
install -D -m 0600 scripts/_bouncer.sh -t "debian/$$PKG/usr/lib/$$PKG/"; \
BACKEND=$$BACKEND envsubst '$$BACKEND' < config/$$BOUNCER.yaml | install -D -m 0600 /dev/stdin "debian/$$PKG/etc/crowdsec/bouncers/$$BOUNCER.yaml"; \
BIN="/usr/bin/$$BOUNCER" CFG="/etc/crowdsec/bouncers" envsubst '$$BIN $$CFG' < "config/$$BOUNCER.service" | install -D -m 0644 /dev/stdin "debian/$$PKG/etc/systemd/system/$$BOUNCER.service"; \
done
Loading

0 comments on commit 5e2c977

Please sign in to comment.