Repository navigation
feat(storage): add bucket IP filtering samples and tests #14328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9dce71
feat(storage): add bucket IP filtering samples and tests
nidhiii-27 e63a822
fix(storage): resolve PR comments for bucket IP filtering samples
nidhiii-27 596323f
fix(storage): merge main to resolve Kokoro SCM checkout failure
nidhiii-27 92ff60a
Merge branch 'main' into samples-bucket-ip-filter-java
nidhiii-27 c09fbfd
fix(storage): simplify bucket IP filter samples based on review comments
nidhiii-27 d743008
fix(storage): simplify DeleteBucketIpFilter using direct builder updates
nidhiii-27 24a0fdb
docs(storage): clarify rule removal vs clearing IP filter in DeleteBu…
nidhiii-27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...orage/samples/snippets/src/main/java/com/example/storage/bucket/CreateBucketIpFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_create_bucket_ip_filtering] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter.PublicNetworkSource; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.util.Arrays; | ||
|
|
||
| public class CreateBucketIpFilter { | ||
| public static void createBucketIpFilter( | ||
| String projectId, String bucketName, String publicCidrRange) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID to give your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The public IPv4 CIDR range to allow | ||
| // String publicCidrRange = "192.0.2.0/24"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
|
|
||
| IpFilter ipFilter = | ||
| IpFilter.newBuilder() | ||
| .setMode("Disabled") | ||
| .setAllowAllServiceAgentAccess(true) | ||
| .setPublicNetworkSource(PublicNetworkSource.of(Arrays.asList(publicCidrRange))) | ||
| .build(); | ||
|
|
||
| BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setIpFilter(ipFilter).build(); | ||
|
|
||
| Bucket bucket = storage.create(bucketInfo); | ||
|
|
||
| System.out.println( | ||
| "Created bucket " + bucketName + " with IP filter mode " + bucket.getIpFilter().getMode()); | ||
| } | ||
| } | ||
| // [END storage_create_bucket_ip_filtering] |
75 changes: 75 additions & 0 deletions
75
...orage/samples/snippets/src/main/java/com/example/storage/bucket/DeleteBucketIpFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_delete_ip_filtering_rules] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter.PublicNetworkSource; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter.VpcNetworkSource; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class DeleteBucketIpFilter { | ||
| public static Bucket deleteBucketIpFilterRules( | ||
| String projectId, String bucketName, String publicRange, String vpcNetwork) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The public IPv4/IPv6 CIDR range to remove | ||
| // String publicRange = "192.0.2.0/24"; | ||
|
|
||
| // The VPC network name to remove | ||
| // String vpcNetwork = "projects/my-project/global/networks/my-vpc"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
| IpFilter filter = bucket.getIpFilter(); | ||
|
|
||
| if (filter == null) { | ||
| System.out.println("Bucket " + bucketName + " has no IP Filter configured."); | ||
| return bucket; | ||
| } | ||
|
|
||
| IpFilter.Builder builder = filter.toBuilder(); | ||
| if (publicRange != null && filter.getPublicNetworkSource() != null) { | ||
| List<String> ranges = | ||
| new ArrayList<>(filter.getPublicNetworkSource().getAllowedIpCidrRanges()); | ||
| ranges.remove(publicRange); | ||
| builder.setPublicNetworkSource(ranges.isEmpty() ? null : PublicNetworkSource.of(ranges)); | ||
| } | ||
|
|
||
| if (vpcNetwork != null && filter.getVpcNetworkSources() != null) { | ||
| List<VpcNetworkSource> vpcs = new ArrayList<>(filter.getVpcNetworkSources()); | ||
| vpcs.removeIf(source -> vpcNetwork.equals(source.getNetwork())); | ||
| builder.setVpcNetworkSources(vpcs.isEmpty() ? null : vpcs); | ||
| } | ||
|
|
||
| // Note: This sample selectively removes specific rules (a public CIDR range or VPC network) | ||
| // from an existing IP filter configuration. To completely clear the IP filter configuration | ||
| // from the bucket, use: storage.update(bucket.toBuilder().setIpFilter(null).build()); | ||
| Bucket updatedBucket = storage.update(bucket.toBuilder().setIpFilter(builder.build()).build()); | ||
| System.out.println("Deleted specified IP filtering rules for bucket " + bucketName); | ||
| return updatedBucket; | ||
| } | ||
| } | ||
| // [END storage_delete_ip_filtering_rules] | ||
48 changes: 48 additions & 0 deletions
48
...rage/samples/snippets/src/main/java/com/example/storage/bucket/DisableBucketIpFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_disable_ip_filtering] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class DisableBucketIpFilter { | ||
| public static Bucket disableBucketIpFilter(String projectId, String bucketName) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
|
|
||
| if (bucket.getIpFilter() == null) { | ||
| System.out.println("Bucket " + bucketName + " has no IP Filter configured."); | ||
| return bucket; | ||
| } | ||
|
|
||
| IpFilter disabledIpFilter = bucket.getIpFilter().toBuilder().setMode("Disabled").build(); | ||
| Bucket updatedBucket = storage.update(bucket.toBuilder().setIpFilter(disabledIpFilter).build()); | ||
|
|
||
| System.out.println("IP filtering disabled for bucket " + bucketName); | ||
| return updatedBucket; | ||
| } | ||
| } | ||
| // [END storage_disable_ip_filtering] |
48 changes: 48 additions & 0 deletions
48
...orage/samples/snippets/src/main/java/com/example/storage/bucket/EnableBucketIpFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_enable_ip_filtering] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class EnableBucketIpFilter { | ||
| public static Bucket enableBucketIpFilter(String projectId, String bucketName) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
|
|
||
| if (bucket.getIpFilter() == null) { | ||
| System.out.println("Bucket " + bucketName + " has no IP Filter configured."); | ||
| return bucket; | ||
| } | ||
|
|
||
| IpFilter enabledIpFilter = bucket.getIpFilter().toBuilder().setMode("Enabled").build(); | ||
| Bucket updatedBucket = storage.update(bucket.toBuilder().setIpFilter(enabledIpFilter).build()); | ||
|
|
||
| System.out.println("IP filtering enabled for bucket " + bucketName); | ||
| return updatedBucket; | ||
| } | ||
| } | ||
| // [END storage_enable_ip_filtering] |
46 changes: 46 additions & 0 deletions
46
...-storage/samples/snippets/src/main/java/com/example/storage/bucket/GetBucketIpFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_get_ip_filtering] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class GetBucketIpFilter { | ||
| public static IpFilter getBucketIpFilter(String projectId, String bucketName) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
| IpFilter ipFilter = bucket.getIpFilter(); | ||
|
|
||
| if (ipFilter == null) { | ||
| System.out.println("Bucket " + bucketName + " has no IP Filter configured."); | ||
| return null; | ||
| } | ||
|
|
||
| System.out.println("Bucket " + bucketName + " IP filter configuration: " + ipFilter); | ||
| return ipFilter; | ||
| } | ||
| } | ||
| // [END storage_get_ip_filtering] |
42 changes: 42 additions & 0 deletions
42
...torage/samples/snippets/src/main/java/com/example/storage/bucket/ListBucketsIpFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_list_buckets_ip_filtering] | ||
| import com.google.api.gax.paging.Page; | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo.IpFilter; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class ListBucketsIpFilter { | ||
| public static void listBucketsIpFilter(String projectId) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Page<Bucket> buckets = storage.list(); | ||
|
|
||
| for (Bucket bucket : buckets.iterateAll()) { | ||
| IpFilter ipFilter = bucket.getIpFilter(); | ||
| String status = | ||
| (ipFilter == null || ipFilter.getMode() == null) ? "Not Configured" : ipFilter.getMode(); | ||
| System.out.println("Bucket: " + bucket.getName() + ", IP Filter Mode: " + status); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_list_buckets_ip_filtering] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.