-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingCommandEnforcement.java
553 lines (489 loc) · 25.9 KB
/
ThingCommandEnforcement.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright (c) 2022 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.things.service.enforcement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.contenttype.ContentType;
import org.eclipse.ditto.base.model.signals.FeatureToggle;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.base.model.signals.commands.CommandToExceptionRegistry;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.cacheloaders.AskWithRetry;
import org.eclipse.ditto.internal.utils.cacheloaders.config.AskWithRetryConfig;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonPointerInvalidException;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.api.Permission;
import org.eclipse.ditto.policies.enforcement.AbstractEnforcementReloaded;
import org.eclipse.ditto.policies.enforcement.EnforcementReloaded;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcer;
import org.eclipse.ditto.policies.enforcement.config.EnforcementConfig;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.PoliciesResourceType;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.ResourceKey;
import org.eclipse.ditto.policies.model.enforcers.Enforcer;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyNotAccessibleException;
import org.eclipse.ditto.policies.model.signals.commands.modify.DeletePolicy;
import org.eclipse.ditto.policies.model.signals.commands.modify.DeletePolicyResponse;
import org.eclipse.ditto.rql.model.ParserException;
import org.eclipse.ditto.rql.model.predicates.ast.RootNode;
import org.eclipse.ditto.rql.parser.RqlPredicateParser;
import org.eclipse.ditto.rql.query.things.FieldNamesPredicateVisitor;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingConstants;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandResponse;
import org.eclipse.ditto.things.model.signals.commands.exceptions.LiveChannelConditionNotAllowedException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingCommandToAccessExceptionRegistry;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingCommandToModifyExceptionRegistry;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingConditionFailedException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingConditionInvalidException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotModifiableException;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeature;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommand;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommandResponse;
import akka.Done;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
/**
* Authorizes {@link ThingCommand}s and filters {@link ThingCommandResponse}s.
*/
final class ThingCommandEnforcement
extends AbstractEnforcementReloaded<ThingCommand<?>, ThingCommandResponse<?>>
implements ThingEnforcementStrategy {
private static final Map<String, ThreadSafeDittoLogger> NAMESPACE_INSPECTION_LOGGERS = new HashMap<>();
/**
* Json fields that are always shown regardless of authorization.
*/
private static final JsonFieldSelector THING_QUERY_COMMAND_RESPONSE_ALLOWLIST =
JsonFactory.newFieldSelector(Thing.JsonFields.ID);
private final ActorSystem actorSystem;
private final ActorRef policiesShardRegion;
private final AskWithRetryConfig askWithRetryConfig;
/**
* Creates a new instance of the thing command enforcer.
*
* @param actorSystem the actor system to load config, dispatchers from.
* @param policiesShardRegion the policies shard region to load policies from and to use in order to create new
* (inline) policies when creating new things.
* @param enforcementConfig the configuration to apply for this command enforcement implementation.
*/
public ThingCommandEnforcement(
final ActorSystem actorSystem,
final ActorRef policiesShardRegion,
final EnforcementConfig enforcementConfig) {
this.actorSystem = actorSystem;
this.policiesShardRegion = policiesShardRegion;
this.askWithRetryConfig = enforcementConfig.getAskWithRetryConfig();
enforcementConfig.getSpecialLoggingInspectedNamespaces()
.forEach(loggedNamespace -> NAMESPACE_INSPECTION_LOGGERS.put(
loggedNamespace,
DittoLoggerFactory.getThreadSafeLogger(ThingCommandEnforcement.class.getName() +
".namespace." + loggedNamespace)));
}
@Override
public boolean isApplicable(final Signal<?> signal) {
return signal instanceof ThingCommand<?> && !Command.isLiveCommand(signal);
}
@Override
public boolean responseIsApplicable(final CommandResponse<?> commandResponse) {
return commandResponse instanceof ThingCommandResponse<?> && !CommandResponse.isLiveCommandResponse(
commandResponse);
}
@Override
@SuppressWarnings("unchecked")
public EnforcementReloaded<ThingCommand<?>, ThingCommandResponse<?>> getEnforcement() {
return this;
}
@Override
public CompletionStage<ThingCommand<?>> authorizeSignal(final ThingCommand<?> command,
final PolicyEnforcer policyEnforcer) {
if (command.getCategory() == Command.Category.QUERY && !command.getDittoHeaders().isResponseRequired()) {
// ignore query command with response-required=false
return CompletableFuture.completedStage(null);
}
final ThingCommand<?> authorizedCommand;
if (isWotTdRequestingThingQueryCommand(command)) {
// authorization is not necessary for WoT TD requesting thing query command, this is treated as public
// information:
FeatureToggle.checkWotIntegrationFeatureEnabled(command.getType(), command.getDittoHeaders());
// for retrieving the WoT TD, assume that full TD gets returned unfiltered:
authorizedCommand = prepareThingCommandBeforeSendingToPersistence(command);
} else {
try {
final ThingCommand<?> commandWithReadSubjects = authorizeByPolicyOrThrow(policyEnforcer.getEnforcer(),
command);
if (commandWithReadSubjects instanceof ThingQueryCommand<?> thingQueryCommand) {
authorizedCommand = prepareThingCommandBeforeSendingToPersistence(
ensureTwinChannel(thingQueryCommand)
);
} else if (commandWithReadSubjects.getDittoHeaders().getLiveChannelCondition().isPresent()) {
throw LiveChannelConditionNotAllowedException.newBuilder()
.dittoHeaders(commandWithReadSubjects.getDittoHeaders())
.build();
} else {
authorizedCommand = prepareThingCommandBeforeSendingToPersistence(commandWithReadSubjects);
}
} catch (final Throwable error) {
if (command instanceof CreateThing createThing && !Signal.isChannelLive(createThing)) {
return handleFailedCreateThing(createThing, policyEnforcer)
.thenCompose(done -> CompletableFuture.failedStage(error));
}
return CompletableFuture.failedStage(error);
}
}
return CompletableFuture.completedStage(authorizedCommand);
}
private static ThingQueryCommand<?> ensureTwinChannel(final ThingQueryCommand<?> command) {
if (Signal.isChannelLive(command)) {
return command.setDittoHeaders(command.getDittoHeaders()
.toBuilder()
.channel(Signal.CHANNEL_TWIN)
.build());
} else {
return command;
}
}
@Override
public CompletionStage<ThingCommand<?>> authorizeSignalWithMissingEnforcer(final ThingCommand<?> signal) {
throw ThingNotAccessibleException.newBuilder(signal.getEntityId())
.dittoHeaders(signal.getDittoHeaders())
.build();
}
@Override
public boolean shouldFilterCommandResponse(final ThingCommandResponse<?> commandResponse) {
return commandResponse instanceof ThingQueryCommandResponse<?>;
}
@Override
public CompletionStage<ThingCommandResponse<?>> filterResponse(final ThingCommandResponse<?> commandResponse,
final PolicyEnforcer policyEnforcer) {
if (commandResponse instanceof ThingQueryCommandResponse<?> thingQueryCommandResponse) {
try {
return CompletableFuture.completedStage(
buildJsonViewForThingQueryCommandResponse(thingQueryCommandResponse,
policyEnforcer.getEnforcer())
);
} catch (final RuntimeException e) {
throw reportError("Error after building JsonView", e, commandResponse.getDittoHeaders());
}
} else {
// no filtering required for non ThingQueryCommandResponse:
return CompletableFuture.completedStage(commandResponse);
}
}
private boolean isWotTdRequestingThingQueryCommand(final ThingCommand<?> thingCommand) {
// all listed thingCommand types here must handle "Accept: application/td+json", otherwise access control
// will be bypassed when using the header "Accept: application/td+json"!
return (thingCommand instanceof RetrieveThing || thingCommand instanceof RetrieveFeature) &&
thingCommand.getDittoHeaders().getAccept()
.filter(ContentType.APPLICATION_TD_JSON.getValue()::equals)
.isPresent();
}
/**
* Limit view on entity of {@code ThingQueryCommandResponse} by enforcer.
*
* @param response the response.
* @param enforcer the enforcer.
* @return response with view on entity restricted by enforcer.
*/
@SuppressWarnings("unchecked")
static <T extends ThingQueryCommandResponse<T>> T buildJsonViewForThingQueryCommandResponse(
final ThingQueryCommandResponse<T> response, final Enforcer enforcer) {
final JsonValue entity = response.getEntity();
if (entity.isObject()) {
final JsonObject filteredView =
getJsonViewForCommandResponse(entity.asObject(), response, enforcer);
return response.setEntity(filteredView);
} else {
return (T) response;
}
}
/**
* Prepares a passed in {@code authorizedThingCommand} before sending it to the persistence.
*
* @param authorizedThingCommand command to prepare.
* @return the passed in authorizedThingCommand.
*/
private ThingCommand<?> prepareThingCommandBeforeSendingToPersistence(
final ThingCommand<?> authorizedThingCommand) {
if (NAMESPACE_INSPECTION_LOGGERS.containsKey(authorizedThingCommand.getEntityId().getNamespace())) {
final ThreadSafeDittoLogger namespaceLogger = NAMESPACE_INSPECTION_LOGGERS
.get(authorizedThingCommand.getEntityId().getNamespace()).withCorrelationId(authorizedThingCommand);
if (authorizedThingCommand instanceof ThingModifyCommand) {
final JsonValue value = ((ThingModifyCommand<?>) authorizedThingCommand).getEntity().orElse(null);
if (null != value) {
final Set<ResourceKey> resourceKeys =
calculateLeaves(authorizedThingCommand.getResourcePath(), value);
namespaceLogger.info("Forwarding modify command type <{}> with resourceKeys <{}>",
authorizedThingCommand.getType(),
resourceKeys);
}
}
namespaceLogger
.debug("Forwarding command type <{}>: <{}>", authorizedThingCommand.getType(),
authorizedThingCommand);
}
return authorizedThingCommand;
}
/**
* Restrict view on a JSON object by enforcer.
*
* @param responseEntity the JSON object to restrict view on.
* @param response the command response containing the object.
* @param enforcer the enforcer.
* @return JSON object with view restricted by enforcer.
*/
static JsonObject getJsonViewForCommandResponse(final JsonObject responseEntity,
final CommandResponse<?> response, final Enforcer enforcer) {
final var resourceKey = ResourceKey.newInstance(ThingConstants.ENTITY_TYPE, response.getResourcePath());
final var authorizationContext = response.getDittoHeaders().getAuthorizationContext();
return enforcer.buildJsonView(resourceKey, responseEntity, authorizationContext,
THING_QUERY_COMMAND_RESPONSE_ALLOWLIST, Permissions.newInstance(Permission.READ));
}
/**
* Create error for commands to an existing thing whose policy is deleted.
*
* @param thingCommand the triggering command.
* @param thingId ID of the thing.
* @param policyId ID of the deleted policy.
* @return an appropriate error.
*/
private static DittoRuntimeException errorForExistingThingWithDeletedPolicy(final ThingCommand<?> thingCommand,
final ThingId thingId, final CharSequence policyId) {
//TODO: Yannic check when this exception should be thrown
final var message = String.format(
"The Thing with ID '%s' could not be accessed as its Policy with ID '%s' is not or no longer existing.",
thingId, policyId);
final var description = String.format(
"Recreate/create the Policy with ID '%s' in order to get access to the Thing again.",
policyId);
if (thingCommand instanceof ThingModifyCommand) {
return ThingNotModifiableException.newBuilder(thingId)
.message(message)
.description(description)
.dittoHeaders(thingCommand.getDittoHeaders())
.build();
} else {
return ThingNotAccessibleException.newBuilder(thingId)
.message(message)
.description(description)
.dittoHeaders(thingCommand.getDittoHeaders())
.build();
}
}
/**
* Create error due to failing to execute a thing-command in the expected way.
*
* @param thingCommand the command.
* @return the error.
*/
static DittoRuntimeException errorForThingCommand(final ThingCommand<?> thingCommand) {
final CommandToExceptionRegistry<ThingCommand<?>, DittoRuntimeException> registry =
thingCommand instanceof ThingModifyCommand
? ThingCommandToModifyExceptionRegistry.getInstance()
: ThingCommandToAccessExceptionRegistry.getInstance();
return registry.exceptionFrom(thingCommand);
}
/**
* Authorize a thing-command by a policy enforcer.
*
* @param <T> type of the thing-command.
* @param enforcer the enforcer.
* @param command the command to authorize.
* @return optionally the authorized command extended by read subjects.
*/
static <T extends ThingCommand<T>> T authorizeByPolicyOrThrow(final Enforcer enforcer,
final ThingCommand<T> command) {
final var thingResourceKey = PoliciesResourceType.thingResource(command.getResourcePath());
final var dittoHeaders = command.getDittoHeaders();
final var authorizationContext = dittoHeaders.getAuthorizationContext();
final boolean commandAuthorized;
if (command instanceof MergeThing mergeThing) {
commandAuthorized = enforceMergeThingCommand(enforcer, mergeThing, thingResourceKey, authorizationContext);
} else if (command instanceof ThingModifyCommand) {
commandAuthorized = enforcer.hasUnrestrictedPermissions(thingResourceKey, authorizationContext,
Permission.WRITE);
} else {
commandAuthorized =
enforcer.hasPartialPermissions(thingResourceKey, authorizationContext, Permission.READ);
}
final var condition = dittoHeaders.getCondition();
if (!(command instanceof CreateThing) && condition.isPresent()) {
enforceReadPermissionOnCondition(condition.get(), enforcer, dittoHeaders, () ->
ThingConditionFailedException.newBuilderForInsufficientPermission(dittoHeaders).build());
}
final var liveChannelCondition = dittoHeaders.getLiveChannelCondition();
if ((command instanceof ThingQueryCommand) && liveChannelCondition.isPresent()) {
enforceReadPermissionOnCondition(liveChannelCondition.get(), enforcer, dittoHeaders, () ->
ThingConditionFailedException.newBuilderForInsufficientLiveChannelPermission(dittoHeaders).build());
}
if (commandAuthorized) {
return addEffectedReadSubjectsToThingSignal(command, enforcer);
} else {
throw errorForThingCommand(command);
}
}
private CompletionStage<Done> handleFailedCreateThing(
final CreateThing createThing,
final PolicyEnforcer policyEnforcer) {
if (shouldDeletePolicy(createThing)) {
return deletePolicy(policyEnforcer.getPolicy().flatMap(Policy::getEntityId).orElseThrow(), createThing);
}
return CompletableFuture.completedStage(Done.getInstance());
}
private static boolean shouldDeletePolicy(final CreateThing createThing) {
return wasPolicyCopied(createThing)
|| wasInlinePolicyCreated(createThing)
|| wasDefaultPolicyCreated(createThing);
}
private static boolean wasPolicyCopied(final CreateThing createThing) {
return createThing.getPolicyIdOrPlaceholder().isPresent();
}
private static boolean wasInlinePolicyCreated(final CreateThing createThing) {
return createThing.getInitialPolicy().isPresent();
}
private static boolean wasDefaultPolicyCreated(final CreateThing createThing) {
return createThing.getThing().getPolicyId().isEmpty() && createThing.getPolicyIdOrPlaceholder().isEmpty();
}
private CompletionStage<Done> deletePolicy(final PolicyId policyId, final CreateThing createThing) {
final DittoHeaders dittoHeaders = createThing.getDittoHeaders();
final var dittoHeadersForCreatePolicy = DittoHeaders.newBuilder(dittoHeaders)
.removePreconditionHeaders()
.responseRequired(true)
.putHeader(DittoHeaderDefinition.DITTO_SUDO.getKey(), "true")
.build();
return doDeletePolicy(DeletePolicy.of(policyId, dittoHeadersForCreatePolicy));
}
private CompletionStage<Done> doDeletePolicy(final DeletePolicy deletePolicy) {
return AskWithRetry.askWithRetry(policiesShardRegion, deletePolicy, askWithRetryConfig, actorSystem,
this::handleDeletePolicyResponse)
.thenCompose(success -> {
if (!success) {
return doDeletePolicy(deletePolicy);
}
return CompletableFuture.completedStage(Done.getInstance());
});
}
private boolean handleDeletePolicyResponse(final Object policyResponse) {
//not accessible means already deleted and can be considered as success
return policyResponse instanceof DeletePolicyResponse || policyResponse instanceof PolicyNotAccessibleException;
}
/**
* Extend a signal by subject headers given with granted and revoked READ access.
* The subjects are provided by the given enforcer for the resource type {@link ThingConstants#ENTITY_TYPE}.
*
* @param signal the signal to extend.
* @param enforcer the enforcer.
* @return the extended signal.
*/
static <T extends Signal<T>> T addEffectedReadSubjectsToThingSignal(final Signal<T> signal,
final Enforcer enforcer) {
final var resourceKey = ResourceKey.newInstance(ThingConstants.ENTITY_TYPE, signal.getResourcePath());
final var authorizationSubjects = enforcer.getSubjectsWithUnrestrictedPermission(resourceKey, Permission.READ);
final var newHeaders = DittoHeaders.newBuilder(signal.getDittoHeaders())
.readGrantedSubjects(authorizationSubjects)
.build();
return signal.setDittoHeaders(newHeaders);
}
private static void enforceReadPermissionOnCondition(final String condition,
final Enforcer enforcer,
final DittoHeaders dittoHeaders,
final Supplier<DittoRuntimeException> exceptionSupplier) {
final var authorizationContext = dittoHeaders.getAuthorizationContext();
final var rootNode = tryParseRqlCondition(condition, dittoHeaders);
final var resourceKeys = determineResourceKeys(rootNode, dittoHeaders);
if (!enforcer.hasUnrestrictedPermissions(resourceKeys, authorizationContext, Permission.READ)) {
throw exceptionSupplier.get();
}
}
private static RootNode tryParseRqlCondition(final String condition, final DittoHeaders dittoHeaders) {
try {
return RqlPredicateParser.getInstance().parse(condition);
} catch (final ParserException e) {
throw ThingConditionInvalidException.newBuilder(condition, e.getMessage())
.dittoHeaders(dittoHeaders)
.build();
}
}
private static Set<ResourceKey> determineResourceKeys(final RootNode rootNode, final DittoHeaders dittoHeaders) {
final var visitor = FieldNamesPredicateVisitor.getNewInstance();
visitor.visit(rootNode);
final var extractedFieldNames = visitor.getFieldNames();
return extractedFieldNames.stream()
.map(fieldName -> tryGetResourceKey(fieldName, dittoHeaders))
.collect(Collectors.toSet());
}
private static ResourceKey tryGetResourceKey(final String fieldName, final DittoHeaders dittoHeaders) {
try {
return PoliciesResourceType.thingResource(fieldName);
} catch (final JsonPointerInvalidException e) {
throw ThingConditionInvalidException.newBuilder(fieldName, e.getDescription().orElse(""))
.dittoHeaders(dittoHeaders)
.build();
}
}
private static boolean enforceMergeThingCommand(final Enforcer enforcer,
final MergeThing command,
final ResourceKey thingResourceKey,
final AuthorizationContext authorizationContext) {
if (enforcer.hasUnrestrictedPermissions(thingResourceKey, authorizationContext, Permission.WRITE)) {
// unrestricted permissions at thingResourceKey level
return true;
} else if (enforcer.hasPartialPermissions(thingResourceKey, authorizationContext, Permission.WRITE)) {
// in case of partial permissions at thingResourceKey level check all leaves of merge patch for
// unrestricted permissions
final Set<ResourceKey> resourceKeys = calculateLeaves(command.getPath(), command.getValue());
return enforcer.hasUnrestrictedPermissions(resourceKeys, authorizationContext, Permission.WRITE);
} else {
// not even partial permission
return false;
}
}
private static Set<ResourceKey> calculateLeaves(final JsonPointer path, final JsonValue value) {
if (value.isObject()) {
return value.asObject().stream()
.map(f -> calculateLeaves(path.append(f.getKey().asPointer()), f.getValue()))
.reduce(new HashSet<>(), ThingCommandEnforcement::addAll, ThingCommandEnforcement::addAll);
} else {
return Set.of(PoliciesResourceType.thingResource(path));
}
}
private static Set<ResourceKey> addAll(final Set<ResourceKey> result, final Set<ResourceKey> toBeAdded) {
result.addAll(toBeAdded);
return result;
}
}