Repository navigation
feat(storage): add bucket IP filtering samples and tests - #14594
nidhiii-27 wants to merge 5 commits into
Conversation
Add Python code samples and tests demonstrating Cloud Storage Bucket IP filtering. Fixes: b/544985518 [Generated-by: AI]
There was a problem hiding this comment.
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.
Address review feedback regarding self-assignment, defensive None checks, and CLI arg handling. [Generated-by: AI]
|
Here is the summary of changes. You are about to add 6 region tags.
This comment is generated by snippet-bot.
|
|
why all the kokoro tests are failing ? |
The kokoro configs have been disabled hence these tests do not run at all. |
shradhakatyal
left a comment
There was a problem hiding this comment.
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.
[Generated-by: AI]
shradhakatyal
left a comment
There was a problem hiding this comment.
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.
…a pattern - Simplify enable_ip_filtering to take bucket_name only, verify ip_filter exists, set mode to Enabled, reassign, patch, print confirmation, and return bucket. - Simplify get_ip_filtering to display mode and print serialized ip_filter dictionary. - Streamline delete_ip_filtering_rules logic by eliminating unnecessary modification flags. - Remove projection="full" in list_buckets_ip_filtering and handle potential None mode. - Pin google-cloud-storage==3.14.1 in requirements.txt. - Update bucket_ip_filter_test.py to match simplified signatures, add None check on public_network_source, and handle IP filter network constraints. [Generated-by: AI]
shradhakatyal
left a comment
There was a problem hiding this comment.
Thank you for updating requirements.txt, storage_list_buckets_ip_filtering.py, and the None check in bucket_ip_filter_test.py. Please address the inline comments below regarding the changes in the latest commit.
| print(f"IP Filter mode: {ip_filter.mode}") | ||
| print(f"IP Filter configuration: {ip_filter._to_api_resource()}") |
There was a problem hiding this comment.
_to_api_resource() is a private internal helper on IPFilter and should not be used in public documentation samples. Please print the public attributes of IPFilter directly (as done in the previous revision) so users can see how to inspect the configuration fields.
| print(f"IP Filter mode: {ip_filter.mode}") | |
| print(f"IP Filter configuration: {ip_filter._to_api_resource()}") | |
| print(f"IP Filter Configuration for {bucket_name}:") | |
| print(f"Mode: {ip_filter.mode}") | |
| print( | |
| f"Allow All Service Agent Access: {ip_filter.allow_all_service_agent_access}" | |
| ) | |
| print(f"Allow Cross Org VPCs: {ip_filter.allow_cross_org_vpcs}") | |
| if ip_filter.public_network_source: | |
| print( | |
| "Public CIDR Ranges:" | |
| f" {ip_filter.public_network_source.allowed_ip_cidr_ranges}" | |
| ) | |
| if ip_filter.vpc_network_sources: | |
| for vpc in ip_filter.vpc_network_sources: | |
| print( | |
| f"VPC Network: {vpc.network}, CIDR Ranges:" | |
| f" {vpc.allowed_ip_cidr_ranges}" | |
| ) |
There was a problem hiding this comment.
Done. Replaced the internal helper _to_api_resource() with direct inspection of public attributes (mode, allow_all_service_agent_access, allow_cross_org_vpcs, public_network_source, and vpc_network_sources).
Co-authored by AI Agent
| except (exceptions.Forbidden, exceptions.BadRequest) as e: | ||
| pytest.skip( | ||
| "Skipping test due to insufficient permissions or IP filter" | ||
| f" network restriction: {e}" | ||
| ) |
There was a problem hiding this comment.
Catching exceptions.BadRequest and calling pytest.skip() will mask 400 Bad Request errors caused by invalid ipFilter payloads in the samples. Please do not skip on BadRequest.
Also, in the test_bucket fixture teardown, please attempt to disable IP filtering before deleting the bucket (similar to the Java test teardown) so buckets are cleaned up if an assertion fails while IP filtering is enabled.
There was a problem hiding this comment.
Done. Removed exceptions.BadRequest from the skip handler so 400 errors are not masked, added defensive IP filter disabling during test_bucket fixture teardown before bucket deletion, and updated enable_ip_filtering call in the lifecycle test with VPC network parameters.
Co-authored by AI Agent
| def enable_ip_filtering(bucket_name: str) -> storage.Bucket: | ||
| """Enables IP filtering on an existing bucket.""" | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "your-bucket-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
|
|
||
| ip_filter = bucket.ip_filter | ||
| if not ip_filter: | ||
| print(f"No IP filter configuration found for bucket {bucket_name}.") | ||
| return bucket | ||
|
|
||
| ip_filter.mode = "Enabled" | ||
| # Re-assign to the bucket property to force google-cloud-storage to register | ||
| # the nested changes for the patch() call. | ||
| bucket.ip_filter = ip_filter | ||
| bucket.patch() | ||
|
|
||
| print(f"Enabled IP filtering for bucket {bucket.name}.") | ||
| return bucket |
There was a problem hiding this comment.
The storage_enable_ip_filtering region tag documents how to create or update IP filtering rules on an existing bucket. With the current simplification:
- Calling
enable_ip_filteringon an existing bucket without pre-configured rules exits early without enabling IP filtering. - None of the samples show how to configure
VpcNetworkSource. - In
bucket_ip_filter_test.py,vpc_network_to_delete=vpc_networkbecomes a no-op becausevpc_networkis never added to the bucket.
Please restore the public_range and vpc_network configuration in enable_ip_filtering (with the if public_range: guard) and update the call in bucket_ip_filter_test.py.
There was a problem hiding this comment.
Done. Restored public_range, vpc_network, and vpc_range configuration in enable_ip_filtering (with defensive None guards and PEP 484 type annotations), enabling both initial IP filter configuration and VPC network source addition on existing buckets. Updated test calls accordingly.
Co-authored by AI Agent
| bucket.ip_filter = ip_filter | ||
| bucket.patch() |
There was a problem hiding this comment.
Nit: Please keep the explanatory comment before bucket.ip_filter = ip_filter (as in storage_enable_ip_filtering.py and storage_disable_ip_filtering.py) so readers understand why re-assigning ip_filter is necessary after mutating ranges in place.
| bucket.ip_filter = ip_filter | |
| bucket.patch() | |
| # Re-assign to the bucket property to force google-cloud-storage to register | |
| # the nested changes for the patch() call. | |
| bucket.ip_filter = ip_filter | |
| bucket.patch() |
There was a problem hiding this comment.
Done. Added the explanatory comment before bucket.ip_filter = ip_filter.
Co-authored by AI Agent
…nd tests [Generated-by: AI]
Add Python code samples and tests demonstrating Cloud Storage Bucket IP filtering.
Fixes: b/544985518
[Generated-by: AI]