Repository navigation
Conversation
Fine-grained access control (FGAC) defines hierarchical SQL roles with CREATE ROLE, GRANT, REVOKE, and DROP ROLE DDL statements. Add a standalone spanner_add_and_drop_database_role sample.
|
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 sample script add_and_drop_database_role.py along with its corresponding tests to demonstrate creating, granting, and dropping fine-grained database roles in Google Cloud Spanner. The feedback suggests improving the live integration test by wrapping the sample execution in a try...finally block to guarantee that the created database roles are properly cleaned up even if the test fails.
| for cleanup_stmt in [ | ||
| "REVOKE ROLE new_parent FROM ROLE new_child", | ||
| "DROP ROLE new_child", | ||
| "REVOKE SELECT ON TABLE Singers FROM ROLE new_parent", | ||
| "DROP ROLE new_parent", | ||
| ]: | ||
| try: | ||
| admin.update_database_ddl( | ||
| request={"database": db_path, "statements": [cleanup_stmt]} | ||
| ).result(timeout=120) | ||
| except exceptions.GoogleAPICallError: | ||
| pass | ||
|
|
||
| add_and_drop_database_role.add_and_drop_database_role( | ||
| conftest.LIVE_PROJECT_ID, | ||
| conftest.LIVE_INSTANCE_ID, | ||
| conftest.LIVE_DATABASE_ID, | ||
| ) |
There was a problem hiding this comment.
The live integration test currently performs cleanup only before running the sample. Since the sample creates new_parent and new_child but only drops new_child, the new_parent role is left in the database after a successful run. Additionally, if the test fails midway, both roles may remain. Wrapping the sample execution in a try...finally block ensures that resources are always cleaned up, keeping the live test database clean.
def cleanup():
for cleanup_stmt in [
"REVOKE ROLE new_parent FROM ROLE new_child",
"DROP ROLE new_child",
"REVOKE SELECT ON TABLE Singers FROM ROLE new_parent",
"DROP ROLE new_parent",
]:
try:
admin.update_database_ddl(
request={"database": db_path, "statements": [cleanup_stmt]}
).result(timeout=120)
except exceptions.GoogleAPICallError:
pass
cleanup()
try:
add_and_drop_database_role.add_and_drop_database_role(
conftest.LIVE_PROJECT_ID,
conftest.LIVE_INSTANCE_ID,
conftest.LIVE_DATABASE_ID,
)
finally:
cleanup()References
- Maintain consistency in test structures by matching the patterns employed by existing or neighboring tests in the same directory, such as using a try...finally block with a teardown function for resource cleanup.
There was a problem hiding this comment.
Good catch, done: the role cleanup now also runs in a finally block, so new_parent is not left behind after the test (5c3ce28).
Problem: review of this sample identified correctness and robustness improvements in the sample and its tests. Solution: apply the accepted review suggestions and extend the tests to cover the updated behavior. Details are in the review thread replies. Files updated: - spanner/database_admin/tests/add_and_drop_database_role_test.py
Description
Adds
spanner_add_and_drop_database_rolecreating, granting, revoking, and dropping fine-grained Spanner database roles.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions