|
| 1 | +# Cloud Run End User Authentication with PostgreSQL Database Sample |
| 2 | + |
| 3 | +This sample integrates with the Identity Platform to authenticate users to the |
| 4 | +application and connects to a Cloud SQL postgreSQL database for data storage. |
| 5 | + |
| 6 | +Use it with the [End user Authentication for Cloud Run](http://cloud.google.com/run/docs/tutorials/identity-platform). |
| 7 | + |
| 8 | +For more details on how to work with this sample read the [Google Cloud Run Python Samples README](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/run). |
| 9 | + |
| 10 | +[](https://deploy.cloud.run) |
| 11 | + |
| 12 | +## Dependencies |
| 13 | + |
| 14 | +* **flask**: web server framework |
| 15 | +* **google-cloud-secret-manager**: Google Secret Manager client library |
| 16 | +* **firebase-admin**: verifying JWT token |
| 17 | +* **sqlalchemy + pg8000**: postgresql interface |
| 18 | +* **Firebase JavaScript SDK**: client-side library for authentication flow |
| 19 | + |
| 20 | +## Environment Variables |
| 21 | + |
| 22 | +Cloud Run services can be [configured with Environment Variables](https://cloud.google.com/run/docs/configuring/environment-variables). |
| 23 | +Required variables for this sample include: |
| 24 | + |
| 25 | +* `CLOUD_SQL_CREDENTIALS_SECRET`: the resource ID of the secret, in format: `projects/PROJECT_ID/secrets/SECRET_ID/versions/VERSION`. See [postgres-secrets.json](postgres-secrets.json) for secret content. |
| 26 | + |
| 27 | +OR |
| 28 | + |
| 29 | +* `CLOUD_SQL_CONNECTION_NAME`: Cloud SQL instance name, in format: `<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>` |
| 30 | +* `DB_NAME`: Cloud SQL postgreSQL database name |
| 31 | +* `DB_USER`: database user |
| 32 | +* `DB_PASSWORD`: database password |
| 33 | + |
| 34 | +Other environment variables: |
| 35 | + |
| 36 | +* Set `TABLE` to change the postgreSQL database table name. |
| 37 | + |
| 38 | +* Set `DB_HOST` to use the proxy with TCP. See instructions below. |
| 39 | + |
| 40 | +* Set `DB_SOCKET_PATH` to change the directory when using the proxy with Unix sockets. |
| 41 | + See instructions below. |
| 42 | + |
| 43 | +## Production Considerations |
| 44 | + |
| 45 | +* Both `postgres-secrets.json` and `static/config.js` should not be committed to |
| 46 | + a git repository and should be added to `.gitignore`. |
| 47 | + |
| 48 | +* Saving credentials directly as environment variables is convenient for local testing, |
| 49 | + but not secure for production; therefore using `CLOUD_SQL_CREDENTIALS_SECRET` |
| 50 | + in combination with the Cloud Secrets Manager is recommended. |
| 51 | + |
| 52 | +## Running Locally |
| 53 | + |
| 54 | +1. Set [environment variables](#environment-variables). |
| 55 | + |
| 56 | +1. To run this application locally, download and install the `cloud_sql_proxy` by |
| 57 | +[following the instructions](https://cloud.google.com/sql/docs/postgres/sql-proxy#install). |
| 58 | + |
| 59 | +The proxy can be used with a TCP connection or a Unix Domain Socket. On Linux or |
| 60 | +Mac OS you can use either option, but on Windows the proxy currently requires a TCP |
| 61 | +connection. |
| 62 | + |
| 63 | +[Instructions to launch proxy with Unix Domain Socket](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/cloud-sql/postgres/sqlalchemy#launch-proxy-with-unix-domain-socket) |
| 64 | + |
| 65 | +[Instructions to launch proxy with TCP](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/cloud-sql/postgres/sqlalchemy#launch-proxy-with-tcp) |
| 66 | + |
| 67 | + |
| 68 | +## Testing |
| 69 | + |
| 70 | +Tests expect the Cloud SQL instance to already be created and environment Variables |
| 71 | +to be set. |
| 72 | + |
| 73 | +### Unit tests |
| 74 | + |
| 75 | +``` |
| 76 | +pytest test/test_app.py |
| 77 | +``` |
| 78 | + |
| 79 | +### System Tests |
| 80 | + |
| 81 | +``` |
| 82 | +export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID> |
| 83 | +export CLOUD_SQL_CONNECTION_NAME=<YOUR_CLOUD_SQL_CONNECTION_NAME> |
| 84 | +export DB_PASSWORD=<POSTGRESQL_PASSWORD> |
| 85 | +export IDP_KEY=<IDENTITY_PLATFORM_API_KEY> # See tutorial for creation of this key ("API_KEY") |
| 86 | +pytest test/e2e_test.py |
| 87 | +``` |
0 commit comments