|
| 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} ****" |
0 commit comments