Repository navigation
Conversation
Applications enforcing least-privilege SQL access open a Database handle scoped to a specific database_role. Add a standalone spanner_read_data_with_database_role sample querying Singers.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new script read_data_with_database_role.py along with its unit and live tests to query Spanner data using a fine-grained database role. The review feedback suggests making the project_id parameter and CLI argument optional (defaulting to None or os.environ.get("GOOGLE_CLOUD_PROJECT") instead of a hardcoded string) to allow the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
| def read_data_with_database_role( | ||
| project_id: str, instance_id: str, database_id: str | ||
| ) -> None: |
There was a problem hiding this comment.
Update the project_id type annotation to str | None to allow it to be optional. This allows the client to fall back to Application Default Credentials (ADC) for project detection when no project ID is explicitly provided.
| def read_data_with_database_role( | |
| project_id: str, instance_id: str, database_id: str | |
| ) -> None: | |
| def read_data_with_database_role( | |
| project_id: str | None, instance_id: str, database_id: str | |
| ) -> None: |
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| parser.add_argument( | ||
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) |
There was a problem hiding this comment.
Make the --project-id argument truly optional by defaulting to os.environ.get("GOOGLE_CLOUD_PROJECT") instead of a hardcoded dummy string like "example-project-id". If neither the argument nor the environment variable is set, it will default to None, allowing the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
| parser.add_argument( | |
| "--project-id", | |
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | |
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | |
| ) | |
| parser.add_argument( | |
| "--project-id", | |
| default=os.environ.get("GOOGLE_CLOUD_PROJECT"), | |
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | |
| ) |
References
- When using
argparsefor a command-line script, the--project_idargument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
Description
Adds
spanner_read_data_with_database_rolequeryingSingersusing aDatabaseconfigured withdatabase_role.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions