Skip to content

Commit

Permalink
Error early if MYSQL_USER=root to prevent partially initialized datab…
Browse files Browse the repository at this point in the history
…ases

- warning when only one of MYSQL_USER and MYSQL_PASSWORD is supplied
- improved `mysql_log` functions to accept `stdin` for the text
  • Loading branch information
yosifkit committed Mar 16, 2021
1 parent d9fecf6 commit 6a9edeb
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
31 changes: 29 additions & 2 deletions 5.6/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions 5.7/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions 8.0/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions template/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ shopt -s nullglob
# logging functions
mysql_log() {
local type="$1"; shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
# accept argument string or stdin
local text="$*"; if [ "$#" -eq 0 ]; then text="$(cat)"; fi
local dt; dt="$(date --rfc-3339=seconds)"
printf '%s [%s] [Entrypoint]: %s\n' "$dt" "$type" "$text"
}
mysql_note() {
mysql_log Note "$@"
Expand Down Expand Up @@ -141,7 +144,31 @@ docker_temp_server_stop() {
# Verify that the minimally required password settings are set for new databases.
docker_verify_minimum_env() {
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
mysql_error $'Database is uninitialized and password option is not specified\n\tYou need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
mysql_error <<-'EOF'
Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
EOF
fi

# This will prevent the CREATE USER from failing (and thus exiting with a half-initialized database)
if [ "$MYSQL_USER" = 'root' ]; then
mysql_error <<-'EOF'
MYSQL_USER="root", MYSQL_PASSWORD cannot be used for the root user
Use one of the following to control the root user password:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
EOF
fi

# warn when missing one of MYSQL_USER or MYSQL_PASSWORD
if [ -n "$MYSQL_USER" ] && [ -z "$MYSQL_PASSWORD" ]; then
mysql_warn 'MYSQL_USER specified, but missing MYSQL_PASSWORD; MYSQL_USER will not be created'
elif [ -z "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ]; then
mysql_warn 'MYSQL_PASSWORD specified, but missing MYSQL_USER; MYSQL_PASSWORD will be ignored'
fi
}

Expand Down

0 comments on commit 6a9edeb

Please sign in to comment.