Skip to content

feat(storage): add bucket IP filtering samples and tests - #14594

Open
nidhiii-27 wants to merge 3 commits into
mainfrom
samples-bucket-ip-filter-python
Open

nidhiii-27 wants to merge 3 commits into
mainfrom
samples-bucket-ip-filter-python

Conversation

@nidhiii-27

Copy link
Copy Markdown
Contributor

Add Python code samples and tests demonstrating Cloud Storage Bucket IP filtering.

Fixes: b/544985518

[Generated-by: AI]

Add Python code samples and tests demonstrating Cloud Storage Bucket IP filtering.

Fixes: b/544985518

[Generated-by: AI]
@product-auto-label product-auto-label Bot added api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. labels Sep 9, 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 Python code snippets and tests demonstrating Google Cloud Storage bucket IP filtering features, including creating, enabling, disabling, retrieving, listing, and deleting IP filtering rules. The review feedback highlights several potential bugs and improvement opportunities: handling potential IndexError exceptions when executing the scripts from the command line without arguments, defensively checking for None values to avoid TypeError exceptions on list operations, and refactoring direct self-assignments to prevent linter warnings or accidental code removal that would break the SDK's change registration.

Comment thread storage/samples/snippets/storage_delete_ip_filtering_rules.py
Comment thread storage/samples/snippets/storage_disable_ip_filtering.py Outdated
Comment thread storage/samples/snippets/storage_delete_ip_filtering_rules.py Outdated
Comment thread storage/samples/snippets/storage_delete_ip_filtering_rules.py
Comment thread storage/samples/snippets/storage_disable_ip_filtering.py
Comment thread storage/samples/snippets/storage_enable_ip_filtering.py
Comment thread storage/samples/snippets/storage_enable_ip_filtering.py
Comment thread storage/samples/snippets/storage_enable_ip_filtering.py
Comment thread storage/samples/snippets/storage_create_bucket_ip_filtering.py Outdated
Comment thread storage/samples/snippets/storage_get_ip_filtering.py
Address review feedback regarding self-assignment, defensive None checks, and CLI arg handling.

[Generated-by: AI]
@nidhiii-27
nidhiii-27 marked this pull request as ready for review September 9, 2026 08:53
@nidhiii-27
nidhiii-27 requested review from a team as code owners September 9, 2026 08:53
@snippet-bot

snippet-bot Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 6 region tags.

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

@chandra-siri

Copy link
Copy Markdown
Contributor

why all the kokoro tests are failing ?

@nidhiii-27

Copy link
Copy Markdown
Contributor Author

why all the kokoro tests are failing ?

The kokoro configs have been disabled hence these tests do not run at all.

@shradhakatyal shradhakatyal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Blocking dependency issue (storage/samples/snippets/requirements.txt):
storage/samples/snippets/requirements.txt currently pins google-cloud-storage==3.1.0, which predates google.cloud.storage.ip_filter (added in 3.3.0). Please bump google-cloud-storage to >=3.3.0 (e.g., 3.14.1) so nox tests do not fail with ModuleNotFoundError.

Comment thread storage/samples/snippets/storage_delete_ip_filtering_rules.py
Comment thread storage/samples/snippets/bucket_ip_filter_test.py
Comment thread storage/samples/snippets/bucket_ip_filter_test.py Outdated
Comment thread storage/samples/snippets/storage_disable_ip_filtering.py Outdated
Comment thread storage/samples/snippets/storage_create_bucket_ip_filtering.py Outdated

@shradhakatyal shradhakatyal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for addressing the previous inline feedback. Please resolve the remaining dependency version issue in requirements.txt and the inline comments below.

Blocking dependency issue (storage/samples/snippets/requirements.txt):
storage/samples/snippets/requirements.txt currently pins google-cloud-storage==3.1.0. The google.cloud.storage.ip_filter module was added in version 3.3.0. Without updating this dependency, nox test sessions will fail with ModuleNotFoundError.

from google.cloud import storage
import pytest

import storage_create_bucket_ip_filtering

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

storage/samples/snippets/requirements.txt currently pins google-cloud-storage==3.1.0. The google.cloud.storage.ip_filter module was introduced in version 3.3.0. Please update storage/samples/snippets/requirements.txt to 3.14.1 so nox tests do not fail with ModuleNotFoundError:

-google-cloud-storage==3.1.0
+google-cloud-storage==3.14.1

Comment on lines +78 to +81
assert (
public_range
not in modified.ip_filter.public_network_source.allowed_ip_cidr_ranges
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When the last CIDR range is removed, the GCS API returns an empty publicNetworkSource object. In google-cloud-storage, IPFilter._from_api_resource() evaluates if public_network_source_data: on {} as False. This sets modified.ip_filter.public_network_source to None, which will raise an AttributeError here. Please check for None before accessing allowed_ip_cidr_ranges.

Suggested change
assert (
public_range
not in modified.ip_filter.public_network_source.allowed_ip_cidr_ranges
)
assert (
modified.ip_filter.public_network_source is None
or public_range
not in modified.ip_filter.public_network_source.allowed_ip_cidr_ranges
)

Comment on lines +26 to +30
buckets = list(storage_client.list_buckets(projection="full"))

for bucket in buckets:
status = bucket.ip_filter.mode if bucket.ip_filter else "Not Configured"
print(f"Bucket: {bucket.name}, IP Filter Mode: {status}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The default noAcl projection in list_buckets() already returns the summarized ipFilter configuration, including mode. Using projection="full" unnecessarily fetches ACL metadata for every bucket and requires extra IAM permissions. Also, please guard against bucket.ip_filter.mode being None.

Suggested change
buckets = list(storage_client.list_buckets(projection="full"))
for bucket in buckets:
status = bucket.ip_filter.mode if bucket.ip_filter else "Not Configured"
print(f"Bucket: {bucket.name}, IP Filter Mode: {status}")
buckets = list(storage_client.list_buckets())
for bucket in buckets:
status = (
bucket.ip_filter.mode
if bucket.ip_filter and bucket.ip_filter.mode
else "Not Configured"
)
print(f"Bucket: {bucket.name}, IP Filter Mode: {status}")

Comment on lines +46 to +55
if ip_filter.public_network_source is None:
ip_filter.public_network_source = PublicNetworkSource(allowed_ip_cidr_ranges=[])
elif ip_filter.public_network_source.allowed_ip_cidr_ranges is None:
ip_filter.public_network_source.allowed_ip_cidr_ranges = []

if (
public_range
and public_range not in ip_filter.public_network_source.allowed_ip_cidr_ranges
):
ip_filter.public_network_source.allowed_ip_cidr_ranges.append(public_range)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: Currently, ip_filter.public_network_source is initialized to an empty PublicNetworkSource even when public_range is empty. This sends "publicNetworkSource": {"allowedIpCidrRanges": []} in the PATCH payload when only VPC rules are configured. Please move the initialization inside if public_range: to match the vpc_network handling below.

Suggested change
if ip_filter.public_network_source is None:
ip_filter.public_network_source = PublicNetworkSource(allowed_ip_cidr_ranges=[])
elif ip_filter.public_network_source.allowed_ip_cidr_ranges is None:
ip_filter.public_network_source.allowed_ip_cidr_ranges = []
if (
public_range
and public_range not in ip_filter.public_network_source.allowed_ip_cidr_ranges
):
ip_filter.public_network_source.allowed_ip_cidr_ranges.append(public_range)
if public_range:
if ip_filter.public_network_source is None:
ip_filter.public_network_source = PublicNetworkSource(
allowed_ip_cidr_ranges=[]
)
elif ip_filter.public_network_source.allowed_ip_cidr_ranges is None:
ip_filter.public_network_source.allowed_ip_cidr_ranges = []
if (
public_range
not in ip_filter.public_network_source.allowed_ip_cidr_ranges
):
ip_filter.public_network_source.allowed_ip_cidr_ranges.append(
public_range
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-generated api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants