From 969ebefef3302a98a5030a06a44c0a02481b20db Mon Sep 17 00:00:00 2001 From: Pavol Vargovcik Date: Mon, 18 Jun 2018 14:13:10 +0200 Subject: [PATCH 1/2] feature: generate htpasswd file from raw credentials --- Dockerfile | 2 +- launch.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 20f86e7..b67c326 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ ENV HTPASSWD='foo:$apr1$odHl5EJN$KbxMfo86Qdve2FH4owePn.' \ WORKDIR /opt -RUN apk add --no-cache gettext +RUN apk add --no-cache gettext apache2-utils COPY auth.conf auth.htpasswd launch.sh ./ diff --git a/launch.sh b/launch.sh index f5aba9e..1ba65b5 100755 --- a/launch.sh +++ b/launch.sh @@ -1,5 +1,15 @@ #!/bin/sh +if [ "$RAW_CREDENTIALS" = 1 ]; then + HTPASSWD=$( + for line in $(echo $HTPASSWD); do + USERNAME="$(echo "$line" | cut -d':' -f1)" + PASSWORD="$(echo "$line" | cut -d':' -f2)" + htpasswd -nb "$USERNAME" "$PASSWORD" | head -n1 + done + ) +fi + rm /etc/nginx/conf.d/default.conf || : envsubst < auth.conf > /etc/nginx/conf.d/auth.conf envsubst < auth.htpasswd > /etc/nginx/auth.htpasswd From 160504be64acbbc1e98993444c4d0669e8098d5c Mon Sep 17 00:00:00 2001 From: Pavol Vargovcik Date: Mon, 18 Jun 2018 14:21:34 +0200 Subject: [PATCH 2/2] add note about raw credentials to README --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 4d98fb0..3971a28 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,20 @@ docker run -d --link web:web --name auth \ ``` results in 2 users (`foo:bar` and `test:test`). +## Raw Credentials +If passing the contents of the HTPASSWD file is not convenient for you (because +you need to perform additional step of generating it via `htpasswd -nb foo +bar`), you can pass the credentials in a raw form and the contents of HTPASSWD +variable will be generated for you. The `RAW_CREDENTIALS=1` must be set to +enable this feature. + +``` +docker run -d --link web:web --name auth \ + -e HTPASSWD=$'foo:bar\ntest:test' \ + -e RAW_CREDENTIALS=1 \ + beevelop/nginx-basic-auth +``` + ## Troubleshooting ``` nginx: [emerg] host not found in upstream "web" in /etc/nginx/conf.d/auth.conf:80