Skip to content

Commit 5c337fa

Browse files
authored
Merge pull request #411 from linuxserver/swag-crowdsec-v3
2 parents b994953 + dc03c94 commit 5c337fa

File tree

8 files changed

+106
-6
lines changed

8 files changed

+106
-6
lines changed

root/etc/cont-init.d/98-crowdsec

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,18 @@ tar xf \
3535
/tmp/crowdsec.tar.gz -C \
3636
/tmp/crowdsec --strip-components=1
3737

38-
# Inject API keys into config file
3938
mkdir -p "${CONFIG_PATH}"
40-
API_KEY=${CROWDSEC_API_KEY} CROWDSEC_LAPI_URL=${CROWDSEC_LAPI_URL} envsubst < /tmp/crowdsec/lua-mod/config_example.conf > "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
39+
if [ ! -f "${CONFIG_PATH}crowdsec-nginx-bouncer.conf" ]; then \
40+
cp /tmp/crowdsec/lua-mod/config_example.conf "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
41+
fi
42+
43+
# Inject API keys into config file
44+
sed -i -r "s|API_KEY=.*$|API_KEY=${CROWDSEC_API_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
45+
sed -i -r "s|API_URL=.*$|API_URL=${CROWDSEC_LAPI_URL}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
46+
47+
# Sed in ReCaptcha keys
48+
sed -i -r "s|SECRET_KEY=.*$|SECRET_KEY=${CROWDSEC_SECRET_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
49+
sed -i -r "s|SITE_KEY=.*$|SITE_KEY=${CROWDSEC_SITE_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
4150

4251
# Change config path
4352
sed -i "s|/etc/crowdsec/bouncers/|${CONFIG_PATH}|" /tmp/crowdsec/nginx/crowdsec_nginx.conf
@@ -51,10 +60,6 @@ cp -r /tmp/crowdsec/lua-mod/lib/* ${LIB_PATH}
5160

5261
cp /tmp/crowdsec/nginx/crowdsec_nginx.conf /etc/nginx/http.d
5362

54-
# Sed in ReCaptcha keys
55-
sed -i -r "s|SECRET_KEY=.*$|SECRET_KEY=${CROWDSEC_SECRET_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
56-
sed -i -r "s|SITE_KEY=.*$|SITE_KEY=${CROWDSEC_SITE_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
57-
5863
# Sed in crowdsec include
5964
if ! grep -q '[^#]include /etc/nginx/http.d/\*.conf;' '/config/nginx/nginx.conf' && ! grep -q '[^#]include /etc/nginx/conf.d/\*.conf;' '/config/nginx/nginx.conf'; then
6065
if grep -q '#include /etc/nginx/http.d/\*.conf;' '/config/nginx/nginx.conf'; then

root/etc/s6-overlay/s6-rc.d/init-mod-swag-crowdsec/dependencies.d/init-mods

Whitespace-only changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/with-contenv bash
2+
3+
CONFIG_PATH="/config/crowdsec/"
4+
LIB_PATH="/usr/local/lua/crowdsec/"
5+
DATA_PATH="/var/lib/crowdsec/lua/"
6+
7+
echo "**** Configuring CrowdSec nginx Bouncer ****"
8+
9+
# If API keys are missing, disable mod and exit
10+
if [[ -z $CROWDSEC_API_KEY ]] || [[ -z $CROWDSEC_LAPI_URL ]]; then
11+
echo "**** Missing API key or CrowdSec LAPI URL, cannot configure bouncer ****"
12+
exit 1
13+
fi
14+
15+
echo "\
16+
gettext \
17+
lua5.1 \
18+
lua5.1-cjson \
19+
lua-resty-http \
20+
lua-sec \
21+
nginx-mod-http-lua" >> /mod-repo-packages-to-install.list
22+
23+
# Download nginx bouncer
24+
if [ -z ${CROWDSEC_VERSION+x} ]; then \
25+
CROWDSEC_VERSION=$(curl -sX GET "https://api.github.com/repos/crowdsecurity/cs-nginx-bouncer/releases/latest" | awk '/tag_name/{print $4;exit}' FS='[""]');
26+
fi
27+
28+
curl -so \
29+
/tmp/crowdsec.tar.gz -L \
30+
"https://github.com/crowdsecurity/cs-nginx-bouncer/releases/download/${CROWDSEC_VERSION}/crowdsec-nginx-bouncer.tgz"
31+
32+
mkdir -p /tmp/crowdsec
33+
34+
tar xf \
35+
/tmp/crowdsec.tar.gz -C \
36+
/tmp/crowdsec --strip-components=1
37+
38+
mkdir -p "${CONFIG_PATH}"
39+
if [ ! -f "${CONFIG_PATH}crowdsec-nginx-bouncer.conf" ]; then \
40+
cp /tmp/crowdsec/lua-mod/config_example.conf "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
41+
fi
42+
43+
# Inject API keys into config file
44+
sed -i -r "s|API_KEY=.*$|API_KEY=${CROWDSEC_API_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
45+
sed -i -r "s|API_URL=.*$|API_URL=${CROWDSEC_LAPI_URL}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
46+
47+
# Sed in ReCaptcha keys
48+
sed -i -r "s|SECRET_KEY=.*$|SECRET_KEY=${CROWDSEC_SECRET_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
49+
sed -i -r "s|SITE_KEY=.*$|SITE_KEY=${CROWDSEC_SITE_KEY}|" "${CONFIG_PATH}crowdsec-nginx-bouncer.conf"
50+
51+
# Change config path
52+
sed -i "s|/etc/crowdsec/bouncers/|${CONFIG_PATH}|" /tmp/crowdsec/nginx/crowdsec_nginx.conf
53+
54+
# Copy files
55+
mkdir -p ${DATA_PATH}/templates/
56+
cp -r /tmp/crowdsec/lua-mod/templates/* ${DATA_PATH}/templates/
57+
58+
mkdir -p ${LIB_PATH}plugins/crowdsec
59+
cp -r /tmp/crowdsec/lua-mod/lib/* ${LIB_PATH}
60+
61+
cp /tmp/crowdsec/nginx/crowdsec_nginx.conf /etc/nginx/http.d
62+
63+
# Sed in crowdsec include
64+
if ! grep -q '[^#]include /etc/nginx/http.d/\*.conf;' '/config/nginx/nginx.conf' && ! grep -q '[^#]include /etc/nginx/conf.d/\*.conf;' '/config/nginx/nginx.conf'; then
65+
if grep -q '#include /etc/nginx/http.d/\*.conf;' '/config/nginx/nginx.conf'; then
66+
# Enable http.d include
67+
sed -i 's|#include /etc/nginx/http.d/\*.conf;|include /etc/nginx/http.d/\*.conf;|' /config/nginx/nginx.conf
68+
else
69+
# Warn about missing http.d include
70+
echo "
71+
********************************************************************
72+
* Warning: Your nginx.conf is missing required settings *
73+
* Please add: *
74+
* include /etc/nginx/http.d/*.conf; *
75+
* to the http{} block and restart the container. *
76+
* *
77+
* The CrowdSec bouncer will not function until this is done. *
78+
********************************************************************"
79+
fi
80+
fi
81+
82+
# Clean up
83+
rm -rf \
84+
/tmp/crowdsec \
85+
/tmp/crowdsec.tar.gz
86+
87+
# Disable f2b if requested
88+
if [[ $CROWDSEC_F2B_DISABLE == "true" ]]; then
89+
echo "**** Disabling fail2ban Service ****"
90+
touch /etc/services.d/fail2ban/down
91+
fi
92+
93+
echo "**** Successfully configured CrowdSec nginx Bouncer ${CROWDSEC_VERSION} ****"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oneshot
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/etc/s6-overlay/s6-rc.d/init-mod-swag-crowdsec/run

root/etc/s6-overlay/s6-rc.d/init-mods-end/dependencies.d/init-mod-swag-crowdsec

Whitespace-only changes.

root/etc/s6-overlay/s6-rc.d/init-mods-package-install/dependencies.d/init-mod-swag-crowdsec

Whitespace-only changes.

root/etc/s6-overlay/s6-rc.d/user/contents.d/init-mod-swag-crowdsec

Whitespace-only changes.

0 commit comments

Comments
 (0)