-
Notifications
You must be signed in to change notification settings - Fork 215
/
Policy.java
executable file
·500 lines (445 loc) · 20.1 KB
/
Policy.java
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.policies.model;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.entity.Entity;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
/**
* A Policy contains {@link PolicyEntry}s containing information about which {@link Subjects} are granted/revoked
* which {@link Permissions} on which {@link Resources}.
*/
public interface Policy extends Iterable<PolicyEntry>, Entity<PolicyRevision> {
/**
* The name of the Json field when a policy is inlined in another Json object.
*/
String INLINED_FIELD_NAME = "_policy";
/**
* Returns a mutable builder with a fluent API for an immutable {@code Policy}.
*
* @param policyId the ID of the new Policy.
* @return the new builder.
* @throws PolicyIdInvalidException if {@code id} is invalid.
*/
static PolicyBuilder newBuilder(final PolicyId policyId) {
return PoliciesModelFactory.newPolicyBuilder(policyId);
}
/**
* Returns a mutable builder with a fluent API for an immutable {@code Policy}.
*
* @return the new builder.
*/
static PolicyBuilder newBuilder() {
return PoliciesModelFactory.newPolicyBuilder();
}
/**
* Returns a mutable builder with a fluent API for an immutable {@code Policy}. The builder is initialised
* with the Policy entries of this instance.
*
* @return the new builder.
*/
default PolicyBuilder toBuilder() {
return PoliciesModelFactory.newPolicyBuilder(this);
}
@Override
Optional<PolicyId> getEntityId();
/**
* Policy is only available in JsonSchemaVersion V_2.
*
* @return the supported JsonSchemaVersions of Policy.
*/
@Override
default JsonSchemaVersion[] getSupportedSchemaVersions() {
return new JsonSchemaVersion[]{JsonSchemaVersion.V_2};
}
/**
* Returns the namespace of this Policy.
*
* @return the namespace of this Policy.
*/
Optional<String> getNamespace();
/**
* Returns the current lifecycle of this Policy.
*
* @return the current lifecycle of this Policy.
*/
Optional<PolicyLifecycle> getLifecycle();
/**
* Indicates whether this Policy has the given lifecycle.
*
* @param lifecycle the lifecycle to be checked for.
* @return {@code true} if this Policy has {@code lifecycle} as its lifecycle, {@code false} else.
*/
default boolean hasLifecycle(@Nullable final PolicyLifecycle lifecycle) {
return getLifecycle()
.filter(actualLifecycle -> Objects.equals(actualLifecycle, lifecycle))
.isPresent();
}
/**
* Returns all available {@link Label}s of this Policy.
*
* @return all available labels.
*/
Set<Label> getLabels();
/**
* Indicates whether this Policy contains a PolicyEntry for the specified label.
*
* @param label the label to check if this Policy has a PolicyEntry for.
* @return {@code true} if this Policy contains a PolicyEntry for {@code label}, {@code false} else.
* @throws NullPointerException if {@code label} is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
boolean contains(CharSequence label);
/**
* Sets the specified entry to a copy of this Policy. A previous entry for the same label is replaced by
* the specified one.
*
* @param entry the entry to be set to this Policy.
* @return a copy of this Policy with {@code entry} set.
* @throws NullPointerException if {@code entry} is {@code null}.
*/
Policy setEntry(PolicyEntry entry);
/**
* Returns an Policy entry for the specified label.
*
* @param label the label to get the Policy entry for.
* @return the Policy entry.
* @throws NullPointerException if {@code label} is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Optional<PolicyEntry> getEntryFor(CharSequence label);
/**
* Removes the entry identified by the specified label from this Policy.
*
* @param label the nabel identifying the entry to be removed from this Policy.
* @return a copy of this Policy which does not contain the identified entry anymore.
* @throws NullPointerException if {@code entry} is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Policy removeEntry(CharSequence label);
/**
* Removes the specified entry from this Policy.
*
* @param entry the entry to be removed from this Policy.
* @return a copy of this Policy which does not contain the specified entry anymore.
* @throws NullPointerException if {@code entry} is {@code null}.
*/
Policy removeEntry(PolicyEntry entry);
/**
* Sets the given {@link Subjects} to the specified label. All previous entries with the same subject ID in the
* label are replaced by the specified ones.
*
* @param label the label identifying the PolicyEntry to modify.
* @param subjects the Subjects to set for the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the changed state.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Policy setSubjectsFor(CharSequence label, Subjects subjects);
/**
* Sets the given {@link Subject} to the specified label. A previous entry with the same subject ID in the label is
* replaced by the specified one.
*
* @param label the label identifying the PolicyEntry to modify.
* @param subject the Subject to set for the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the changed state.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Policy setSubjectFor(CharSequence label, Subject subject);
/**
* Removes the subject identified by the specified subject ID from this Policy.
*
* @param label the label identifying the PolicyEntry to modify.
* @param subjectId the Subject ID to remove from the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the removed subject.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Policy removeSubjectFor(CharSequence label, SubjectId subjectId);
/**
* Removes the specified subject from this Policy.
*
* @param label the label identifying the PolicyEntry to modify.
* @param subject the Subject to remove from the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the removed subject.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
default Policy removeSubjectFor(final CharSequence label, final Subject subject) {
checkNotNull(subject, "subject to be removed");
return removeSubjectFor(label, subject.getId());
}
/**
* Sets the given {@link Resources} to the specified label. All previous entries with the same resource key in the
* label are replaced by the specified ones.
*
* @param label the label identifying the PolicyEntry to modify.
* @param resources the Resources to set for the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the changed state.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Policy setResourcesFor(CharSequence label, Resources resources);
/**
* Sets the given {@link Resource} to the specified label. A previous entry with the same resource key in the label
* is replaced by the specified one.
*
* @param label the label identifying the PolicyEntry to modify.
* @param resource the Resource to set for the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the changed state.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Policy setResourceFor(CharSequence label, Resource resource);
/**
* Removes the resource identified by the specified resource path from this Policy.
*
* @param label the label identifying the PolicyEntry to modify.
* @param resourceType the type of the Resource to remove from the PolicyEntry identified by the {@code label}.
* @param resourcePath the path of the Resource to remove from the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the removed subject.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} or {@code resourceType} is empty.
*/
default Policy removeResourceFor(final CharSequence label, final String resourceType,
final CharSequence resourcePath) {
return removeResourceFor(label, PoliciesModelFactory.newResourceKey(resourceType, resourcePath));
}
/**
* Removes the resource identified by the specified resource key from this Policy.
*
* @param label the label identifying the PolicyEntry to modify.
* @param resourceKey the ResourceKey of the Resource to remove from the PolicyEntry identified by the
* {@code label}.
* @return a copy of this Policy with the removed subject.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Policy removeResourceFor(CharSequence label, ResourceKey resourceKey);
/**
* Removes the specified Resource from this Policy.
*
* @param label the label identifying the PolicyEntry to modify.
* @param resource the Resource to remove from the PolicyEntry identified by the {@code label}.
* @return a copy of this Policy with the removed subject.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
default Policy removeResourceFor(final CharSequence label, final Resource resource) {
return removeResourceFor(label, checkNotNull(resource, "resource").getResourceKey());
}
/**
* Set the given {@link EffectedPermissions} on the specified resource key in the specified label.
*
* @param label the label identifying the PolicyEntry to modify.
* @param resourceKey the ResourceKey to set the effected permissions on.
* @param effectedPermissions the EffectedPermissions to set on the resource in the label.
* @return a copy of this Policy with the changed state.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
default Policy setEffectedPermissionsFor(final CharSequence label, final ResourceKey resourceKey,
final EffectedPermissions effectedPermissions) {
return setResourceFor(label, PoliciesModelFactory.newResource(resourceKey, effectedPermissions));
}
/**
* Returns the {@link EffectedPermissions} for the specified subject ID on the passed resource type and resource
* path.
*
* @param label the label identifying the PolicyEntry to modify.
* @param subjectId the Subject ID to get the effected permissions for.
* @param resourceType the Resource type to get the effected permissions for.
* @param resourcePath the Resource path to get the effected permissions for.
* @return the effected permissions which are associated with {@code subject} on the passed {@code resourceType} and
* {@code resourcePath}. The returned set is mutable but disjoint from this Policy; thus modifying the set does not
* have an impact on this Policy.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} or {@code resourceType} is empty.
*/
default Optional<EffectedPermissions> getEffectedPermissionsFor(final CharSequence label,
final SubjectId subjectId,
final CharSequence resourceType,
final CharSequence resourcePath) {
return getEffectedPermissionsFor(label, subjectId,
PoliciesModelFactory.newResourceKey(resourceType, resourcePath));
}
/**
* Returns the {@link EffectedPermissions} for the specified subject ID on the passed resource key.
*
* @param label the label identifying the PolicyEntry to modify.
* @param subjectId the Subject ID to get the effected permissions for.
* @param resourceKey the ResourceKey to get the effected permissions for.
* @return the effected permissions which are associated with {@code subject} on the passed {@code resourceKey}. The
* returned set is mutable but disjoint from this Policy; thus modifying the set does not have an impact on this
* Policy.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code label} is empty.
*/
Optional<EffectedPermissions> getEffectedPermissionsFor(CharSequence label, SubjectId subjectId,
ResourceKey resourceKey);
/**
* Indicates whether this Policy is empty.
*
* @return {@code true} if this Policy does not contain any entry, {@code false} else.
*/
boolean isEmpty();
/**
* Returns the amount of entries this Policy has.
*
* @return this Policy's entries amount.
*/
int getSize();
/**
* Returns the entries of this Policy as set. The returned set is modifiable but disjoint from this Policy; thus
* modifying the entry set has no impact on this Policy.
*
* @return an unsorted set of this Policy's entries.
*/
Set<PolicyEntry> getEntriesSet();
/**
* Returns a sequential {@code Stream} with the entries of this Policy as its source.
*
* @return a sequential stream of the entries of this Policy.
*/
Stream<PolicyEntry> stream();
/**
* Checks if the passed {@code otherPolicyEntries} are semantically the same as the in this policy contained policy
* entries.
* I.e. that those contain the same policy entries with the same subject ids having the same resources.
*
* @param otherPolicyEntries the other policy entries to check against.
* @return {@code true} if the other policy entries are semantically the same as the in this policy contained policy
* entries.
* @since 3.0.0
*/
boolean isSemanticallySameAs(Collection<PolicyEntry> otherPolicyEntries);
/**
* Returns a JSON object representation of this policy to embed in another JSON object.
*
* @param schemaVersion the JsonSchemaVersion in which to return the JSON.
* @param predicate determines the content of the result.
* @return a JSON object representation of this policy to embed in another JSON object.
* @throws NullPointerException if {@code predicate} is {@code null}.
*/
default JsonObject toInlinedJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> predicate) {
return JsonFactory.newObjectBuilder()
.set(INLINED_FIELD_NAME, toJson(schemaVersion, predicate))
.build();
}
/**
* Returns a JSON object representation of this policy to embed in another JSON object.
*
* @param schemaVersion the JsonSchemaVersion in which to return the JSON.
* @param fieldSelector determines the content of the result.
* @return a JSON object representation of this policy to embed in another JSON object.
* @throws NullPointerException if {@code predicate} is {@code null}.
*/
default JsonObject toInlinedJson(final JsonSchemaVersion schemaVersion, final JsonFieldSelector fieldSelector) {
return JsonFactory.newObjectBuilder()
.set(INLINED_FIELD_NAME, toJson(schemaVersion, fieldSelector))
.build();
}
/**
* An enumeration of the known {@link JsonField}s of a Policy.
*/
@Immutable
final class JsonFields {
/**
* JSON field containing the {@link JsonSchemaVersion}.
*
* @deprecated as of 2.3.0 this field definition is not used anymore.
*/
@Deprecated
public static final JsonFieldDefinition<Integer> SCHEMA_VERSION = JsonFactory.newIntFieldDefinition(
JsonSchemaVersion.getJsonKey(),
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2
);
/**
* JSON field containing the Policy's lifecycle.
*/
public static final JsonFieldDefinition<String> LIFECYCLE = JsonFactory.newStringFieldDefinition("__lifecycle",
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2);
/**
* JSON field containing the Policy's namespace.
*/
public static final JsonFieldDefinition<String> NAMESPACE = JsonFactory.newStringFieldDefinition("_namespace",
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2);
/**
* JSON field containing the Policy's revision.
*/
public static final JsonFieldDefinition<Long> REVISION = JsonFactory.newLongFieldDefinition("_revision",
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2);
/**
* JSON field containing the Policy's modified timestamp in ISO-8601 format.
*/
public static final JsonFieldDefinition<String> MODIFIED = JsonFactory.newStringFieldDefinition("_modified",
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2);
/**
* JSON field containing the Policy's created timestamp in ISO-8601 format.
*
* @since 1.2.0
*/
public static final JsonFieldDefinition<String> CREATED = JsonFactory.newStringFieldDefinition("_created",
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2);
/**
* JSON field containing the Policy's ID.
*/
public static final JsonFieldDefinition<String> ID =
JsonFactory.newStringFieldDefinition("policyId", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the Policy's entries.
*/
public static final JsonFieldDefinition<JsonObject> ENTRIES =
JsonFactory.newJsonObjectFieldDefinition("entries", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the Policy's metadata.
*
* @since 2.0.0
*/
public static final JsonFieldDefinition<JsonObject> METADATA = JsonFactory.newJsonObjectFieldDefinition(
"_metadata",
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2
);
private JsonFields() {
throw new AssertionError();
}
}
}