Skip to content

Commit 8bc6019

Browse files
authored
feat: introduce inherited value for pap (googleapis#7693)
Fixes googleapis#7682
1 parent 1fb798e commit 8bc6019

4 files changed

Lines changed: 63 additions & 15 deletions

File tree

‎google-cloud-examples/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<dependency>
7777
<groupId>com.google.cloud</groupId>
7878
<artifactId>google-cloud-storage</artifactId>
79-
<version>2.1.6</version>
79+
<version>2.1.7</version>
8080
</dependency>
8181
<dependency>
8282
<groupId>com.google.apis</groupId>

‎google-cloud-examples/src/main/java/com/google/cloud/examples/storage/buckets/GetPublicAccessPrevention.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void getPublicAccessPrevention(String projectId, String bucketName
3232
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
3333
Bucket bucket = storage.get(bucketName);
3434

35-
// Gets Bucket Metadata and prints publicAccessPrevention value (either 'unspecified' or
35+
// Gets Bucket Metadata and prints publicAccessPrevention value (either 'inherited' or
3636
// 'enforced').
3737
BucketInfo.PublicAccessPrevention publicAccessPrevention =
3838
bucket.getIamConfiguration().getPublicAccessPrevention();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.examples.storage.buckets;
17+
18+
// [START storage_set_public_access_prevention_inherited]
19+
import com.google.cloud.storage.Bucket;
20+
import com.google.cloud.storage.BucketInfo;
21+
import com.google.cloud.storage.Storage;
22+
import com.google.cloud.storage.StorageOptions;
23+
24+
public class SetPublicAccessPreventionInherited {
25+
public static void setPublicAccessPreventionInherited(String projectId, String bucketName) {
26+
// The ID of your GCP project
27+
// String projectId = "your-project-id";
28+
29+
// The ID of your GCS bucket
30+
// String bucketName = "your-unique-bucket-name";
31+
32+
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
33+
Bucket bucket = storage.get(bucketName);
34+
35+
// Sets public access prevention to 'inherited' for the bucket
36+
bucket
37+
.toBuilder()
38+
.setIamConfiguration(
39+
BucketInfo.IamConfiguration.newBuilder()
40+
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
41+
.build())
42+
.build()
43+
.update();
44+
45+
System.out.println("Public access prevention is set to 'inherited' for " + bucketName);
46+
}
47+
}
48+
// [END storage_set_public_access_prevention_inherited]

‎google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBucketSnippets.java‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import com.google.cloud.examples.storage.buckets.RemoveBucketLabel;
4848
import com.google.cloud.examples.storage.buckets.SetBucketWebsiteInfo;
4949
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionEnforced;
50-
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionUnspecified;
50+
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionInherited;
5151
import com.google.cloud.examples.storage.objects.DownloadRequesterPaysObject;
5252
import com.google.cloud.storage.Acl;
5353
import com.google.cloud.storage.Acl.Role;
@@ -310,7 +310,7 @@ public void testDisableLifecycleManagement() {
310310
@Test
311311
public void testGetPublicAccessPrevention() {
312312
try {
313-
// By default a bucket PAP state is UNSPECIFIED and we are changing the state to validate
313+
// By default a bucket PAP state is INHERITED and we are changing the state to validate
314314
// non-default state.
315315
storage
316316
.get(BUCKET)
@@ -333,18 +333,18 @@ public void testGetPublicAccessPrevention() {
333333
.toBuilder()
334334
.setIamConfiguration(
335335
BucketInfo.IamConfiguration.newBuilder()
336-
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
336+
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
337337
.build())
338338
.build()
339339
.update();
340340
} finally {
341-
// No matter what happens make sure test set bucket back to UNSPECIFIED
341+
// No matter what happens make sure test set bucket back to INHERITED
342342
storage
343343
.get(BUCKET)
344344
.toBuilder()
345345
.setIamConfiguration(
346346
BucketInfo.IamConfiguration.newBuilder()
347-
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
347+
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
348348
.build())
349349
.build()
350350
.update();
@@ -363,26 +363,26 @@ public void testSetPublicAccessPreventionEnforced() {
363363
.toBuilder()
364364
.setIamConfiguration(
365365
BucketInfo.IamConfiguration.newBuilder()
366-
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
366+
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
367367
.build())
368368
.build()
369369
.update();
370370
} finally {
371-
// No matter what happens make sure test set bucket back to UNSPECIFIED
371+
// No matter what happens make sure test set bucket back to INHERITED
372372
storage
373373
.get(BUCKET)
374374
.toBuilder()
375375
.setIamConfiguration(
376376
BucketInfo.IamConfiguration.newBuilder()
377-
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
377+
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
378378
.build())
379379
.build()
380380
.update();
381381
}
382382
}
383383

384384
@Test
385-
public void testSetPublicAccessPreventionUnspecified() {
385+
public void testSetPublicAccessPreventionInherited() {
386386
try {
387387
storage
388388
.get(BUCKET)
@@ -393,18 +393,18 @@ public void testSetPublicAccessPreventionUnspecified() {
393393
.build())
394394
.build()
395395
.update();
396-
SetPublicAccessPreventionUnspecified.setPublicAccessPreventionUnspecified(PROJECT_ID, BUCKET);
396+
SetPublicAccessPreventionInherited.setPublicAccessPreventionInherited(PROJECT_ID, BUCKET);
397397
assertEquals(
398398
storage.get(BUCKET).getIamConfiguration().getPublicAccessPrevention(),
399-
BucketInfo.PublicAccessPrevention.UNSPECIFIED);
399+
BucketInfo.PublicAccessPrevention.INHERITED);
400400
} finally {
401-
// No matter what happens make sure test set bucket back to UNSPECIFIED
401+
// No matter what happens make sure test set bucket back to INHERITED
402402
storage
403403
.get(BUCKET)
404404
.toBuilder()
405405
.setIamConfiguration(
406406
BucketInfo.IamConfiguration.newBuilder()
407-
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
407+
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED)
408408
.build())
409409
.build()
410410
.update();

0 commit comments

Comments
 (0)