Skip to content

Commit 0c14528

Browse files
authored
Merge pull request #143 from vovimayhem/story/default_pass_file
Support for reading the default password from a file (i.e. docker secret)
2 parents 07a16a2 + 6a95e08 commit 0c14528

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

alpine/docker-entrypoint.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#!/bin/bash
22
set -eu
33

4+
# usage: file_env VAR [DEFAULT]
5+
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
6+
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
7+
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
8+
file_env() {
9+
local var="$1"
10+
local fileVar="${var}_FILE"
11+
local def="${2:-}"
12+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
13+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
14+
exit 1
15+
fi
16+
local val="$def"
17+
if [ "${!var:-}" ]; then
18+
val="${!var}"
19+
elif [ "${!fileVar:-}" ]; then
20+
val="$(< "${!fileVar}")"
21+
fi
22+
export "$var"="$val"
23+
unset "$fileVar"
24+
}
25+
426
# allow the container to be started with `--user`
527
if [[ "$1" == rabbitmq* ]] && [ "$(id -u)" = '0' ]; then
628
if [ "$1" = 'rabbitmq-server' ]; then
@@ -19,6 +41,12 @@ fi
1941
: "${RABBITMQ_MANAGEMENT_SSL_CERTFILE:=$RABBITMQ_SSL_CERTFILE}"
2042
: "${RABBITMQ_MANAGEMENT_SSL_KEYFILE:=$RABBITMQ_SSL_KEYFILE}"
2143

44+
# Allowed env vars that will be read from mounted files (i.e. Docker Secrets):
45+
fileEnvKeys=(
46+
default_user
47+
default_pass
48+
)
49+
2250
# https://www.rabbitmq.com/configure.html
2351
sslConfigKeys=(
2452
cacertfile
@@ -61,6 +89,7 @@ declare -A configDefaults=(
6189
haveConfig=
6290
haveSslConfig=
6391
haveManagementSslConfig=
92+
for fileEnvKey in "${fileEnvKeys[@]}"; do file_env "RABBITMQ_${fileEnvKey^^}"; done
6493
for conf in "${allConfigKeys[@]}"; do
6594
var="RABBITMQ_${conf^^}"
6695
val="${!var:-}"

debian/docker-entrypoint.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#!/bin/bash
22
set -eu
33

4+
# usage: file_env VAR [DEFAULT]
5+
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
6+
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
7+
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
8+
file_env() {
9+
local var="$1"
10+
local fileVar="${var}_FILE"
11+
local def="${2:-}"
12+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
13+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
14+
exit 1
15+
fi
16+
local val="$def"
17+
if [ "${!var:-}" ]; then
18+
val="${!var}"
19+
elif [ "${!fileVar:-}" ]; then
20+
val="$(< "${!fileVar}")"
21+
fi
22+
export "$var"="$val"
23+
unset "$fileVar"
24+
}
25+
426
# allow the container to be started with `--user`
527
if [[ "$1" == rabbitmq* ]] && [ "$(id -u)" = '0' ]; then
628
if [ "$1" = 'rabbitmq-server' ]; then
@@ -19,6 +41,12 @@ fi
1941
: "${RABBITMQ_MANAGEMENT_SSL_CERTFILE:=$RABBITMQ_SSL_CERTFILE}"
2042
: "${RABBITMQ_MANAGEMENT_SSL_KEYFILE:=$RABBITMQ_SSL_KEYFILE}"
2143

44+
# Allowed env vars that will be read from mounted files (i.e. Docker Secrets):
45+
fileEnvKeys=(
46+
default_user
47+
default_pass
48+
)
49+
2250
# https://www.rabbitmq.com/configure.html
2351
sslConfigKeys=(
2452
cacertfile
@@ -61,6 +89,7 @@ declare -A configDefaults=(
6189
haveConfig=
6290
haveSslConfig=
6391
haveManagementSslConfig=
92+
for fileEnvKey in "${fileEnvKeys[@]}"; do file_env "RABBITMQ_${fileEnvKey^^}"; done
6493
for conf in "${allConfigKeys[@]}"; do
6594
var="RABBITMQ_${conf^^}"
6695
val="${!var:-}"

0 commit comments

Comments
 (0)