Skip to content

Commit 2162ff2

Browse files
feat: update all v1 Cloud SQL Proxy usages to v2
1 parent ca273b2 commit 2162ff2

File tree

8 files changed

+142
-94
lines changed

8 files changed

+142
-94
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,16 @@ export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json
132132
export DATALABELING_ENDPOINT="test-datalabeling.sandbox.googleapis.com:443"
133133

134134
# Run Cloud SQL proxy (background process exit when script does)
135-
wget --quiet https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 \
136-
-O ${HOME}/cloud_sql_proxy && chmod +x ${HOME}/cloud_sql_proxy
137-
${HOME}/cloud_sql_proxy -instances="${MYSQL_INSTANCE}"=tcp:3306,"${MYSQL_INSTANCE}" -dir "${HOME}" &>> \
138-
${HOME}/cloud_sql_proxy.log &
135+
wget --quiet https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.15.1/cloud-sql-proxy.linux.amd64 \
136+
-O ${HOME}/cloud-sql-proxy && chmod +x ${HOME}/cloud-sql-proxy
137+
${HOME}/cloud-sql-proxy --port 3306 ${MYSQL_INSTANCE} &>> \
138+
${HOME}/cloud-sql-proxy.log &
139139
echo -e "\Cloud SQL proxy started for MySQL."
140-
${HOME}/cloud_sql_proxy -instances="${POSTGRES_INSTANCE}"=tcp:5432,"${POSTGRES_INSTANCE}" -dir "${HOME}" &>> \
141-
${HOME}/cloud_sql_proxy-postgres.log &
140+
${HOME}/cloud-sql-proxy --port 5432 ${POSTGRES_INSTANCE} &>> \
141+
${HOME}/cloud-sql-proxy-postgres.log &
142142
echo -e "\Cloud SQL proxy started for Postgres."
143-
${HOME}/cloud_sql_proxy -instances="${SQLSERVER_INSTANCE}"=tcp:1433 &>> \
144-
${HOME}/cloud_sql_proxy-sqlserver.log &
143+
${HOME}/cloud-sql-proxy --port 1433 ${SQLSERVER_INSTANCE} &>> \
144+
${HOME}/cloud-sql-proxy-sqlserver.log &
145145
echo -e "\Cloud SQL proxy started for SQL Server."
146146

147147
echo -e "\n******************** TESTING PROJECTS ********************"

.kokoro/tests/run_tests_orig.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json
9292
export DATALABELING_ENDPOINT="test-datalabeling.sandbox.googleapis.com:443"
9393

9494
# Run Cloud SQL proxy (background process exit when script does)
95-
wget --quiet https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 \
96-
-O ${HOME}/cloud_sql_proxy && chmod +x ${HOME}/cloud_sql_proxy
97-
${HOME}/cloud_sql_proxy -instances="${MYSQL_INSTANCE}"=tcp:3306 &>> \
98-
${HOME}/cloud_sql_proxy.log &
99-
${HOME}/cloud_sql_proxy -instances="${POSTGRES_INSTANCE}"=tcp:5432 &>> \
100-
${HOME}/cloud_sql_proxy-postgres.log &
95+
wget --quiet https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.15.1/cloud-sql-proxy.linux.amd64 \
96+
-O ${HOME}/cloud-sql-proxy && chmod +x ${HOME}/cloud-sql-proxy
97+
${HOME}/cloud-sql-proxy --port 3306 ${MYSQL_INSTANCE} &>> \
98+
${HOME}/cloud-sql-proxy.log &
99+
${HOME}/cloud-sql-proxy --port 5432 ${POSTGRES_INSTANCE} &>> \
100+
${HOME}/cloud-sql-proxy-postgres.log &
101101
echo -e "\nCloud SQL proxy started."
102102

103103
echo -e "\n******************** TESTING PROJECTS ********************"

appengine/standard/cloudsql/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def connect_to_cloudsql():
5050
# will work if you're running a local MySQL server or using the Cloud SQL
5151
# proxy, for example:
5252
#
53-
# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
53+
# $ cloud-sql-proxy --port 3306 <your-connection-name>
5454
#
5555
else:
5656
db = MySQLdb.connect(

cloud-sql/mysql/client-side-encryption/README.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,101 @@
22

33
## Before you begin
44

5-
1. If you haven't already, set up a Python Development Environment by following the [python setup guide](https://cloud.google.com/python/setup) and
5+
1. If you haven't already, set up a Python Development Environment by following the [python setup guide](https://cloud.google.com/python/setup) and
66
[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).
77

8-
1. Create a 2nd Gen Cloud SQL Instance by following these
8+
1. Create a Cloud SQL Instance by following these
99
[instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string,
1010
database user, and database password that you create.
1111

12-
1. Create a database for your application by following these
12+
1. Create a database for your application by following these
1313
[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database
1414
name.
1515

1616
1. Create a KMS key for your application by following these
1717
[instructions](https://cloud.google.com/kms/docs/creating-keys). Copy the resource name of your
1818
created key.
1919

20-
1. Create a service account with the 'Cloud SQL Client' permissions by following these
21-
[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).
22-
Download a JSON key to use to authenticate your connection.
20+
1. Grant an IAM user or service account the 'Cloud SQL Client' permissions by following these
21+
[instructions](https://cloud.google.com/sql/docs/mysql/roles-and-permissions#introduction).
2322

2423
1. **macOS / Windows only**: Configure gRPC Root Certificates: On some platforms you may need to
2524
accept the Google server certificates, see instructions for setting up
2625
[root certs](https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/bigtable/examples/README.md#configure-grpc-root-certificates).
2726

2827
## Running locally
2928

30-
To run this application locally, download and install the `cloud_sql_proxy` by
29+
Set up Application Default Credentials (ADC) for local development (using the
30+
IAM user or service account who was granted the `Cloud SQL Client` role) by
31+
following these [instructions](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).
32+
33+
To run this application locally, download and install the `cloud-sql-proxy` by
3134
following the instructions
3235
[here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install).
3336

3437
Instructions are provided below for using the proxy with a TCP connection or a Unix Domain Socket.
35-
On Linux or Mac OS you can use either option, but on Windows the proxy currently requires a TCP
36-
connection.
3738

3839
### Launch proxy with TCP
3940

4041
To run the sample locally with a TCP connection, set environment variables and launch the proxy as
4142
shown below.
4243

4344
#### Linux / Mac OS
45+
4446
Use these terminal commands to initialize environment variables:
47+
4548
```bash
46-
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
4749
export DB_HOST='127.0.0.1:3306'
4850
export DB_USER='<DB_USER_NAME>'
4951
export DB_PASS='<DB_PASSWORD>'
5052
export DB_NAME='<DB_NAME>'
5153
export GCP_KMS_URI='<GCP_KMS_URI>'
5254
```
55+
5356
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
5457
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
5558
help keep secrets safe.
5659

5760
Then use this command to launch the proxy in the background:
61+
5862
```bash
59-
./cloud_sql_proxy -instances=<project-id>:<region>:<instance-name>=tcp:3306 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
63+
./cloud-sql-proxy --port 3306 <project-id>:<region>:<instance-name> &
6064
```
6165

6266
#### Windows/PowerShell
67+
6368
Use these PowerShell commands to initialize environment variables:
69+
6470
```powershell
65-
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
6671
$env:DB_HOST="127.0.0.1:3306"
6772
$env:DB_USER="<DB_USER_NAME>"
6873
$env:DB_PASS="<DB_PASSWORD>"
6974
$env:DB_NAME="<DB_NAME>"
7075
$env:GCP_KMS_URI='<GCP_KMS_URI>'
7176
```
77+
7278
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
7379
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
7480
help keep secrets safe.
7581

7682
Then use this command to launch the proxy in a separate PowerShell session:
83+
7784
```powershell
78-
Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<project-id>:<region>:<instance-name>=tcp:3306 -credential_file=<CREDENTIALS_JSON_FILE>"
85+
Start-Process -filepath "C:\<path to cloud-sql-proxy.exe>" -ArgumentList "--port 3306 <project-id>:<region>:<instance-name>"
7986
```
8087

8188
### Launch proxy with Unix Domain Socket
82-
NOTE: this option is currently only supported on Linux and Mac OS. Windows users should use the
83-
[Launch proxy with TCP](#launch-proxy-with-tcp) option.
89+
90+
> [!NOTE]
91+
>
92+
> The Proxy supports Unix domain sockets on recent versions of Windows, but
93+
> replaces colons with periods:
94+
>
95+
> ```shell
96+
> # Starts a Unix domain socket at the path:
97+
> # C:\cloudsql\my-project.my-region.my-instance
98+
> ./cloud-sql-proxy.exe --unix-socket C:\cloudsql my-project:my-region:my-instance
99+
> ```
84100
85101
To use a Unix socket, you'll need to create a directory and give write access to the user running
86102
the proxy. For example:
@@ -91,31 +107,35 @@ sudo chown -R $USER /cloudsql
91107
```
92108
93109
You'll also need to initialize an environment variable containing the directory you just created:
110+
94111
```bash
95112
export DB_SOCKET_DIR=/path/to/the/new/directory
96113
```
97114
98115
Use these terminal commands to initialize other environment variables as well:
116+
99117
```bash
100-
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
101118
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
102119
export DB_USER='<DB_USER_NAME>'
103120
export DB_PASS='<DB_PASSWORD>'
104121
export DB_NAME='<DB_NAME>'
105122
export GCP_KMS_URI='<GCP_KMS_URI>'
106123
```
124+
107125
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
108126
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
109127
help keep secrets safe.
110128

111129
Then use this command to launch the proxy in the background:
130+
112131
```bash
113-
./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
132+
./cloud-sql-proxy --unix-socket $DB_SOCKET_DIR $INSTANCE_CONNECTION_NAME &
114133
```
115134

116135
### Install requirements
117136

118137
Next, setup install the requirements into a virtual environment:
138+
119139
```bash
120140
virtualenv --python python3 env
121141
source env/bin/activate
@@ -125,6 +145,7 @@ pip install -r requirements.txt
125145
### Run the demo
126146

127147
Add new votes and the collected votes:
148+
128149
```bash
129150
python snippets/query_and_decrypt_data.py
130151
```

cloud-sql/postgres/client-side-encryption/README.md

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,104 @@
22

33
## Before you begin
44

5-
1. If you haven't already, set up a Python Development Environment by following the [python setup guide](https://cloud.google.com/python/setup) and
5+
1. If you haven't already, set up a Python Development Environment by following the [python setup guide](https://cloud.google.com/python/setup) and
66
[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).
77

8-
1. Create a 2nd Gen Cloud SQL Instance by following these
8+
1. Create a 2nd Gen Cloud SQL Instance by following these
99
[instructions](https://cloud.google.com/sql/docs/postgres/create-instance). Note the connection string,
1010
database user, and database password that you create.
1111

12-
1. Create a database for your application by following these
12+
1. Create a database for your application by following these
1313
[instructions](https://cloud.google.com/sql/docs/postgres/create-manage-databases). Note the database
1414
name.
1515

1616
1. Create a KMS key for your application by following these
1717
[instructions](https://cloud.google.com/kms/docs/creating-keys). Copy the resource name of your
1818
created key.
1919

20-
1. Create a service account with the 'Cloud SQL Client' permissions by following these
21-
[instructions](https://cloud.google.com/sql/docs/postgres/connect-admin-proxy#create-service-account).
22-
Download a JSON key to use to authenticate your connection.
20+
1. Grant an IAM user or service account the 'Cloud SQL Client' permissions by following these
21+
[instructions](https://cloud.google.com/sql/docs/postgres/roles-and-permissions#introduction).
2322

2423
1. **macOS / Windows only**: Configure gRPC Root Certificates: On some platforms you may need to
2524
accept the Google server certificates, see instructions for setting up
2625
[root certs](https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/bigtable/examples/README.md#configure-grpc-root-certificates).
27-
2826

2927
## Running locally
3028

31-
To run this application locally, download and install the `cloud_sql_proxy` by
29+
Set up Application Default Credentials (ADC) for local development (using the
30+
IAM user or service account who was granted the `Cloud SQL Client` role) by
31+
following these [instructions](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).
32+
33+
To run this application locally, download and install the `cloud-sql-proxy` by
3234
following the instructions [here](https://cloud.google.com/sql/docs/postgres/connect-admin-proxy#install).
3335

3436
Instructions are provided below for using the proxy with a TCP connection or a Unix Domain Socket.
35-
On Linux or Mac OS you can use either option, but on Windows the proxy currently requires a TCP
36-
connection.
3737

3838
### Launch proxy with TCP
3939

4040
To run the sample locally with a TCP connection, set environment variables and launch the proxy as
4141
shown below.
4242

4343
#### Linux / Mac OS
44+
4445
Use these terminal commands to initialize environment variables:
46+
4547
```bash
46-
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
4748
export DB_HOST='127.0.0.1:5432'
4849
export DB_USER='<DB_USER_NAME>'
4950
export DB_PASS='<DB_PASSWORD>'
5051
export DB_NAME='<DB_NAME>'
5152
export GCP_KMS_URI='<GCP_KMS_URI>'
5253
```
54+
5355
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
5456
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/quickstart) to
5557
help keep secrets safe.
5658

5759
Then use this command to launch the proxy in the background:
60+
5861
```bash
59-
./cloud_sql_proxy -instances=<project-id>:<region>:<instance-name>=tcp:5432 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
62+
./cloud-sql-proxy --port 5432 <project-id>:<region>:<instance-name> &
6063
```
6164

6265
Note: if you are running a local Postgres server, you will need to turn it off before running the command above or use a different port.
6366

6467
#### Windows/PowerShell
68+
6569
Use these PowerShell commands to initialize environment variables:
70+
6671
```powershell
67-
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
6872
$env:DB_HOST="127.0.0.1:5432"
6973
$env:DB_USER="<DB_USER_NAME>"
7074
$env:DB_PASS="<DB_PASSWORD>"
7175
$env:DB_NAME="<DB_NAME>"
7276
$env:GCP_KMS_URI='<GCP_KMS_URI>'
7377
```
78+
7479
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
7580
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/quickstart) to
7681
help keep secrets safe.
7782

7883
Then use this command to launch the proxy in a separate PowerShell session:
84+
7985
```powershell
80-
Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<project-id>:<region>:<instance-name>=tcp:5432 -credential_file=<CREDENTIALS_JSON_FILE>"
86+
Start-Process -filepath "C:\<path to cloud-sql-proxy.exe>" -ArgumentList "--port 5432 <project-id>:<region>:<instance-name>"
8187
```
8288

8389
Note: if you are running a local Postgres server, you will need to turn it off before running the command above or use a different port.
8490

8591
### Launch proxy with Unix Domain Socket
86-
NOTE: this option is currently only supported on Linux and Mac OS. Windows users should use the
87-
[Launch proxy with TCP](#launch-proxy-with-tcp) option.
92+
93+
> [!NOTE]
94+
>
95+
> The Proxy supports Unix domain sockets on recent versions of Windows, but
96+
> replaces colons with periods:
97+
>
98+
> ```shell
99+
> # Starts a Unix domain socket at the path:
100+
> # C:\cloudsql\my-project.my-region.my-instance
101+
> ./cloud-sql-proxy.exe --unix-socket C:\cloudsql my-project:my-region:my-instance
102+
> ```
88103
89104
To use a Unix socket, you'll need to create a directory for the sockets and
90105
initialize an environment variable containing the directory you just created.
@@ -95,26 +110,29 @@ export DB_SOCKET_DIR=$(mktemp -d cloudsql)
95110
```
96111
97112
Use these terminal commands to initialize other environment variables as well:
113+
98114
```bash
99-
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
100115
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
101116
export DB_USER='<DB_USER_NAME>'
102117
export DB_PASS='<DB_PASSWORD>'
103118
export DB_NAME='<DB_NAME>'
104119
export GCP_KMS_URI='<GCP_KMS_URI>'
105120
```
121+
106122
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
107123
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/quickstart) to
108124
help keep secrets safe.
109125
110126
Then use this command to launch the proxy in the background:
127+
111128
```bash
112-
./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
129+
./cloud-sql-proxy --unix-socket $DB_SOCKET_DIR $INSTANCE_CONNECTION_NAME &
113130
```
114131
115132
### Install requirements
116133
117134
Next, setup install the requirements into a virtual environment:
135+
118136
```bash
119137
virtualenv --python python3 env
120138
source env/bin/activate
@@ -124,11 +142,13 @@ pip install -r requirements.txt
124142
### Run the demo
125143
126144
Add new votes:
145+
127146
```bash
128147
python snippets/encrypt_and_insert_data.py
129148
```
130149
131150
View the collected votes:
151+
132152
```bash
133153
python snippets/query_and_decrypt_data.py
134154
```

0 commit comments

Comments
 (0)