Repository navigation
Conversation
Granting an IAM principal access to a specific Spanner database role attaches a version-3 IAM condition on roles/spanner.fineGrainedAccessUser. Add spanner_enable_fine_grained_access.
|
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 utility script and module, enable_fine_grained_access.py, to grant conditional IAM fine-grained access to a Google Cloud Spanner database role, along with corresponding unit and live tests. The feedback recommends improving the robustness of the IAM policy update by checking for existing bindings to prevent duplicates, and safely handling the SPANNER_FGAC_IAM_MEMBER environment variable in tests to avoid potential KeyError crashes.
| binding = policy.bindings.add( | ||
| role="roles/spanner.fineGrainedAccessUser", | ||
| members=[iam_member], | ||
| ) | ||
| binding.condition.title = f"fgac_{database_role}" | ||
| binding.condition.expression = condition_expr |
There was a problem hiding this comment.
Directly appending a new binding to the policy can result in duplicate bindings or duplicate members if the function is called multiple times. It is more robust to check if a binding for the specified role and condition already exists, and if so, append the member to it only if they are not already present.
binding = None
for b in policy.bindings:
if (
b.role == "roles/spanner.fineGrainedAccessUser"
and b.condition.expression == condition_expr
):
binding = b
break
if binding is None:
binding = policy.bindings.add(
role="roles/spanner.fineGrainedAccessUser",
)
binding.condition.title = f"fgac_{database_role}"
binding.condition.expression = condition_expr
if iam_member not in binding.members:
binding.members.append(iam_member)There was a problem hiding this comment.
Thanks! Keeping this as is: adding merge logic for existing bindings would distract from what the sample shows, which is adding a conditional binding for a database role.
|
|
||
| @conftest.live_only | ||
| def test_enable_fine_grained_access_live(capsys: pytest.CaptureFixture[str]) -> None: | ||
| iam_member = os.environ["SPANNER_FGAC_IAM_MEMBER"] |
There was a problem hiding this comment.
If SPANNER_FGAC_IAM_MEMBER is not set in the environment, accessing it via os.environ will raise a KeyError and crash the test suite. It is safer to use os.environ.get and skip the test gracefully if the variable is missing.
iam_member = os.environ.get("SPANNER_FGAC_IAM_MEMBER")
if not iam_member:
pytest.skip("SPANNER_FGAC_IAM_MEMBER environment variable not set.")There was a problem hiding this comment.
Good catch, done: the live test now checks for its required environment variables up front and skips with a clear message when one is missing, instead of failing with a KeyError (3ea847b).
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/enable_fine_grained_access_test.py
Description
Adds
spanner_enable_fine_grained_accessbindingroles/spanner.fineGrainedAccessUserwith adatabaseRolesIAM condition.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions