diff --git a/.kokoro/presubmit/samples.cfg b/.kokoro/presubmit/samples.cfg index 01e096004..74bcff8ae 100644 --- a/.kokoro/presubmit/samples.cfg +++ b/.kokoro/presubmit/samples.cfg @@ -30,4 +30,9 @@ env_vars: { env_vars: { key: "SECRET_MANAGER_KEYS" value: "java-docs-samples-service-account" -} \ No newline at end of file +} + +env_vars: { + key: "IT_SERVICE_ACCOUNT_EMAIL" + value: "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com" +} diff --git a/README.md b/README.md index 9d8bb830b..f0df79ebb 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/ | --------------------------- | --------------------------------- | ------ | | Configure Retries | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) | | Quickstart Sample | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) | +| Print Bucket Acl | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java) | +| Print Bucket Acl Filter By User | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java) | diff --git a/owlbot.py b/owlbot.py index 368e76845..91cd1935d 100644 --- a/owlbot.py +++ b/owlbot.py @@ -31,5 +31,6 @@ '.kokoro/nightly/integration.cfg', '.kokoro/nightly/java11-integration.cfg', '.kokoro/presubmit/integration.cfg', + '.kokoro/presubmit/samples.cfg', 'CONTRIBUTING.md' ]) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java b/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java new file mode 100644 index 000000000..54c66d63b --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java @@ -0,0 +1,52 @@ +/* + * Copyright 2022 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_print_bucket_acl] + +import com.google.cloud.storage.Acl; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.util.List; + +public class PrintBucketAcl { + + public static void printBucketAcl(String bucketName) { + + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + Storage storage = StorageOptions.newBuilder().build().getService(); + Bucket bucket = storage.get(bucketName); + List bucketAcls = bucket.getAcl(); + + for (Acl acl : bucketAcls) { + + // This will give you the role. + // See https://cloud.google.com/storage/docs/access-control/lists#permissions + String role = acl.getRole().name(); + + // This will give you the Entity type (i.e. User, Group, Project etc.) + // See https://cloud.google.com/storage/docs/access-control/lists#scopes + String entityType = acl.getEntity().getType().name(); + + System.out.printf("%s: %s \n", role, entityType); + } + } +} +// [END storage_print_bucket_acl] diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java b/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java new file mode 100644 index 000000000..68690299e --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java @@ -0,0 +1,46 @@ +/* + * Copyright 2022 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_print_bucket_acl_for_user] + +import com.google.cloud.storage.Acl; +import com.google.cloud.storage.Acl.User; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +public class PrintBucketAclFilterByUser { + + public static void printBucketAclFilterByUser(String bucketName, String userEmail) { + + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + // The email of the user whose acl is being retrieved. + // String userEmail = "someuser@domain.com" + + Storage storage = StorageOptions.newBuilder().build().getService(); + Bucket bucket = storage.get(bucketName); + + Acl userAcl = bucket.getAcl(new User(userEmail)); + String userRole = userAcl.getRole().name(); + System.out.println("User " + userEmail + " has role " + userRole); + } +} + +// [END storage_print_bucket_acl_for_user] diff --git a/samples/snippets/src/test/java/com/example/storage/TestBase.java b/samples/snippets/src/test/java/com/example/storage/TestBase.java new file mode 100644 index 000000000..3adaddabd --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/TestBase.java @@ -0,0 +1,53 @@ +/* + * Copyright 2022 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; + +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.testing.RemoteStorageHelper; +import com.google.cloud.testing.junit4.StdOutCaptureRule; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; + +public abstract class TestBase { + + @Rule public StdOutCaptureRule stdOut = new StdOutCaptureRule(); + + protected String bucketName; + protected Storage storage; + protected String blobName; + + protected Blob blob; + + @Before + public void setUp() { + blobName = "blob"; + bucketName = RemoteStorageHelper.generateBucketName(); + storage = StorageOptions.getDefaultInstance().getService(); + storage.create(BucketInfo.of(bucketName)); + blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build()); + } + + @After + public void tearDown() { + RemoteStorageHelper.forceDelete(storage, bucketName); + } +} diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclFilterByUserTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclFilterByUserTest.java new file mode 100644 index 000000000..95443a237 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclFilterByUserTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2022 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; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.example.storage.TestBase; +import com.google.cloud.storage.Acl; +import com.google.cloud.storage.Acl.Entity; +import com.google.cloud.storage.Acl.Role; +import com.google.cloud.storage.Acl.User; +import org.junit.Test; + +public class PrintBucketAclFilterByUserTest extends TestBase { + + public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); + + @Test + public void testPrintBucketAclByUser() { + // Check for user email before the actual test. + assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); + + Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL); + storage.createAcl(bucketName, Acl.of(testUser, Role.READER)); + PrintBucketAclFilterByUser.printBucketAclFilterByUser(bucketName, IT_SERVICE_ACCOUNT_EMAIL); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(Role.READER.name()); + } +} diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclTest.java new file mode 100644 index 000000000..5b3092ba9 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2022 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; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.example.storage.TestBase; +import com.google.cloud.storage.Acl; +import com.google.cloud.storage.Acl.Entity; +import com.google.cloud.storage.Acl.Role; +import com.google.cloud.storage.Acl.User; +import org.junit.Test; + +public class PrintBucketAclTest extends TestBase { + + public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); + + @Test + public void testPrintBucketAcls() { + // Check for user email before the actual test. + assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); + + Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL); + storage.createAcl(bucketName, Acl.of(testUser, Role.READER)); + PrintBucketAcl.printBucketAcl(bucketName); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("READER: USER"); + } +}