feat(secretmanager): Add Cloud SQL managed-rotation samples - #14562
feat(secretmanager): Add Cloud SQL managed-rotation samples#14562suvidha-malaviya wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces new regional samples and corresponding tests for Google Cloud Secret Manager, specifically covering creating secrets with Cloud SQL credentials, enabling managed rotation, and triggering rotation. Additionally, the google-cloud-secret-manager dependency is updated to version 2.30.0 in requirements.txt. However, there are critical runtime issues in both enable_regional_secret_managed_rotation.py and rotate_regional_secret.py where the API request payloads incorrectly use the key parent instead of name for the secret's resource name, which will result in a ValueError at runtime.
| # Build the resource name of the secret. | ||
| parent = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | ||
|
|
||
| # Enable managed rotation. Leaving password unset lets Secret Manager | ||
| # generate a secure password itself. | ||
| response = client.enable_managed_rotation( | ||
| request={ | ||
| "parent": parent, | ||
| "cloud_sql_single_user_credentials": { | ||
| "instance_id": instance_id, | ||
| "username": username, | ||
| }, | ||
| } | ||
| ) |
There was a problem hiding this comment.
In EnableManagedRotationRequest, the field to specify the secret's resource name is name, not parent. Using parent as the key in the request dictionary will cause a ValueError at runtime because the field does not exist on the request message.
Please update the request dictionary key to name (and consider renaming the local variable parent to name for clarity).
| # Build the resource name of the secret. | |
| parent = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | |
| # Enable managed rotation. Leaving password unset lets Secret Manager | |
| # generate a secure password itself. | |
| response = client.enable_managed_rotation( | |
| request={ | |
| "parent": parent, | |
| "cloud_sql_single_user_credentials": { | |
| "instance_id": instance_id, | |
| "username": username, | |
| }, | |
| } | |
| ) | |
| # Build the resource name of the secret. | |
| name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | |
| # Enable managed rotation. Leaving password unset lets Secret Manager | |
| # generate a secure password itself. | |
| response = client.enable_managed_rotation( | |
| request={ | |
| "name": name, | |
| "cloud_sql_single_user_credentials": { | |
| "instance_id": instance_id, | |
| "username": username, | |
| }, | |
| } | |
| ) |
There was a problem hiding this comment.
Verified against the installed SDK — both enable_managed_rotation and rotate_secret only define a parent field (no name), so parent is correct here.
| # Build the resource name of the secret. | ||
| parent = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | ||
|
|
||
| # Rotate the secret. | ||
| response = client.rotate_secret(request={"parent": parent}) |
There was a problem hiding this comment.
In RotateSecretRequest, the field to specify the secret's resource name is name, not parent. Using parent as the key in the request dictionary will cause a ValueError at runtime because the field does not exist on the request message.
Please update the request dictionary key to name (and consider renaming the local variable parent to name for clarity).
| # Build the resource name of the secret. | |
| parent = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | |
| # Rotate the secret. | |
| response = client.rotate_secret(request={"parent": parent}) | |
| # Build the resource name of the secret. | |
| name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | |
| # Rotate the secret. | |
| response = client.rotate_secret(request={"name": name}) |
|
Here is the summary of changes. You are about to add 3 region tags.
This comment is generated by snippet-bot.
|
Added samples for Secret Manager's Cloud SQL managed-rotation feature (regional secrets only — this feature isn't available for global secrets)
Added a test case for creating the Cloud SQL DB credentials secret type.
Note: requires google-cloud-secret-manager>=2.30.0, which itself requires Python>=3.10 — this feature does not exist in 2.29.0 or earlier.
##Checklist
nox -s py-3.9(see Test Environment Setup)nox -s lint(see Test Environment Setup)-
CLOUD_SQL_INSTANCE/CLOUD_SQL_USER— a pre-provisioned, long-lived Cloud SQL instance + DB user for managed-rotation tests to point at (same pattern as this repo's other Cloud SQL-backed sample tests)- The identity running these tests additionally needs
resourcemanager.projects.getIamPolicy/setIamPolicyon the test project (e.g.roles/resourcemanager.projectIamAdmin)