-
Notifications
You must be signed in to change notification settings - Fork 86
samples: add samples for encryption enforcement config feature #3369
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
Open
nidhiii-27
wants to merge
5
commits into
googleapis:main
Choose a base branch
from
nidhiii-27:encryption-config-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
73e0de3
samples: add samples for encryption enforcement config feature
nidhiii-27 b02b150
fix region tag
nidhiii-27 d29fa87
Merge branch 'main' into encryption-config-samples
nidhiii-27 3b6fdd5
review fixes
nidhiii-27 406dc67
Merge branch 'main' into encryption-config-samples
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
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...les/snippets/src/main/java/com/example/storage/bucket/GetEncryptionEnforcementConfig.java
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,71 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_get_encryption_enforcement_config] | ||
|
|
||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo.CustomerManagedEncryptionEnforcementConfig; | ||
| import com.google.cloud.storage.BucketInfo.CustomerSuppliedEncryptionEnforcementConfig; | ||
| import com.google.cloud.storage.BucketInfo.GoogleManagedEncryptionEnforcementConfig; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class GetEncryptionEnforcementConfig { | ||
| public static void getEncryptionEnforcementConfig(String projectId, String bucketName) | ||
| throws Exception { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| try (Storage storage = | ||
| StorageOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
| System.out.println( | ||
| "\n--- Getting Encryption Enforcement Policy for bucket " + bucketName + " ---"); | ||
|
|
||
| Bucket bucket = storage.get(bucketName); | ||
|
|
||
| if (bucket == null) { | ||
| System.out.println("Bucket " + bucketName + " not found."); | ||
| return; | ||
| } | ||
|
|
||
| System.out.println(" Bucket Name: " + bucket.getName()); | ||
| System.out.println(" Default KMS Key: " + bucket.getDefaultKmsKeyName()); | ||
|
|
||
| GoogleManagedEncryptionEnforcementConfig gmekConfig = | ||
| bucket.getGoogleManagedEncryptionEnforcementConfig(); | ||
| CustomerManagedEncryptionEnforcementConfig cmekConfig = | ||
| bucket.getCustomerManagedEncryptionEnforcementConfig(); | ||
| CustomerSuppliedEncryptionEnforcementConfig csekConfig = | ||
| bucket.getCustomerSuppliedEncryptionEnforcementConfig(); | ||
|
|
||
| System.out.println( | ||
| " GMEK Enforcement: " | ||
| + (gmekConfig != null ? gmekConfig.getRestrictionMode() : "NOT SET (Default)")); | ||
| System.out.println( | ||
| " CMEK Enforcement: " | ||
| + (cmekConfig != null ? cmekConfig.getRestrictionMode() : "NOT SET (Default)")); | ||
| System.out.println( | ||
| " CSEK Enforcement: " | ||
| + (csekConfig != null ? csekConfig.getRestrictionMode() : "NOT SET (Default)")); | ||
nidhiii-27 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| // [END storage_get_encryption_enforcement_config] | ||
59 changes: 59 additions & 0 deletions
59
.../snippets/src/main/java/com/example/storage/bucket/RemoveEncryptionEnforcementConfig.java
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,59 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_remove_encryption_enforcement_config] | ||
|
|
||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class RemoveEncryptionEnforcementConfig { | ||
| public static void removeEncryptionEnforcementConfig(String projectId, String bucketName) | ||
| throws Exception { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| try (Storage storage = | ||
| StorageOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
| Bucket bucket = storage.get(bucketName); | ||
| if (bucket == null) { | ||
| System.out.println("Bucket " + bucketName + " does not exist."); | ||
| return; | ||
| } | ||
| // To remove an existing policy, the corresponding field must be explicitly set to 'null' | ||
| // in the BucketInfo object passed to the update call. | ||
| BucketInfo bucketInfo = | ||
| bucket.toBuilder() | ||
| .setGoogleManagedEncryptionEnforcementConfig(null) | ||
| .setCustomerManagedEncryptionEnforcementConfig(null) | ||
| .setCustomerSuppliedEncryptionEnforcementConfig(null) | ||
| .build(); | ||
|
|
||
| storage.update(bucketInfo); | ||
| System.out.println( | ||
| "Encryption enforcement policy removed from bucket " | ||
| + bucketName | ||
| + ". Bucket reverted to default behavior."); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_remove_encryption_enforcement_config] |
121 changes: 121 additions & 0 deletions
121
...ippets/src/main/java/com/example/storage/bucket/SetBucketEncryptionEnforcementConfig.java
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,121 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_set_encryption_enforcement_config] | ||
|
|
||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo; | ||
| import com.google.cloud.storage.BucketInfo.CustomerManagedEncryptionEnforcementConfig; | ||
| import com.google.cloud.storage.BucketInfo.CustomerSuppliedEncryptionEnforcementConfig; | ||
| import com.google.cloud.storage.BucketInfo.EncryptionEnforcementRestrictionMode; | ||
| import com.google.cloud.storage.BucketInfo.GoogleManagedEncryptionEnforcementConfig; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class SetBucketEncryptionEnforcementConfig { | ||
| public static void setBucketEncryptionEnforcementConfig( | ||
| String projectId, String bucketName, String kmsKeyName) throws Exception { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The name of the KMS key to use | ||
| // String kmsKeyName = | ||
| // "projects/your-project-id/locations/us/keyRings/my_key_ring/cryptoKeys/my_key" | ||
|
|
||
| try (Storage storage = | ||
| StorageOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
|
|
||
| // Example 1: Enforce GMEK Only | ||
| setGmekOnlyPolicy(storage, bucketName + "_gmek_only"); | ||
nidhiii-27 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Example 2: Enforce CMEK Only | ||
| setCmekOnlyPolicy(storage, bucketName + "_cmek_only", kmsKeyName); | ||
|
|
||
| // Example 3: Restrict CSEK (Ransomware Protection) | ||
| restrictCsekPolicy(storage, bucketName + "_restrict_csek"); | ||
| } | ||
| } | ||
|
|
||
| public static void setGmekOnlyPolicy(Storage storage, String bucketName) { | ||
| System.out.println("--- Setting GMEK-Only Policy on bucket " + bucketName + " ---"); | ||
| GoogleManagedEncryptionEnforcementConfig gmekConfig = | ||
| GoogleManagedEncryptionEnforcementConfig.of( | ||
| EncryptionEnforcementRestrictionMode.NOT_RESTRICTED); | ||
| CustomerManagedEncryptionEnforcementConfig cmekConfig = | ||
| CustomerManagedEncryptionEnforcementConfig.of( | ||
| EncryptionEnforcementRestrictionMode.FULLY_RESTRICTED); | ||
| CustomerSuppliedEncryptionEnforcementConfig csekConfig = | ||
| CustomerSuppliedEncryptionEnforcementConfig.of( | ||
| EncryptionEnforcementRestrictionMode.FULLY_RESTRICTED); | ||
|
|
||
| BucketInfo bucketInfo = | ||
| BucketInfo.newBuilder(bucketName) | ||
| .setGoogleManagedEncryptionEnforcementConfig(gmekConfig) | ||
| .setCustomerManagedEncryptionEnforcementConfig(cmekConfig) | ||
| .setCustomerSuppliedEncryptionEnforcementConfig(csekConfig) | ||
| .build(); | ||
|
|
||
| Bucket bucket = storage.create(bucketInfo); | ||
| System.out.println( | ||
| "Bucket " + bucket.getName() + " created with GMEK-only enforcement policy."); | ||
| } | ||
|
|
||
| public static void setCmekOnlyPolicy(Storage storage, String bucketName, String kmsKeyName) { | ||
| System.out.println("--- Setting CMEK-Only Policy on bucket " + bucketName + " ---"); | ||
| GoogleManagedEncryptionEnforcementConfig gmekConfig = | ||
| GoogleManagedEncryptionEnforcementConfig.of( | ||
| EncryptionEnforcementRestrictionMode.FULLY_RESTRICTED); | ||
| CustomerManagedEncryptionEnforcementConfig cmekConfig = | ||
| CustomerManagedEncryptionEnforcementConfig.of( | ||
| EncryptionEnforcementRestrictionMode.NOT_RESTRICTED); | ||
| CustomerSuppliedEncryptionEnforcementConfig csekConfig = | ||
| CustomerSuppliedEncryptionEnforcementConfig.of( | ||
| EncryptionEnforcementRestrictionMode.FULLY_RESTRICTED); | ||
|
|
||
| BucketInfo bucketInfo = | ||
| BucketInfo.newBuilder(bucketName) | ||
| .setDefaultKmsKeyName(kmsKeyName) | ||
nidhiii-27 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .setGoogleManagedEncryptionEnforcementConfig(gmekConfig) | ||
| .setCustomerManagedEncryptionEnforcementConfig(cmekConfig) | ||
| .setCustomerSuppliedEncryptionEnforcementConfig(csekConfig) | ||
| .build(); | ||
|
|
||
| Bucket bucket = storage.create(bucketInfo); | ||
| System.out.println( | ||
| "Bucket " + bucket.getName() + " created with CMEK-only enforcement policy."); | ||
| } | ||
|
|
||
| public static void restrictCsekPolicy(Storage storage, String bucketName) { | ||
| System.out.println("--- Setting Restrict-CSEK Policy on bucket " + bucketName + " ---"); | ||
| CustomerSuppliedEncryptionEnforcementConfig csekConfig = | ||
| CustomerSuppliedEncryptionEnforcementConfig.of( | ||
| EncryptionEnforcementRestrictionMode.FULLY_RESTRICTED); | ||
|
|
||
| BucketInfo bucketInfo = | ||
| BucketInfo.newBuilder(bucketName) | ||
| .setCustomerSuppliedEncryptionEnforcementConfig(csekConfig) | ||
| .build(); | ||
|
|
||
| Bucket bucket = storage.create(bucketInfo); | ||
| System.out.println("Bucket " + bucket.getName() + " created with a policy to restrict CSEK."); | ||
| } | ||
| } | ||
| // [END storage_set_encryption_enforcement_config] | ||
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.