@@ -25,10 +25,10 @@ function os_id() ( set +x ; grep '^ID=' /etc/os-release | cut -d= -f2 | x
2525function os_version() ( set +x ; grep ' ^VERSION_ID=' /etc/os-release | cut -d= -f2 | xargs ; )
2626function os_codename() ( set +x ; grep ' ^VERSION_CODENAME=' /etc/os-release | cut -d= -f2 | xargs ; )
2727
28- function version_ge() ( set +x ; [ " $1 " = " $( echo -e " $1 \n$2 " | sort -V | tail -n1) " ] ; )
29- function version_gt() ( set +x ; [ " $1 " = " $2 " ] && return 1 || version_ge $1 $2 ; )
30- function version_le() ( set +x ; [ " $1 " = " $( echo -e " $1 \n$2 " | sort -V | head -n1) " ] ; )
31- function version_lt() ( set +x ; [ " $1 " = " $2 " ] && return 1 || version_le $1 $2 ; )
28+ function version_ge(){ [[ " $1 " = " $( echo -e " $1 \n$2 " | sort -V| tail -n1) " ]]; }
29+ function version_gt(){ [[ " $1 " = " $2 " ]] && return 1 || version_ge " $1 " " $2 " ; }
30+ function version_le(){ [[ " $1 " = " $( echo -e " $1 \n$2 " | sort -V| head -n1) " ]]; }
31+ function version_lt(){ [[ " $1 " = " $2 " ]] && return 1 || version_le " $1 " " $2 " ; }
3232
3333readonly -A supported_os=(
3434 [' debian' ]=" 10 11 12"
209209readonly CLOUDSQL_INSTANCE_TYPE
210210
211211METASTORE_PROXY_PORT=" $( /usr/share/google/get_metadata_value attributes/metastore-proxy-port || echo ' ' ) "
212- if [[ " ${METASTORE_INSTANCE} " =~ = tcp:[0-9]+$ ]]; then
213- METASTORE_PROXY_PORT=" ${METASTORE_INSTANCE##*: } "
214- else
215- METASTORE_PROXY_PORT=${DEFAULT_DB_PORT["${CLOUDSQL_INSTANCE_TYPE}"]}
212+ if [[ -z " ${METASTORE_PROXY_PORT} " ]] ; then
213+ if [[ " ${METASTORE_INSTANCE} " =~ = tcp:[0-9]+$ ]]; then
214+ METASTORE_PROXY_PORT=" ${METASTORE_INSTANCE##*: } "
215+ else
216+ METASTORE_PROXY_PORT=${DEFAULT_DB_PORT["${CLOUDSQL_INSTANCE_TYPE}"]}
217+ fi
216218fi
217219readonly METASTORE_PROXY_PORT
218220
221+ # Allow users to specify hive password using secret manager
222+ DB_HIVE_SECRET=" $( /usr/share/google/get_metadata_value attributes/db-hive-secret || echo ' ' ) "
223+ DB_ADMIN_SECRET=" $( /usr/share/google/get_metadata_value attributes/db-admin-secret || echo ' ' ) "
224+
219225# Database user to use to access metastore.
220226DB_HIVE_USER=" $( /usr/share/google/get_metadata_value attributes/db-hive-user || echo ' hive' ) "
221227readonly DB_HIVE_USER
@@ -233,39 +239,46 @@ readonly KMS_KEY_URI
233239DB_ADMIN_PASSWORD_URI=" $( /usr/share/google/get_metadata_value attributes/db-admin-password-uri || echo ' ' ) "
234240readonly DB_ADMIN_PASSWORD_URI
235241
236- DB_ADMIN_PASSWORD=' '
237- if [[ -n " ${DB_ADMIN_PASSWORD_URI} " ]]; then
242+ if [[ -n " ${DB_ADMIN_SECRET} " ]] ; then
243+ gcloud secrets versions access " ${DB_ADMIN_SECRET#*: } " \
244+ --project=" ${METASTORE_INSTANCE%%:* } " \
245+ --secret=" ${DB_ADMIN_SECRET%:* } " > /dev/shm/db-pw
246+ elif [[ -n " ${DB_ADMIN_PASSWORD_URI} " ]]; then
238247 # Decrypt password
239- DB_ADMIN_PASSWORD= " $( gsutil cat " ${DB_ADMIN_PASSWORD_URI} " |
248+ gsutil cat " ${DB_ADMIN_PASSWORD_URI} " |
240249 gcloud kms decrypt \
241250 --ciphertext-file - \
242- --plaintext-file - \
243- --key " ${KMS_KEY_URI} " ) "
251+ --plaintext-file /dev/shm/db-pw \
252+ --key " ${KMS_KEY_URI} "
253+ else
254+ touch /dev/shm/db-pw
244255fi
245- if [[ " ${CLOUDSQL_INSTANCE_TYPE} " == " POSTGRES" && -z " ${DB_ADMIN_PASSWORD} " ]]; then
256+ if [[ " ${CLOUDSQL_INSTANCE_TYPE} " == " POSTGRES" ]] && [[ " $( perl -pe ' chomp ' < /dev/shm/db-pw | wc -c ) " != " 0 " ]]; then
246257 log ' POSTGRES DB admin password is not set'
247258fi
248- readonly DB_ADMIN_PASSWORD
249259
250260# Database password used to access metastore.
251261DB_HIVE_PASSWORD_URI=" $( /usr/share/google/get_metadata_value attributes/db-hive-password-uri || echo ' ' ) "
252262readonly DB_HIVE_PASSWORD_URI
253- if [[ -n " ${DB_HIVE_PASSWORD_URI} " ]]; then
263+ if [[ -n " ${DB_HIVE_SECRET} " ]] ; then
264+ gcloud secrets versions access " ${DB_HIVE_SECRET#*: } " \
265+ --project=" ${METASTORE_INSTANCE%%:* } " \
266+ --secret=" ${DB_HIVE_SECRET%:* } " > /dev/shm/hive-pw
267+ elif [[ -n " ${DB_HIVE_PASSWORD_URI} " ]]; then
254268 # Decrypt password
255- DB_HIVE_PASSWORD= " $( gsutil cat " ${DB_HIVE_PASSWORD_URI} " |
269+ gsutil cat " ${DB_HIVE_PASSWORD_URI} " |
256270 gcloud kms decrypt \
257271 --ciphertext-file - \
258- --plaintext-file - \
259- --key " ${KMS_KEY_URI} " ) "
260- readonly DB_HIVE_PASSWORD
272+ --plaintext-file /dev/shm/hive-pw \
273+ --key " ${KMS_KEY_URI} "
261274else
262- db_hive_pwd= $( bdconfig get_property_value \
275+ /usr/local/bin/ bdconfig get_property_value \
263276 --configuration_file " /etc/hive/conf/hive-site.xml" \
264- --name " javax.jdo.option.ConnectionPassword" 2> /dev/null)
265- if [[ " ${db_hive_pwd} " == " None " ]] ; then
266- db_hive_pwd= " hive-password "
267- fi
268- readonly DB_HIVE_PASSWORD= ${db_hive_pwd}
277+ --name " javax.jdo.option.ConnectionPassword" 2> /dev/null > /dev/shm/hive-pw
278+ fi
279+
280+ if perl -e ' $l=<STDIN>; chomp $l; exit( $l eq "None" ? 0 : 1 ) ' < /dev/shm/hive-pw ; then
281+ echo " hive-password " > /dev/shm/hive-pw
269282fi
270283
271284# Name of MySQL database to use for the metastore.
@@ -394,13 +407,6 @@ function install_cloud_sql_proxy() {
394407 local proxy_flags
395408 proxy_flags=" $( get_proxy_flags) "
396409
397- # Validate db_hive_password and escape invalid xml characters if found.
398- local db_hive_password_xml_escaped
399- db_hive_password_xml_escaped=${DB_HIVE_PASSWORD// &/ &}
400- db_hive_password_xml_escaped=${db_hive_password_xml_escaped// </ <}
401- db_hive_password_xml_escaped=${db_hive_password_xml_escaped// >/ >}
402- db_hive_password_xml_escaped=${db_hive_password_xml_escaped// ' "' / "}
403-
404410 # Install proxy as systemd service for reboot tolerance.
405411 cat << EOF >${INIT_SCRIPT}
406412[Unit]
@@ -443,12 +449,12 @@ EOF
443449 </property>
444450 <property>
445451 <name>javax.jdo.option.ConnectionPassword</name>
446- <value>${db_hive_password_xml_escaped} </value>
452+ <value>$( perl -pe ' chomp ; s:<:<:g; s:>:>:g ; s:":":g ' < /dev/shm/hive-pw ) </value>
447453 </property>
448454</configuration>
449455EOF
450456
451- bdconfig merge_configurations \
457+ /usr/local/bin/ bdconfig merge_configurations \
452458 --configuration_file /etc/hive/conf/hive-site.xml \
453459 --source_configuration_file hive-template.xml \
454460 --clobber
@@ -457,37 +463,70 @@ EOF
457463 log ' Cloud SQL Proxy installation succeeded'
458464}
459465
466+ function admin_mysql() {
467+ local admin_defaults_file=" /dev/shm/admin-db.cnf"
468+ local db_password_param=" --defaults-file=${admin_defaults_file} "
469+ mysql " ${db_password_param} " $*
470+ }
471+
472+ function hive_mysql() {
473+ local hive_defaults_file=" /dev/shm/hive-db.cnf"
474+ local db_hive_password_param=" --defaults-file=${hive_defaults_file} "
475+ mysql " ${db_hive_password_param} " $*
476+ }
477+
460478function initialize_mysql_metastore_db() {
461- log ' Initialzing MYSQL DB for Hive metastore ...'
462- local db_password_param=' --password='
463- if [[ -n ${DB_ADMIN_PASSWORD} ]]; then
464- db_password_param+=${DB_ADMIN_PASSWORD}
465- fi
466- local db_hive_password_param=' '
467- if [[ -n ${DB_HIVE_PASSWORD} ]]; then
468- db_hive_password_param+=" -p${DB_HIVE_PASSWORD} "
469- fi
479+ log ' Initialzing MySQL DB for Hive metastore ...'
480+ local admin_defaults_file=" /dev/shm/admin-db.cnf"
481+ local hive_defaults_file=" /dev/shm/hive-db.cnf"
482+ local db_password_param=" --defaults-file=${admin_defaults_file} "
483+ local db_hive_password_param=" --defaults-file=${hive_defaults_file} "
484+
485+ (
486+ printf " [client]\nhost=127.0.0.1\nport=${METASTORE_PROXY_PORT} \nuser=${DB_ADMIN_USER} \npassword=\" "
487+ perl -pe ' chomp' < /dev/shm/db-pw
488+ echo ' "'
489+ ) > " ${admin_defaults_file} "
490+ (
491+ printf " [client]\nhost=127.0.0.1\nport=${METASTORE_PROXY_PORT} \nuser=${DB_HIVE_USER} \npassword=\" "
492+ perl -pe ' chomp' < /dev/shm/hive-pw
493+ echo ' "'
494+ ) > " ${hive_defaults_file} "
495+ (
496+ echo -n " CREATE USER IF NOT EXISTS '${DB_HIVE_USER} '@'cloudsqlproxy~%' IDENTIFIED BY '"
497+ perl -pe ' chomp' < /dev/shm/hive-pw
498+ echo -n " ';"
499+ ) > /dev/shm/create_hive_user.sql
500+
501+ # create hive user if it does not exist
502+ hive_mysql -e ' ' || \
503+ admin_mysql < /dev/shm/create_hive_user.sql
470504
471505 # Check if metastore is initialized.
472- if ! mysql -h 127.0.0.1 -P " ${METASTORE_PROXY_PORT} " -u " ${DB_HIVE_USER} " " ${db_hive_password_param} " -e ' ' ; then
473- mysql -h 127.0.0.1 -P " ${METASTORE_PROXY_PORT} " -u " ${DB_ADMIN_USER} " " ${db_password_param} " -e \
474- " CREATE USER '${DB_HIVE_USER} ' IDENTIFIED BY '${DB_HIVE_PASSWORD} ';"
475- fi
476- if ! mysql -h 127.0.0.1 -P " ${METASTORE_PROXY_PORT} " -u " ${DB_HIVE_USER} " " ${db_hive_password_param} " -e " use ${METASTORE_DB} " ; then
506+ if ! hive_mysql -e " use ${METASTORE_DB} " ; then
477507 # Initialize a Hive metastore DB
478- mysql -h 127.0.0.1 -P " ${METASTORE_PROXY_PORT} " -u " ${DB_ADMIN_USER} " " ${db_password_param} " -e \
479- " CREATE DATABASE ${METASTORE_DB} ;
480- GRANT ALL PRIVILEGES ON ${METASTORE_DB} .* TO '${DB_HIVE_USER} ';"
481- /usr/lib/hive/bin/schematool -dbType mysql -initSchema ||
508+ admin_mysql -e \
509+ " CREATE DATABASE IF NOT EXISTS ${METASTORE_DB} ;
510+ GRANT ALL PRIVILEGES ON ${METASTORE_DB} .* TO '${DB_HIVE_USER} '@'cloudsqlproxy~%';"
511+ /usr/lib/hive/bin/schematool -dbType mysql -initSchema || {
512+ rm -f /dev/shm/* -db.cnf /dev/shm/* .sql
482513 err ' Failed to set mysql schema.'
514+ }
483515 fi
484516 log ' MYSQL DB initialized for Hive metastore'
517+ rm -f /dev/shm/* -db.cnf /dev/shm/* .sql
518+ }
519+
520+ function exit_handler() {
521+ rm -f /dev/shm/* -pw /dev/shm/* -db.cnf /dev/shm/* .sql
485522}
486523
524+ trap exit_handler EXIT
525+
487526function initialize_postgres_metastore_db() {
488527 log ' Initialzing POSTGRES DB for Hive metastore ...'
489- local admin_connection=postgresql://" ${DB_ADMIN_USER} " :" ${DB_ADMIN_PASSWORD} " @127.0.0.1:" ${METASTORE_PROXY_PORT} " /
490- local hive_connection=postgresql://" ${DB_HIVE_USER} " :" ${DB_HIVE_PASSWORD} " @127.0.0.1:" ${METASTORE_PROXY_PORT} " /postgres
528+ local admin_connection=postgresql://" ${DB_ADMIN_USER} " :" $( perl -pe ' chomp ' < /dev/shm/db-pw ) " @127.0.0.1:" ${METASTORE_PROXY_PORT} " /
529+ local hive_connection=postgresql://" ${DB_HIVE_USER} " :" $( perl -pe ' chomp ' < /dev/shm/hive-pw ) " @127.0.0.1:" ${METASTORE_PROXY_PORT} " /postgres
491530
492531 # Check if metastore is initialized.
493532 if ! psql " ${hive_connection} " -c ' ' ; then
0 commit comments