Skip to content

Commit

Permalink
Enabling the configuration of the hostname during initialization. Don…
Browse files Browse the repository at this point in the history
…e via the environment variable MONGODB_LOCALHOST_NAME

Signed-off-by: Douglas Thomson <djt210@gmail.com>
  • Loading branch information
dtrts committed May 17, 2023
1 parent 406a099 commit 3171d7a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ mongodb_create_user() {
[[ -z "$database" ]] && query="db.getSiblingDB(db.stats().db).createUser({ user: '$user', pwd: '$password', roles: [{role: 'readWrite', db: db.getSiblingDB(db.stats().db).stats().db }] })"
# Create user, discarding mongo CLI output for clean logs
info "Creating user '$user'..."
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "127.0.0.1" <<<"$query"
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "$MONGODB_LOCALHOST_NAME" <<<"$query"
}

########################
Expand All @@ -684,7 +684,7 @@ mongodb_create_users() {

if [[ -n "$MONGODB_ROOT_PASSWORD" ]] && ! [[ "$MONGODB_REPLICA_SET_MODE" =~ ^(secondary|arbiter|hidden) ]]; then
info "Creating $MONGODB_ROOT_USER user..."
mongodb_execute "" "" "" "127.0.0.1" <<EOF
mongodb_execute "" "" "" "$MONGODB_LOCALHOST_NAME" <<EOF
db.getSiblingDB('admin').createUser({ user: '$MONGODB_ROOT_USER', pwd: '$MONGODB_ROOT_PASSWORD', roles: [{role: 'root', db: 'admin'}] })
EOF
fi
Expand Down Expand Up @@ -714,7 +714,7 @@ EOF

if [[ -n "$MONGODB_METRICS_USERNAME" ]] && [[ -n "$MONGODB_METRICS_PASSWORD" ]]; then
info "Creating '$MONGODB_METRICS_USERNAME' user..."
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "127.0.0.1" <<EOF
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "$MONGODB_LOCALHOST_NAME" <<EOF
db.getSiblingDB('admin').createUser({ user: '$MONGODB_METRICS_USERNAME', pwd: '$MONGODB_METRICS_PASSWORD', roles: [{role: 'clusterMonitor', db: 'admin'},{ role: 'read', db: 'local' }] })
EOF
fi
Expand Down Expand Up @@ -784,7 +784,7 @@ mongodb_is_primary_node_initiated() {
local port="${2:?port is required}"
local result
result=$(
mongodb_execute_print_output "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "admin" "127.0.0.1" "$MONGODB_PORT_NUMBER" <<EOF
mongodb_execute_print_output "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "admin" "$MONGODB_LOCALHOST_NAME" "$MONGODB_PORT_NUMBER" <<EOF
rs.initiate({"_id":"$MONGODB_REPLICA_SET_NAME", "members":[{"_id":0,"host":"$node:$port","priority":5}]})
EOF
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mongodb_env_vars=(
MONGODB_PRIMARY_PORT_NUMBER
MONGODB_PRIMARY_ROOT_PASSWORD
MONGODB_PRIMARY_ROOT_USER
MONGODB_LOCALHOST_NAME
)
for env_var in "${mongodb_env_vars[@]}"; do
file_env_var="${env_var}_FILE"
Expand Down Expand Up @@ -127,6 +128,7 @@ export MONGODB_ENABLE_IPV6="${MONGODB_ENABLE_IPV6:-}"
export MONGODB_DEFAULT_ENABLE_IPV6="false"
export MONGODB_SYSTEM_LOG_VERBOSITY="${MONGODB_SYSTEM_LOG_VERBOSITY:-}"
export MONGODB_DEFAULT_SYSTEM_LOG_VERBOSITY="0"
export MONGODB_LOCALHOST_NAME="${MONGODB_LOCALHOST_NAME:-127.0.0.1}"

# User and database creation settings
export MONGODB_ROOT_USER="${MONGODB_ROOT_USER:-root}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ mongodb_create_user() {
[[ -z "$database" ]] && query="db.getSiblingDB(db.stats().db).createUser({ user: '$user', pwd: '$password', roles: [{role: 'readWrite', db: db.getSiblingDB(db.stats().db).stats().db }] })"
# Create user, discarding mongo CLI output for clean logs
info "Creating user '$user'..."
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "127.0.0.1" <<<"$query"
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "$MONGODB_LOCALHOST_NAME" <<<"$query"
}

########################
Expand All @@ -684,7 +684,7 @@ mongodb_create_users() {

if [[ -n "$MONGODB_ROOT_PASSWORD" ]] && ! [[ "$MONGODB_REPLICA_SET_MODE" =~ ^(secondary|arbiter|hidden) ]]; then
info "Creating $MONGODB_ROOT_USER user..."
mongodb_execute "" "" "" "127.0.0.1" <<EOF
mongodb_execute "" "" "" "$MONGODB_LOCALHOST_NAME" <<EOF
db.getSiblingDB('admin').createUser({ user: '$MONGODB_ROOT_USER', pwd: '$MONGODB_ROOT_PASSWORD', roles: [{role: 'root', db: 'admin'}] })
EOF
fi
Expand Down Expand Up @@ -714,7 +714,7 @@ EOF

if [[ -n "$MONGODB_METRICS_USERNAME" ]] && [[ -n "$MONGODB_METRICS_PASSWORD" ]]; then
info "Creating '$MONGODB_METRICS_USERNAME' user..."
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "127.0.0.1" <<EOF
mongodb_execute "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "" "$MONGODB_LOCALHOST_NAME" <<EOF
db.getSiblingDB('admin').createUser({ user: '$MONGODB_METRICS_USERNAME', pwd: '$MONGODB_METRICS_PASSWORD', roles: [{role: 'clusterMonitor', db: 'admin'},{ role: 'read', db: 'local' }] })
EOF
fi
Expand Down Expand Up @@ -784,7 +784,7 @@ mongodb_is_primary_node_initiated() {
local port="${2:?port is required}"
local result
result=$(
mongodb_execute_print_output "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "admin" "127.0.0.1" "$MONGODB_PORT_NUMBER" <<EOF
mongodb_execute_print_output "$MONGODB_ROOT_USER" "$MONGODB_ROOT_PASSWORD" "admin" "$MONGODB_LOCALHOST_NAME" "$MONGODB_PORT_NUMBER" <<EOF
rs.initiate({"_id":"$MONGODB_REPLICA_SET_NAME", "members":[{"_id":0,"host":"$node:$port","priority":5}]})
EOF
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mongodb_env_vars=(
MONGODB_PRIMARY_PORT_NUMBER
MONGODB_PRIMARY_ROOT_PASSWORD
MONGODB_PRIMARY_ROOT_USER
MONGODB_LOCALHOST_NAME
)
for env_var in "${mongodb_env_vars[@]}"; do
file_env_var="${env_var}_FILE"
Expand Down Expand Up @@ -127,6 +128,7 @@ export MONGODB_ENABLE_IPV6="${MONGODB_ENABLE_IPV6:-}"
export MONGODB_DEFAULT_ENABLE_IPV6="false"
export MONGODB_SYSTEM_LOG_VERBOSITY="${MONGODB_SYSTEM_LOG_VERBOSITY:-}"
export MONGODB_DEFAULT_SYSTEM_LOG_VERBOSITY="0"
export MONGODB_LOCALHOST_NAME="${MONGODB_LOCALHOST_NAME:-127.0.0.1}"

# User and database creation settings
export MONGODB_ROOT_USER="${MONGODB_ROOT_USER:-root}"
Expand Down
2 changes: 2 additions & 0 deletions bitnami/mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ Before starting the cluster you need to generate PEM certificates as required by

Another option would be to use letsencrypt certificates; the required configuration steps for that scenario are left as an exercise for the user and are beyond the scope of this README.

The hostname used during initialization can be configured using `MONGODB_LOCALHOST_NAME`. Use this to provide a vaild domain for your certificate. Preferably this hostname should be routed back to `127.0.0.1` through the hosts file. If not possible then progress can be made bybinding the mongo server to the correct ip, or all ips, in the configuration.

#### Generating self-signed certificates

* Generate a new private key which will be used to create your own Certificate Authority (CA):
Expand Down

0 comments on commit 3171d7a

Please sign in to comment.