Skip to content

Commit 84c2cba

Browse files
dilipgodhiaDilip Godhiagemini-code-assist[bot]
authored
Making use of CloudSQL PRoxy V2 binaries and maitaining backward compatibility (#1355)
* Update the incompatibility notice to alert users not to use CloudSQL Proxy (V1) with CloudSQL MySQL 8.4. * Making use of CloudSQL PRoxy V2 binaries and maitaining backward compatibility * Update cloud-sql-proxy/cloud-sql-proxy.sh Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: Dilip Godhia <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 6694175 commit 84c2cba

File tree

2 files changed

+84
-28
lines changed

2 files changed

+84
-28
lines changed

cloud-sql-proxy/README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@ metadata on a given Cloud SQL instance.
1010

1111
**⚠️ IMPORTANT COMPATIBILITY NOTICE ⚠️**
1212

13-
**Cloud SQL Proxy V1 (the version that's currently the default in this initialization action) is not fully compatible with newer database versions like MySQL 8.4 and some newer Cloud SQL features.**
13+
**Cloud SQL Proxy V2 script has been upgraded to use the latest binary client that is compatible with MySQL 8.4 and some newer Cloud SQL features.**
1414

1515
Specifically:
16-
* **MySQL 8.4's updated security model is incompatible with Cloud SQL Proxy V1.**
17-
* **Enabling "Shared CA"** or **"Customer-managed CA"** features for any Cloud SQL database (including older MySQL versions and PostgreSQL) will break connectivity with Cloud SQL Proxy V1.
18-
16+
* **MySQL 8.4 has updated its security model that were incompatible with the earlier version of this CloudSQL Proxy script.**
1917

2018
To avoid connectivity issues, we highly recommend:
2119
* **Always testing your Cloud SQL Proxy configuration thoroughly** before adopting new Cloud SQL database versions or enabling advanced security features like Shared CA or Customer-managed CA.
2220

23-
* **Consider upgrading to Cloud SQL Proxy V2** as soon as it's available. The Dataproc team is actively working on updating the underlying Cloud SQL clients in Dataproc images to use Cloud SQL Proxy V2.
24-
25-
* This documentation will be updated once the Cloud SQL Proxy V2 is available along with the Dataproc versions where the fix is available. See [Dataproc release notes](https://cloud.google.com/dataproc/docs/release-notes) for upcoming updates.
21+
* The Dataproc team has updated the underlying Cloud SQL clients in Dataproc images to make use of Cloud SQL Proxy V2. The dataproc versions that are compatible with CloudSQL MySQL 8.4 are Dataproc versions 2.0.147, 2.1.96, 2.2.64 and 2.3.10 released on August 29, 2025. See [Dataproc release notes](https://cloud.google.com/dataproc/docs/release-notes) for any new updates.
2622

2723
## Using this initialization action
2824

@@ -71,12 +67,24 @@ shared hive metastore.
7167
--properties hive:hive.metastore.warehouse.dir=gs://${HIVE_DATA_BUCKET}/hive-warehouse \
7268
--metadata "hive-metastore-instance=${PROJECT_ID}:${REGION}:${INSTANCE_NAME}"
7369
```
70+
To use an earlier version of the CloudSQL Binary, add the following property to your Dataproc Cluster Creation
71+
```bash
72+
--metadata "cloud_sql_proxy_version=1"
73+
```
74+
Or for a specific version of the CloudSQL Binary Client, use:
75+
```bash
76+
--metadata "cloud_sql_proxy_version_number=v2.17.1"
77+
```
78+
You can obtain the list of all CloudSQL Proxy Client version by running the following command
79+
```
80+
curl -s "https://api.github.com/repos/GoogleCloudPlatform/cloud-sql-proxy/releases"|jq -r '.[].tag_name'|grep 'v2'
81+
```
82+
7483

75-
a. Optionally add other instances, paired with distict TCP ports for further
76-
I/O.
84+
a. Optionally add other instances, paired with distinct TCP ports for further I/O.
7785

7886
```bash
79-
--metadata "additional-cloud-sql-instances=<PROJECT_ID>:<REGION>:<ANOTHER_INSTANCE_NAME>=tcp<PORT_#>[,...]"
87+
--metadata "additional-cloud-sql-instances=<PROJECT_ID>:<REGION>:<ANOTHER_INSTANCE_NAME>=tcp<PORT_#>[,...]"
8088
```
8189

8290
1. Submit pyspark_metastore_test.py to the cluster to validate the metatstore
@@ -87,7 +95,7 @@ shared hive metastore.
8795
```
8896

8997
a. You can test connections to your other instance(s) using the url
90-
`"jdbc:mysql//localhost:<PORT_#>?user=root"`
98+
`"jdbc:mysql//localhost:<PORT_#>?allowPublicKeyRetrieval=true&user=root"`
9199

92100
1. Create another dataproc cluster with the same Cloud SQL metastore.
93101

@@ -177,7 +185,7 @@ additional setup.
177185
178186
```bash
179187
gcloud sql instances create <INSTANCE_NAME> \
180-
--database-version="MYSQL_5_7" \
188+
--database-version="MYSQL_8_0" \
181189
--activation-policy=ALWAYS \
182190
--zone <ZONE>
183191
```

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

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ readonly METASTORE_INSTANCE
144144
ADDITIONAL_INSTANCES="$(/usr/share/google/get_metadata_value ${ADDITIONAL_INSTANCES_KEY} || echo '')"
145145
readonly ADDITIONAL_INSTANCES
146146

147+
PROXY_VERSION="$(get_metadata_attribute cloud_sql_proxy_version '2')"
148+
readonly PROXY_VERSION
149+
150+
# CURRENT_PROXY_VERSION=$(curl -s "https://api.github.com/repos/GoogleCloudPlatform/cloud-sql-proxy/releases"|jq -r '.[].tag_name'|grep 'v2'| head -n 1)
151+
152+
CURRENT_PROXY_VERSION="v2.18.1"
153+
OVERRIDE_PROXY_VERSION_NUMBER="$(get_metadata_attribute cloud_sql_proxy_version_number '' | tr '[:upper:]' '[:lower:]' || echo '')"
154+
readonly OVERRIDE_PROXY_VERSION_NUMBER
155+
147156
function repair_old_backports {
148157
if ! is_debuntu ; then return ; fi
149158
# This script uses 'apt-get update' and is therefore potentially dependent on
@@ -209,8 +218,12 @@ fi
209218
readonly CLOUDSQL_INSTANCE_TYPE
210219

211220
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##*:}"
221+
if [[ "${METASTORE_INSTANCE}" =~ tcp:[0-9]+$ ]]; then
222+
if [[ ${PROXY_VERSION} == "1" ]]; then
223+
METASTORE_PROXY_PORT="${METASTORE_INSTANCE##=*:}"
224+
else
225+
METASTORE_PROXY_PORT="${METASTORE_INSTANCE##?*:}"
226+
fi
214227
else
215228
METASTORE_PROXY_PORT=${DEFAULT_DB_PORT["${CLOUDSQL_INSTANCE_TYPE}"]}
216229
fi
@@ -355,7 +368,11 @@ function run_with_retries() {
355368
function get_metastore_instance() {
356369
local metastore_instance="${METASTORE_INSTANCE}"
357370
if ! [[ "${metastore_instance}" =~ =tcp:[0-9]+$ ]]; then
358-
metastore_instance+="=tcp:${METASTORE_PROXY_PORT}"
371+
if [[ ${PROXY_VERSION} == "1" ]]; then
372+
metastore_instance+="=tcp:${METASTORE_PROXY_PORT}"
373+
else
374+
metastore_instance+="?port=${METASTORE_PROXY_PORT}"
375+
fi
359376
fi
360377
echo "${metastore_instance}"
361378
}
@@ -364,17 +381,29 @@ function get_proxy_flags() {
364381
local proxy_instances_flags=''
365382
# If a Cloud SQL instance has both public and private IP, use private IP.
366383
if [[ ${USE_CLOUD_SQL_PRIVATE_IP} == "true" ]]; then
367-
proxy_instances_flags+=" --ip_address_types=PRIVATE"
384+
if [[ ${PROXY_VERSION} == "1" ]]; then
385+
proxy_instances_flags+=" --ip_address_types=PRIVATE"
386+
else
387+
proxy_instances_flags+=" --private-ip"
388+
fi
368389
fi
369390
if [[ ${ENABLE_CLOUD_SQL_METASTORE} == "true" ]]; then
370391
local metastore_instance
371392
metastore_instance=$(get_metastore_instance)
372-
proxy_instances_flags+=" -instances=${metastore_instance}"
393+
if [[ ${PROXY_VERSION} == "1" ]]; then
394+
proxy_instances_flags+=" -instances=${metastore_instance}"
395+
else
396+
proxy_instances_flags+=" ${metastore_instance}"
397+
fi
373398
fi
374399

375400
if [[ -n "${ADDITIONAL_INSTANCES}" ]]; then
376401
# Pass additional instances straight to the proxy.
377-
proxy_instances_flags+=" -instances_metadata=instance/${ADDITIONAL_INSTANCES_KEY}"
402+
if [[ ${PROXY_VERSION} == "1" ]]; then
403+
proxy_instances_flags+=" -instances_metadata=instance/${ADDITIONAL_INSTANCES_KEY}"
404+
else
405+
proxy_instances_flags+=" instances_metadata=instance/${ADDITIONAL_INSTANCES_KEY}"
406+
fi
378407
fi
379408

380409
echo "${proxy_instances_flags}"
@@ -383,9 +412,24 @@ function get_proxy_flags() {
383412
function install_cloud_sql_proxy() {
384413
echo 'Installing Cloud SQL Proxy ...' >&2
385414
# Install proxy.
386-
wget -nv --timeout=30 --tries=5 --retry-connrefused \
387-
https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64
388-
mv cloud_sql_proxy.linux.amd64 ${PROXY_BIN}
415+
local proxy_version
416+
417+
if [[ ${PROXY_VERSION} == "1" ]]; then
418+
wget -nv --timeout=30 --tries=5 --retry-connrefused \
419+
https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O /tmp/cloud_sql_proxy.linux.amd64
420+
echo "Using an OUTDATED version of CloudSQL Proxy V1"
421+
else
422+
if [[ -n ${OVERRIDE_PROXY_VERSION_NUMBER} ]]; then
423+
proxy_version="${OVERRIDE_PROXY_VERSION_NUMBER}"
424+
else
425+
proxy_version="${CURRENT_PROXY_VERSION}"
426+
fi
427+
wget -nv --timeout=30 --tries=5 --retry-connrefused \
428+
https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/"${proxy_version}"/cloud-sql-proxy.linux.amd64 -O /tmp/cloud_sql_proxy.linux.amd64
429+
echo "Using CloudSQL Proxy V2 - Version ${proxy_version}"
430+
fi
431+
mv /tmp/cloud_sql_proxy.linux.amd64 ${PROXY_BIN}
432+
389433
chmod +x ${PROXY_BIN}
390434

391435
mkdir -p ${PROXY_DIR}
@@ -401,6 +445,11 @@ function install_cloud_sql_proxy() {
401445
db_hive_password_xml_escaped=${db_hive_password_xml_escaped//>/&gt;}
402446
db_hive_password_xml_escaped=${db_hive_password_xml_escaped//'"'/&quot;}
403447

448+
dir_options=""
449+
if [[ ${PROXY_VERSION} == "1" ]]; then
450+
dir_options="-dir=${PROXY_DIR}"
451+
fi
452+
404453
# Install proxy as systemd service for reboot tolerance.
405454
cat <<EOF >${INIT_SCRIPT}
406455
[Unit]
@@ -411,8 +460,7 @@ Before=shutdown.target
411460
412461
[Service]
413462
Type=simple
414-
ExecStart=/bin/sh -c '${PROXY_BIN} \
415-
-dir=${PROXY_DIR} \
463+
ExecStart=/bin/sh -c '${PROXY_BIN} ${dir_options} \
416464
${proxy_flags} >> /var/log/cloud-sql-proxy/cloud-sql-proxy.log 2>&1'
417465
418466
[Install]
@@ -421,7 +469,7 @@ EOF
421469
chmod a+rw ${INIT_SCRIPT}
422470

423471
if [[ $ENABLE_CLOUD_SQL_METASTORE == "true" ]]; then
424-
local db_url=jdbc:${DEFAULT_DB_PROTO["${CLOUDSQL_INSTANCE_TYPE}"]}://localhost:${METASTORE_PROXY_PORT}/${METASTORE_DB}
472+
local db_url=jdbc:${DEFAULT_DB_PROTO["${CLOUDSQL_INSTANCE_TYPE}"]}://localhost:${METASTORE_PROXY_PORT}/${METASTORE_DB}?allowPublicKeyRetrieval=true
425473
local db_driver=${DEFAULT_DB_DRIVER["${CLOUDSQL_INSTANCE_TYPE}"]}
426474

427475
# Update hive-site.xml
@@ -469,13 +517,13 @@ function initialize_mysql_metastore_db() {
469517
fi
470518

471519
# 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 \
520+
if ! mysql -h 127.0.0.1 -P "${METASTORE_PROXY_PORT}" -u "${DB_HIVE_USER}" "${db_hive_password_param}" --get-server-public-key -e ''; then
521+
mysql -h 127.0.0.1 -P "${METASTORE_PROXY_PORT}" -u "${DB_ADMIN_USER}" "${db_password_param}" --get-server-public-key -e \
474522
"CREATE USER '${DB_HIVE_USER}' IDENTIFIED BY '${DB_HIVE_PASSWORD}';"
475523
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
524+
if ! mysql -h 127.0.0.1 -P "${METASTORE_PROXY_PORT}" -u "${DB_HIVE_USER}" "${db_hive_password_param}" --get-server-public-key -e "use ${METASTORE_DB}"; then
477525
# Initialize a Hive metastore DB
478-
mysql -h 127.0.0.1 -P "${METASTORE_PROXY_PORT}" -u "${DB_ADMIN_USER}" "${db_password_param}" -e \
526+
mysql -h 127.0.0.1 -P "${METASTORE_PROXY_PORT}" -u "${DB_ADMIN_USER}" "${db_password_param}" --get-server-public-key -e \
479527
"CREATE DATABASE ${METASTORE_DB};
480528
GRANT ALL PRIVILEGES ON ${METASTORE_DB}.* TO '${DB_HIVE_USER}';"
481529
/usr/lib/hive/bin/schematool -dbType mysql -initSchema ||

0 commit comments

Comments
 (0)