-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathCredentialAccessBoundary.java
More file actions
382 lines (330 loc) · 13.1 KB
/
CredentialAccessBoundary.java
File metadata and controls
382 lines (330 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright 2021 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.auth.oauth2;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.json.GenericJson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
/**
* Defines an upper bound of permissions available for a GCP credential via {@link
* AccessBoundaryRule}s.
*
* <p>See <a href='https://cloud.google.com/iam/docs/downscoping-short-lived-credentials'>for more
* information.</a>
*/
public final class CredentialAccessBoundary {
private static final int RULES_SIZE_LIMIT = 10;
private final List<AccessBoundaryRule> accessBoundaryRules;
CredentialAccessBoundary(List<AccessBoundaryRule> accessBoundaryRules) {
checkNotNull(accessBoundaryRules);
checkArgument(
!accessBoundaryRules.isEmpty(), "At least one access boundary rule must be provided.");
checkArgument(
accessBoundaryRules.size() < RULES_SIZE_LIMIT,
String.format(
"The provided list has more than %s access boundary rules.", RULES_SIZE_LIMIT));
this.accessBoundaryRules = accessBoundaryRules;
}
/**
* Internal method that returns the JSON string representation of the credential access boundary.
*/
String toJson() {
List<GenericJson> rules = new ArrayList<>();
for (AccessBoundaryRule rule : accessBoundaryRules) {
GenericJson ruleJson = new GenericJson();
ruleJson.setFactory(OAuth2Utils.JSON_FACTORY);
ruleJson.put("availableResource", rule.getAvailableResource());
ruleJson.put("availablePermissions", rule.getAvailablePermissions());
AccessBoundaryRule.AvailabilityCondition availabilityCondition =
rule.getAvailabilityCondition();
if (availabilityCondition != null) {
GenericJson availabilityConditionJson = new GenericJson();
availabilityConditionJson.setFactory(OAuth2Utils.JSON_FACTORY);
availabilityConditionJson.put("expression", availabilityCondition.getExpression());
if (availabilityCondition.getTitle() != null) {
availabilityConditionJson.put("title", availabilityCondition.getTitle());
}
if (availabilityCondition.getDescription() != null) {
availabilityConditionJson.put("description", availabilityCondition.getDescription());
}
ruleJson.put("availabilityCondition", availabilityConditionJson);
}
rules.add(ruleJson);
}
GenericJson accessBoundaryRulesJson = new GenericJson();
accessBoundaryRulesJson.setFactory(OAuth2Utils.JSON_FACTORY);
accessBoundaryRulesJson.put("accessBoundaryRules", rules);
GenericJson json = new GenericJson();
json.setFactory(OAuth2Utils.JSON_FACTORY);
json.put("accessBoundary", accessBoundaryRulesJson);
return json.toString();
}
public List<AccessBoundaryRule> getAccessBoundaryRules() {
return accessBoundaryRules;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private List<AccessBoundaryRule> accessBoundaryRules;
private Builder() {}
/**
* Sets the list of {@link AccessBoundaryRule}'s.
*
* <p>This list must not exceed 10 rules.
*
* @param rule the collection of rules to be set, should not be null
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public Builder setRules(List<AccessBoundaryRule> rule) {
accessBoundaryRules = new ArrayList<>(checkNotNull(rule));
return this;
}
@CanIgnoreReturnValue
public CredentialAccessBoundary.Builder addRule(AccessBoundaryRule rule) {
if (accessBoundaryRules == null) {
accessBoundaryRules = new ArrayList<>();
}
accessBoundaryRules.add(checkNotNull(rule));
return this;
}
public CredentialAccessBoundary build() {
return new CredentialAccessBoundary(accessBoundaryRules);
}
}
/**
* Defines an upper bound of permissions on a particular resource.
*
* <p>The following snippet shows an AccessBoundaryRule that applies to the Cloud Storage bucket
* bucket-one to set the upper bound of permissions to those defined by the
* roles/storage.objectViewer role.
*
* <pre><code>
* AccessBoundaryRule rule = AccessBoundaryRule.newBuilder()
* .setAvailableResource("//storage.googleapis.com/projects/_/buckets/bucket-one")
* .addAvailablePermission("inRole:roles/storage.objectViewer")
* .build();
* </code></pre>
*/
public static final class AccessBoundaryRule {
private final String availableResource;
private final List<String> availablePermissions;
@Nullable private final AvailabilityCondition availabilityCondition;
AccessBoundaryRule(
String availableResource,
List<String> availablePermissions,
@Nullable AvailabilityCondition availabilityCondition) {
this.availableResource = checkNotNull(availableResource);
this.availablePermissions = new ArrayList<>(checkNotNull(availablePermissions));
this.availabilityCondition = availabilityCondition;
checkArgument(!availableResource.isEmpty(), "The provided availableResource is empty.");
checkArgument(
!availablePermissions.isEmpty(), "The list of provided availablePermissions is empty.");
for (String permission : availablePermissions) {
if (permission == null) {
throw new IllegalArgumentException("One of the provided available permissions is null.");
}
if (permission.isEmpty()) {
throw new IllegalArgumentException("One of the provided available permissions is empty.");
}
}
}
public String getAvailableResource() {
return availableResource;
}
public List<String> getAvailablePermissions() {
return availablePermissions;
}
@Nullable
public AvailabilityCondition getAvailabilityCondition() {
return availabilityCondition;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private String availableResource;
private List<String> availablePermissions;
@Nullable private AvailabilityCondition availabilityCondition;
private Builder() {}
/**
* Sets the available resource, which is the full resource name of the GCP resource to allow
* access to.
*
* <p>For example: "//storage.googleapis.com/projects/_/buckets/example".
*
* @param availableResource the resource name to set
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public Builder setAvailableResource(String availableResource) {
this.availableResource = availableResource;
return this;
}
/**
* Sets the list of permissions that can be used on the resource. This should be a list of IAM
* roles prefixed by inRole.
*
* <p>For example: {"inRole:roles/storage.objectViewer"}.
*
* @param availablePermissions the collection of permissions to set, should not be null
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public Builder setAvailablePermissions(List<String> availablePermissions) {
this.availablePermissions = new ArrayList<>(checkNotNull(availablePermissions));
return this;
}
/**
* Adds a permission that can be used on the resource. This should be an IAM role prefixed by
* inRole.
*
* <p>For example: "inRole:roles/storage.objectViewer".
*
* @param availablePermission a permission to add, should not be null
* @return this {@code Builder} object
*/
public Builder addAvailablePermission(String availablePermission) {
if (availablePermissions == null) {
availablePermissions = new ArrayList<>();
}
availablePermissions.add(availablePermission);
return this;
}
/**
* Sets the availability condition which is an IAM condition that defines constraints to apply
* to the token expressed in CEL format.
*
* @param availabilityCondition the {@code AvailabilityCondition} to set
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public Builder setAvailabilityCondition(AvailabilityCondition availabilityCondition) {
this.availabilityCondition = availabilityCondition;
return this;
}
public AccessBoundaryRule build() {
return new AccessBoundaryRule(
availableResource, availablePermissions, availabilityCondition);
}
}
/**
* An optional condition that can be used as part of a {@link AccessBoundaryRule} to further
* restrict permissions.
*
* <p>For example, you can define an AvailabilityCondition that applies to a set of Cloud
* Storage objects whose names start with auth:
*
* <pre><code>
* AvailabilityCondition availabilityCondition = AvailabilityCondition.newBuilder()
* .setExpression("resource.name.startsWith('projects/_/buckets/bucket-123/objects/auth')")
* .build();
* </code></pre>
*/
public static final class AvailabilityCondition {
private final String expression;
@Nullable private final String title;
@Nullable private final String description;
AvailabilityCondition(
String expression, @Nullable String title, @Nullable String description) {
this.expression = checkNotNull(expression);
this.title = title;
this.description = description;
checkArgument(!expression.isEmpty(), "The provided expression is empty.");
}
public String getExpression() {
return expression;
}
@Nullable
public String getTitle() {
return title;
}
@Nullable
public String getDescription() {
return description;
}
public static Builder newBuilder() {
return new Builder();
}
public static final class Builder {
private String expression;
@Nullable private String title;
@Nullable private String description;
private Builder() {}
/**
* Sets the required expression which must be defined in Common Expression Language (CEL)
* format.
*
* <p>This expression specifies the Cloud Storage object where permissions are available.
* See <a href='https://cloud.google.com/iam/docs/conditions-overview#cel'>for more
* information.</a>
*
* @param expression the expression to set
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public Builder setExpression(String expression) {
this.expression = expression;
return this;
}
/**
* Sets the optional title that identifies the purpose of the condition.
*
* @param title the title to set
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Sets the description that details the purpose of the condition.
*
* @param description the description to set
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public Builder setDescription(String description) {
this.description = description;
return this;
}
public AvailabilityCondition build() {
return new AvailabilityCondition(expression, title, description);
}
}
}
}
}