Skip to content

feat(storage): add Cloud Storage bucket IP filtering samples#2218

Closed
salilg-eng wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
salilg-eng:feat/bucket-ip-filter
Closed

feat(storage): add Cloud Storage bucket IP filtering samples#2218
salilg-eng wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
salilg-eng:feat/bucket-ip-filter

Conversation

@salilg-eng

Copy link
Copy Markdown
Contributor

This PR add PHP code samples demonstrating how to manage IP filtering for Cloud Storage buckets.

What's included:

  • Snippets for getting, enabling, disabling, listing, and deleting bucket IP filtering rules.
  • A full lifecycle integration test (IpFilterTest.php) to cover the new snippets.

Note on enable_ip_filtering.php:
I set the mode to Disabled by default in the snippet (with an accompanying comment). If we actually set it to Enabled during the automated tests, we risk locking the test runner out of the bucket and causing permission failures during the cleanup phase. Users can easily flip it to Enabled when adapting the code for their own use.

@salilg-eng salilg-eng requested review from a team as code owners June 16, 2026 05:57
@snippet-bot

snippet-bot Bot commented Jun 16, 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 Jun 16, 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 PHP code samples and integration tests for managing Cloud Storage bucket IP filtering rules, including enabling, disabling, retrieving, listing, and deleting rules. The review feedback highlights a critical issue where enabling IP filtering in the sample locks out the test runner, causing tests to fail silently; it is recommended to set the mode to 'Disabled' during testing. Additionally, the feedback suggests avoiding redundant API updates when no rules are modified, and using null coalescing operators to prevent PHP warnings on potentially missing array keys.

$bucket = $storage->bucket($bucketName);

$ipFilter = [
'mode' => 'Enabled',

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

In the PR description, you mentioned: 'I set the mode to Disabled by default in the snippet (with an accompanying comment)'. However, in the actual code, the mode is set to 'Enabled'.

Because the test runner's IP is not in the allowed range (1.2.3.0/24), setting the mode to 'Enabled' will immediately lock out the test runner. This causes the subsequent $bucket->reload() on line 1295 of the test to throw a 403 error, which is caught by the catch block, effectively skipping almost all of the lifecycle assertions (get, disable, list, delete).

Please update the mode to 'Disabled' in the snippet as intended, and update the corresponding test assertions in storageTest.php (lines 1297 and 1305) to expect 'Disabled' instead of 'Enabled'.

        // Set to 'Enabled' to enforce the IP filters.
        // We use 'Disabled' here to prevent locking out the test runner.
        'mode' => 'Disabled',

Comment on lines +47 to +57
if (isset($ipFilter['publicNetworkSource']['allowedIpCidrRanges'])) {
$ranges = $ipFilter['publicNetworkSource']['allowedIpCidrRanges'];
$ranges = array_filter($ranges, function ($range) {
return $range !== '1.2.3.0/24';
});
$ipFilter['publicNetworkSource']['allowedIpCidrRanges'] = array_values($ranges);
}

$bucket->update(['ipFilter' => $ipFilter]);

printf('Specific IP filtering rules deleted for bucket %s' . PHP_EOL, $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

If the IP range '1.2.3.0/24' is not present in the allowed ranges, or if allowedIpCidrRanges is not set, calling $bucket->update() is redundant and wastes an API call.

We can optimize this by tracking whether any changes were actually made, and only calling $bucket->update() if $updated is true.

    $updated = false;
    if (isset($ipFilter['publicNetworkSource']['allowedIpCidrRanges'])) {
        $ranges = $ipFilter['publicNetworkSource']['allowedIpCidrRanges'];
        $filteredRanges = array_filter($ranges, function ($range) {
            return $range !== '1.2.3.0/24';
        });
        if (count($ranges) !== count($filteredRanges)) {
            $ipFilter['publicNetworkSource']['allowedIpCidrRanges'] = array_values($filteredRanges);
            $updated = true;
        }
    }

    if ($updated) {
        $bucket->update(['ipFilter' => $ipFilter]);
        printf('Specific IP filtering rules deleted for bucket %s' . PHP_EOL, $bucketName);
    } else {
        printf('No matching IP filtering rules found to delete for bucket %s.' . PHP_EOL, $bucketName);
    }

printf('IP Filter Configuration for the Bucket %s:' . PHP_EOL, $bucketName);
printf('Mode: %s' . PHP_EOL, $ipFilter['mode']);

printf('Allow All Service Agent Access: %s' . PHP_EOL, var_export($ipFilter['allowAllServiceAgentAccess'], true));

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 key allowAllServiceAgentAccess might not be present in the $ipFilter array if it is not set or returned by the API. Accessing an undefined array key will trigger a PHP warning (which can be promoted to an exception in strict environments).

Using the null coalescing operator ?? provides a safe fallback.

    printf('Allow All Service Agent Access: %s' . PHP_EOL, var_export($ipFilter['allowAllServiceAgentAccess'] ?? false, true));

}
}

printf('Allow Cross Organization VPCs Access: %s' . PHP_EOL, var_export($ipFilter['allowCrossOrgVpcs'], true));

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 key allowCrossOrgVpcs might not be present in the $ipFilter array if it is not set or returned by the API. Accessing an undefined array key will trigger a PHP warning.

Using the null coalescing operator ?? provides a safe fallback.

    printf('Allow Cross Organization VPCs Access: %s' . PHP_EOL, var_export($ipFilter['allowCrossOrgVpcs'] ?? false, true));

try {
$bucket->reload();
$info = $bucket->info();
$this->assertEquals('Enabled', $info['ipFilter']['mode']);

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

If enable_ip_filtering.php is updated to use 'mode' => 'Disabled' to prevent locking out the test runner, this assertion should be updated to expect 'Disabled'.

            $this->assertEquals('Disabled', $info['ipFilter']['mode']);

$bucketName,
]);

$this->assertStringContainsString('Mode: Enabled', $output);

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

If enable_ip_filtering.php is updated to use 'mode' => 'Disabled', this assertion should be updated to expect 'Mode: Disabled'.

            $this->assertStringContainsString('Mode: Disabled', $output);

@salilg-eng salilg-eng force-pushed the feat/bucket-ip-filter branch from c016f56 to 0285e09 Compare June 16, 2026 06:01
@salilg-eng salilg-eng force-pushed the feat/bucket-ip-filter branch from 0285e09 to a7063fc Compare June 16, 2026 06:05
@salilg-eng salilg-eng closed this Jun 16, 2026
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.

1 participant