Skip to content

Commit f24e0b6

Browse files
authored
feat(storage): add Cloud Storage bucket IP filtering samples (#2221)
* Sample Bucket Ip Filter * Gemini review * feat(storage): address PR feedback and add create bucket IP filter sample
1 parent 6fec27e commit f24e0b6

7 files changed

Lines changed: 508 additions & 8 deletions
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Storage;
19+
20+
# [START storage_create_bucket_ip_filtering]
21+
use Google\Cloud\Storage\StorageClient;
22+
23+
/**
24+
* Create a new bucket with an IP filtering configuration.
25+
*
26+
* @param string $bucketName The name of your Cloud Storage bucket.
27+
* (e.g. 'my-new-bucket')
28+
*/
29+
function create_bucket_ip_filtering(string $bucketName): void
30+
{
31+
$storage = new StorageClient();
32+
33+
// Use 'Disabled' during tests or initial setup to prevent immediate lockout.
34+
$storage->createBucket($bucketName, [
35+
'ipFilter' => [
36+
'mode' => 'Disabled',
37+
'publicNetworkSource' => [
38+
'allowedIpCidrRanges' => ['1.2.3.0/24']
39+
],
40+
'allowAllServiceAgentAccess' => true
41+
]
42+
]);
43+
44+
printf('Bucket %s created with IP filtering rules.' . PHP_EOL, $bucketName);
45+
}
46+
# [END storage_create_bucket_ip_filtering]
47+
48+
// The following 2 lines are only needed to run the samples
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_delete_ip_filtering_rules]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Delete IP filtering rules from a bucket.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function delete_ip_filtering_rules(string $bucketName): void
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
40+
$info = $bucket->info();
41+
if (!isset($info['ipFilter'])) {
42+
printf('No IP Filter configuration found for bucket %s.' . PHP_EOL, $bucketName);
43+
return;
44+
}
45+
46+
$ipFilter = $info['ipFilter'];
47+
$updated = false;
48+
49+
if (isset($ipFilter['publicNetworkSource']['allowedIpCidrRanges'])) {
50+
$ranges = $ipFilter['publicNetworkSource']['allowedIpCidrRanges'];
51+
$filteredRanges = array_filter($ranges, function ($range) {
52+
return $range !== '1.2.3.0/24';
53+
});
54+
if (count($ranges) !== count($filteredRanges)) {
55+
$ipFilter['publicNetworkSource']['allowedIpCidrRanges'] = array_values($filteredRanges);
56+
$updated = true;
57+
}
58+
}
59+
60+
if ($updated) {
61+
$bucket->update(['ipFilter' => $ipFilter]);
62+
printf('Specific IP filtering rules deleted for bucket %s' . PHP_EOL, $bucketName);
63+
} else {
64+
printf('No matching IP filtering rules found to delete for bucket %s.' . PHP_EOL, $bucketName);
65+
}
66+
}
67+
# [END storage_delete_ip_filtering_rules]
68+
69+
// The following 2 lines are only needed to run the samples
70+
require_once __DIR__ . '/../../testing/sample_helpers.php';
71+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_disable_ip_filtering]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Disable IP filtering on a bucket.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function disable_ip_filtering(string $bucketName): void
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
40+
$bucket->update([
41+
'ipFilter' => [
42+
'mode' => 'Disabled',
43+
'publicNetworkSource' => null,
44+
'vpcNetworkSources' => []
45+
]
46+
]);
47+
48+
printf('Disabled IP filtering Rules for bucket %s' . PHP_EOL, $bucketName);
49+
}
50+
# [END storage_disable_ip_filtering]
51+
52+
// The following 2 lines are only needed to run the samples
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_enable_ip_filtering]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Enable IP filtering on a bucket.
31+
*
32+
* @param string $projectId The ID of your Google Cloud project.
33+
* (e.g. 'my-project-id')
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
* @param string $mode The mode to set IP filtering to.
37+
* (e.g. 'Enabled')
38+
*/
39+
function enable_ip_filtering(string $projectId, string $bucketName, string $mode = 'Enabled'): void
40+
{
41+
$storage = new StorageClient();
42+
$bucket = $storage->bucket($bucketName);
43+
44+
$ipFilter = [
45+
'mode' => $mode,
46+
'allowAllServiceAgentAccess' => true,
47+
'publicNetworkSource' => [
48+
'allowedIpCidrRanges' => ['1.2.3.0/24']
49+
],
50+
'vpcNetworkSources' => [
51+
[
52+
'network' => sprintf('projects/%s/global/networks/default', $projectId),
53+
'allowedIpCidrRanges' => ['10.0.0.0/24']
54+
]
55+
]
56+
];
57+
58+
$info = $bucket->update(['ipFilter' => $ipFilter]);
59+
60+
printf(
61+
'Enabled IP filtering Rules for the Bucket: %s' . PHP_EOL,
62+
$bucketName
63+
);
64+
}
65+
# [END storage_enable_ip_filtering]
66+
67+
// The following 2 lines are only needed to run the samples
68+
require_once __DIR__ . '/../../testing/sample_helpers.php';
69+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

‎storage/src/get_ip_filtering.php‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_ip_filtering]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Retrieve the IP filtering rules for the bucket.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* (e.g. 'my-bucket')
34+
*/
35+
function get_ip_filtering(string $bucketName): void
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
40+
$info = $bucket->info();
41+
42+
if (!isset($info['ipFilter'])) {
43+
printf('Bucket %s has no IP Filter configured.' . PHP_EOL, $bucketName);
44+
return;
45+
}
46+
47+
$ipFilter = $info['ipFilter'];
48+
49+
printf('IP Filter Configuration for the Bucket %s:' . PHP_EOL, $bucketName);
50+
printf('Mode: %s' . PHP_EOL, $ipFilter['mode']);
51+
52+
printf('Allow All Service Agent Access: %s' . PHP_EOL, var_export($ipFilter['allowAllServiceAgentAccess'] ?? false, true));
53+
54+
if (isset($ipFilter['publicNetworkSource']['allowedIpCidrRanges'])) {
55+
printf('Allowed Public CIDR Ranges:' . PHP_EOL);
56+
foreach ($ipFilter['publicNetworkSource']['allowedIpCidrRanges'] as $range) {
57+
printf('- %s' . PHP_EOL, $range);
58+
}
59+
}
60+
61+
printf('Allow Cross Organization VPCs Access: %s' . PHP_EOL, var_export($ipFilter['allowCrossOrgVpcs'] ?? false, true));
62+
63+
if (isset($ipFilter['vpcNetworkSources'])) {
64+
printf('Allowed VPC Network:' . PHP_EOL);
65+
foreach ($ipFilter['vpcNetworkSources'] as $vpcNetwork) {
66+
printf('- Network: %s' . PHP_EOL, $vpcNetwork['network']);
67+
if (isset($vpcNetwork['allowedIpCidrRanges'])) {
68+
printf('Allowed VPC CIDR Ranges: %s' . PHP_EOL, implode(', ', $vpcNetwork['allowedIpCidrRanges']));
69+
}
70+
}
71+
}
72+
}
73+
# [END storage_get_ip_filtering]
74+
75+
// The following 2 lines are only needed to run the samples
76+
require_once __DIR__ . '/../../testing/sample_helpers.php';
77+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_list_buckets_ip_filtering]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Lists all buckets including their IP filtering status.
31+
*
32+
* @param string $projectId The ID of your Google Cloud project.
33+
* (e.g. 'my-project-id')
34+
*/
35+
function list_buckets_ip_filtering(string $projectId): void
36+
{
37+
$storage = new StorageClient([
38+
'projectId' => $projectId
39+
]);
40+
41+
printf('Buckets:' . PHP_EOL);
42+
foreach ($storage->buckets(['projection' => 'full']) as $bucket) {
43+
$info = $bucket->info();
44+
$mode = $info['ipFilter']['mode'] ?? 'Not Configured';
45+
46+
printf('Bucket Name: %s, IP Filtering Mode: %s' . PHP_EOL, $bucket->name(), $mode);
47+
}
48+
}
49+
# [END storage_list_buckets_ip_filtering]
50+
51+
// The following 2 lines are only needed to run the samples
52+
require_once __DIR__ . '/../../testing/sample_helpers.php';
53+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)