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,
1010database 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
1414name.
1515
16161 . 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
1818created 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
24231 . ** macOS / Windows only** : Configure gRPC Root Certificates: On some platforms you may need to
2524accept 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
3234following the instructions [ here] ( https://cloud.google.com/sql/docs/postgres/connect-admin-proxy#install ) .
3335
3436Instructions 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
4040To run the sample locally with a TCP connection, set environment variables and launch the proxy as
4141shown below.
4242
4343#### Linux / Mac OS
44+
4445Use these terminal commands to initialize environment variables:
46+
4547``` bash
46- export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
4748export DB_HOST=' 127.0.0.1:5432'
4849export DB_USER=' <DB_USER_NAME>'
4950export DB_PASS=' <DB_PASSWORD>'
5051export DB_NAME=' <DB_NAME>'
5152export GCP_KMS_URI=' <GCP_KMS_URI>'
5253```
54+
5355Note: Saving credentials in environment variables is convenient, but not secure - consider a more
5456secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/quickstart ) to
5557help keep secrets safe.
5658
5759Then 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
6265Note: 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+
6569Use 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+
7479Note: Saving credentials in environment variables is convenient, but not secure - consider a more
7580secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/quickstart ) to
7681help keep secrets safe.
7782
7883Then 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
8389Note: 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:\c loudsql my-project:my-region:my-instance
102+ > ` ` `
88103
89104To use a Unix socket, you' ll need to create a directory for the sockets and
90105initialize an environment variable containing the directory you just created.
@@ -95,26 +110,29 @@ export DB_SOCKET_DIR=$(mktemp -d cloudsql)
95110```
96111
97112Use these terminal commands to initialize other environment variables as well:
113+
98114```bash
99- export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
100115export INSTANCE_CONNECTION_NAME=' < MY-PROJECT> :< INSTANCE-REGION> :< INSTANCE-NAME> '
101116export DB_USER=' < DB_USER_NAME> '
102117export DB_PASS=' < DB_PASSWORD> '
103118export DB_NAME=' < DB_NAME> '
104119export GCP_KMS_URI=' < GCP_KMS_URI> '
105120```
121+
106122Note: Saving credentials in environment variables is convenient, but not secure - consider a more
107123secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/quickstart) to
108124help keep secrets safe.
109125
110126Then 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
117134Next, setup install the requirements into a virtual environment:
135+
118136```bash
119137virtualenv --python python3 env
120138source env/bin/activate
@@ -124,11 +142,13 @@ pip install -r requirements.txt
124142### Run the demo
125143
126144Add new votes:
145+
127146```bash
128147python snippets/encrypt_and_insert_data.py
129148```
130149
131150View the collected votes:
151+
132152```bash
133153python snippets/query_and_decrypt_data.py
134154```
0 commit comments