Skip to content

Commit e87c444

Browse files
[IAM] Update quickstart samples (#2829)
* Add updated quickstart and tests * Update testPermissions method to be accurate * Update flow to remove testIamPermissions * Fix removeMember ConcurrentModificationException * Lint and style fixes * Change package to iam.snippets * Update test to use UUID * Fix capitalization of UUID in variable names
1 parent 51fa090 commit e87c444

23 files changed

+356
-32
lines changed

iam/api-client/src/main/java/com/google/iam/snippets/AddBinding.java renamed to iam/api-client/src/main/java/iam/snippets/AddBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_modify_policy_add_binding]
1919
import com.google.api.services.cloudresourcemanager.model.Binding;

iam/api-client/src/main/java/com/google/iam/snippets/AddMember.java renamed to iam/api-client/src/main/java/iam/snippets/AddMember.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_modify_policy_add_member]
1919
import com.google.api.services.cloudresourcemanager.model.Binding;
@@ -32,7 +32,7 @@ public static void addMember(Policy policy) {
3232
List<Binding> bindings = policy.getBindings();
3333

3434
for (Binding b : bindings) {
35-
if (b.getRole() == role) {
35+
if (b.getRole().equals(role)) {
3636
b.getMembers().add(member);
3737
System.out.println("Member " + member + " added to role " + role);
3838
return;

iam/api-client/src/main/java/com/google/iam/snippets/CreateServiceAccount.java renamed to iam/api-client/src/main/java/iam/snippets/CreateServiceAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_create_service_account]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/CreateServiceAccountKey.java renamed to iam/api-client/src/main/java/iam/snippets/CreateServiceAccountKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_create_key]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/DeleteServiceAccount.java renamed to iam/api-client/src/main/java/iam/snippets/DeleteServiceAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_delete_service_account]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/DeleteServiceAccountKey.java renamed to iam/api-client/src/main/java/iam/snippets/DeleteServiceAccountKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_delete_key]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/DisableServiceAccount.java renamed to iam/api-client/src/main/java/iam/snippets/DisableServiceAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_disable_service_account]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/EnableServiceAccount.java renamed to iam/api-client/src/main/java/iam/snippets/EnableServiceAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_enable_service_account]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/GetPolicy.java renamed to iam/api-client/src/main/java/iam/snippets/GetPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_get_policy]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/GrantableRoles.java renamed to iam/api-client/src/main/java/iam/snippets/GrantableRoles.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
1919
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;

iam/api-client/src/main/java/com/google/iam/snippets/ListServiceAccountKeys.java renamed to iam/api-client/src/main/java/iam/snippets/ListServiceAccountKeys.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_list_keys]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/ListServiceAccounts.java renamed to iam/api-client/src/main/java/iam/snippets/ListServiceAccounts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_list_service_accounts]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/Quickstart.java renamed to iam/api-client/src/main/java/iam/snippets/Quickstart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
// [START iam_quickstart]
1717

18-
package com.google.iam.snippets;
18+
package iam.snippets;
1919

2020
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
2121
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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]

iam/api-client/src/main/java/com/google/iam/snippets/RemoveMember.java renamed to iam/api-client/src/main/java/iam/snippets/RemoveMember.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_modify_policy_remove_member]
1919
import com.google.api.services.cloudresourcemanager.model.Binding;
@@ -30,18 +30,19 @@ public static void removeMember(Policy policy) {
3030
String member = "user:[email protected]";
3131

3232
List<Binding> bindings = policy.getBindings();
33-
33+
Binding binding = null;
3434
for (Binding b : bindings) {
35-
if (b.getRole() == role) {
36-
if (b.getMembers().contains(member)) {
37-
b.getMembers().remove(member);
38-
System.out.println("Member " + member + " removed from " + role);
39-
}
40-
if (b.getMembers().size() == 0) {
41-
policy.getBindings().remove(b);
42-
}
43-
return;
35+
if (b.getRole().equals(role)) {
36+
binding = b;
37+
}
38+
}
39+
if (binding.getMembers().contains(member)) {
40+
binding.getMembers().remove(member);
41+
System.out.println("Member " + member + " removed from " + role);
42+
if (binding.getMembers().isEmpty()) {
43+
policy.getBindings().remove(binding);
4444
}
45+
return;
4546
}
4647

4748
System.out.println("Role not found in policy; member not removed");

iam/api-client/src/main/java/com/google/iam/snippets/RenameServiceAccount.java renamed to iam/api-client/src/main/java/iam/snippets/RenameServiceAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_rename_service_account]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/SetPolicy.java renamed to iam/api-client/src/main/java/iam/snippets/SetPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_set_policy]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/main/java/com/google/iam/snippets/TestPermissions.java renamed to iam/api-client/src/main/java/iam/snippets/TestPermissions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
// [START iam_test_permissions]
1919
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

iam/api-client/src/test/java/com/google/iam/snippets/AccessTests.java renamed to iam/api-client/src/test/java/iam/snippets/AccessTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
import static org.junit.Assert.assertNotNull;
1919
import static org.junit.Assert.assertTrue;

iam/api-client/src/test/java/com/google/iam/snippets/GrantableRolesIT.java renamed to iam/api-client/src/test/java/iam/snippets/GrantableRolesIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
import static org.junit.Assert.assertTrue;
1919

iam/api-client/src/test/java/com/google/iam/snippets/QuickstartIT.java renamed to iam/api-client/src/test/java/iam/snippets/QuickstartIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package com.google.iam.snippets;
16+
package iam.snippets;
1717

1818
import static org.junit.Assert.assertTrue;
1919

0 commit comments

Comments
 (0)