|
| 1 | +/* Copyright 2020 Google LLC |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package iam.snippets; |
| 17 | +// [START iam_quickstart_v2] |
| 18 | + |
| 19 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 20 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 21 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 22 | +import com.google.api.services.cloudresourcemanager.CloudResourceManager; |
| 23 | +import com.google.api.services.cloudresourcemanager.model.Binding; |
| 24 | +import com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest; |
| 25 | +import com.google.api.services.cloudresourcemanager.model.Policy; |
| 26 | +import com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest; |
| 27 | +import com.google.api.services.iam.v1.IamScopes; |
| 28 | +import java.io.IOException; |
| 29 | +import java.security.GeneralSecurityException; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class QuickstartV2 { |
| 34 | + |
| 35 | + public static void main(String[] args) { |
| 36 | + // TODO: Replace with your project ID. |
| 37 | + String projectId = "your-project"; |
| 38 | + // TODO: Replace with the ID of your member in the form "member:[email protected]" |
| 39 | + String member = "your-member"; |
| 40 | + // The role to be granted. |
| 41 | + String role = "roles/logging.logWriter"; |
| 42 | + |
| 43 | + // Initializes the Cloud Resource Manager service. |
| 44 | + CloudResourceManager crmService = null; |
| 45 | + try { |
| 46 | + crmService = initializeService(); |
| 47 | + } catch (IOException | GeneralSecurityException e) { |
| 48 | + System.out.println("Unable to initialize service: \n" + e.toString()); |
| 49 | + } |
| 50 | + |
| 51 | + // Grants your member the "Log writer" role for your project. |
| 52 | + addBinding(crmService, projectId, member, role); |
| 53 | + |
| 54 | + // Get the project's policy and print all members with the "Log Writer" role |
| 55 | + Policy policy = getPolicy(crmService, projectId); |
| 56 | + Binding binding = null; |
| 57 | + List<Binding> bindings = policy.getBindings(); |
| 58 | + for (Binding b : bindings) { |
| 59 | + if (b.getRole().equals(role)) { |
| 60 | + binding = b; |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + System.out.println("Role: " + binding.getRole()); |
| 65 | + System.out.print("Members: "); |
| 66 | + for (String m : binding.getMembers()) { |
| 67 | + System.out.print("[" + m + "] "); |
| 68 | + } |
| 69 | + System.out.println(); |
| 70 | + |
| 71 | + // Removes member from the "Log writer" role. |
| 72 | + removeMember(crmService, projectId, member, role); |
| 73 | + } |
| 74 | + |
| 75 | + public static CloudResourceManager initializeService() |
| 76 | + throws IOException, GeneralSecurityException { |
| 77 | + // Use the Application Default Credentials strategy for authentication. For more info, see: |
| 78 | + // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically |
| 79 | + GoogleCredential credential = |
| 80 | + GoogleCredential.getApplicationDefault() |
| 81 | + .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); |
| 82 | + |
| 83 | + // Creates the Cloud Resource Manager service object. |
| 84 | + CloudResourceManager service = |
| 85 | + new CloudResourceManager.Builder( |
| 86 | + GoogleNetHttpTransport.newTrustedTransport(), |
| 87 | + JacksonFactory.getDefaultInstance(), |
| 88 | + credential) |
| 89 | + .setApplicationName("service-accounts") |
| 90 | + .build(); |
| 91 | + return service; |
| 92 | + } |
| 93 | + |
| 94 | + public static void addBinding( |
| 95 | + CloudResourceManager crmService, String projectId, String member, String role) { |
| 96 | + |
| 97 | + // Gets the project's policy. |
| 98 | + Policy policy = getPolicy(crmService, projectId); |
| 99 | + |
| 100 | + // If binding already exists, adds member to binding. |
| 101 | + List<Binding> bindings = policy.getBindings(); |
| 102 | + for (Binding b : bindings) { |
| 103 | + if (b.getRole().equals(role)) { |
| 104 | + b.getMembers().add(member); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // If binding does not exist, adds binding to policy. |
| 110 | + Binding binding = new Binding(); |
| 111 | + binding.setRole(role); |
| 112 | + binding.setMembers(Collections.singletonList(member)); |
| 113 | + policy.getBindings().add(binding); |
| 114 | + |
| 115 | + // Set the updated policy |
| 116 | + setPolicy(crmService, projectId, policy); |
| 117 | + } |
| 118 | + |
| 119 | + public static void removeMember( |
| 120 | + CloudResourceManager crmService, String projectId, String member, String role) { |
| 121 | + // Gets the project's policy. |
| 122 | + Policy policy = getPolicy(crmService, projectId); |
| 123 | + |
| 124 | + // Removes the member from the role. |
| 125 | + List<Binding> bindings = policy.getBindings(); |
| 126 | + Binding binding = null; |
| 127 | + for (Binding b : bindings) { |
| 128 | + if (b.getRole().equals(role)) { |
| 129 | + binding = b; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + if (binding.getMembers().contains(member)) { |
| 134 | + binding.getMembers().remove(member); |
| 135 | + if (binding.getMembers().isEmpty()) { |
| 136 | + policy.getBindings().remove(binding); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Sets the updated policy. |
| 141 | + setPolicy(crmService, projectId, policy); |
| 142 | + } |
| 143 | + |
| 144 | + public static Policy getPolicy(CloudResourceManager crmService, String projectId) { |
| 145 | + // Gets the project's policy by calling the |
| 146 | + // Cloud Resource Manager Projects API. |
| 147 | + Policy policy = null; |
| 148 | + try { |
| 149 | + GetIamPolicyRequest request = new GetIamPolicyRequest(); |
| 150 | + policy = crmService.projects().getIamPolicy(projectId, request).execute(); |
| 151 | + } catch (IOException e) { |
| 152 | + System.out.println("Unable to get policy: \n" + e.toString()); |
| 153 | + } |
| 154 | + return policy; |
| 155 | + } |
| 156 | + |
| 157 | + private static void setPolicy(CloudResourceManager crmService, String projectId, Policy policy) { |
| 158 | + // Sets the project's policy by calling the |
| 159 | + // Cloud Resource Manager Projects API. |
| 160 | + try { |
| 161 | + SetIamPolicyRequest request = new SetIamPolicyRequest(); |
| 162 | + request.setPolicy(policy); |
| 163 | + crmService.projects().setIamPolicy(projectId, request).execute(); |
| 164 | + } catch (IOException e) { |
| 165 | + System.out.println("Unable to set policy: \n" + e.toString()); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +// [END iam_quickstart_v2] |
0 commit comments