| 1 |
Name: crowdsec-firewall-bouncer-iptables |
| 2 |
Version: %(echo $VERSION) |
| 3 |
Release: %(echo $PACKAGE_NUMBER)%{?dist} |
| 4 |
Summary: Firewall bouncer for Crowdsec (iptables+ipset configuration) |
| 5 |
|
| 6 |
License: MIT |
| 7 |
URL: https://crowdsec.net |
| 8 |
Source0: https://github.com/crowdsecurity/%{name}/archive/v%(echo $VERSION).tar.gz |
| 9 |
Source1: 80-crowdsec-firewall-bouncer.preset |
| 10 |
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) |
| 11 |
|
| 12 |
BuildRequires: git |
| 13 |
BuildRequires: make |
| 14 |
BuildRequires: jq |
| 15 |
%{?fc33:BuildRequires: systemd-rpm-macros} |
| 16 |
|
| 17 |
Requires: iptables,ipset,gettext,ipset-libs |
| 18 |
|
| 19 |
%define debug_package %{nil} |
| 20 |
|
| 21 |
%description |
| 22 |
|
| 23 |
%define version_number %(echo $VERSION) |
| 24 |
%define releasever %(echo $RELEASEVER) |
| 25 |
%global local_version v%{version_number}-%{releasever}-rpm |
| 26 |
%global name crowdsec-firewall-bouncer |
| 27 |
%global __mangle_shebangs_exclude_from /usr/bin/env |
| 28 |
|
| 29 |
%prep |
| 30 |
%setup -q -T -b 0 -n crowdsec-firewall-bouncer-%{version_number} |
| 31 |
|
| 32 |
%build |
| 33 |
BUILD_VERSION=%{local_version} make |
| 34 |
TMP=$(mktemp -p /tmp/) |
| 35 |
cp config/%{name}.service ${TMP} |
| 36 |
BIN=%{_bindir}/%{name} CFG=/etc/crowdsec/bouncers/ envsubst < ${TMP} > config/%{name}.service |
| 37 |
rm ${TMP} |
| 38 |
|
| 39 |
%install |
| 40 |
rm -rf %{buildroot} |
| 41 |
mkdir -p %{buildroot}/usr/sbin |
| 42 |
mkdir -p %{buildroot}%{_presetdir} |
| 43 |
install -m 755 -D %{name} %{buildroot}%{_bindir}/%{name} |
| 44 |
install -m 600 -D config/%{name}.yaml %{buildroot}/etc/crowdsec/bouncers/%{name}.yaml |
| 45 |
install -m 644 -D config/%{name}.service %{buildroot}%{_unitdir}/%{name}.service |
| 46 |
install -m 644 -D %{SOURCE1} %{buildroot}%{_presetdir} |
| 47 |
%clean |
| 48 |
rm -rf %{buildroot} |
| 49 |
|
| 50 |
%files |
| 51 |
%defattr(-,root,root,-) |
| 52 |
/usr/bin/%{name} |
| 53 |
%{_unitdir}/%{name}.service |
| 54 |
%config(noreplace) /etc/crowdsec/bouncers/%{name}.yaml |
| 55 |
%config(noreplace) %{_presetdir}/80-crowdsec-firewall-bouncer.preset |
| 56 |
|
| 57 |
%post -p /bin/bash |
| 58 |
|
| 59 |
systemctl daemon-reload |
| 60 |
|
| 61 |
START=0 |
| 62 |
|
| 63 |
#install |
| 64 |
if [ "$1" == "1" ]; then |
| 65 |
|
| 66 |
if command -v cscli >/dev/null; then |
| 67 |
START=1 |
| 68 |
echo "cscli/crowdsec is present, generating API key" |
| 69 |
unique=$(date +%s) |
| 70 |
API_KEY=$(cscli -oraw bouncers add FirewallBouncer-"$unique") |
| 71 |
if [ $? -eq 1 ]; then |
| 72 |
echo "failed to create API token, service won't be started." |
| 73 |
START=0 |
| 74 |
API_KEY="<API_KEY>" |
| 75 |
else |
| 76 |
echo "API Key : ${API_KEY}" |
| 77 |
fi |
| 78 |
fi |
| 79 |
|
| 80 |
TMP=$(mktemp -p /tmp/) |
| 81 |
install -m 0600 /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml "$TMP" |
| 82 |
BACKEND=iptables API_KEY="$API_KEY" envsubst < "$TMP" | install -m 0600 /dev/stdin /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml |
| 83 |
rm "$TMP" |
| 84 |
else |
| 85 |
START=1 |
| 86 |
fi |
| 87 |
|
| 88 |
%systemd_post crowdsec-firewall-bouncer.service |
| 89 |
|
| 90 |
if command -v cscli >/dev/null; then |
| 91 |
START=1 |
| 92 |
PORT=$(cscli config show --key "Config.API.Server.ListenURI" | cut -d ":" -f2) |
| 93 |
if [ "$PORT" != "" ]; then |
| 94 |
sed -i "s/localhost:8080/127.0.0.1:${PORT}/g" /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml |
| 95 |
sed -i "s/127.0.0.1:8080/127.0.0.1:${PORT}/g" /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml |
| 96 |
fi |
| 97 |
fi |
| 98 |
|
| 99 |
|
| 100 |
if [ "$START" -eq 0 ]; then |
| 101 |
echo "no api key was generated, won't start or enable the service" |
| 102 |
else |
| 103 |
%if 0%{?fc35} |
| 104 |
systemctl enable crowdsec-firewall-bouncer |
| 105 |
%endif |
| 106 |
systemctl start crowdsec-firewall-bouncer |
| 107 |
fi |
| 108 |
|
| 109 |
%changelog |
| 110 |
* Tue Feb 16 2021 Manuel Sabban <[email protected]> |
| 111 |
- First initial packaging |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
%package -n crowdsec-firewall-bouncer-nftables |
| 116 |
Summary: Firewall bouncer for Crowdsec (nftables configuration) |
| 117 |
Requires: nftables,gettext |
| 118 |
%description -n crowdsec-firewall-bouncer-nftables |
| 119 |
|
| 120 |
%files -n crowdsec-firewall-bouncer-nftables |
| 121 |
/usr/bin/%{name} |
| 122 |
%{_unitdir}/%{name}.service |
| 123 |
%config(noreplace) /etc/crowdsec/bouncers/%{name}.yaml |
| 124 |
|
| 125 |
%post -p /bin/bash -n crowdsec-firewall-bouncer-nftables |
| 126 |
|
| 127 |
systemctl daemon-reload |
| 128 |
|
| 129 |
START=0 |
| 130 |
|
| 131 |
# install |
| 132 |
if [ "$1" == "1" ]; then |
| 133 |
if command -v cscli >/dev/null; then |
| 134 |
START=1 |
| 135 |
echo "cscli/crowdsec is present, generating API key" |
| 136 |
unique=$(date +%s) |
| 137 |
API_KEY=$(cscli -oraw bouncers add FirewallBouncer-"$unique") |
| 138 |
if [ $? -eq 1 ]; then |
| 139 |
echo "failed to create API token, service won't be started." |
| 140 |
START=0 |
| 141 |
API_KEY="<API_KEY>" |
| 142 |
else |
| 143 |
echo "API Key : ${API_KEY}" |
| 144 |
fi |
| 145 |
fi |
| 146 |
|
| 147 |
TMP=$(mktemp -p /tmp/) |
| 148 |
install -m 0600 /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml "$TMP" |
| 149 |
BACKEND=nftables API_KEY="$API_KEY" envsubst < "$TMP" | install -m 0600 /dev/stdin /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml |
| 150 |
rm "$TMP" |
| 151 |
else |
| 152 |
START=1 |
| 153 |
fi |
| 154 |
|
| 155 |
%systemd_post crowdsec-firewall-bouncer.service |
| 156 |
|
| 157 |
if command -v cscli >/dev/null; then |
| 158 |
PORT=$(cscli config show --key "Config.API.Server.ListenURI" | cut -d ":" -f2) |
| 159 |
if [ "$PORT" != "" ]; then |
| 160 |
sed -i "s/localhost:8080/127.0.0.1:${PORT}/g" /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml |
| 161 |
sed -i "s/127.0.0.1:8080/127.0.0.1:${PORT}/g" /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml |
| 162 |
fi |
| 163 |
fi |
| 164 |
|
| 165 |
|
| 166 |
if [ "$START" -eq 0 ]; then |
| 167 |
echo "no api key was generated, won't start or enable the service" |
| 168 |
else |
| 169 |
%if 0%{?fc35} |
| 170 |
systemctl enable crowdsec-firewall-bouncer |
| 171 |
%endif |
| 172 |
systemctl start crowdsec-firewall-bouncer |
| 173 |
fi |
| 174 |
|
| 175 |
|
| 176 |
%preun -p /bin/bash |
| 177 |
|
| 178 |
if [ "$1" == "0" ]; then |
| 179 |
systemctl stop crowdsec-firewall-bouncer || echo "cannot stop service" |
| 180 |
systemctl disable crowdsec-firewall-bouncer || echo "cannot disable service" |
| 181 |
fi |
| 182 |
|
| 183 |
%preun -p /bin/bash -n crowdsec-firewall-bouncer-nftables |
| 184 |
|
| 185 |
if [ "$1" == "0" ]; then |
| 186 |
systemctl stop crowdsec-firewall-bouncer || echo "cannot stop service" |
| 187 |
systemctl disable crowdsec-firewall-bouncer || echo "cannot disable service" |
| 188 |
fi |
| 189 |
|
| 190 |
|
| 191 |
%postun -p /bin/bash |
| 192 |
|
| 193 |
if [ "$1" == "1" ]; then |
| 194 |
systemctl restart crowdsec-firewall-bouncer || echo "cannot restart service" |
| 195 |
fi |
| 196 |
|
| 197 |
|
| 198 |
%postun -p /bin/bash -n crowdsec-firewall-bouncer-nftables |
| 199 |
|
| 200 |
if [ "$1" == "1" ]; then |
| 201 |
systemctl restart crowdsec-firewall-bouncer || echo "cannot restart service" |
| 202 |
fi |
| 203 |
|
| 204 |
|