Skip to content

Commit 9e31ed4

Browse files
jifeonmaZahaca
authored andcommitted
ability to set several users for basic auth using env vars (#1)
redundant .gitignore removed nginx configs reduced to server block only docker-compose added as example of usage
1 parent 624df5c commit 9e31ed4

File tree

7 files changed

+42
-77
lines changed

7 files changed

+42
-77
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FROM nginx:1.11.10
22

3-
COPY nginx.conf /etc/nginx/nginx.conf
4-
COPY bin/* /usr/local/bin/
3+
COPY default.conf /etc/nginx/conf.d/default.conf
4+
COPY bin/entry.sh /usr/local/bin/
55

66
RUN chmod 744 /usr/local/bin/entry.sh && \
77
chown root:root /usr/local/bin/entry.sh
88

9-
CMD ["/usr/local/bin/entry.sh"]
9+
CMD /usr/local/bin/entry.sh

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# nginx-basic-auth
2-
A simple nginx template for challenging basic auth
2+
3+
A simple nginx template for challenging basic auth. See docker-compose.yml for example of usage.

bin/entry.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#!/bin/bash
22

3-
# Providing defaults values for missing env variables
4-
[ "$DEFAULT_USER" = "" ] && export DEFAULT_USER="admin"
5-
[ "$DEFAULT_PASSWORD" = "" ] && export DEFAULT_PASSWORD="$(openssl rand -base64 12)"
3+
touch /htpasswd
64

7-
printf "$DEFAULT_USER:$(openssl passwd -crypt "${DEFAULT_PASSWORD}")\n" > /htpasswd
5+
i=0
6+
user=${USER_0}
7+
password=${PASSWORD_0}
88

9-
echo "=====[ Nginx Basic Auth ]============================================"
10-
echo "Generated default user"
11-
echo "Login: $DEFAULT_USER"
12-
echo "Password: $DEFAULT_PASSWORD"
13-
echo "=========================================================================="
9+
while [ "$user" ]; do
10+
printf "$user:$(openssl passwd -crypt "$password")\n" >> /htpasswd
11+
12+
let "i += 1"
13+
user_var_name="USER_$i"
14+
user=${!user_var_name}
15+
password_var_name="PASSWORD_$i"
16+
password=${!password_var_name}
17+
done
1418

1519
nginx -g "daemon off;"

default.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server {
2+
listen 80;
3+
4+
location / {
5+
auth_basic "Are you authorized to be there?";
6+
auth_basic_user_file /htpasswd;
7+
8+
try_files DUMMY @return200;
9+
}
10+
11+
location @return200 {
12+
add_header Content-Type text/plain;
13+
return 200 'Welcome';
14+
}
15+
}

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
nginx-basic-auth:
2+
build: .
3+
ports:
4+
- 80:80
5+
environment:
6+
USER_0: user0
7+
PASSWORD_0: test0
8+
USER_1: user1
9+
PASSWORD_1: test1

nginx.conf

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)