Skip to content
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

Issue #329: Initialization of a user defined database, username, and … #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions 3.4/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ _dbPath() {
if [ "$originalArgOne" = 'mongod' ]; then
file_env 'MONGO_INITDB_ROOT_USERNAME'
file_env 'MONGO_INITDB_ROOT_PASSWORD'
file_env 'MONGO_NON_ROOT_USERNAME'
file_env 'MONGO_NON_ROOT_PASSWORD'
# pre-check a few factors to see if it's even worth bothering with initdb
shouldPerformInitdb=
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
Expand Down Expand Up @@ -207,6 +209,11 @@ if [ "$originalArgOne" = 'mongod' ]; then
;;
esac
done

# we should also initdb if we have set a non-root username and password
if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
shouldPerformInitdb="$f"
fi
fi

# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
Expand Down Expand Up @@ -300,8 +307,28 @@ if [ "$originalArgOne" = 'mongod' ]; then
EOJS
fi

# we use the test database if one was not set and this will be used
# if we have specified scripts or a non-root username and password
export MONGO_INITDB_DATABASE="${MONGO_INITDB_DATABASE:-test}"

# we set a default non-root role of readWrite if one is not supplied
export MONGO_NON_ROOT_ROLE="${MONGO_NON_ROOT_ROLE:-readWrite}"

if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
"${mongo[@]}" "$MONGO_INITDB_DATABASE" <<-EOJS
db.createUser({
user: $(_js_escape "$MONGO_NON_ROOT_USERNAME"),
pwd: $(_js_escape "$MONGO_NON_ROOT_PASSWORD"),
roles: [ { role: $(_js_escape "$MONGO_NON_ROOT_ROLE"), db: $(_js_escape "$MONGO_INITDB_DATABASE") } ]
})
EOJS

# we don't need an else here because we checked for a non-root
# username and password above and set shouldPerformInitdb so we
# are already under the condition where they are set
# or /docker-entrypoint-initdb.d/* scripts have been found
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
Expand Down
27 changes: 27 additions & 0 deletions 3.6/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ _dbPath() {
if [ "$originalArgOne" = 'mongod' ]; then
file_env 'MONGO_INITDB_ROOT_USERNAME'
file_env 'MONGO_INITDB_ROOT_PASSWORD'
file_env 'MONGO_NON_ROOT_USERNAME'
file_env 'MONGO_NON_ROOT_PASSWORD'
# pre-check a few factors to see if it's even worth bothering with initdb
shouldPerformInitdb=
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
Expand Down Expand Up @@ -207,6 +209,11 @@ if [ "$originalArgOne" = 'mongod' ]; then
;;
esac
done

# we should also initdb if we have set a non-root username and password
if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
shouldPerformInitdb="$f"
fi
fi

# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
Expand Down Expand Up @@ -300,8 +307,28 @@ if [ "$originalArgOne" = 'mongod' ]; then
EOJS
fi

# we use the test database if one was not set and this will be used
# if we have specified scripts or a non-root username and password
export MONGO_INITDB_DATABASE="${MONGO_INITDB_DATABASE:-test}"

# we set a default non-root role of readWrite if one is not supplied
export MONGO_NON_ROOT_ROLE="${MONGO_NON_ROOT_ROLE:-readWrite}"

if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
"${mongo[@]}" "$MONGO_INITDB_DATABASE" <<-EOJS
db.createUser({
user: $(_js_escape "$MONGO_NON_ROOT_USERNAME"),
pwd: $(_js_escape "$MONGO_NON_ROOT_PASSWORD"),
roles: [ { role: $(_js_escape "$MONGO_NON_ROOT_ROLE"), db: $(_js_escape "$MONGO_INITDB_DATABASE") } ]
})
EOJS

# we don't need an else here because we checked for a non-root
# username and password above and set shouldPerformInitdb so we
# are already under the condition where they are set
# or /docker-entrypoint-initdb.d/* scripts have been found
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
Expand Down
27 changes: 27 additions & 0 deletions 4.0/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ _dbPath() {
if [ "$originalArgOne" = 'mongod' ]; then
file_env 'MONGO_INITDB_ROOT_USERNAME'
file_env 'MONGO_INITDB_ROOT_PASSWORD'
file_env 'MONGO_NON_ROOT_USERNAME'
file_env 'MONGO_NON_ROOT_PASSWORD'
# pre-check a few factors to see if it's even worth bothering with initdb
shouldPerformInitdb=
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
Expand Down Expand Up @@ -207,6 +209,11 @@ if [ "$originalArgOne" = 'mongod' ]; then
;;
esac
done

# we should also initdb if we have set a non-root username and password
if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
shouldPerformInitdb="$f"
fi
fi

# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
Expand Down Expand Up @@ -300,8 +307,28 @@ if [ "$originalArgOne" = 'mongod' ]; then
EOJS
fi

# we use the test database if one was not set and this will be used
# if we have specified scripts or a non-root username and password
export MONGO_INITDB_DATABASE="${MONGO_INITDB_DATABASE:-test}"

# we set a default non-root role of readWrite if one is not supplied
export MONGO_NON_ROOT_ROLE="${MONGO_NON_ROOT_ROLE:-readWrite}"

if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
"${mongo[@]}" "$MONGO_INITDB_DATABASE" <<-EOJS
db.createUser({
user: $(_js_escape "$MONGO_NON_ROOT_USERNAME"),
pwd: $(_js_escape "$MONGO_NON_ROOT_PASSWORD"),
roles: [ { role: $(_js_escape "$MONGO_NON_ROOT_ROLE"), db: $(_js_escape "$MONGO_INITDB_DATABASE") } ]
})
EOJS

# we don't need an else here because we checked for a non-root
# username and password above and set shouldPerformInitdb so we
# are already under the condition where they are set
# or /docker-entrypoint-initdb.d/* scripts have been found
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
Expand Down
27 changes: 27 additions & 0 deletions 4.1/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ _dbPath() {
if [ "$originalArgOne" = 'mongod' ]; then
file_env 'MONGO_INITDB_ROOT_USERNAME'
file_env 'MONGO_INITDB_ROOT_PASSWORD'
file_env 'MONGO_NON_ROOT_USERNAME'
file_env 'MONGO_NON_ROOT_PASSWORD'
# pre-check a few factors to see if it's even worth bothering with initdb
shouldPerformInitdb=
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
Expand Down Expand Up @@ -207,6 +209,11 @@ if [ "$originalArgOne" = 'mongod' ]; then
;;
esac
done

# we should also initdb if we have set a non-root username and password
if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
shouldPerformInitdb="$f"
fi
fi

# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
Expand Down Expand Up @@ -300,8 +307,28 @@ if [ "$originalArgOne" = 'mongod' ]; then
EOJS
fi

# we use the test database if one was not set and this will be used
# if we have specified scripts or a non-root username and password
export MONGO_INITDB_DATABASE="${MONGO_INITDB_DATABASE:-test}"

# we set a default non-root role of readWrite if one is not supplied
export MONGO_NON_ROOT_ROLE="${MONGO_NON_ROOT_ROLE:-readWrite}"

if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
"${mongo[@]}" "$MONGO_INITDB_DATABASE" <<-EOJS
db.createUser({
user: $(_js_escape "$MONGO_NON_ROOT_USERNAME"),
pwd: $(_js_escape "$MONGO_NON_ROOT_PASSWORD"),
roles: [ { role: $(_js_escape "$MONGO_NON_ROOT_ROLE"), db: $(_js_escape "$MONGO_INITDB_DATABASE") } ]
})
EOJS

# we don't need an else here because we checked for a non-root
# username and password above and set shouldPerformInitdb so we
# are already under the condition where they are set
# or /docker-entrypoint-initdb.d/* scripts have been found
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
Expand Down
31 changes: 29 additions & 2 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ _dbPath() {
if [ "$originalArgOne" = 'mongod' ]; then
file_env 'MONGO_INITDB_ROOT_USERNAME'
file_env 'MONGO_INITDB_ROOT_PASSWORD'
file_env 'MONGO_NON_ROOT_USERNAME'
file_env 'MONGO_NON_ROOT_PASSWORD'
# pre-check a few factors to see if it's even worth bothering with initdb
shouldPerformInitdb=
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
Expand Down Expand Up @@ -207,6 +209,11 @@ if [ "$originalArgOne" = 'mongod' ]; then
;;
esac
done

# we should also initdb if we have set a non-root username and password
if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
shouldPerformInitdb="$f"
fi
fi

# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
Expand Down Expand Up @@ -288,6 +295,10 @@ if [ "$originalArgOne" = 'mongod' ]; then
sleep 1
done

# we use the test database if one was not set and this will be used
# if we have specified scripts or a non-root username and password
export MONGO_INITDB_DATABASE="${MONGO_INITDB_DATABASE:-test}"

if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
rootAuthDatabase='admin'

Expand All @@ -298,9 +309,25 @@ if [ "$originalArgOne" = 'mongod' ]; then
roles: [ { role: 'root', db: $(_js_escape "$rootAuthDatabase") } ]
})
EOJS
fi

export MONGO_INITDB_DATABASE="${MONGO_INITDB_DATABASE:-test}"
if [ -n "${MONGO_NON_ROOT_USERNAME:-}" ] && [ -n "${MONGO_NON_ROOT_PASSWORD:-}" ]; then
# we set a default non-root role of readWrite if one is not supplied
export MONGO_NON_ROOT_ROLE="${MONGO_NON_ROOT_ROLE:-readWrite}"

"${mongo[@]}" "$MONGO_INITDB_DATABASE" <<-EOJS
db.createUser({
user: $(_js_escape "$MONGO_NON_ROOT_USERNAME"),
pwd: $(_js_escape "$MONGO_NON_ROOT_PASSWORD"),
roles: [ { role: $(_js_escape "$MONGO_NON_ROOT_ROLE"), db: $(_js_escape "$MONGO_INITDB_DATABASE") } ]
})
EOJS

# we don't need an else here because we checked for a non-root
# username and password above and set shouldPerformInitdb so we
# are already under the condition where they are set
# or /docker-entrypoint-initdb.d/* scripts have been found
fi
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
Expand Down