|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 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 | + |
| 17 | +package com.google.cloud.examples.storage.snippets; |
| 18 | + |
| 19 | +import com.google.cloud.Identity; |
| 20 | +import com.google.cloud.Policy; |
| 21 | +import com.google.cloud.Role; |
| 22 | +import com.google.cloud.storage.Storage; |
| 23 | +import com.google.cloud.storage.StorageOptions; |
| 24 | + |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +/** |
| 29 | + * This class contains Bucket-level IAM snippets for the {@link Storage} interface. |
| 30 | + */ |
| 31 | +public class BucketIamSnippets { |
| 32 | + |
| 33 | + /** |
| 34 | + * Example of listing the Bucket-Level IAM Roles and Members |
| 35 | + */ |
| 36 | + public Policy listBucketIamMembers(String bucketName) { |
| 37 | + // [START view_bucket_iam_members] |
| 38 | + // Initialize a Cloud Storage client |
| 39 | + Storage storage = StorageOptions.getDefaultInstance().getService(); |
| 40 | + |
| 41 | + // Get IAM Policy for a bucket |
| 42 | + Policy policy = storage.getIamPolicy(bucketName); |
| 43 | + |
| 44 | + // Print Roles and its identities |
| 45 | + Map<Role, Set<Identity>> policyBindings = policy.getBindings(); |
| 46 | + for(Map.Entry<Role, Set<Identity>> entry : policyBindings.entrySet()) { |
| 47 | + System.out.printf("Role: %s Identities: %s\n", entry.getKey(), entry.getValue()); |
| 48 | + } |
| 49 | + // [END view_bucket_iam_members] |
| 50 | + return policy; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Example of adding a member to the Bucket-level IAM |
| 55 | + */ |
| 56 | + public Policy addBucketIamMember(String bucketName, Role role, Identity identity) { |
| 57 | + // [START add_bucket_iam_member] |
| 58 | + // Initialize a Cloud Storage client |
| 59 | + Storage storage = StorageOptions.getDefaultInstance().getService(); |
| 60 | + |
| 61 | + // Get IAM Policy for a bucket |
| 62 | + Policy policy = storage.getIamPolicy(bucketName); |
| 63 | + |
| 64 | + // Add identity to Bucket-level IAM role |
| 65 | + Policy updatedPolicy = storage.setIamPolicy(bucketName, |
| 66 | + policy.toBuilder().addIdentity(role, identity).build()); |
| 67 | + |
| 68 | + if (updatedPolicy.getBindings().get(role).contains(identity)) { |
| 69 | + System.out.printf("Added %s with role %s to %s\n", identity, role, bucketName); |
| 70 | + } |
| 71 | + // [END add_bucket_iam_member] |
| 72 | + return updatedPolicy; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Example of removing a member from the Bucket-level IAM |
| 77 | + */ |
| 78 | + public Policy removeBucketIamMember(String bucketName, Role role, Identity identity) { |
| 79 | + // [START remove_bucket_iam_member] |
| 80 | + // Initialize a Cloud Storage client |
| 81 | + Storage storage = StorageOptions.getDefaultInstance().getService(); |
| 82 | + |
| 83 | + // Get IAM Policy for a bucket |
| 84 | + Policy policy = storage.getIamPolicy(bucketName); |
| 85 | + |
| 86 | + // Remove an identity from a Bucket-level IAM role |
| 87 | + Policy updatedPolicy = storage.setIamPolicy(bucketName, |
| 88 | + policy.toBuilder().removeIdentity(role, identity).build()); |
| 89 | + |
| 90 | + if (updatedPolicy.getBindings().get(role) == null || |
| 91 | + !updatedPolicy.getBindings().get(role).contains(identity)) { |
| 92 | + System.out.printf("Removed %s with role %s from %s\n", identity, role, bucketName); |
| 93 | + } |
| 94 | + // [END remove_bucket_iam_member] |
| 95 | + return updatedPolicy; |
| 96 | + } |
| 97 | +} |
0 commit comments