This repository was archived by the owner on Mar 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 394
Feat: add bucket encryption enforcement configuration #2718
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b73b084
feat: add encryption enforcement configuration to bucket metadata
thiyaguk09 f2af573
add sample & tests
thiyaguk09 64799ff
test: add samples and system tests for bucket encryption enforcement
thiyaguk09 1a72da1
fix: typo
thiyaguk09 b9ec631
feat: support encryption enforcement configurations
thiyaguk09 31b7917
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 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,76 @@ | ||
| // 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Get Bucket Encryption Enforcement | ||
| // description: Retrieves the current encryption enforcement configurations for a bucket. | ||
| // usage: node getBucketEncryptionEnforcementConfig.js <BUCKET_NAME> | ||
|
|
||
| function main(bucketName = 'my-bucket') { | ||
| // [START storage_get_encryption_enforcement_config] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function getBucketEncryptionEnforcementConfig() { | ||
| const [metadata] = await storage.bucket(bucketName).getMetadata(); | ||
|
|
||
| console.log( | ||
| `Encryption enforcement configuration for bucket ${bucketName}.` | ||
| ); | ||
| const enc = metadata.encryption; | ||
| if (!enc) { | ||
| console.log( | ||
| 'No encryption configuration found (Default GMEK is active).' | ||
| ); | ||
| return; | ||
| } | ||
| console.log(`Default KMS Key: ${enc.defaultKmsKeyName || 'None'}`); | ||
|
|
||
| const printConfig = (label, config) => { | ||
| if (config) { | ||
| console.log(`${label}:`); | ||
| console.log(` Mode: ${config.restrictionMode}`); | ||
| console.log(` Effective: ${config.effectiveTime}`); | ||
| } | ||
| }; | ||
|
|
||
| printConfig( | ||
| 'Google Managed (GMEK) Enforcement', | ||
| enc.googleManagedEncryptionEnforcementConfig | ||
| ); | ||
| printConfig( | ||
| 'Customer Managed (CMEK) Enforcement', | ||
| enc.customerManagedEncryptionEnforcementConfig | ||
| ); | ||
| printConfig( | ||
| 'Customer Supplied (CSEK) Enforcement', | ||
| enc.customerSuppliedEncryptionEnforcementConfig | ||
| ); | ||
| } | ||
|
|
||
| getBucketEncryptionEnforcementConfig().catch(console.error); | ||
| // [END storage_get_encryption_enforcement_config] | ||
| } | ||
| main(...process.argv.slice(2)); |
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,58 @@ | ||
| // 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Remove All Bucket Encryption Enforcement | ||
| // description: Removes all encryption enforcement configurations and resets to default behavior. | ||
| // usage: node removeAllBucketEncryptionEnforcementConfig.js <BUCKET_NAME> | ||
|
|
||
| function main(bucketName = 'my-bucket') { | ||
| // [START storage_remove_all_encryption_enforcement_config] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| // Setting these to null explicitly removes the enforcement policy. | ||
| // We also include defaultKmsKeyName: null to fully reset the bucket encryption state. | ||
| async function removeAllBucketEncryptionEnforcementConfig() { | ||
| const options = { | ||
| encryption: { | ||
| defaultKmsKeyName: null, | ||
| googleManagedEncryptionEnforcementConfig: null, | ||
| customerSuppliedEncryptionEnforcementConfig: null, | ||
| customerManagedEncryptionEnforcementConfig: null, | ||
| }, | ||
| }; | ||
|
|
||
| await storage.bucket(bucketName).setMetadata(options); | ||
|
|
||
| console.log( | ||
| `Encryption enforcement configuration removed from bucket ${bucketName}.` | ||
| ); | ||
| } | ||
|
|
||
| removeAllBucketEncryptionEnforcementConfig().catch(console.error); | ||
| // [END storage_remove_all_encryption_enforcement_config] | ||
| } | ||
| main(...process.argv.slice(2)); |
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,93 @@ | ||
| // 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Set Bucket Encryption Enforcement | ||
| // description: Configures a bucket to enforce specific encryption types (e.g., CMEK-only). | ||
| // usage: node setBucketEncryptionEnforcementConfig.js <BUCKET_NAME> <KMS_KEY_NAME> | ||
|
|
||
| function main( | ||
| bucketName = 'my-bucket', | ||
| defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA | ||
| ) { | ||
| // [START storage_set_encryption_enforcement_config] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The name of the KMS key to be used as the default | ||
| // const defaultKmsKeyName = 'my-key'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function setBucketEncryptionEnforcementConfig() { | ||
| const options = { | ||
| encryption: { | ||
| defaultKmsKeyName: defaultKmsKeyName, | ||
| googleManagedEncryptionEnforcementConfig: { | ||
| restrictionMode: 'FullyRestricted', | ||
| }, | ||
| customerSuppliedEncryptionEnforcementConfig: { | ||
| restrictionMode: 'FullyRestricted', | ||
| }, | ||
| customerManagedEncryptionEnforcementConfig: { | ||
| restrictionMode: 'NotRestricted', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const [metadata] = await storage.bucket(bucketName).setMetadata(options); | ||
|
|
||
| console.log( | ||
| `Encryption enforcement configuration updated for bucket ${bucketName}.` | ||
| ); | ||
| const enc = metadata.encryption; | ||
| if (enc) { | ||
| console.log(`Default KMS Key: ${enc.defaultKmsKeyName}`); | ||
|
|
||
| const logEnforcement = (label, config) => { | ||
| if (config) { | ||
| console.log(`${label}:`); | ||
| console.log(` Mode: ${config.restrictionMode}`); | ||
| console.log(` Effective: ${config.effectiveTime}`); | ||
| } | ||
| }; | ||
|
|
||
| logEnforcement( | ||
| 'Google Managed (GMEK) Enforcement', | ||
| enc.googleManagedEncryptionEnforcementConfig | ||
| ); | ||
| logEnforcement( | ||
| 'Customer Managed (CMEK) Enforcement', | ||
| enc.customerManagedEncryptionEnforcementConfig | ||
| ); | ||
| logEnforcement( | ||
| 'Customer Supplied (CSEK) Enforcement', | ||
| enc.customerSuppliedEncryptionEnforcementConfig | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| setBucketEncryptionEnforcementConfig().catch(console.error); | ||
| // [END storage_set_encryption_enforcement_config] | ||
| } | ||
| main(...process.argv.slice(2)); |
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
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
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
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.