Skip to content

Commit b41174f

Browse files
committed
[cloud-sql-proxy] Remove env var assignment of mysql passwords
Add secret manager as a database password specification source
1 parent 57cbfc6 commit b41174f

File tree

1 file changed

+80
-53
lines changed

1 file changed

+80
-53
lines changed

cloud-sql-proxy/cloud-sql-proxy.sh

Lines changed: 80 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ function os_id() ( set +x ; grep '^ID=' /etc/os-release | cut -d= -f2 | x
2525
function os_version() ( set +x ; grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | xargs ; )
2626
function 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

3333
readonly -A supported_os=(
3434
['debian']="10 11 12"
@@ -209,13 +209,19 @@ fi
209209
readonly CLOUDSQL_INSTANCE_TYPE
210210

211211
METASTORE_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
216218
fi
217219
readonly 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.
220226
DB_HIVE_USER="$(/usr/share/google/get_metadata_value attributes/db-hive-user || echo 'hive')"
221227
readonly DB_HIVE_USER
@@ -233,39 +239,46 @@ readonly KMS_KEY_URI
233239
DB_ADMIN_PASSWORD_URI="$(/usr/share/google/get_metadata_value attributes/db-admin-password-uri || echo '')"
234240
readonly 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 - \
242251
--plaintext-file - \
243-
--key "${KMS_KEY_URI}")"
252+
--key "${KMS_KEY_URI}" > /dev/shm/db-pw
253+
else
254+
touch /dev/shm/db-pw
244255
fi
245-
if [[ "${CLOUDSQL_INSTANCE_TYPE}" == "POSTGRES" && -z "${DB_ADMIN_PASSWORD}" ]]; then
256+
if [[ "${CLOUDSQL_INSTANCE_TYPE}" == "POSTGRES" && -z "$(perl -pe 'chomp' < /dev/shm/db-pw)" ]]; then
246257
log 'POSTGRES DB admin password is not set'
247258
fi
248-
readonly DB_ADMIN_PASSWORD
249259

250260
# Database password used to access metastore.
251261
DB_HIVE_PASSWORD_URI="$(/usr/share/google/get_metadata_value attributes/db-hive-password-uri || echo '')"
252262
readonly 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 - \
258272
--plaintext-file - \
259-
--key "${KMS_KEY_URI}")"
260-
readonly DB_HIVE_PASSWORD
273+
--key "${KMS_KEY_URI}" > /dev/shm/hive-pw
261274
else
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 -pe 'chomp' < /dev/shm/hive-pw)" == "None" ]]; then
281+
echo "hive-password" > /dev/shm/hive-pw
269282
fi
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//&/&amp;}
400-
db_hive_password_xml_escaped=${db_hive_password_xml_escaped//</&lt;}
401-
db_hive_password_xml_escaped=${db_hive_password_xml_escaped//>/&gt;}
402-
db_hive_password_xml_escaped=${db_hive_password_xml_escaped//'"'/&quot;}
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:<:&lt;:g; s:>:&gt;:g ; s:":&quot;:g' < /dev/shm/hive-pw)</value>
447453
</property>
448454
</configuration>
449455
EOF
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
@@ -458,36 +464,57 @@ EOF
458464
}
459465

460466
function 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
467+
log 'Initialzing MySQL DB for Hive metastore ...'
468+
local admin_defaults_file="/dev/shm/admin-db.cnf"
469+
local hive_defaults_file="/dev/shm/hive-db.cnf"
470+
local db_password_param="--defaults-file=${admin_defaults_file}"
471+
local db_hive_password_param="--defaults-file=${hive_defaults_file}"
472+
473+
(
474+
printf "[client]\nhost=127.0.0.1\nport=${METASTORE_PROXY_PORT}\nuser=${DB_ADMIN_USER}\npassword=\""
475+
perl -pe 'chomp' < /dev/shm/db-pw
476+
echo '"'
477+
) > "${admin_defaults_file}"
478+
(
479+
printf "[client]\nhost=127.0.0.1\nport=${METASTORE_PROXY_PORT}\nuser=${DB_HIVE_USER}\npassword=\""
480+
perl -pe 'chomp' < /dev/shm/hive-pw
481+
echo '"'
482+
) > "${hive_defaults_file}"
483+
(
484+
echo -n "CREATE USER IF NOT EXISTS '${DB_HIVE_USER}'@'cloudsqlproxy~%' IDENTIFIED BY '"
485+
perl -pe 'chomp' < /dev/shm/hive-pw
486+
echo -n "';"
487+
) > /dev/shm/create_hive_user.sql
488+
489+
# create hive user if it does not exist
490+
mysql "${db_hive_password_param}" -e '' || \
491+
mysql "${db_password_param}" < /dev/shm/create_hive_user.sql
470492

471493
# 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
494+
if ! mysql "${db_hive_password_param}" -e "use ${METASTORE_DB}"; then
477495
# 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 ||
496+
mysql "${db_password_param}" -e \
497+
"CREATE DATABASE IF NOT EXISTS ${METASTORE_DB};
498+
GRANT ALL PRIVILEGES ON ${METASTORE_DB}.* TO '${DB_HIVE_USER}'@'cloudsqlproxy~%';"
499+
/usr/lib/hive/bin/schematool -dbType mysql -initSchema || {
500+
rm -f /dev/shm/*-db.cnf /dev/shm/*.sql
482501
err 'Failed to set mysql schema.'
502+
}
483503
fi
484504
log 'MYSQL DB initialized for Hive metastore'
505+
rm -f /dev/shm/*-db.cnf /dev/shm/*.sql
485506
}
486507

508+
function exit_handler() {
509+
rm -f /dev/shm/*-pw /dev/shm/*-db.cnf /dev/shm/*.sql
510+
}
511+
512+
trap exit_handler EXIT
513+
487514
function initialize_postgres_metastore_db() {
488515
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
516+
local admin_connection=postgresql://"${DB_ADMIN_USER}":"$(perl -pe 'chomp' < /dev/shm/db-pw)"@127.0.0.1:"${METASTORE_PROXY_PORT}"/
517+
local hive_connection=postgresql://"${DB_HIVE_USER}":"$(perl -pe 'chomp' < /dev/shm/hive-pw)"@127.0.0.1:"${METASTORE_PROXY_PORT}"/postgres
491518

492519
# Check if metastore is initialized.
493520
if ! psql "${hive_connection}" -c ''; then

0 commit comments

Comments
 (0)