-
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
base: main
Are you sure you want to change the base?
Changes from all commits
73e0de3
b02b150
d29fa87
3b6fdd5
406dc67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * 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()); | ||
|
|
||
| GoogleManagedEncryptionEnforcementConfig gmekConfig = | ||
| bucket.getGoogleManagedEncryptionEnforcementConfig(); | ||
| CustomerManagedEncryptionEnforcementConfig cmekConfig = | ||
| bucket.getCustomerManagedEncryptionEnforcementConfig(); | ||
| CustomerSuppliedEncryptionEnforcementConfig csekConfig = | ||
| bucket.getCustomerSuppliedEncryptionEnforcementConfig(); | ||
|
|
||
| System.out.println( | ||
| " GMEK Enforcement: " | ||
| + (gmekConfig != null | ||
| ? String.format( | ||
| "Mode: %s, Effective Time: %s", | ||
| gmekConfig.getRestrictionMode(), gmekConfig.getEffectiveTime()) | ||
| : "NOT SET (Default)")); | ||
| System.out.println( | ||
| " CMEK Enforcement: " | ||
| + (cmekConfig != null | ||
| ? String.format( | ||
| "Mode: %s, Effective Time: %s", | ||
| cmekConfig.getRestrictionMode(), cmekConfig.getEffectiveTime()) | ||
| : "NOT SET (Default)")); | ||
| System.out.println( | ||
| " CSEK Enforcement: " | ||
| + (csekConfig != null | ||
| ? String.format( | ||
| "Mode: %s, Effective Time: %s", | ||
| csekConfig.getRestrictionMode(), csekConfig.getEffectiveTime()) | ||
| : "NOT SET (Default)")); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_get_encryption_enforcement_config] |
| 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_all_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 RemoveAllEncryptionEnforcementConfig { | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we repuspose this class to be more generic like to "Update" EncryptionEnforcementConfig instead?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But we are already updating and doing this in the three different examples in SetBucketEncryptionEnforcementConfig There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't SetBucketEncryptionEnforcementConfig for "bucket create, along with encryption-enforcement config", |
||
| System.out.println( | ||
| "Encryption enforcement policy removed from bucket " | ||
| + bucketName | ||
| + ". Bucket reverted to default behavior."); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_remove_all_encryption_enforcement_config] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * 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) | ||
| 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()) { | ||
|
|
||
| // Example 1: Enforce GMEK Only | ||
| setGmekEnforcedPolicy(storage, bucketName + "_gmek_only"); | ||
|
|
||
| // Example 2: Enforce CMEK Only | ||
| setCmekEnforcedPolicy(storage, bucketName + "_cmek_only"); | ||
|
|
||
| // Example 3: Restrict CSEK (Ransomware Protection) | ||
| restrictCsekPolicy(storage, bucketName + "_restrict_csek"); | ||
| } | ||
| } | ||
|
|
||
| public static void setGmekEnforcedPolicy(Storage storage, String 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 setCmekEnforcedPolicy(Storage storage, String 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) | ||
| .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) { | ||
| 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] |
Uh oh!
There was an error while loading. Please reload this page.