Skip to content

samples(Storage): Add samples and tests for Bucket IP filter#3334

Open
mahendra-google wants to merge 5 commits into
GoogleCloudPlatform:mainfrom
mahendra-google:bucket-ip-filter
Open

samples(Storage): Add samples and tests for Bucket IP filter#3334
mahendra-google wants to merge 5 commits into
GoogleCloudPlatform:mainfrom
mahendra-google:bucket-ip-filter

Conversation

@mahendra-google

@mahendra-google mahendra-google commented May 19, 2026

Copy link
Copy Markdown
Contributor

This PR introduces a set of code samples and corresponding tests for bucket IP filtering .

storage_enable_ip_filtering: Code to create or update IP filtering rules. It handles first-time initialization of the configuration block and appends unique public or VPC CIDR ranges safely without overwriting pre-existing rules.

storage_delete_ip_filtering_rules: Code to selectively remove specific CIDR blocks from PublicNetworkSource or remove specified VpcNetworkSources, ensuring that IP filtering remains active for other public CIDR ranges.

storage_disable_ip_filtering: Code to completely disable IP filtering on a targeted bucket.

storage_get_ip_filtering: Code to retrieve and log the current active IP rules and access flags for an existing bucket.

storage_list_buckets_ip_filtering : Code to iterate through all buckets , outputting a log of their names along with their respective IP filtering status.

@mahendra-google
mahendra-google requested review from a team as code owners May 19, 2026 13:52
@snippet-bot

snippet-bot Bot commented May 19, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 5 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

@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 May 19, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a suite of samples and tests for managing Google Cloud Storage bucket IP filtering, covering operations such as creation, updates, deletion, and bypass rules. The review feedback identifies several improvement opportunities: replacing hardcoded project IDs in tests and samples with dynamic values to ensure portability, optimizing the deletion logic to prevent unnecessary API calls when an IP range is missing, and adding a project ID parameter to the update sample for consistent resource name construction.

Assert.NotNull(publicRanges);
Assert.Contains(newPublicRange, publicRanges);
var vpcNetwork = updatedBucket.IpFilter.VpcNetworkSources?
.FirstOrDefault(v => v.Network == "projects/storage-sdk-vendor/global/networks/default");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The project ID storage-sdk-vendor is hardcoded in the VPC network path assertion. This will cause the test to fail in any environment where the project ID differs. Use _fixture.ProjectId to make the test portable.

            .FirstOrDefault(v => v.Network == $"projects/{_fixture.ProjectId}/global/networks/default");

Comment on lines +37 to +39
bucket.IpFilter.PublicNetworkSource.AllowedIpCidrRanges.Remove(ipAddress);
storage.UpdateBucket(bucket);
Console.WriteLine($"IP Address ({ipAddress}) from Allowed CIDR Ranges has been deleted from the bucket: {bucketName}");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The code performs a bucket update and logs a success message even if the specified IP address was not found in the allowed ranges. It is better to check the return value of Remove() to avoid redundant API calls and provide accurate feedback to the user.

        if (bucket.IpFilter.PublicNetworkSource.AllowedIpCidrRanges.Remove(ipAddress))
        {
            storage.UpdateBucket(bucket);
            Console.WriteLine($"IP Address ({ipAddress}) from Allowed CIDR Ranges has been deleted from the bucket: {bucketName}");
        }
        else
        {
            Console.WriteLine($"IP Address ({ipAddress}) was not found in the Allowed CIDR Ranges for bucket: {bucketName}");
        }

Comment on lines +31 to +32
public Bucket UpdateBucketIpFilter(string bucketName = "your-unique-bucket-name", string publicRange = "192.0.2.0/24",
string vpcRange = "10.0.0.0/24")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This sample should accept a projectId parameter to construct the VPC network resource name dynamically, rather than using a hardcoded placeholder like your-project. This ensures consistency with other samples in this pull request, such as CreateBucketWithIpFilterSample.

    public Bucket UpdateBucketIpFilter(string projectId = "your-project-id", string bucketName = "your-unique-bucket-name", string publicRange = "192.0.2.0/24",
        string vpcRange = "10.0.0.0/24")

}
else
{
string networkName = "projects/your-project/global/networks/default";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Use the projectId parameter to construct the network resource name instead of a hardcoded placeholder.

            string networkName = $"projects/{projectId}/global/networks/default";

string newPublicRange = "192.0.2.0/24";
string newVpcRange = "10.0.0.0/24";
_fixture.CreateBucket(bucketName, multiVersion: false, ipFilter: true, registerForDeletion: true);
var updatedBucket = updateSample.UpdateBucketIpFilter(bucketName, newPublicRange, newVpcRange);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Pass the project ID from the fixture to the updated UpdateBucketIpFilter method.

        var updatedBucket = updateSample.UpdateBucketIpFilter(_fixture.ProjectId, bucketName, newPublicRange, newVpcRange);

@Rebel2169

Rebel2169 commented May 19, 2026 via email

Copy link
Copy Markdown

@mahendra-google

Copy link
Copy Markdown
Contributor Author

my buckets are supposed to be private

Thanks for taking the time to review the code!

Could you please point me to the specific line of code where the buckets are private? I want to make sure I get that fixed and —keeping that data private is definitely a priority for our customers.

Appreciate the help!

@mahendra-google
mahendra-google marked this pull request as draft May 20, 2026 05:21
@mahendra-google

Copy link
Copy Markdown
Contributor Author

I have moved this PR to Draft mode for now. Based on the updates in issue #422766398 (comment 8), the backend tags for the Bucket IP Filter samples are not fully ready. I will mark this back as ready for review as soon as those tags are fixed.

@mahendra-google
mahendra-google marked this pull request as ready for review June 3, 2026 12:05
string newPublicRange = "192.0.2.0/24";
string newVpcRange = "10.0.0.0/24";
_fixture.CreateBucket(bucketName, multiVersion: false, ipFilter: false, registerForDeletion: true);
var updatedBucket = updateSample.EnableBucketIpFilter(projectId, bucketName);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the bucket is updated with the IP, will the test later be able to delete the bucket? If not ,this will turn into a leak. Maybe pass '0.0.0.0/0' to allow the test to delete the bucket later?

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.

Passed 0.0.0.0/0 as the value for newPublicRange and newVpcRange in the EnableBucketIpFilter sample method call within EnableBucketIpFilterTest

int delayMilliseconds = 5000;
bool isPropagationBlocked = false;

for (int i = 0; i < maxRetries; i++)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove this retry as well. If the sample doesn't enable in the first try, just let the test fail to see what is happening.

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.

The retry loop is removed.

Comment thread storage/api/Storage.Samples/EnableBucketIpFilter.cs Outdated
Comment thread storage/api/Storage.Samples/EnableBucketIpFilter.cs
Comment thread storage/api/Storage.Samples.Tests/StorageFixture.cs Outdated
…ateBucket signature after registerForDeletion
…tly in the EnableBucketIpFilterTest

- This will prevent the failure of EnableBucketIpFilterTest in the case of sample optional value changes of vpcRange and publicRange
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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