@@ -45,7 +45,7 @@ mysql_client_validate() {
45
45
}
46
46
47
47
# Only validate environment variables if any action needs to be performed
48
- check_yes_no_value " DB_TLS_ENABLED "
48
+ check_yes_no_value " DB_ENABLE_SSL_WRAPPER "
49
49
50
50
if [[ -n " $DB_CREATE_DATABASE_USER " || -n " $DB_CREATE_DATABASE_NAME " ]]; then
51
51
if is_boolean_yes " $ALLOW_EMPTY_PASSWORD " ; then
@@ -78,9 +78,9 @@ mysql_client_validate() {
78
78
# None
79
79
# ########################
80
80
mysql_client_initialize () {
81
- # Wrap binary to force the usage of TLS
82
- if is_boolean_yes " $DB_TLS_ENABLED " ; then
83
- mysql_client_wrap_binary_for_tls
81
+ # Wrap binary to force the usage of SSL
82
+ if is_boolean_yes " $DB_ENABLE_SSL_WRAPPER " ; then
83
+ mysql_client_wrap_binary_for_ssl
84
84
fi
85
85
# Wait for the database to be accessible if any action needs to be performed
86
86
if [[ -n " $DB_CREATE_DATABASE_USER " || -n " $DB_CREATE_DATABASE_NAME " ]]; then
@@ -113,22 +113,24 @@ mysql_client_initialize() {
113
113
}
114
114
115
115
# #######################
116
- # Wrap binary to force the usage of TLS
116
+ # Wrap binary to force the usage of SSL
117
117
# Globals:
118
118
# DB_*
119
119
# Arguments:
120
120
# None
121
121
# Returns:
122
122
# None
123
123
# ########################
124
- mysql_client_wrap_binary_for_tls () {
124
+ mysql_client_wrap_binary_for_ssl () {
125
125
local -r wrapper_file=" ${DB_BIN_DIR} /mysql"
126
126
local -r wrapped_binary_file=" ${DB_BASE_DIR} /.bin/mysql"
127
+ local -a ssl_opts=()
128
+ read -r -a ssl_opts <<< " $(mysql_client_extra_opts)"
127
129
128
130
mv " $wrapper_file " " $wrapped_binary_file "
129
131
cat > " $wrapper_file " << EOF
130
132
#!/bin/sh
131
- exec "${wrapped_binary_file} " "\$ @" --ssl=1
133
+ exec "${wrapped_binary_file} " "\$ @" ${ssl_opts[@] :- }
132
134
EOF
133
135
chmod +x " $wrapper_file "
134
136
}
@@ -209,8 +211,9 @@ mysql_execute_print_output() {
209
211
local -r db=" ${1:- } "
210
212
local -r user=" ${2:- root} "
211
213
local -r pass=" ${3:- } "
212
- local mysql_cmd opts
213
- read -r -a opts <<< " ${@:4}"
214
+ local -a opts extra_opts
215
+ read -r -a opts <<< " ${@:4}"
216
+ read -r -a extra_opts <<< " $(mysql_client_extra_opts)"
214
217
215
218
# Process mysql CLI arguments
216
219
local -a args=()
@@ -219,9 +222,11 @@ mysql_execute_print_output() {
219
222
fi
220
223
args+=(" -N" " -u" " $user " " $db " )
221
224
[[ -n " $pass " ]] && args+=(" -p$pass " )
222
- [[ -n " ${opts[*]:- } " ]] && args+=(" ${opts[@]:- } " )
225
+ [[ " ${# opts[@]} " -gt 0 ]] && args+=(" ${opts[@]} " )
226
+ [[ " ${# extra_opts[@]} " -gt 0 ]] && args+=(" ${extra_opts[@]} " )
223
227
224
228
# Obtain the command specified via stdin
229
+ local mysql_cmd
225
230
mysql_cmd=" $( < /dev/stdin) "
226
231
debug " Executing SQL command:\n$mysql_cmd "
227
232
" $DB_BIN_DIR /mysql" " ${args[@]} " <<< " $mysql_cmd"
@@ -246,7 +251,7 @@ mysql_execute() {
246
251
}
247
252
248
253
# #######################
249
- # Execute an arbitrary query/queries against a remote MySQL/MariaDB service
254
+ # Execute an arbitrary query/queries against a remote MySQL/MariaDB service and print to stdout
250
255
# Stdin:
251
256
# Query/queries to execute
252
257
# Globals:
@@ -261,12 +266,32 @@ mysql_execute() {
261
266
# $6 - Extra MySQL CLI options
262
267
# Returns:
263
268
# None
264
- mysql_remote_execute () {
269
+ mysql_remote_execute_print_output () {
265
270
local -r hostname=" ${1:? hostname is required} "
266
271
local -r port=" ${2:? port is required} "
267
272
local -a args=(" -h" " $hostname " " -P" " $port " " --connect-timeout=5" )
268
273
shift 2
269
- debug_execute " mysql_execute_print_output" " $@ " " ${args[@]} "
274
+ " mysql_execute_print_output" " $@ " " ${args[@]} "
275
+ }
276
+
277
+ # #######################
278
+ # Execute an arbitrary query/queries against a remote MySQL/MariaDB service
279
+ # Stdin:
280
+ # Query/queries to execute
281
+ # Globals:
282
+ # BITNAMI_DEBUG
283
+ # DB_*
284
+ # Arguments:
285
+ # $1 - Remote MySQL/MariaDB service hostname
286
+ # $2 - Remote MySQL/MariaDB service port
287
+ # $3 - Database where to run the queries
288
+ # $4 - User to run queries
289
+ # $5 - Password
290
+ # $6 - Extra MySQL CLI options
291
+ # Returns:
292
+ # None
293
+ mysql_remote_execute () {
294
+ debug_execute " mysql_remote_execute_print_output" " $@ "
270
295
}
271
296
272
297
# #######################
@@ -468,7 +493,7 @@ mysql_upgrade() {
468
493
# Returns:
469
494
# None
470
495
# ########################
471
- migrate_old_configuration () {
496
+ mysql_migrate_old_configuration () {
472
497
local -r old_custom_conf_file=" $DB_VOLUME_DIR /conf/my_custom.cnf"
473
498
local -r custom_conf_file=" $DB_CONF_DIR /bitnami/my_custom.cnf"
474
499
debug " Persisted configuration detected. Migrating any existing 'my_custom.cnf' file to new location"
@@ -529,10 +554,6 @@ mysql_ensure_user_exists() {
529
554
--use-ldap)
530
555
use_ldap=" yes"
531
556
;;
532
- --ssl-ca)
533
- shift
534
- ssl_ca=" ${1:? missing path to ssl CA} "
535
- ;;
536
557
--host)
537
558
shift
538
559
db_host=" ${1:? missing database host} "
@@ -558,22 +579,26 @@ mysql_ensure_user_exists() {
558
579
fi
559
580
fi
560
581
debug " creating database user \'$user \'"
561
- local -a opts=()
562
- [[ -n " $db_host " ]] && opts+=(" -h" " ${db_host} " )
563
- [[ -n " $db_port " ]] && opts+=(" -P" " ${db_port} " )
564
- [[ -n " $ssl_ca " ]] && opts+=(" --ssl-ca" " $ssl_ca " )
582
+
583
+ local -a mysql_execute_cmd=(" mysql_execute" )
584
+ local -a mysql_execute_print_output_cmd=(" mysql_execute_print_output" )
585
+ if [[ -n " $db_host " && -n " $db_port " ]]; then
586
+ mysql_execute_cmd=(" mysql_remote_execute" " $db_host " " $db_port " )
587
+ mysql_execute_print_output_cmd=(" mysql_remote_execute_print_output" " $db_host " " $db_port " )
588
+ fi
589
+
565
590
local mysql_create_user_cmd
566
591
[[ " $DB_FLAVOR " = " mariadb" ]] && mysql_create_user_cmd=" create or replace user" || mysql_create_user_cmd=" create user if not exists"
567
- mysql_execute " mysql" " $DB_ROOT_USER " " $DB_ROOT_PASSWORD " " ${opts[@] :- } " << EOF
592
+ " ${mysql_execute_cmd[@]} " " mysql" " $DB_ROOT_USER " " $DB_ROOT_PASSWORD " << EOF
568
593
${mysql_create_user_cmd} '${user} '@'%' ${auth_string} ;
569
594
EOF
570
595
debug " Removing all other hosts for the user"
571
- hosts=$( mysql_execute_print_output " mysql" " $DB_ROOT_USER " " $DB_ROOT_PASSWORD " " ${opts[@] :- } " << EOF
596
+ hosts=$( " ${mysql_execute_print_output_cmd[@]} " " mysql" " $DB_ROOT_USER " " $DB_ROOT_PASSWORD " << EOF
572
597
select Host from user where User='${user} ' and Host!='%';
573
598
EOF
574
599
)
575
600
for host in $hosts ; do
576
- mysql_execute " mysql" " $DB_ROOT_USER " " $DB_ROOT_PASSWORD " " ${opts[@] :- } " << EOF
601
+ " ${mysql_execute_cmd[@]} " " mysql" " $DB_ROOT_USER " " $DB_ROOT_PASSWORD " << EOF
577
602
drop user '$user '@'$host ';
578
603
EOF
579
604
done
@@ -713,16 +738,16 @@ mysql_ensure_database_exists() {
713
738
shift
714
739
done
715
740
716
- local -a extra_args=()
717
- [[ -n " $character_set " ]] && extra_args=(" character set = '${character_set} '" )
718
- [[ -n " $collate " ]] && extra_args=(" collate = '${collate} '" )
719
-
720
741
local -a mysql_execute_cmd=(" mysql_execute" )
721
742
[[ -n " $db_host " && -n " $db_port " ]] && mysql_execute_cmd=(" mysql_remote_execute" " $db_host " " $db_port " )
722
743
744
+ local -a create_database_args=()
745
+ [[ -n " $character_set " ]] && create_database_args+=(" character set = '${character_set} '" )
746
+ [[ -n " $collate " ]] && create_database_args+=(" collate = '${collate} '" )
747
+
723
748
debug " Creating database $database "
724
749
" ${mysql_execute_cmd[@]} " " mysql" " $DB_ROOT_USER " " $DB_ROOT_PASSWORD " << EOF
725
- create database if not exists \` $database \` ${extra_args [@]:- } ;
750
+ create database if not exists \` $database \` ${create_database_args [@]:- } ;
726
751
EOF
727
752
}
728
753
@@ -924,7 +949,7 @@ mysql_conf_set() {
924
949
# ########################
925
950
mysql_update_custom_config () {
926
951
# Persisted configuration files from old versions
927
- ! is_dir_empty " $DB_VOLUME_DIR " && [[ -d " $DB_VOLUME_DIR /conf" ]] && migrate_old_configuration
952
+ ! is_dir_empty " $DB_VOLUME_DIR " && [[ -d " $DB_VOLUME_DIR /conf" ]] && mysql_migrate_old_configuration
928
953
929
954
# User injected custom configuration
930
955
if [[ -f " $DB_CONF_DIR /my_custom.cnf" ]]; then
@@ -986,3 +1011,57 @@ mysql_healthcheck() {
986
1011
987
1012
mysqladmin " ${args[@]} " ping && mysqladmin " ${args[@]} " status
988
1013
}
1014
+
1015
+ # #######################
1016
+ # Prints flavor of 'mysql' client (useful to determine proper CLI flags that can be used)
1017
+ # Globals:
1018
+ # DB_*
1019
+ # Arguments:
1020
+ # None
1021
+ # Returns:
1022
+ # mysql client flavor
1023
+ # ########################
1024
+ mysql_client_flavor () {
1025
+ if " ${DB_BIN_DIR} /mysql" " --version" 2>&1 | grep -q MariaDB; then
1026
+ echo " mariadb"
1027
+ else
1028
+ echo " mysql"
1029
+ fi
1030
+ }
1031
+
1032
+ # #######################
1033
+ # Prints extra options for MySQL client calls (i.e. SSL options)
1034
+ # Globals:
1035
+ # DB_*
1036
+ # Arguments:
1037
+ # None
1038
+ # Returns:
1039
+ # List of options to pass to "mysql" CLI
1040
+ # ########################
1041
+ mysql_client_extra_opts () {
1042
+ # Helper to get the proper value for the MySQL client environment variable
1043
+ mysql_client_env_value () {
1044
+ local env_name=" MYSQL_CLIENT_${1:? missing name} "
1045
+ if [[ -n " ${! env_name:- } " ]]; then
1046
+ echo " ${! env_name:- } "
1047
+ else
1048
+ env_name=" DB_CLIENT_${1} "
1049
+ echo " ${! env_name:- } "
1050
+ fi
1051
+ }
1052
+ local -a opts=()
1053
+ local key value
1054
+ if is_boolean_yes " $DB_ENABLE_SSL " ; then
1055
+ if [[ " $( mysql_client_flavor) " = " mysql" ]]; then
1056
+ opts+=(" --ssl-mode=REQUIRED" )
1057
+ else
1058
+ opts+=(" --ssl=TRUE" )
1059
+ fi
1060
+ # Add "--ssl-ca", "--ssl-key" and "--ssl-cert" options if the env vars are defined
1061
+ for key in ca key cert; do
1062
+ value=" $( mysql_client_env_value " SSL_${key^^} _FILE" ) "
1063
+ [[ -n " ${value} " ]] && opts+=(" --ssl-${key} =${value} " )
1064
+ done
1065
+ fi
1066
+ echo " ${opts[@]:- } "
1067
+ }
0 commit comments