Skip to content

Commit

Permalink
docs: Adding Samples for Printing Bucket ACLs and Printing Bucket ACL…
Browse files Browse the repository at this point in the history
… for a specific user. (#1236)

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: BenWhitehead <BenWhitehead@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 17, 2022
1 parent 6b08589 commit d82333b
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .kokoro/presubmit/samples.cfg
Expand Up @@ -30,4 +30,9 @@ env_vars: {
env_vars: {
key: "SECRET_MANAGER_KEYS"
value: "java-docs-samples-service-account"
}
}

env_vars: {
key: "IT_SERVICE_ACCOUNT_EMAIL"
value: "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"
}
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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) |



Expand Down
1 change: 1 addition & 0 deletions owlbot.py
Expand Up @@ -31,5 +31,6 @@
'.kokoro/nightly/integration.cfg',
'.kokoro/nightly/java11-integration.cfg',
'.kokoro/presubmit/integration.cfg',
'.kokoro/presubmit/samples.cfg',
'CONTRIBUTING.md'
])
@@ -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<Acl> 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]
@@ -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]
53 changes: 53 additions & 0 deletions 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);
}
}
@@ -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());
}
}
@@ -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");
}
}

0 comments on commit d82333b

Please sign in to comment.