|
1 | | -/* Copyright 2018 Google LLC |
| 1 | +/* Copyright 2020 Google LLC |
2 | 2 | * |
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | * you may not use this file except in compliance with the License. |
|
13 | 13 | * limitations under the License. |
14 | 14 | */ |
15 | 15 |
|
16 | | -// [START iam_quickstart] |
17 | | - |
18 | 16 | package iam.snippets; |
19 | 17 |
|
| 18 | +// [START iam_quickstart] |
20 | 19 | import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
21 | 20 | import com.google.api.client.json.jackson2.JacksonFactory; |
22 | | -import com.google.api.services.iam.v1.Iam; |
| 21 | +import com.google.api.services.cloudresourcemanager.CloudResourceManager; |
| 22 | +import com.google.api.services.cloudresourcemanager.model.Binding; |
| 23 | +import com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest; |
| 24 | +import com.google.api.services.cloudresourcemanager.model.Policy; |
| 25 | +import com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest; |
23 | 26 | import com.google.api.services.iam.v1.IamScopes; |
24 | | -import com.google.api.services.iam.v1.model.ListRolesResponse; |
25 | | -import com.google.api.services.iam.v1.model.Role; |
26 | 27 | import com.google.auth.http.HttpCredentialsAdapter; |
27 | 28 | import com.google.auth.oauth2.GoogleCredentials; |
| 29 | +import java.io.IOException; |
| 30 | +import java.security.GeneralSecurityException; |
28 | 31 | import java.util.Collections; |
29 | 32 | import java.util.List; |
30 | 33 |
|
31 | 34 | public class Quickstart { |
32 | 35 |
|
33 | | - public static void main(String[] args) throws Exception { |
34 | | - // Get credentials |
| 36 | + public static void main(String[] args) { |
| 37 | + // TODO: Replace with your project ID. |
| 38 | + String projectId = "your-project"; |
| 39 | + // TODO: Replace with the ID of your member in the form "user:member@example.com" |
| 40 | + String member = "your-member"; |
| 41 | + // The role to be granted. |
| 42 | + String role = "roles/logging.logWriter"; |
| 43 | + |
| 44 | + // Initializes the Cloud Resource Manager service. |
| 45 | + CloudResourceManager crmService = null; |
| 46 | + try { |
| 47 | + crmService = initializeService(); |
| 48 | + } catch (IOException | GeneralSecurityException e) { |
| 49 | + System.out.println("Unable to initialize service: \n" + e.getMessage() + e.getStackTrace()); |
| 50 | + } |
| 51 | + |
| 52 | + // Grants your member the "Log writer" role for your project. |
| 53 | + addBinding(crmService, projectId, member, role); |
| 54 | + |
| 55 | + // Get the project's policy and print all members with the "Log Writer" role |
| 56 | + Policy policy = getPolicy(crmService, projectId); |
| 57 | + Binding binding = null; |
| 58 | + List<Binding> bindings = policy.getBindings(); |
| 59 | + for (Binding b : bindings) { |
| 60 | + if (b.getRole().equals(role)) { |
| 61 | + binding = b; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + System.out.println("Role: " + binding.getRole()); |
| 66 | + System.out.print("Members: "); |
| 67 | + for (String m : binding.getMembers()) { |
| 68 | + System.out.print("[" + m + "] "); |
| 69 | + } |
| 70 | + System.out.println(); |
| 71 | + |
| 72 | + // Removes member from the "Log writer" role. |
| 73 | + removeMember(crmService, projectId, member, role); |
| 74 | + } |
| 75 | + |
| 76 | + public static CloudResourceManager initializeService() |
| 77 | + throws IOException, GeneralSecurityException { |
| 78 | + // Use the Application Default Credentials strategy for authentication. For more info, see: |
| 79 | + // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically |
35 | 80 | GoogleCredentials credential = |
36 | 81 | GoogleCredentials.getApplicationDefault() |
37 | 82 | .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); |
38 | 83 |
|
39 | | - // Create the Cloud IAM service object |
40 | | - Iam service = |
41 | | - new Iam.Builder( |
| 84 | + // Creates the Cloud Resource Manager service object. |
| 85 | + CloudResourceManager service = |
| 86 | + new CloudResourceManager.Builder( |
42 | 87 | GoogleNetHttpTransport.newTrustedTransport(), |
43 | 88 | JacksonFactory.getDefaultInstance(), |
44 | 89 | new HttpCredentialsAdapter(credential)) |
45 | | - .setApplicationName("quickstart") |
| 90 | + .setApplicationName("iam-quickstart") |
46 | 91 | .build(); |
| 92 | + return service; |
| 93 | + } |
| 94 | + |
| 95 | + public static void addBinding( |
| 96 | + CloudResourceManager crmService, String projectId, String member, String role) { |
| 97 | + |
| 98 | + // Gets the project's policy. |
| 99 | + Policy policy = getPolicy(crmService, projectId); |
47 | 100 |
|
48 | | - // Call the Cloud IAM Roles API |
49 | | - ListRolesResponse response = service.roles().list().execute(); |
50 | | - List<Role> roles = response.getRoles(); |
| 101 | + // Finds binding in policy, if it exists |
| 102 | + Binding binding = null; |
| 103 | + for (Binding b : policy.getBindings()) { |
| 104 | + if (b.getRole().equals(role)) { |
| 105 | + binding = b; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (binding != null) { |
| 111 | + // If binding already exists, adds member to binding. |
| 112 | + binding.getMembers().add(member); |
| 113 | + } else { |
| 114 | + // If binding does not exist, adds binding to policy. |
| 115 | + binding = new Binding(); |
| 116 | + binding.setRole(role); |
| 117 | + binding.setMembers(Collections.singletonList(member)); |
| 118 | + policy.getBindings().add(binding); |
| 119 | + } |
| 120 | + |
| 121 | + // Sets the updated policy |
| 122 | + setPolicy(crmService, projectId, policy); |
| 123 | + } |
| 124 | + |
| 125 | + public static void removeMember( |
| 126 | + CloudResourceManager crmService, String projectId, String member, String role) { |
| 127 | + // Gets the project's policy. |
| 128 | + Policy policy = getPolicy(crmService, projectId); |
| 129 | + |
| 130 | + // Removes the member from the role. |
| 131 | + Binding binding = null; |
| 132 | + for (Binding b : policy.getBindings()) { |
| 133 | + if (b.getRole().equals(role)) { |
| 134 | + binding = b; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + if (binding.getMembers().contains(member)) { |
| 139 | + binding.getMembers().remove(member); |
| 140 | + if (binding.getMembers().isEmpty()) { |
| 141 | + policy.getBindings().remove(binding); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Sets the updated policy. |
| 146 | + setPolicy(crmService, projectId, policy); |
| 147 | + } |
| 148 | + |
| 149 | + public static Policy getPolicy(CloudResourceManager crmService, String projectId) { |
| 150 | + // Gets the project's policy by calling the |
| 151 | + // Cloud Resource Manager Projects API. |
| 152 | + Policy policy = null; |
| 153 | + try { |
| 154 | + GetIamPolicyRequest request = new GetIamPolicyRequest(); |
| 155 | + policy = crmService.projects().getIamPolicy(projectId, request).execute(); |
| 156 | + } catch (IOException e) { |
| 157 | + System.out.println("Unable to get policy: \n" + e.getMessage() + e.getStackTrace()); |
| 158 | + } |
| 159 | + return policy; |
| 160 | + } |
51 | 161 |
|
52 | | - // Process the response |
53 | | - for (Role role : roles) { |
54 | | - System.out.println("Title: " + role.getTitle()); |
55 | | - System.out.println("Name: " + role.getName()); |
56 | | - System.out.println("Description: " + role.getDescription()); |
57 | | - System.out.println(); |
| 162 | + private static void setPolicy(CloudResourceManager crmService, String projectId, Policy policy) { |
| 163 | + // Sets the project's policy by calling the |
| 164 | + // Cloud Resource Manager Projects API. |
| 165 | + try { |
| 166 | + SetIamPolicyRequest request = new SetIamPolicyRequest(); |
| 167 | + request.setPolicy(policy); |
| 168 | + crmService.projects().setIamPolicy(projectId, request).execute(); |
| 169 | + } catch (IOException e) { |
| 170 | + System.out.println("Unable to set policy: \n" + e.getMessage() + e.getStackTrace()); |
58 | 171 | } |
59 | 172 | } |
60 | 173 | } |
|
0 commit comments