Skip to content

feat(spanner): Add conditional IAM fine-grained access binding sample - #14960

Open
iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-enable-fine-grained-access
Open

iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-enable-fine-grained-access

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_enable_fine_grained_access binding roles/spanner.fineGrainedAccessUser with a databaseRoles IAM condition.

Note: Stacked on #14943 (feat/spanner-create-database) so this PR shows only this sample's diff. It will merge into main after #14943.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

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.
@iennae
iennae requested review from a team as code owners November 18, 2026 22:18
@snippet-bot

snippet-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +49 to +54
binding = policy.bindings.add(
role="roles/spanner.fineGrainedAccessUser",
members=[iam_member],
)
binding.condition.title = f"fgac_{database_role}"
binding.condition.expression = condition_expr

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant