Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions google-cloud-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@
<artifactId>google-cloud-storage</artifactId>
<version>1.117.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev20201112-1.31.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.31.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-translate</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020 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_get_public_access_prevention]
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 GetPublicAccessPrevention {
public static void getPublicAccessPrevention(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);

// Gets Bucket Metadata and prints publicAccessPrevention value (either 'unspecified' or
// 'enforced').
BucketInfo.PublicAccessPrevention publicAccessPrevention =
bucket.getIamConfiguration().getPublicAccessPrevention();

System.out.println(
"Public access prevention is set to "
+ publicAccessPrevention.getValue()
+ " for "
+ bucketName);
}
}
// [END storage_get_public_access_prevention]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020 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_enforced]
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 SetPublicAccessPreventionEnforced {
public static void setPublicAccessPreventionEnforced(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);

// Enforces public access prevention for the bucket
bucket
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED)
.build())
.build()
.update();

System.out.println("Public access prevention is set to enforced for " + bucketName);
}
}
// [END storage_set_public_access_prevention_enforced]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020 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_unspecified]
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 SetPublicAccessPreventionUnspecified {
public static void setPublicAccessPreventionUnspecified(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 'unspecified' for the bucket
bucket
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.build())
.build()
.update();

System.out.println("Public access prevention is set to unspecified for " + bucketName);
}
}
// [END storage_set_public_access_prevention_unspecified]
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.cloud.examples.storage.buckets.EnableLifecycleManagement;
import com.google.cloud.examples.storage.buckets.EnableRequesterPays;
import com.google.cloud.examples.storage.buckets.GetBucketMetadata;
import com.google.cloud.examples.storage.buckets.GetPublicAccessPrevention;
import com.google.cloud.examples.storage.buckets.ListBucketIamMembers;
import com.google.cloud.examples.storage.buckets.ListBuckets;
import com.google.cloud.examples.storage.buckets.MakeBucketPublic;
Expand All @@ -45,6 +46,8 @@
import com.google.cloud.examples.storage.buckets.RemoveBucketIamMember;
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.objects.DownloadRequesterPaysObject;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.Role;
Expand Down Expand Up @@ -304,6 +307,110 @@ public void testDisableLifecycleManagement() {
assertEquals(0, storage.get(BUCKET).getLifecycleRules().size());
}

@Test
public void testGetPublicAccessPrevention() {
try {
// By default a bucket PAP state is UNSPECIFIED and we are changing the state to validate
// non-default state.
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED)
.build())
.build()
.update();
PrintStream standardOut = System.out;
final ByteArrayOutputStream snippetOutputCapture = new ByteArrayOutputStream();
System.setOut(new PrintStream(snippetOutputCapture));
GetPublicAccessPrevention.getPublicAccessPrevention(PROJECT_ID, BUCKET);
String snippetOutput = snippetOutputCapture.toString();
System.setOut(standardOut);
assertTrue(snippetOutput.contains("enforced"));
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.build())
.build()
.update();
} finally {
// No matter what happens make sure test set bucket back to UNSPECIFIED
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.build())
.build()
.update();
}
}

@Test
public void testSetPublicAccessPreventionEnforced() {
try {
SetPublicAccessPreventionEnforced.setPublicAccessPreventionEnforced(PROJECT_ID, BUCKET);
assertEquals(
storage.get(BUCKET).getIamConfiguration().getPublicAccessPrevention(),
BucketInfo.PublicAccessPrevention.ENFORCED);
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.build())
.build()
.update();
} finally {
// No matter what happens make sure test set bucket back to UNSPECIFIED
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.build())
.build()
.update();
}
}

@Test
public void testSetPublicAccessPreventionUnspecified() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these tests be verifying public access prevention without the API call to getPublicAccessPrevention? This is testing the setPublicAccessPreventionUnspecified function, but also relies on the functionality of getPublicAccessPrevention. Not sure what practices are on how small a unit should be for each unit test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I'll decouple.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah for these tests, I keep them simple for samples. This case is covered now by library integration tests.

try {
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED)
.build())
.build()
.update();
SetPublicAccessPreventionUnspecified.setPublicAccessPreventionUnspecified(PROJECT_ID, BUCKET);
assertEquals(
storage.get(BUCKET).getIamConfiguration().getPublicAccessPrevention(),
BucketInfo.PublicAccessPrevention.UNSPECIFIED);
} finally {
// No matter what happens make sure test set bucket back to UNSPECIFIED
storage
.get(BUCKET)
.toBuilder()
.setIamConfiguration(
BucketInfo.IamConfiguration.newBuilder()
.setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED)
.build())
.build()
.update();
}
}

@Test
public void testAddListRemoveBucketIamMembers() {
storage.update(
Expand Down