This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
samples: add samples for bucket encryption enforcement config #1772
Merged
nidhiii-27
merged 20 commits into
main
from
add-bucket-encryption-enforcement-samples-3410657303470871774
Mar 27, 2026
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
da08329
feat: add samples for bucket encryption enforcement config
google-labs-jules[bot] 9e0c411
samples: add samples for bucket encryption enforcement config
google-labs-jules[bot] 9c84456
samples: add samples for bucket encryption enforcement config
google-labs-jules[bot] 725d610
samples: add samples for bucket encryption enforcement config
google-labs-jules[bot] 7eb6b93
samples: add samples for bucket encryption enforcement config
google-labs-jules[bot] 4e6dce7
fix tests
nidhiii-27 4e9a2e6
change update sample
nidhiii-27 aa15dc7
Merge branch 'main' into add-bucket-encryption-enforcement-samples-34…
nidhiii-27 ff1b6c5
correct assertions
nidhiii-27 3735d89
small correction
nidhiii-27 49d293b
samples: add samples for bucket encryption enforcement config
google-labs-jules[bot] a6bea98
Revert "samples: add samples for bucket encryption enforcement config"
nidhiii-27 774236c
review fixes
nidhiii-27 519a994
samples: add samples for bucket encryption enforcement config
google-labs-jules[bot] 2da2960
Revert jules commit
nidhiii-27 8029eb7
modify the update sample
nidhiii-27 bde76ba
fix lint
nidhiii-27 db98bd3
samples: add samples for bucket encryption enforcement config
google-labs-jules[bot] 25db286
Revert "samples: add samples for bucket encryption enforcement config"
nidhiii-27 8f17f0f
Merge branch 'main' into add-bucket-encryption-enforcement-samples-34…
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
Some comments aren't visible on the classic Files Changed page.
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
48 changes: 48 additions & 0 deletions
48
samples/snippets/storage_get_bucket_encryption_enforcement_config.py
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. | ||
|
|
||
| # [START storage_get_bucket_encryption_enforcement_config] | ||
| from google.cloud import storage | ||
|
|
||
|
|
||
| def get_bucket_encryption_enforcement_config(bucket_name): | ||
| """Gets the bucket encryption enforcement configuration.""" | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "your-unique-bucket-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
|
|
||
| print(f"Encryption Enforcement Config for bucket {bucket.name}:") | ||
|
|
||
| cmek_config = bucket.encryption.customer_managed_encryption_enforcement_config | ||
| csek_config = bucket.encryption.customer_supplied_encryption_enforcement_config | ||
| gmek_config = bucket.encryption.google_managed_encryption_enforcement_config | ||
|
|
||
| print( | ||
| f"Customer-managed encryption enforcement config restriction mode: {cmek_config.restriction_mode if cmek_config else None}" | ||
| ) | ||
| print( | ||
| f"Customer-supplied encryption enforcement config restriction mode: {csek_config.restriction_mode if csek_config else None}" | ||
| ) | ||
| print( | ||
| f"Google-managed encryption enforcement config restriction mode: {gmek_config.restriction_mode if gmek_config else None}" | ||
| ) | ||
|
|
||
|
|
||
| # [END storage_get_bucket_encryption_enforcement_config] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| get_bucket_encryption_enforcement_config(bucket_name="your-unique-bucket-name") |
55 changes: 55 additions & 0 deletions
55
samples/snippets/storage_set_bucket_encryption_enforcement_config.py
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,55 @@ | ||
| # 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. | ||
|
|
||
| # [START storage_set_bucket_encryption_enforcement_config] | ||
| from google.cloud import storage | ||
| from google.cloud.storage.bucket import EncryptionEnforcementConfig | ||
|
|
||
|
|
||
| def set_bucket_encryption_enforcement_config(bucket_name): | ||
| """Creates a bucket with encryption enforcement configuration.""" | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "your-unique-bucket-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| bucket = storage_client.bucket(bucket_name) | ||
|
|
||
| # Setting restriction_mode to "FullyRestricted" for Google-managed encryption (GMEK) | ||
| # means objects cannot be created using the default Google-managed keys. | ||
| bucket.encryption.google_managed_encryption_enforcement_config = ( | ||
| EncryptionEnforcementConfig(restriction_mode="FullyRestricted") | ||
| ) | ||
|
|
||
| # Setting restriction_mode to "NotRestricted" for Customer-managed encryption (CMEK) | ||
| # ensures that objects ARE permitted to be created using Cloud KMS keys. | ||
| bucket.encryption.customer_managed_encryption_enforcement_config = ( | ||
| EncryptionEnforcementConfig(restriction_mode="NotRestricted") | ||
| ) | ||
|
|
||
| # Setting restriction_mode to "FullyRestricted" for Customer-supplied encryption (CSEK) | ||
| # prevents objects from being created using raw, client-side provided keys. | ||
| bucket.encryption.customer_supplied_encryption_enforcement_config = ( | ||
| EncryptionEnforcementConfig(restriction_mode="FullyRestricted") | ||
| ) | ||
|
|
||
| bucket.create() | ||
|
|
||
| print(f"Created bucket {bucket.name} with Encryption Enforcement Config.") | ||
|
|
||
|
|
||
| # [END storage_set_bucket_encryption_enforcement_config] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| set_bucket_encryption_enforcement_config(bucket_name="your-unique-bucket-name") |
60 changes: 60 additions & 0 deletions
60
samples/snippets/storage_update_bucket_encryption_enforcement_config.py
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,60 @@ | ||
| # 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. | ||
|
|
||
| # [START storage_update_bucket_encryption_enforcement_config] | ||
| from google.cloud import storage | ||
| from google.cloud.storage.bucket import EncryptionEnforcementConfig | ||
|
|
||
|
|
||
| def update_bucket_encryption_enforcement_config(bucket_name): | ||
| """Updates the encryption enforcement policy for a bucket.""" | ||
| # The ID of your GCS bucket with GMEK and CSEK restricted | ||
| # bucket_name = "your-unique-bucket-name" | ||
|
|
||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
|
|
||
| # Update a specific type (e.g., change GMEK to NotRestricted) | ||
| bucket.encryption.google_managed_encryption_enforcement_config = ( | ||
| EncryptionEnforcementConfig(restriction_mode="NotRestricted") | ||
| ) | ||
|
|
||
| # Update another type (e.g., change CMEK to FullyRestricted) | ||
| bucket.encryption.customer_managed_encryption_enforcement_config = ( | ||
| EncryptionEnforcementConfig(restriction_mode="FullyRestricted") | ||
| ) | ||
|
|
||
| # Keeping CSEK unchanged | ||
| bucket.encryption.customer_supplied_encryption_enforcement_config = ( | ||
| EncryptionEnforcementConfig(restriction_mode="FullyRestricted") | ||
| ) | ||
|
|
||
| bucket.patch() | ||
|
|
||
| print(f"Encryption enforcement policy updated for bucket {bucket.name}.") | ||
|
|
||
| gmek = bucket.encryption.google_managed_encryption_enforcement_config | ||
| cmek = bucket.encryption.customer_managed_encryption_enforcement_config | ||
| csek = bucket.encryption.customer_supplied_encryption_enforcement_config | ||
|
|
||
| print(f"GMEK restriction mode: {gmek.restriction_mode if gmek else 'None'}") | ||
| print(f"CMEK restriction mode: {cmek.restriction_mode if cmek else 'None'}") | ||
| print(f"CSEK restriction mode: {csek.restriction_mode if csek else 'None'}") | ||
|
|
||
|
|
||
| # [END storage_update_bucket_encryption_enforcement_config] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| update_bucket_encryption_enforcement_config(bucket_name="your-unique-bucket-name") |
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.