-
Notifications
You must be signed in to change notification settings - Fork 215
/
Enforcer.java
executable file
·316 lines (290 loc) · 17.2 KB
/
Enforcer.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
/*
* 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.enforcers;
import java.util.Set;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.ResourceKey;
/**
* Abstraction for algorithms enforcing {@link org.eclipse.ditto.policies.model.Permissions} on a {@code resource} for a given
* {@link org.eclipse.ditto.base.model.auth.AuthorizationContext}.
*/
public interface Enforcer {
/**
* Checks whether for the {@code authorizationContext} either implicitly or explicitly
* has "GRANT" for the {@code permissions} on the specified {@code resourceKey} considering "REVOKE"s down in the
* hierarchy, so if there is a REVOKE for the {@code authorizationContext} somewhere down the hierarchy of the
* {@code resourceKey}, the result will be {@code false}.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to check the permission(s) for.
* @param authorizationContext the authorization context to check.
* @param permission the permission to check.
* @param furtherPermissions further permissions to check.
* @return {@code true} if {@code authorizationContext} has the given permission(s), {@code false} otherwise.
* @throws NullPointerException if any argument is {@code null}.
*/
default boolean hasUnrestrictedPermissions(final ResourceKey resourceKey,
final AuthorizationContext authorizationContext,
final String permission,
final String... furtherPermissions) {
return hasUnrestrictedPermissions(resourceKey, authorizationContext,
Permissions.newInstance(permission, furtherPermissions));
}
/**
* Checks whether for the {@code authorizationContext} either implicitly or explicitly
* has "GRANT" for the {@code permissions} on the specified set of {@code resourceKeys} considering "REVOKE"s
* down in the hierarchy, so if there is a REVOKE for the {@code authorizationContext} somewhere down the hierarchy
* for any of the {@code resourceKeys}, the result will be {@code false}.
*
* @param resourceKeys the ResourceKeys (containing Resource type and path) to check the permission(s) for.
* @param authorizationContext the authorization context to check.
* @param permission the permission to check.
* @param furtherPermissions further permissions to check.
* @return {@code true} if {@code authorizationContext} has the given permission(s), {@code false} otherwise.
* @throws NullPointerException if any argument is {@code null}.
*/
default boolean hasUnrestrictedPermissions(final Set<ResourceKey> resourceKeys,
final AuthorizationContext authorizationContext,
final String permission,
final String... furtherPermissions) {
return resourceKeys.stream()
.allMatch(resourceKey -> hasUnrestrictedPermissions(resourceKey, authorizationContext,
Permissions.newInstance(permission, furtherPermissions)));
}
/**
* Checks whether the {@code authorizationContext} either implicitly or explicitly
* has "GRANT" for the {@code permissions} on the specified {@code resourceKey} considering "REVOKE"s down in the
* hierarchy, so if there is a REVOKE for the {@code authorizationContext} somewhere down the hierarchy of the
* {@code resourceKey}, the result will be {@code false}.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to check the permission(s) for.
* @param authorizationContext the authorization context to check.
* @param permissions the permissions to check.
* @return {@code true} if {@code authorizationContext} has the given permissions, {@code false} otherwise.
* @throws NullPointerException if any argument is {@code null}.
*/
boolean hasUnrestrictedPermissions(ResourceKey resourceKey, AuthorizationContext authorizationContext,
Permissions permissions);
/**
* Returns a set of authorization subjects each of which has all the given permissions granted on exactly the given
* resource, and a set of authorization subjects each of which has 1 or more given permissions revoked on the given
* resource.
* Does not consider "REVOKE"s down in the hierarchy.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to check the permission(s) for.
* @param permission the permission to check.
* @param furtherPermissions further permissions to check.
* @return the effected subjects.
* @throws NullPointerException if any argument is {@code null}.
* @since 1.1.0
*/
default EffectedSubjects getSubjectsWithPermission(final ResourceKey resourceKey, final String permission,
final String... furtherPermissions) {
return getSubjectsWithPermission(resourceKey, Permissions.newInstance(permission, furtherPermissions));
}
/**
* Returns a set of authorization subjects each of which has all the given permissions granted on exactly the given
* resource, and a set of authorization subjects each of which has 1 or more given permissions revoked on the given
* resource.
* Does not consider "REVOKE"s down in the hierarchy.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to check the permission(s) for.
* @param permissions the permissions to check
* @return the effected subjects.
* @throws NullPointerException if any argument is {@code null}.
* @since 1.1.0
*/
EffectedSubjects getSubjectsWithPermission(ResourceKey resourceKey, Permissions permissions);
/**
* Returns a set of authorization subjects each of which has all the given permissions granted on the given resource
* or on any sub resource down in the hierarchy.
* Revoked permissions are not taken into account.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to use as starting point to check the
* partial permission(s) in the hierarchy for.
* @param permissions the permissions to be checked.
* @return the authorization subjects with partial permissions on the passed resourceKey or any other resources in
* the hierarchy below.
* @throws NullPointerException if any argument is {@code null}.
* @since 1.1.0
*/
Set<AuthorizationSubject> getSubjectsWithPartialPermission(ResourceKey resourceKey, Permissions permissions);
/**
* Checks whether the {@code authorizationContext} either implicitly or explicitly
* has "GRANT" for the specified permissions on the passed in {@code resourceKey} or on any {@code resource} down in
* the hierarchy of the {@code resourceKey}.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to check the permission(s) for.
* @param authorizationContext the authorization context to check.
* @param permission the permission to check.
* @param furtherPermissions further permissions to check.
* @return {@code true} if {@code authorizationContext} has the given permission(s) somewhere down in the hierarchy,
* {@code false} otherwise.
* @throws NullPointerException if any argument is {@code null}.
*/
default boolean hasPartialPermissions(final ResourceKey resourceKey,
final AuthorizationContext authorizationContext,
final String permission,
final String... furtherPermissions) {
return hasPartialPermissions(resourceKey, authorizationContext,
Permissions.newInstance(permission, furtherPermissions));
}
/**
* Checks whether the {@code authorizationContext} either implicitly or explicitly
* has "GRANT" for the specified permissions on the passed in {@code resourceKey} or on any {@code resource} down in
* the hierarchy of the {@code resourceKey}.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to check the permission(s) for and use for
* looking downwards the hierarchy.
* @param authorizationContext the authorization context to check.
* @param permissions the permission to check.
* @return {@code true} if {@code authorizationContext} has the given permissions somewhere down in the hierarchy,
* {@code false} otherwise.
* @throws NullPointerException if any argument is {@code null}.
*/
boolean hasPartialPermissions(ResourceKey resourceKey, AuthorizationContext authorizationContext,
Permissions permissions);
/**
* Returns a set of authorization subjects each of which has all the given permissions granted on the given resource
* or on any sub resource down in the hierarchy.
* Revoked permissions are taken into account.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to use as starting point to check the
* partial permission(s) in the hierarchy for.
* @param permission the permission to check.
* @param furtherPermissions further permissions to check.
* @return the authorization subjects with unrestricted permissions on the passed resourceKey or any other
* resources in the hierarchy below.
* @throws NullPointerException if any argument is {@code null}.
* @since 2.2.0
*/
default Set<AuthorizationSubject> getSubjectsWithUnrestrictedPermission(ResourceKey resourceKey,
final String permission, final String... furtherPermissions) {
return getSubjectsWithUnrestrictedPermission(resourceKey,
Permissions.newInstance(permission, furtherPermissions));
}
/**
* Returns a set of authorization subjects each of which has all the given permissions granted on the given resource
* or on any sub resource down in the hierarchy.
* Revoked permissions are taken into account.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to use as starting point to check the
* partial permission(s) in the hierarchy for.
* @param permissions the permissions to be checked.
* @return the authorization subjects with unrestricted permissions on the passed resourceKey or any other
* resources in the hierarchy below.
* @throws NullPointerException if any argument is {@code null}.
* @since 2.2.0
*/
Set<AuthorizationSubject> getSubjectsWithUnrestrictedPermission(ResourceKey resourceKey, Permissions permissions);
/**
* Builds a view of the passed {@code jsonFields} (e.g. a {@link org.eclipse.ditto.json.JsonObject} or a {@link
* org.eclipse.ditto.json.JsonObjectBuilder}) {@code authorizationContext} and {@code permissions}. The resulting
* {@code JsonObject} only contains {@code JsonFields} for which the {@code authorizationContext} has the required
* permissions.
*
* @param jsonFields the full JsonFields from which to build the view based on the permissions.
* @param resourceType the type of the Resource
* @param authorizationContext the AuthorizationContext containing the AuthorizationSubjects.
* @param permission the permission.
* @param furtherPermissions further permissions.
* @return a view of the passed {@code jsonFields} as JsonObject for which the required permissions are given.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if {@code resourceType} is empty.
*/
default JsonObject buildJsonView(final Iterable<JsonField> jsonFields,
final CharSequence resourceType,
final AuthorizationContext authorizationContext,
final String permission,
final String... furtherPermissions) {
return buildJsonView(ResourceKey.newInstance(resourceType, JsonFactory.emptyPointer()), jsonFields,
authorizationContext, Permissions.newInstance(permission, furtherPermissions));
}
/**
* Builds a view of the passed {@code jsonFields} (e.g. a {@link org.eclipse.ditto.json.JsonObject} or a {@link
* org.eclipse.ditto.json.JsonObjectBuilder}) {@code authorizationContext} and {@code permissions}. The resulting
* {@code JsonObject} only contains {@code JsonFields} for which the {@code authorizationContext} has the required
* permissions.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to start from.
* @param jsonFields the full JsonFields from which to build the view based on the permissions.
* @param authorizationContext the AuthorizationContext containing the AuthorizationSubjects.
* @param permission the permission.
* @param furtherPermissions further permissions.
* @return a view of the passed {@code jsonFields} as JsonObject for which the required permissions are given.
* @throws NullPointerException if any argument is {@code null}.
*/
default JsonObject buildJsonView(final ResourceKey resourceKey,
final Iterable<JsonField> jsonFields,
final AuthorizationContext authorizationContext,
final String permission,
final String... furtherPermissions) {
return buildJsonView(resourceKey, jsonFields, authorizationContext,
Permissions.newInstance(permission, furtherPermissions));
}
/**
* Builds a view of the passed {@code jsonFields} (e.g. a {@link org.eclipse.ditto.json.JsonObject} or a {@link
* org.eclipse.ditto.json.JsonObjectBuilder}) for {@code authorizationContext} and {@code permissions} with some
* fields allowed. The resulting {@code JsonObject} only contains those {@code JsonFields} for which the {@code
* authorizationContext} has the required permissions or those that are present in the allowlist. Fields in the
* allow list are not present in the output if the authorization subjects are not granted any rights at all.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to start from.
* @param jsonFields the full JsonFields from which to build the view based on the permissions.
* @param authorizationContext the AuthorizationContext containing the AuthorizationSubjects.
* @param permissions the permissions.
* @param allowlist allowed fields to be present in the output as long as the authorization subjects are
* relevant, i.e., some of them are granted the required permissions on some resource.
* @return a view of the passed {@code jsonFields} as JsonObject for which the required permissions are given.
* @throws NullPointerException if any argument is {@code null}.
*/
default JsonObject buildJsonView(final ResourceKey resourceKey,
final Iterable<JsonField> jsonFields,
final AuthorizationContext authorizationContext,
final JsonFieldSelector allowlist,
final Permissions permissions) {
final JsonObject enforcedJsonView =
buildJsonView(resourceKey, jsonFields, authorizationContext, permissions);
final ResourceKey rootResourceKey = ResourceKey.newInstance(resourceKey.getResourceType(),
JsonFactory.emptyPointer());
final boolean isAuthorizationSubjectRelevant =
hasPartialPermissions(rootResourceKey, authorizationContext, permissions);
if (isAuthorizationSubjectRelevant) {
final JsonObject inputJsonObject = JsonFactory.newObject(jsonFields);
final JsonObject allowedJsonView = inputJsonObject.get(allowlist);
return JsonFactory.newObject(enforcedJsonView, allowedJsonView);
} else {
return enforcedJsonView;
}
}
/**
* Builds a view of the passed {@code jsonFields} (e.g. a {@link org.eclipse.ditto.json.JsonObject} or a {@link
* org.eclipse.ditto.json.JsonObjectBuilder}) {@code authorizationContext} and {@code permissions}. The resulting
* {@code JsonObject} only contains {@code JsonFields} for which the {@code authorizationContext} has the required
* permissions.
*
* @param resourceKey the ResourceKey (containing Resource type and path) to start from for building the view
* @param jsonFields the full JsonFields from which to build the view based on the permissions.
* @param authorizationContext the AuthorizationContext containing the AuthorizationSubjects.
* @param permissions the permissions.
* @return a view of the passed {@code jsonFields} as JsonObject for which the required permissions are given.
* @throws NullPointerException if any argument is {@code null}.
*/
JsonObject buildJsonView(ResourceKey resourceKey, Iterable<JsonField> jsonFields,
AuthorizationContext authorizationContext, Permissions permissions);
}