Skip to content

Commit 7f1fb1b

Browse files
authored
feat(run): Update sample to use Secret Manager Integration (GoogleCloudPlatform#6251)
## Description Fixes #<ISSUE-NUMBER> Note: It's a good idea to open an issue first for discussion. ## Checklist - [ ] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file) - [ ] **Tests** pass: `nox -s py-3.6` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] Please **merge** this PR for me once it is approved. - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.github/CODEOWNERS) with the codeowners for this sample
1 parent bfa2361 commit 7f1fb1b

File tree

5 files changed

+9
-15
lines changed

5 files changed

+9
-15
lines changed

run/idp-sql/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ For more details on how to work with this sample read the [Google Cloud Run Pyth
1212
## Dependencies
1313

1414
* **flask**: web server framework
15-
* **google-cloud-secret-manager**: Google Secret Manager client library
1615
* **firebase-admin**: verifying JWT token
1716
* **sqlalchemy + pg8000**: postgresql interface
1817
* **Firebase JavaScript SDK**: client-side library for authentication flow
@@ -22,7 +21,7 @@ For more details on how to work with this sample read the [Google Cloud Run Pyth
2221
Cloud Run services can be [configured with Environment Variables](https://cloud.google.com/run/docs/configuring/environment-variables).
2322
Required variables for this sample include:
2423

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.
24+
* `CLOUD_SQL_CREDENTIALS_SECRET`: the resource ID of the secret, in format: `projects/PROJECT_ID/secrets/SECRET_ID/versions/VERSION` when deployed to Cloud Run. At runtime, Cloud Run will inject the secret value as an environment variable, for more info see [Using secrets](https://cloud.google.com/run/docs/configuring/secrets#command-line). See [postgres-secrets.json](postgres-secrets.json) for secret content.
2625

2726
OR
2827

run/idp-sql/credentials.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@
1616
import os
1717
from typing import Dict
1818

19-
from google.cloud import secretmanager
20-
2119
from middleware import logger
2220

2321

2422
# [START cloudrun_user_auth_secrets]
2523
def get_cred_config() -> Dict[str, str]:
26-
if "CLOUD_SQL_CREDENTIALS_SECRET" in os.environ:
27-
name = os.environ["CLOUD_SQL_CREDENTIALS_SECRET"]
28-
client = secretmanager.SecretManagerServiceClient()
29-
response = client.access_secret_version(request={"name": name})
30-
logger.info("Credentials pulled from CLOUD_SQL_CREDENTIALS_SECRET")
31-
return json.loads(response.payload.data.decode("UTF-8"))
24+
secret = os.environ.get("CLOUD_SQL_CREDENTIALS_SECRET")
25+
if secret:
26+
return json.loads(secret)
3227
# [END cloudrun_user_auth_secrets]
3328
else:
3429
logger.info(

run/idp-sql/postcreate.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export SERVICE_ACCOUNT="idp-sql-identity"
2020

2121
# Update Cloud Run service to include Cloud SQL instance, Secret Manager secret,
2222
# and service account
23-
gcloud run services update ${K_SERVICE} \
23+
gcloud beta run services update ${K_SERVICE} \
2424
--platform managed \
2525
--region ${GOOGLE_CLOUD_REGION} \
2626
--service-account ${SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
2727
--add-cloudsql-instances ${GOOGLE_CLOUD_PROJECT}:${GOOGLE_CLOUD_REGION}:${CLOUD_SQL_INSTANCE_NAME} \
28-
--update-env-vars CLOUD_SQL_CREDENTIALS_SECRET=projects/${GOOGLE_CLOUD_PROJECT}/secrets/${SECRET_NAME}/versions/latest
28+
--update-secrets CLOUD_SQL_CREDENTIALS_SECRET=${SECRET_NAME}:latest

run/idp-sql/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ SQLAlchemy==1.4.11
33
pg8000==1.19.5
44
gunicorn==20.1.0
55
firebase-admin==5.0.1
6-
google-cloud-secret-manager==2.5.0
76
structlog==21.1.0

run/idp-sql/test/e2e_test_setup.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ steps:
6363
args:
6464
- '-c'
6565
- |
66-
./test/retry.sh "gcloud run deploy ${_SERVICE} \
66+
./test/retry.sh "gcloud beta run deploy ${_SERVICE} \
6767
--project $PROJECT_ID \
6868
--image gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} \
6969
--allow-unauthenticated \
7070
--region ${_REGION} \
7171
--platform ${_PLATFORM} \
7272
--add-cloudsql-instances ${_CLOUD_SQL_CONNECTION_NAME} \
73-
--update-env-vars CLOUD_SQL_CREDENTIALS_SECRET=projects/${PROJECT_ID}/secrets/${_SERVICE}-secrets/versions/latest,TRAMPOLINE_CI=kokoro"
73+
--update-secrets CLOUD_SQL_CREDENTIALS_SECRET=${_SERVICE}-secrets:latest \
74+
--update-env-vars TRAMPOLINE_CI=kokoro"
7475
7576
images:
7677
- gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}

0 commit comments

Comments
 (0)