Skip to content

Commit 395a5ab

Browse files
authored
Merge pull request #27 from manelpb/env-variable
Allow env variable for fernet key
2 parents 8aeb8f2 + e845e10 commit 395a5ab

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Here are some example snippets to help you get started creating a container.
6464
docker create \
6565
--name=ldap-auth \
6666
-e TZ=Europe/London \
67+
-e FERNETKEY= `#optional` \
6768
-p 8888:8888 \
6869
-p 9000:9000 \
6970
--restart unless-stopped \
@@ -84,6 +85,7 @@ services:
8485
container_name: ldap-auth
8586
environment:
8687
- TZ=Europe/London
88+
- FERNETKEY= #optional
8789
ports:
8890
- 8888:8888
8991
- 9000:9000
@@ -99,6 +101,7 @@ Container images are configured using parameters passed at runtime (such as thos
99101
| `-p 8888` | the port for ldap auth daemon |
100102
| `-p 9000` | the port for ldap login page |
101103
| `-e TZ=Europe/London` | Specify a timezone to use EG Europe/London |
104+
| `-e FERNETKEY=` | Optionally define a custom fernet key, has to be base64-encoded 32-byte (only needed if container is frequently recreated, or if using multi-node setups, invalidating previous authentications) |
102105

103106
## Environment variables from files (Docker secrets)
104107

@@ -123,7 +126,7 @@ Keep in mind umask is not chmod it subtracts from permissions based on it's valu
123126

124127
- This container itself does not have any settings and it relies on the pertinent information passed through in http headers of incoming requests. Make sure that your webserver is set up with the right config.
125128
- Here's a sample config: [nginx-ldap-auth.conf](https://github.com/nginxinc/nginx-ldap-auth/blob/master/nginx-ldap-auth.conf).
126-
- Unlike the upstream project, this image encodes the cookie information with fernet, using a randomly generated key during container creation.
129+
- Unlike the upstream project, this image encodes the cookie information with fernet, using a randomly generated key during container creation (or optionally user defined).
127130
- Also unlike the upstream project, this image serves the login page at `/ldaplogin` (as well as `/login`) to prevent clashes with reverse proxied apps that may also use `/login` for their internal auth.
128131

129132

@@ -197,6 +200,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
197200

198201
## Versions
199202

203+
* **21.07.20:** - Add support for optional user defined fernet key.
200204
* **02.06.20:** - Rebasing to alpine 3.12, serve login page at `/ldaplogin` as well as `/login`, to prevent clashes with reverese proxied apps.
201205
* **17.05.20:** - Add support for self-signed CA certs.
202206
* **20.02.20:** - Switch to python3.

readme-vars.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,24 @@ param_ports:
2424
- { external_port: "9000", internal_port: "9000", port_desc: "the port for ldap login page" }
2525
param_usage_include_env: true
2626
param_env_vars:
27-
- { env_var: "TZ", env_value: "Europe/London", desc: "Specify a timezone to use EG Europe/London"}
27+
- { env_var: "TZ", env_value: "Europe/London", desc: "Specify a timezone to use EG Europe/London" }
28+
29+
# optional container parameters
30+
opt_param_usage_include_env: true
31+
opt_param_env_vars:
32+
- { env_var: "FERNETKEY", env_value: "", desc: "Optionally define a custom fernet key, has to be base64-encoded 32-byte (only needed if container is frequently recreated, or if using multi-node setups, invalidating previous authentications)" }
2833

2934
# application setup block
3035
app_setup_block_enabled: true
3136
app_setup_block: |
3237
- This container itself does not have any settings and it relies on the pertinent information passed through in http headers of incoming requests. Make sure that your webserver is set up with the right config.
3338
- Here's a sample config: [nginx-ldap-auth.conf](https://github.com/nginxinc/nginx-ldap-auth/blob/master/nginx-ldap-auth.conf).
34-
- Unlike the upstream project, this image encodes the cookie information with fernet, using a randomly generated key during container creation.
39+
- Unlike the upstream project, this image encodes the cookie information with fernet, using a randomly generated key during container creation (or optionally user defined).
3540
- Also unlike the upstream project, this image serves the login page at `/ldaplogin` (as well as `/login`) to prevent clashes with reverse proxied apps that may also use `/login` for their internal auth.
3641
3742
# changelog
3843
changelogs:
44+
- { date: "21.07.20:", desc: "Add support for optional user defined fernet key." }
3945
- { date: "02.06.20:", desc: "Rebasing to alpine 3.12, serve login page at `/ldaplogin` as well as `/login`, to prevent clashes with reverese proxied apps." }
4046
- { date: "17.05.20:", desc: "Add support for self-signed CA certs." }
4147
- { date: "20.02.20:", desc: "Switch to python3." }

root/etc/cont-init.d/30-config

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
# generate fernet key for ldap if it doesn't exist
44
if grep -q 'REPLACEWITHFERNETKEY' /app/ldap-backend-app.py; then
5-
FERNETKEY=$(python3 /app/fernet-key.py)
6-
sed -i "s/REPLACEWITHFERNETKEY/${FERNETKEY}/" /app/ldap-backend-app.py
7-
sed -i "s/REPLACEWITHFERNETKEY/${FERNETKEY}/" /app/nginx-ldap-auth-daemon.py
8-
echo "generated fernet key"
5+
if [ -z "${FERNETKEY}" ]; then
6+
KEY=$(python3 /app/fernet-key.py)
7+
echo "generated fernet key"
8+
elif ! python3 -c "from cryptography.fernet import Fernet; Fernet(b'${FERNETKEY}').encrypt(b'my deep dark secret')" 2>/dev/null; then
9+
echo "FERNETKEY env var is not set to a base64 encoded 32-byte key"
10+
KEY=$(python3 /app/fernet-key.py)
11+
echo "generated fernet key"
12+
else
13+
KEY="b'${FERNETKEY}'"
14+
echo "using FERNETKEY from env variable"
15+
fi
16+
17+
sed -i "s/REPLACEWITHFERNETKEY/${KEY}/" /app/ldap-backend-app.py
18+
sed -i "s/REPLACEWITHFERNETKEY/${KEY}/" /app/nginx-ldap-auth-daemon.py
919
fi

0 commit comments

Comments
 (0)