|
| 1 | +# Encrypting fields in Cloud SQL - MySQL with Tink |
| 2 | + |
| 3 | +## Before you begin |
| 4 | + |
| 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 |
| 6 | +[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project). |
| 7 | + |
| 8 | +1. Create a 2nd Gen Cloud SQL Instance by following these |
| 9 | +[instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string, |
| 10 | +database user, and database password that you create. |
| 11 | + |
| 12 | +1. Create a database for your application by following these |
| 13 | +[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database |
| 14 | +name. |
| 15 | + |
| 16 | +1. Create a KMS key for your application by following these |
| 17 | +[instructions](https://cloud.google.com/kms/docs/creating-keys). Copy the resource name of your |
| 18 | +created key. |
| 19 | + |
| 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. |
| 23 | + |
| 24 | +1. **macOS / Windows only**: Configure gRPC Root Certificates: On some platforms you may need to |
| 25 | +accept the Google server certificates, see instructions for setting up |
| 26 | +[root certs](https://github.com/googleapis/google-cloud-cpp/blob/master/google/cloud/bigtable/examples/README.md#configure-grpc-root-certificates). |
| 27 | + |
| 28 | +## Running locally |
| 29 | + |
| 30 | +To run this application locally, download and install the `cloud_sql_proxy` by |
| 31 | +following the instructions |
| 32 | +[here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install). |
| 33 | + |
| 34 | +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. |
| 37 | + |
| 38 | +### Launch proxy with TCP |
| 39 | + |
| 40 | +To run the sample locally with a TCP connection, set environment variables and launch the proxy as |
| 41 | +shown below. |
| 42 | + |
| 43 | +#### Linux / Mac OS |
| 44 | +Use these terminal commands to initialize environment variables: |
| 45 | +```bash |
| 46 | +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json |
| 47 | +export DB_HOST='127.0.0.1:3306' |
| 48 | +export DB_USER='<DB_USER_NAME>' |
| 49 | +export DB_PASS='<DB_PASSWORD>' |
| 50 | +export DB_NAME='<DB_NAME>' |
| 51 | +export GCP_KMS_URI='<GCP_KMS_URI>' |
| 52 | +``` |
| 53 | +Note: Saving credentials in environment variables is convenient, but not secure - consider a more |
| 54 | +secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to |
| 55 | +help keep secrets safe. |
| 56 | + |
| 57 | +Then use this command to launch the proxy in the background: |
| 58 | +```bash |
| 59 | +./cloud_sql_proxy -instances=<project-id>:<region>:<instance-name>=tcp:3306 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS & |
| 60 | +``` |
| 61 | + |
| 62 | +#### Windows/PowerShell |
| 63 | +Use these PowerShell commands to initialize environment variables: |
| 64 | +```powershell |
| 65 | +$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>" |
| 66 | +$env:DB_HOST="127.0.0.1:3306" |
| 67 | +$env:DB_USER="<DB_USER_NAME>" |
| 68 | +$env:DB_PASS="<DB_PASSWORD>" |
| 69 | +$env:DB_NAME="<DB_NAME>" |
| 70 | +$env:GCP_KMS_URI='<GCP_KMS_URI>' |
| 71 | +``` |
| 72 | +Note: Saving credentials in environment variables is convenient, but not secure - consider a more |
| 73 | +secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to |
| 74 | +help keep secrets safe. |
| 75 | + |
| 76 | +Then use this command to launch the proxy in a separate PowerShell session: |
| 77 | +```powershell |
| 78 | +Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<project-id>:<region>:<instance-name>=tcp:3306 -credential_file=<CREDENTIALS_JSON_FILE>" |
| 79 | +``` |
| 80 | + |
| 81 | +### 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. |
| 84 | + |
| 85 | +To use a Unix socket, you'll need to create a directory and give write access to the user running |
| 86 | +the proxy. For example: |
| 87 | + |
| 88 | +```bash |
| 89 | +sudo mkdir /cloudsql |
| 90 | +sudo chown -R $USER /cloudsql |
| 91 | +``` |
| 92 | + |
| 93 | +You'll also need to initialize an environment variable containing the directory you just created: |
| 94 | +```bash |
| 95 | +export DB_SOCKET_DIR=/path/to/the/new/directory |
| 96 | +``` |
| 97 | + |
| 98 | +Use these terminal commands to initialize other environment variables as well: |
| 99 | +```bash |
| 100 | +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json |
| 101 | +export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>' |
| 102 | +export DB_USER='<DB_USER_NAME>' |
| 103 | +export DB_PASS='<DB_PASSWORD>' |
| 104 | +export DB_NAME='<DB_NAME>' |
| 105 | +export GCP_KMS_URI='<GCP_KMS_URI>' |
| 106 | +``` |
| 107 | +Note: Saving credentials in environment variables is convenient, but not secure - consider a more |
| 108 | +secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to |
| 109 | +help keep secrets safe. |
| 110 | + |
| 111 | +Then use this command to launch the proxy in the background: |
| 112 | +```bash |
| 113 | +./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS & |
| 114 | +``` |
| 115 | + |
| 116 | +### Install requirements |
| 117 | + |
| 118 | +Next, setup install the requirements into a virtual enviroment: |
| 119 | +```bash |
| 120 | +virtualenv --python python3 env |
| 121 | +source env/bin/activate |
| 122 | +pip install -r requirements.txt |
| 123 | +``` |
| 124 | + |
| 125 | +### Run the demo |
| 126 | + |
| 127 | +Add new votes and the collected votes: |
| 128 | +```bash |
| 129 | +python snippets/query_and_decrypt_data.py |
| 130 | +``` |
0 commit comments