Skip to content

Crowdsec Openresty Bouncer Support #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9fc041d
Update Readme with instructions how to use openresty crowdsec bouncer.
LePresidente Feb 18, 2022
f27f0de
use table for tags.
LePresidente Feb 18, 2022
a1755cc
remove version from first line.
LePresidente Feb 18, 2022
6fc43c5
Added known issues.
LePresidente Feb 18, 2022
331ffc9
Fixed formatting issue.
LePresidente Feb 18, 2022
a928ee6
Be clear which docker image this is a drop in replacement for.
LePresidente Feb 19, 2022
efb1281
Merge branch 'jlesage:master' into master
LePresidente Mar 8, 2022
3c26b2e
Updated instructions.
LePresidente Mar 8, 2022
3a31d95
Version bump in ReadMe file.
LePresidente Mar 31, 2022
fcba148
Merge branch 'jlesage:master' into master
LePresidente Nov 10, 2022
bf8c7c9
Merge branch 'jlesage:master' into master
LePresidente Mar 20, 2023
536f08e
Merge branch 'jlesage:master' into master
LePresidente Apr 25, 2023
0843ec2
Merge branch 'jlesage:master' into master
LePresidente May 17, 2023
1b44373
Merge branch 'jlesage:master' into master
LePresidente Jun 23, 2023
13b660d
Added Crowdsec Bouncer 1.0.1.
LePresidente Jan 25, 2024
8a06521
Updated Crowdsec Bouncer to 1.0.2
LePresidente Feb 10, 2024
5ab02e9
Merge branch 'jlesage:master' into master
LePresidente Jul 24, 2024
61fb0e5
Merge branch 'master' of https://github.com/LePresidente/docker-nginx…
LePresidente Jul 24, 2024
79d50af
Add support for environment variables.
LePresidente Jul 24, 2024
a16ccf4
Use /bin/bash instead of /bin/sh
LePresidente Jul 24, 2024
6c055e2
Merge branch 'jlesage:master' into crowdsec_rework
LePresidente Oct 22, 2024
f7243de
Merge branch 'master' of https://github.com/jlesage/docker-nginx-prox…
LePresidente Dec 28, 2024
f56abc7
Updated crowdsec bouncer to 1.0.5
LePresidente Dec 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ ARG NGINX_PROXY_MANAGER_VERSION=2.12.1
ARG NGINX_HTTP_GEOIP2_MODULE_VERSION=3.3
ARG LIBMAXMINDDB_VERSION=1.5.0
ARG BCRYPT_TOOL_VERSION=1.1.2
ARG CROWDSEC_OPENRESTY_BOUNCER_VERSION=1.0.5

# Define software download URLs.
ARG OPENRESTY_URL=https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz
ARG CROWDSEC_OPENRESTY_BOUNCER_URL=https://github.com/crowdsecurity/cs-openresty-bouncer/releases/download/v${CROWDSEC_OPENRESTY_BOUNCER_VERSION}/crowdsec-openresty-bouncer.tgz
ARG NGINX_PROXY_MANAGER_URL=https://github.com/jc21/nginx-proxy-manager/archive/v${NGINX_PROXY_MANAGER_VERSION}.tar.gz
ARG NGINX_HTTP_GEOIP2_MODULE_URL=https://github.com/leev/ngx_http_geoip2_module/archive/${NGINX_HTTP_GEOIP2_MODULE_VERSION}.tar.gz
ARG LIBMAXMINDDB_URL=https://github.com/maxmind/libmaxminddb/releases/download/${LIBMAXMINDDB_VERSION}/libmaxminddb-${LIBMAXMINDDB_VERSION}.tar.gz
Expand Down Expand Up @@ -78,6 +80,14 @@ RUN \
find /tmp/certbot-install/usr/lib/python3.10/site-packages -type f -name "*.exe" -delete && \
find /tmp/certbot-install/usr/lib/python3.10/site-packages -type d -name tests -print0 | xargs -0 rm -r

# Build cs-openresty-boucner.
FROM alpine:3.16 AS cs-openresty-bouncer
ARG TARGETPLATFORM
ARG CROWDSEC_OPENRESTY_BOUNCER_URL
COPY --from=xx / /
COPY src/cs-openresty-bouncer /build
RUN /build/build.sh "$CROWDSEC_OPENRESTY_BOUNCER_URL"

# Pull base image.
FROM jlesage/baseimage:alpine-3.16-v3.6.4

Expand Down Expand Up @@ -117,6 +127,7 @@ COPY --from=nginx /tmp/openresty-install/ /
COPY --from=npm /tmp/nginx-proxy-manager-install/ /
COPY --from=bcrypt-tool /tmp/go/bin/bcrypt-tool /usr/bin/
COPY --from=certbot /tmp/certbot-install/ /
COPY --from=cs-openresty-bouncer /tmp/crowdsec-openresty-bouncer-install/ /

# Set internal environment variables.
RUN \
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
## Note

This is a drop in replacement for [jlesage/nginx-proxy-manager](https://hub.docker.com/r/jlesage/nginx-proxy-manager)

This fork includes the [OpenResty Crowdsec Bouncer](https://github.com/crowdsecurity/cs-openresty-bouncer)

Please see the [crowdsec_support](https://github.com/LePresidente/docker-nginx-proxy-manager/tree/crowdsec_support) branch for the changes as

Docker images hosted on dockerhub.

https://hub.docker.com/r/lepresidente/nginx-proxy-manager

| TAG | cs-openresty-bouncer version|
|-----------|-----------------------------|
| latest | 0.1.10 (PreRelease) |


Instructions to use:
Starting the container at this point will start Nginx-Proxy-Manager as before but will create a new file in /config/crowdsec/ called crowdsec-openresty-bouncer.conf

You will need to edit this file with at least the following changes then restart the container.

```
ENABLED=true
API_URL=http://<crowdsecserver>:8080
API_KEY=<APIKEY>
```

the crowdsec api key can be generated on the crowdsec instance using the following command

```
cscli bouncers add npm-proxy
```

Currently this is a side project and I will try keep this up to date

# Docker container for Nginx Proxy Manager
[![Release](https://img.shields.io/github/release/jlesage/docker-nginx-proxy-manager.svg?logo=github&style=for-the-badge)](https://github.com/jlesage/docker-nginx-proxy-manager/releases/latest)
[![Docker Image Size](https://img.shields.io/docker/image-size/jlesage/nginx-proxy-manager/latest?logo=docker&style=for-the-badge)](https://hub.docker.com/r/jlesage/nginx-proxy-manager/tags)
Expand Down
54 changes: 54 additions & 0 deletions rootfs/etc/cont-init.d/99_crowdsec-openresty-bouncer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

set -e # Exit immediately if a command exits with a non-zero status.

set_properties() {
sed -i "s,^$1=.*,$1=$2,g" "${3}"
}

echo "Deploy Crowdsec Openresty Bouncer.."
if [ -n "${CROWDSEC_OPENRESTY_BOUNCER}" ]; then
while IFS= read -r line
do
if ! [[ "$line" != "^#" ]] || [[ "$line" != "^\n" ]]; then
name=$(echo "$line" | cut -d "=" -f1)
value=$(echo "$line" | cut -d "=" -f2)
if grep -q "${name}" /defaults/crowdsec/crowdsec-openresty-bouncer.conf ; then
set_properties "${name}" "${value}" "/defaults/crowdsec/crowdsec-openresty-bouncer.conf"
fi
fi
done <<< "${CROWDSEC_OPENRESTY_BOUNCER}"
else
mkdir -p /config/crowdsec/templates
sed -i 's|/defaults/crowdsec|/config/crowdsec|' /etc/nginx/conf.d/crowdsec_openresty.conf

if [ -f /data/crowdsec/crowdsec-openresty-bouncer.conf ]; then
echo "Patch crowdsec-openresty-bouncer.conf .."
sed "s/=.*//g" /config/crowdsec/crowdsec-openresty-bouncer.conf > /tmp/crowdsec.conf.raw
sed "s/=.*//g" /defaults/crowdsec/crowdsec-openresty-bouncer.conf > /tmp/crowdsec-openresty-bouncer.conf.raw
if grep -vf /tmp/crowdsec.conf.raw /tmp/crowdsec-openresty-bouncer.conf.raw ; then
grep -vf /tmp/crowdsec.conf.raw /tmp/crowdsec-openresty-bouncer.conf.raw > /tmp/config.newvals
cp /config/crowdsec/crowdsec-openresty-bouncer.conf /config/crowdsec/crowdsec-openresty-bouncer.conf.bak
grep -f /tmp/config.newvals /defaults/crowdsec/crowdsec-openresty-bouncer.conf >> /config/crowdsec/crowdsec-openresty-bouncer.conf
fi
else
echo "Deploy new crowdsec-openresty-bouncer.conf .."
cp /defaults/crowdsec/crowdsec-openresty-bouncer.conf /config/crowdsec/crowdsec-openresty-bouncer.conf
fi
#Make sure the config location is where we get the config from instead of /default/
sed -i 's|/defaults/crowdsec|/data/crowdsec|' /data/crowdsec/crowdsec-openresty-bouncer.conf
fi


#Make sure we only copy files that don't exist in /data/crowdsec.
if [ -d "/data/crowdsec/templates" ]; then
echo "Deploy Crowdsec Templates .."
cd /defaults/crowdsec/templates/
for file in *.html
do
if [ ! -e "/config/crowdsec/templates/${file}" ]
then
cp -r "/defaults/crowdsec/templates/${file}" "/config/crowdsec/templates/"
fi
done
fi
40 changes: 40 additions & 0 deletions src/cs-openresty-bouncer/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

set -e # Exit immediately if a command exits with a non-zero status.
set -u # Treat unset variables as an error.

log() {
echo ">>> $*"
}

CROWDSEC_OPENRESTY_BOUNCER_URL="${1:-}"

ROOTFS=/tmp/crowdsec-openresty-bouncer-install

if [ -z "$CROWDSEC_OPENRESTY_BOUNCER_URL" ]; then
log "ERROR: bcrypt tool version missing."
exit 1
fi

#
# Install required packages.
#

apk --no-cache add \
build-base \
gettext \
curl \
bash \

#
# Build.
#

log "Downloading Crowdsec Openresty Bouncer package..."
mkdir /tmp/crowdsec-openresty-bouncer
curl -# -L "${CROWDSEC_OPENRESTY_BOUNCER_URL}" | tar xz --strip 1 -C /tmp/crowdsec-openresty-bouncer
log "Deploy Crowdsec Openresty Bouncer..."
cd /tmp/crowdsec-openresty-bouncer
bash ./install.sh --NGINX_CONF_DIR=${ROOTFS}/etc/nginx/conf.d --LIB_PATH=${ROOTFS}/var/lib/nginx/lualib --CONFIG_PATH=${ROOTFS}/defaults/crowdsec/ --DATA_PATH=${ROOTFS}/defaults/crowdsec/ --SSL_CERTS_PATH=/etc/ssl/certs/ca-cert-GTS_Root_R1.pem --docker
sed -i 's|/tmp/crowdsec-openresty-bouncer-install||g' ${ROOTFS}/etc/nginx/conf.d/crowdsec_openresty.conf
sed -i 's|ENABLED=.*|ENABLED=false|' ${ROOTFS}/defaults/crowdsec/crowdsec-openresty-bouncer.conf
4 changes: 3 additions & 1 deletion src/openresty/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ make -C /tmp/openresty -j$(nproc)
log "Installing OpenResty..."
make DESTDIR=/tmp/openresty-install -C /tmp/openresty install

#Install lua-resty-http required for Crowdsec OpenResty Bouncer
/tmp/openresty-install/var/lib/nginx/bin/opm --install-dir="/tmp/openresty-install/var/lib/nginx/site/" get pintsized/lua-resty-http

rm -r \
/tmp/openresty-install/etc/nginx/*.default \
/tmp/openresty-install/var/lib/nginx/bin/opm \
Expand All @@ -189,5 +192,4 @@ rm -r \
/tmp/openresty-install/var/lib/nginx/bin/md2pod.pl \
/tmp/openresty-install/var/lib/nginx/pod \
/tmp/openresty-install/var/lib/nginx/resty.index \
/tmp/openresty-install/var/lib/nginx/site \
/tmp/openresty-install/var/run \