Skip to content

Commit a78f0a7

Browse files
committed
Merge pull request #772 from ajkannan/support-more-iam-roles
Make Role and Permission strings to allow for service-specific values
2 parents 5fbb41c + 52bf6c1 commit a78f0a7

File tree

11 files changed

+80
-238
lines changed

11 files changed

+80
-238
lines changed

gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import com.google.gcloud.Identity;
2626
import com.google.gcloud.resourcemanager.Policy;
27-
import com.google.gcloud.resourcemanager.Policy.Role;
27+
import com.google.gcloud.resourcemanager.Policy.ProjectRole;
2828
import com.google.gcloud.resourcemanager.Project;
2929
import com.google.gcloud.resourcemanager.ResourceManager;
3030
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
@@ -49,7 +49,7 @@ public static void main(String... args) {
4949
// Add a viewer
5050
Policy.Builder modifiedPolicy = policy.toBuilder();
5151
Identity newViewer = Identity.user("<insert user's email address here>");
52-
modifiedPolicy.addIdentity(Role.viewer(), newViewer);
52+
modifiedPolicy.addIdentity(ProjectRole.VIEWER.value(), newViewer);
5353

5454
// Write policy
5555
Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build());

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java

Lines changed: 32 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
import com.google.gcloud.IamPolicy;
2424
import com.google.gcloud.Identity;
2525

26-
import java.io.Serializable;
2726
import java.util.ArrayList;
2827
import java.util.HashMap;
2928
import java.util.LinkedList;
3029
import java.util.List;
3130
import java.util.Map;
32-
import java.util.Objects;
3331
import java.util.Set;
3432

3533
/**
@@ -42,120 +40,63 @@
4240
*
4341
* @see <a href="https://cloud.google.com/iam/reference/rest/v1/Policy">Policy</a>
4442
*/
45-
public class Policy extends IamPolicy<Policy.Role> {
43+
public class Policy extends IamPolicy<String> {
4644

4745
private static final long serialVersionUID = -5573557282693961850L;
4846

4947
/**
50-
* Represents legacy roles in an IAM Policy.
48+
* The project-level roles in an IAM policy. This enum is not an exhaustive list of all roles
49+
* you can use in an IAM policy. You can also use service-specific roles (e.g.
50+
* "roles/pubsub.editor"). See the <i>Supported Cloud Platform Services</i> page for links
51+
* to service-specific roles.
52+
*
53+
* @see <a href="https://cloud.google.com/iam/#supported_cloud_platform_services">Supported Cloud
54+
* Platform Services</a>
5155
*/
52-
public static class Role implements Serializable {
56+
public enum ProjectRole {
5357

5458
/**
55-
* The recognized roles in a Project's IAM policy.
59+
* Permissions for read-only actions that preserve state.
5660
*/
57-
public enum Type {
58-
59-
/**
60-
* Permissions for read-only actions that preserve state.
61-
*/
62-
VIEWER,
63-
64-
/**
65-
* All viewer permissions and permissions for actions that modify state.
66-
*/
67-
EDITOR,
68-
69-
/**
70-
* All editor permissions and permissions for the following actions:
71-
* <ul>
72-
* <li>Manage access control for a resource.
73-
* <li>Set up billing (for a project).
74-
* </ul>
75-
*/
76-
OWNER
77-
}
78-
79-
private static final long serialVersionUID = 2421978909244287488L;
80-
81-
private final String value;
82-
private final Type type;
83-
84-
private Role(String value, Type type) {
85-
this.value = value;
86-
this.type = type;
87-
}
88-
89-
String value() {
90-
return value;
91-
}
61+
VIEWER("roles/viewer"),
9262

9363
/**
94-
* Returns the type of role (editor, owner, or viewer). Returns {@code null} if the role type
95-
* is unrecognized.
64+
* All viewer permissions and permissions for actions that modify state.
9665
*/
97-
public Type type() {
98-
return type;
99-
}
66+
EDITOR("roles/editor"),
10067

10168
/**
102-
* Returns a {@code Role} of type {@link Type#VIEWER VIEWER}.
69+
* All editor permissions and permissions for the following actions:
70+
* <ul>
71+
* <li>Manage access control for a resource.
72+
* <li>Set up billing (for a project).
73+
* </ul>
10374
*/
104-
public static Role viewer() {
105-
return new Role("roles/viewer", Type.VIEWER);
106-
}
75+
OWNER("roles/owner");
10776

108-
/**
109-
* Returns a {@code Role} of type {@link Type#EDITOR EDITOR}.
110-
*/
111-
public static Role editor() {
112-
return new Role("roles/editor", Type.EDITOR);
77+
private final String value;
78+
79+
private ProjectRole(String value) {
80+
this.value = value;
11381
}
11482

11583
/**
116-
* Returns a {@code Role} of type {@link Type#OWNER OWNER}.
84+
* Returns the string value associated with the role.
11785
*/
118-
public static Role owner() {
119-
return new Role("roles/owner", Type.OWNER);
120-
}
121-
122-
static Role rawRole(String roleStr) {
123-
return new Role(roleStr, null);
124-
}
125-
126-
static Role fromStr(String roleStr) {
127-
try {
128-
Type type = Type.valueOf(roleStr.split("/")[1].toUpperCase());
129-
return new Role(roleStr, type);
130-
} catch (Exception ex) {
131-
return new Role(roleStr, null);
132-
}
133-
}
134-
135-
@Override
136-
public final int hashCode() {
137-
return Objects.hash(value, type);
138-
}
139-
140-
@Override
141-
public final boolean equals(Object obj) {
142-
if (!(obj instanceof Role)) {
143-
return false;
144-
}
145-
Role other = (Role) obj;
146-
return Objects.equals(value, other.value()) && Objects.equals(type, other.type());
86+
public String value() {
87+
return value;
14788
}
14889
}
14990

15091
/**
15192
* Builder for an IAM Policy.
15293
*/
153-
public static class Builder extends IamPolicy.Builder<Role, Builder> {
94+
public static class Builder extends IamPolicy.Builder<String, Builder> {
15495

15596
private Builder() {}
15697

15798
@VisibleForTesting
158-
Builder(Map<Role, Set<Identity>> bindings, String etag, Integer version) {
99+
Builder(Map<String, Set<Identity>> bindings, String etag, Integer version) {
159100
bindings(bindings).etag(etag).version(version);
160101
}
161102

@@ -188,10 +129,10 @@ com.google.api.services.cloudresourcemanager.model.Policy toPb() {
188129
new com.google.api.services.cloudresourcemanager.model.Policy();
189130
List<com.google.api.services.cloudresourcemanager.model.Binding> bindingPbList =
190131
new LinkedList<>();
191-
for (Map.Entry<Role, Set<Identity>> binding : bindings().entrySet()) {
132+
for (Map.Entry<String, Set<Identity>> binding : bindings().entrySet()) {
192133
com.google.api.services.cloudresourcemanager.model.Binding bindingPb =
193134
new com.google.api.services.cloudresourcemanager.model.Binding();
194-
bindingPb.setRole(binding.getKey().value());
135+
bindingPb.setRole(binding.getKey());
195136
bindingPb.setMembers(
196137
Lists.transform(
197138
new ArrayList<>(binding.getValue()),
@@ -211,11 +152,11 @@ public String apply(Identity identity) {
211152

212153
static Policy fromPb(
213154
com.google.api.services.cloudresourcemanager.model.Policy policyPb) {
214-
Map<Role, Set<Identity>> bindings = new HashMap<>();
155+
Map<String, Set<Identity>> bindings = new HashMap<>();
215156
for (com.google.api.services.cloudresourcemanager.model.Binding bindingPb :
216157
policyPb.getBindings()) {
217158
bindings.put(
218-
Role.fromStr(bindingPb.getRole()),
159+
bindingPb.getRole(),
219160
ImmutableSet.copyOf(
220161
Lists.transform(
221162
bindingPb.getMembers(),

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21-
import com.google.gcloud.resourcemanager.ResourceManager.Permission;
22-
2321
import java.io.IOException;
2422
import java.io.ObjectInputStream;
2523
import java.util.List;
@@ -235,35 +233,22 @@ public Policy replacePolicy(Policy newPolicy) {
235233
* if you're using Google Cloud Platform directly to manage permissions. This method is intended
236234
* for integration with your proprietary software, such as a customized graphical user interface.
237235
* For example, the Cloud Platform Console tests IAM permissions internally to determine which UI
238-
* should be available to the logged-in user.
236+
* should be available to the logged-in user. Each service that supports IAM lists the possible
237+
* permissions; see the <i>Supported Cloud Platform services</i> page below for links to these
238+
* lists.
239239
*
240240
* @return a list of booleans representing whether the caller has the permissions specified (in
241241
* the order of the given permissions)
242242
* @throws ResourceManagerException upon failure
243243
* @see <a href=
244244
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/testIamPermissions">
245245
* Resource Manager testIamPermissions</a>
246-
*/
247-
List<Boolean> testPermissions(List<Permission> permissions) {
248-
return resourceManager.testPermissions(projectId(), permissions);
249-
}
250-
251-
/**
252-
* Returns the permissions that a caller has on this project. You typically don't call this method
253-
* if you're using Google Cloud Platform directly to manage permissions. This method is intended
254-
* for integration with your proprietary software, such as a customized graphical user interface.
255-
* For example, the Cloud Platform Console tests IAM permissions internally to determine which UI
256-
* should be available to the logged-in user.
257-
*
258-
* @return a list of booleans representing whether the caller has the permissions specified (in
259-
* the order of the given permissions)
260-
* @throws ResourceManagerException upon failure
261246
* @see <a href=
262-
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/testIamPermissions">
263-
* Resource Manager testIamPermissions</a>
247+
* "https://cloud.google.com/iam/#supported_cloud_platform_services">Supported Cloud Platform
248+
* Services</a>
264249
*/
265-
List<Boolean> testPermissions(Permission first, Permission... others) {
266-
return resourceManager.testPermissions(projectId(), first, others);
250+
List<Boolean> testPermissions(List<String> permissions) {
251+
return resourceManager.testPermissions(projectId(), permissions);
267252
}
268253

269254
@Override

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -169,38 +169,6 @@ public static ProjectListOption fields(ProjectField... fields) {
169169
}
170170
}
171171

172-
/**
173-
* The permissions associated with a Google Cloud project. These values can be used when calling
174-
* {@link #testPermissions}.
175-
*
176-
* @see <a href=
177-
* "https://cloud.google.com/resource-manager/docs/access-control-proj#project-level_roles">
178-
* Project-level roles</a>
179-
*/
180-
enum Permission {
181-
DELETE("delete"),
182-
GET("get"),
183-
GET_POLICY("getIamPolicy"),
184-
REPLACE("update"),
185-
REPLACE_POLICY("setIamPolicy"),
186-
UNDELETE("undelete");
187-
188-
private static final String PREFIX = "resourcemanager.projects.";
189-
190-
private final String value;
191-
192-
Permission(String suffix) {
193-
this.value = PREFIX + suffix;
194-
}
195-
196-
/**
197-
* Returns the string representation of the permission.
198-
*/
199-
public String value() {
200-
return value;
201-
}
202-
}
203-
204172
/**
205173
* Creates a new project.
206174
*
@@ -358,30 +326,19 @@ public String value() {
358326
* this method if you're using Google Cloud Platform directly to manage permissions. This method
359327
* is intended for integration with your proprietary software, such as a customized graphical user
360328
* interface. For example, the Cloud Platform Console tests IAM permissions internally to
361-
* determine which UI should be available to the logged-in user.
329+
* determine which UI should be available to the logged-in user. Each service that supports IAM
330+
* lists the possible permissions; see the <i>Supported Cloud Platform services</i> page below for
331+
* links to these lists.
362332
*
363333
* @return A list of booleans representing whether the caller has the permissions specified (in
364334
* the order of the given permissions)
365335
* @throws ResourceManagerException upon failure
366336
* @see <a href=
367337
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/testIamPermissions">
368338
* Resource Manager testIamPermissions</a>
369-
*/
370-
List<Boolean> testPermissions(String projectId, List<Permission> permissions);
371-
372-
/**
373-
* Returns the permissions that a caller has on the specified project. You typically don't call
374-
* this method if you're using Google Cloud Platform directly to manage permissions. This method
375-
* is intended for integration with your proprietary software, such as a customized graphical user
376-
* interface. For example, the Cloud Platform Console tests IAM permissions internally to
377-
* determine which UI should be available to the logged-in user.
378-
*
379-
* @return A list of booleans representing whether the caller has the permissions specified (in
380-
* the order of the given permissions)
381-
* @throws ResourceManagerException upon failure
382339
* @see <a href=
383-
* "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/testIamPermissions">
384-
* Resource Manager testIamPermissions</a>
340+
* "https://cloud.google.com/iam/#supported_cloud_platform_services">Supported Cloud Platform
341+
* Services</a>
385342
*/
386-
List<Boolean> testPermissions(String projectId, Permission first, Permission... others);
343+
List<Boolean> testPermissions(String projectId, List<String> permissions);
387344
}

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.ImmutableMap;
2525
import com.google.common.collect.Iterables;
26-
import com.google.common.collect.Lists;
2726
import com.google.common.collect.Maps;
2827
import com.google.gcloud.BaseService;
2928
import com.google.gcloud.Page;
@@ -216,31 +215,20 @@ public com.google.api.services.cloudresourcemanager.model.Policy call() {
216215
}
217216

218217
@Override
219-
public List<Boolean> testPermissions(final String projectId, final List<Permission> permissions) {
218+
public List<Boolean> testPermissions(final String projectId, final List<String> permissions) {
220219
try {
221220
return runWithRetries(
222221
new Callable<List<Boolean>>() {
223222
@Override
224223
public List<Boolean> call() {
225-
return resourceManagerRpc.testPermissions(projectId,
226-
Lists.transform(permissions, new Function<Permission, String>() {
227-
@Override
228-
public String apply(Permission permission) {
229-
return permission.value();
230-
}
231-
}));
224+
return resourceManagerRpc.testPermissions(projectId, permissions);
232225
}
233226
}, options().retryParams(), EXCEPTION_HANDLER);
234227
} catch (RetryHelperException ex) {
235228
throw ResourceManagerException.translateAndThrow(ex);
236229
}
237230
}
238231

239-
@Override
240-
public List<Boolean> testPermissions(String projectId, Permission first, Permission... others) {
241-
return testPermissions(projectId, Lists.asList(first, others));
242-
}
243-
244232
private Map<ResourceManagerRpc.Option, ?> optionMap(Option... options) {
245233
Map<ResourceManagerRpc.Option, Object> temp = Maps.newEnumMap(ResourceManagerRpc.Option.class);
246234
for (Option option : options) {

0 commit comments

Comments
 (0)