-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feat(storage): add bucket encryption enforcement #2190
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
bshaffer
merged 7 commits into
GoogleCloudPlatform:main
from
thiyaguk09:feat/bucket-encryption-config
Mar 18, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
11ace0f
Feat(storage): add bucket encryption enforcement
thiyaguk09 b9f53cd
Lint fix
thiyaguk09 f440264
Code refactor
thiyaguk09 7faba4d
Feat(storage): unify encryption enforcement sample
thiyaguk09 04f0b05
Lint fix
thiyaguk09 34c2075
Feat(storage):add bucket to encryption region tags
thiyaguk09 d4eeb57
Refactor(storage): streamline enforcement flow
thiyaguk09 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
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,66 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2026 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Storage; | ||
|
|
||
| # [START storage_get_bucket_encryption_enforcement_config] | ||
| use Google\Cloud\Storage\StorageClient; | ||
|
|
||
| /** | ||
| * Retrieves the current encryption enforcement configurations for a bucket. | ||
| * | ||
| * @param string $bucketName The ID of your GCS bucket (e.g. "my-bucket"). | ||
| */ | ||
| function get_bucket_encryption_enforcement_config(string $bucketName): void | ||
| { | ||
| $storage = new StorageClient(); | ||
| $bucket = $storage->bucket($bucketName); | ||
| $metadata = $bucket->info(); | ||
|
|
||
| printf('Encryption enforcement configuration for bucket %s.' . PHP_EOL, $bucketName); | ||
|
|
||
| if (!isset($metadata['encryption'])) { | ||
| print('No encryption configuration found (Default GMEK is active).' . PHP_EOL); | ||
| return; | ||
| } | ||
|
|
||
| $enc = $metadata['encryption']; | ||
| printf('Default KMS Key: %s' . PHP_EOL, $enc['defaultKmsKeyName'] ?? 'None'); | ||
|
|
||
| $printConfig = function ($label, $config) { | ||
| if ($config) { | ||
| printf('%s:' . PHP_EOL, $label); | ||
| printf(' Mode: %s' . PHP_EOL, $config['restrictionMode']); | ||
| printf(' Effective: %s' . PHP_EOL, $config['effectiveTime'] ?? 'N/A'); | ||
| } | ||
| }; | ||
|
|
||
| $printConfig('Google Managed (GMEK) Enforcement', $enc['googleManagedEncryptionEnforcementConfig'] ?? null); | ||
| $printConfig('Customer Managed (CMEK) Enforcement', $enc['customerManagedEncryptionEnforcementConfig'] ?? null); | ||
| $printConfig('Customer Supplied (CSEK) Enforcement', $enc['customerSuppliedEncryptionEnforcementConfig'] ?? null); | ||
| } | ||
| # [END storage_get_bucket_encryption_enforcement_config] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
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,64 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2026 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Storage; | ||
|
|
||
| # [START storage_set_bucket_encryption_enforcement_config] | ||
| use Google\Cloud\Storage\StorageClient; | ||
|
|
||
| /** | ||
| * Creates a bucket with specific encryption enforcement (e.g., CMEK-only). | ||
| * | ||
| * @param string $bucketName The ID of your GCS bucket (e.g. "my-bucket"). | ||
| * @param string $kmsKeyName The name of the KMS key to be used as the default (e.g. "projects/my-project/..."). | ||
| */ | ||
| function set_bucket_encryption_enforcement_config(string $bucketName, string $kmsKeyName): void | ||
| { | ||
| $storage = new StorageClient(); | ||
| $bucket = $storage->bucket($bucketName); | ||
|
|
||
| // This configuration enforces that all objects uploaded to the bucket | ||
| // must use Customer Managed Encryption Keys (CMEK). | ||
| $options = [ | ||
| 'encryption' => [ | ||
| 'defaultKmsKeyName' => $kmsKeyName, | ||
| 'googleManagedEncryptionEnforcementConfig' => [ | ||
| 'restrictionMode' => 'FullyRestricted', | ||
| ], | ||
| 'customerSuppliedEncryptionEnforcementConfig' => [ | ||
| 'restrictionMode' => 'FullyRestricted', | ||
| ], | ||
| 'customerManagedEncryptionEnforcementConfig' => [ | ||
| 'restrictionMode' => 'NotRestricted', | ||
| ], | ||
| ], | ||
| ]; | ||
| $storage->createBucket($bucketName, $options); | ||
|
|
||
| printf('Bucket %s created with encryption enforcement configuration.' . PHP_EOL, $bucketName); | ||
| } | ||
| # [END storage_set_bucket_encryption_enforcement_config] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
69 changes: 69 additions & 0 deletions
69
storage/src/update_bucket_encryption_enforcement_config.php
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,69 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2026 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Storage; | ||
|
|
||
| # [START storage_update_bucket_encryption_enforcement_config] | ||
| use Google\Cloud\Storage\StorageClient; | ||
|
|
||
| /** | ||
| * Updates or removes encryption enforcement configurations from a bucket. | ||
| * | ||
| * @param string $bucketName The ID of your GCS bucket (e.g. "my-bucket"). | ||
| */ | ||
| function update_bucket_encryption_enforcement_config(string $bucketName): void | ||
| { | ||
| $storage = new StorageClient(); | ||
| $bucket = $storage->bucket($bucketName); | ||
|
|
||
| // Update a specific encryption type's restriction mode | ||
| // This partial update preserves other existing encryption settings. | ||
| $updateOptions = [ | ||
| 'encryption' => [ | ||
| 'googleManagedEncryptionEnforcementConfig' => [ | ||
| 'restrictionMode' => 'FullyRestricted' | ||
| ] | ||
| ] | ||
| ]; | ||
| $bucket->update($updateOptions); | ||
| printf('Google-managed encryption enforcement set to FullyRestricted for %s.' . PHP_EOL, $bucketName); | ||
|
|
||
| // Remove all encryption enforcement configurations altogether | ||
| // Setting these values to null removes the policies from the bucket metadata. | ||
| $clearOptions = [ | ||
| 'encryption' => [ | ||
| 'defaultKmsKeyName' => null, | ||
| 'googleManagedEncryptionEnforcementConfig' => null, | ||
| 'customerSuppliedEncryptionEnforcementConfig' => null, | ||
| 'customerManagedEncryptionEnforcementConfig' => null, | ||
| ], | ||
| ]; | ||
|
|
||
| $bucket->update($clearOptions); | ||
| printf('All encryption enforcement configurations removed from bucket %s.' . PHP_EOL, $bucketName); | ||
| } | ||
| # [END storage_update_bucket_encryption_enforcement_config] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
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
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.