Skip to content

Commit

Permalink
feat: introduce inherited value for pap (#7693)
Browse files Browse the repository at this point in the history
Fixes #7682
  • Loading branch information
frankyn committed Oct 4, 2021
1 parent 1fb798e commit 8bc6019
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion google-cloud-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.1.6</version>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void getPublicAccessPrevention(String projectId, String bucketName
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);

// Gets Bucket Metadata and prints publicAccessPrevention value (either 'unspecified' or
// Gets Bucket Metadata and prints publicAccessPrevention value (either 'inherited' or
// 'enforced').
BucketInfo.PublicAccessPrevention publicAccessPrevention =
bucket.getIamConfiguration().getPublicAccessPrevention();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 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.google.cloud.examples.storage.buckets;

// [START storage_set_public_access_prevention_inherited]
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 SetPublicAccessPreventionInherited {
public static void setPublicAccessPreventionInherited(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);

// Sets public access prevention to 'inherited' for the bucket
bucket
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
.build())
.build()
.update();

System.out.println("Public access prevention is set to 'inherited' for " + bucketName);
}
}
// [END storage_set_public_access_prevention_inherited]
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.google.cloud.examples.storage.buckets.RemoveBucketLabel;
import com.google.cloud.examples.storage.buckets.SetBucketWebsiteInfo;
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionEnforced;
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionUnspecified;
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionInherited;
import com.google.cloud.examples.storage.objects.DownloadRequesterPaysObject;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.Role;
Expand Down Expand Up @@ -310,7 +310,7 @@ public void testDisableLifecycleManagement() {
@Test
public void testGetPublicAccessPrevention() {
try {
// By default a bucket PAP state is UNSPECIFIED and we are changing the state to validate
// By default a bucket PAP state is INHERITED and we are changing the state to validate
// non-default state.
storage
.get(BUCKET)
Expand All @@ -333,18 +333,18 @@ public void testGetPublicAccessPrevention() {
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
.build())
.build()
.update();
} finally {
// No matter what happens make sure test set bucket back to UNSPECIFIED
// No matter what happens make sure test set bucket back to INHERITED
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
.build())
.build()
.update();
Expand All @@ -363,26 +363,26 @@ public void testSetPublicAccessPreventionEnforced() {
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
.build())
.build()
.update();
} finally {
// No matter what happens make sure test set bucket back to UNSPECIFIED
// No matter what happens make sure test set bucket back to INHERITED
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
.build())
.build()
.update();
}
}

@Test
public void testSetPublicAccessPreventionUnspecified() {
public void testSetPublicAccessPreventionInherited() {
try {
storage
.get(BUCKET)
Expand All @@ -393,18 +393,18 @@ public void testSetPublicAccessPreventionUnspecified() {
.build())
.build()
.update();
SetPublicAccessPreventionUnspecified.setPublicAccessPreventionUnspecified(PROJECT_ID, BUCKET);
SetPublicAccessPreventionInherited.setPublicAccessPreventionInherited(PROJECT_ID, BUCKET);
assertEquals(
storage.get(BUCKET).getIamConfiguration().getPublicAccessPrevention(),
BucketInfo.PublicAccessPrevention.UNSPECIFIED);
BucketInfo.PublicAccessPrevention.INHERITED);
} finally {
// No matter what happens make sure test set bucket back to UNSPECIFIED
// No matter what happens make sure test set bucket back to INHERITED
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
.build())
.build()
.update();
Expand Down

0 comments on commit 8bc6019

Please sign in to comment.