-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractEnforcerActor.java
416 lines (373 loc) · 19.5 KB
/
AbstractEnforcerActor.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
/*
* 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.internal.utils.persistentactors;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.api.commands.sudo.SudoCommand;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.namespaces.NamespaceReader;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.internal.utils.akka.actors.AbstractActorWithStashWithTimers;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.namespaces.BlockedNamespaces;
import org.eclipse.ditto.policies.api.PolicyTag;
import org.eclipse.ditto.policies.enforcement.EnforcementReloaded;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcer;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.enforcers.PolicyEnforcers;
import akka.actor.ActorRef;
import akka.cluster.ddata.ORSet;
import akka.cluster.ddata.Replicator;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.japi.pf.ReceiveBuilder;
/**
* Abstract enforcer of commands performing authorization / enforcement of incoming signals
* targeting to be handled by either the {@link AbstractPersistenceActor} of incoming live signals to be published to
* pub/sub.
*
* @param <I> the type of the EntityId this enforcer actor enforces commands for.
* @param <C> the type of the Commands this enforcer actor enforces.
* @param <R> the type of the CommandResponses this enforcer actor filters.
* @param <E> the type of the EnforcementReloaded this enforcer actor uses for doing command enforcements.
*/
public abstract class AbstractEnforcerActor<I extends EntityId, C extends Command<?>, R extends CommandResponse<?>,
E extends EnforcementReloaded<C, R>>
extends AbstractActorWithStashWithTimers {
/**
* Timeout for local actor invocations - a small timeout should be more than sufficient as those are just method
* calls.
*/
protected static final Duration DEFAULT_LOCAL_ASK_TIMEOUT = Duration.ofSeconds(5);
private static final Duration CACHED_NAMESPACES_INVALIDATION_DELAY = Duration.ofSeconds(5);
protected final DittoDiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
protected final I entityId;
protected final E enforcement;
@Nullable protected PolicyId policyIdForEnforcement;
@Nullable protected PolicyEnforcer policyEnforcer;
protected AbstractEnforcerActor(final I entityId, final E enforcement, final ActorRef pubSubMediator,
@Nullable final BlockedNamespaces blockedNamespaces) {
this.entityId = entityId;
this.enforcement = enforcement;
enforcement.registerPolicyEnforcerLoader(this::providePolicyEnforcer);
enforcement.registerPolicyInjectionConsumer(this::injectPolicy);
// subscribe for PolicyTags in order to reload policyEnforcer when "backing policy" was modified
pubSubMediator.tell(DistPubSubAccess.subscribe(PolicyTag.PUB_SUB_TOPIC_INVALIDATE_ENFORCERS, getSelf()),
getSelf());
if (null != blockedNamespaces) {
blockedNamespaces.subscribeForChanges(getSelf());
}
}
/**
* Provides the {@link PolicyId} to use for the policy enforcement.
* The implementation chooses the most efficient strategy to retrieve it.
*
* @return a successful CompletionStage of either the loaded {@link PolicyId} of the Policy which should be used
* for enforcement or a failed CompletionStage with the cause for the failure.
*/
protected abstract CompletionStage<PolicyId> providePolicyIdForEnforcement();
/**
* Provides the {@link PolicyEnforcer} instance (which holds a {@code Policy} + the built {@code Enforcer}) for the
* provided {@code policyId} asynchronously.
* The implementation chooses the most efficient strategy to retrieve it.
*
* @param policyId the {@link PolicyId} to retrieve the PolicyEnforcer for.
* @return a successful CompletionStage of either the loaded {@link PolicyEnforcer} or a failed CompletionStage with
* the cause for the failure.
*/
protected abstract CompletionStage<PolicyEnforcer> providePolicyEnforcer(@Nullable PolicyId policyId);
/**
* Accepts the passed in {@code injectedPolicy} in order to e.g. update the {@code policyEnforcer} this enforcer
* actor uses for doing policy enforcement.
*
* @param injectedPolicy the Policy to inject.
*/
protected void injectPolicy(final Policy injectedPolicy) {
this.policyEnforcer = PolicyEnforcer.of(injectedPolicy, PolicyEnforcers.defaultEvaluator(injectedPolicy));
}
/**
* Determines whether for the passed in {@code command} the {@code policyEnforcer} of this enforcer actor should be
* invalidated **after** doing the enforcement (because it changed/could have changed) the authorization logic.
*
* @param command the command to check.
* @return {@code true} if the Policy Enforcer should be invalidated after the enforcement.
*/
protected abstract boolean shouldInvalidatePolicyEnforcerAfterEnforcement(C command);
@Override
public void preStart() throws Exception {
super.preStart();
final ActorRef self = getSelf();
reloadPolicyEnforcer(loadedPolicyEnforcer -> {
this.policyEnforcer = loadedPolicyEnforcer;
self.tell(Control.INIT_DONE, self);
});
}
@SuppressWarnings("unchecked")
protected Receive activeBehaviour() {
return ReceiveBuilder.create()
.match(DistributedPubSubMediator.SubscribeAck.class, s -> log.debug("Got subscribeAck <{}>.", s))
.match(PolicyTag.class, pt -> pt.getEntityId().equals(policyIdForEnforcement),
pt -> performPolicyEnforcerReload()
)
.match(PolicyTag.class, pt -> {
// ignore policy tags not intended for this actor - not necessary to log on debug!
})
.match(SudoCommand.class, sudoCommand -> log.withCorrelationId(sudoCommand)
.error("Received SudoCommand in enforcer which should never happen: <{}>", sudoCommand)
)
.match(Command.class, c -> enforceCommand((C) c))
.match(CommandResponse.class, r -> filterResponse((R) r))
.match(Replicator.Changed.class, this::handleChanged)
.match(InvalidateCachedNamespaces.class, this::invalidateCachedNamespaces)
.matchAny(message ->
log.withCorrelationId(
message instanceof WithDittoHeaders withDittoHeaders ? withDittoHeaders : null)
.warning("Got unknown message: '{}'", message))
.build();
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(DistributedPubSubMediator.SubscribeAck.class, s -> log.debug("Got subscribeAck <{}>.", s))
.matchEquals(Control.INIT_DONE, initDone -> {
unstashAll();
becomeActive();
})
.matchAny(this::handleMessagesDuringStartup)
.build();
}
private void reloadPolicyEnforcer(final Consumer<PolicyEnforcer> successConsumer) {
providePolicyIdForEnforcement()
.thenCompose(policyId -> {
this.policyIdForEnforcement = policyId; // policyId could be null, e.g. if entity is not yet existing
return providePolicyEnforcer(policyId);
})
.whenComplete((pEnf, throwable) -> {
if (null != throwable) {
policyEnforcer = null;
log.error(throwable, "Failed to load policy enforcer; stopping myself..");
getContext().stop(getSelf());
} else if (null != pEnf) {
policyEnforcer = pEnf;
successConsumer.accept(pEnf);
} else {
policyEnforcer = null;
successConsumer.accept(null);
}
});
}
private void becomeAwaitingInitialization() {
getContext().become(createReceive());
}
private void becomeActive() {
getContext().become(activeBehaviour());
}
private void handleMessagesDuringStartup(final Object message) {
stash();
log.withCorrelationId(message instanceof WithDittoHeaders withDittoHeaders ? withDittoHeaders : null)
.debug("Stashed received message during startup of enforcer actor: <{}>",
message.getClass().getSimpleName());
}
private void performPolicyEnforcerReload() {
final ActorRef self = getSelf();
reloadPolicyEnforcer(loadedPolicyEnforcer -> {
this.policyEnforcer = loadedPolicyEnforcer;
self.tell(Control.INIT_DONE, self);
});
becomeAwaitingInitialization();
}
@SuppressWarnings("unchecked")
private void handleChanged(final Replicator.Changed<?> changed) {
if (changed.dataValue() instanceof ORSet) {
final ORSet<String> namespaces = (ORSet<String>) changed.dataValue();
logNamespaces("Received", namespaces);
invalidateNamespacesAfterDelay(namespaces);
} else {
log.warning("Unhandled: <{}>", changed);
}
}
private void logNamespaces(final String verb, final ORSet<String> namespaces) {
if (namespaces.size() > 25) {
log.info("{} <{}> namespaces", verb, namespaces.size());
} else {
log.info("{} namespaces: <{}>", verb, namespaces);
}
}
private void invalidateNamespacesAfterDelay(final ORSet<String> namespaces) {
getTimers().startSingleTimer(namespaces, new InvalidateCachedNamespaces(namespaces),
CACHED_NAMESPACES_INVALIDATION_DELAY);
}
private void invalidateCachedNamespaces(final InvalidateCachedNamespaces invalidate) {
final ORSet<String> namespaces = invalidate.namespaces;
logNamespaces("Invalidating", namespaces);
if (!namespaces.isEmpty() && null != policyIdForEnforcement &&
containsNamespaceOfEntityId(namespaces, policyIdForEnforcement)) {
log.info("Reloading policy enforcer because namespace was added to blocked namespaces.");
performPolicyEnforcerReload();
}
}
private static boolean containsNamespaceOfEntityId(final ORSet<String> namespaces,
final EntityId entityId) {
return NamespaceReader.fromEntityId(entityId)
.map(namespaces::contains)
.orElse(false);
}
/**
* Enforces the passed {@code command} using the {@code enforcement} of this actor.
* Successfully enforced commands are sent back to the {@code getSender()} - which is our dear parent, the Supervisor.
* Our parent is responsible for then forwarding the command to the persistence actor.
*
* @param command the {@code Command} to enforce based in the {@code policyEnforcer}.
*/
private void enforceCommand(final C command) {
if (command.getCategory() == Command.Category.QUERY && !command.getDittoHeaders().isResponseRequired()) {
// ignore query command with response-required=false
return;
}
doEnforceCommand(command, getSender())
.thenAccept(successfullyEnforced -> {
if (shouldReloadAfterEnforcement(command, successfullyEnforced)) {
// trigger reloading the policy
performPolicyEnforcerReload();
}
})
.toCompletableFuture()
.join(); // block on the actor's dispatcher in order to guarantee in-order processing and blocking the inbox
}
private boolean shouldReloadAfterEnforcement(final C command, final boolean successfullyEnforced) {
return successfullyEnforced &&
(shouldInvalidatePolicyEnforcerAfterEnforcement(command) || (null == policyEnforcer));
}
private CompletionStage<Boolean> doEnforceCommand(final C command, final ActorRef sender) {
final ActorRef self = getSelf();
try {
final CompletionStage<C> authorizedCommandStage;
if (null != policyEnforcer) {
authorizedCommandStage = enforcement.authorizeSignal(command, policyEnforcer);
} else {
authorizedCommandStage = enforcement.authorizeSignalWithMissingEnforcer(command);
}
return authorizedCommandStage.handle((authorizedCommand, throwable) -> {
final boolean successfullyEnforced;
if (null != authorizedCommand) {
log.withCorrelationId(authorizedCommand)
.info("Completed enforcement of message type <{}> with outcome 'success'",
authorizedCommand.getType());
sender.tell(authorizedCommand, self);
successfullyEnforced = true;
} else if (null != throwable) {
final DittoRuntimeException dittoRuntimeException =
DittoRuntimeException.asDittoRuntimeException(throwable, t ->
DittoInternalErrorException.newBuilder()
.cause(t)
.dittoHeaders(command.getDittoHeaders())
.build()
);
log.withCorrelationId(dittoRuntimeException)
.info("Completed enforcement of message type <{}> with outcome 'failed' and headers: <{}>",
command.getType(), command.getDittoHeaders());
sender.tell(dittoRuntimeException, self);
successfullyEnforced = false;
} else {
log.withCorrelationId(command)
.error("Neither authorizedCommand nor throwable were present during enforcement of command: " +
"<{}>", command);
successfullyEnforced = false;
}
return successfullyEnforced;
});
} catch (final DittoRuntimeException dittoRuntimeException) {
log.withCorrelationId(dittoRuntimeException)
.info("Completed enforcement of message type <{}> with outcome 'failed' and headers: <{}>",
command.getType(), command.getDittoHeaders());
sender.tell(dittoRuntimeException, self);
return CompletableFuture.completedStage(false);
}
}
/**
* Filters the response payload of the passed {@code commandResponse} using the {@code enforcement} of this actor.
* Filtered command responses are sent back to the {@code getSender()} - which is our dear parent, the Supervisor.
* Our parent is responsible for then forwarding the command response to the original sender.
*
* @param commandResponse the {@code CommandResponse} to filter based in the {@code policyEnforcer}.
*/
private void filterResponse(final R commandResponse) {
final ActorRef sender = getSender();
final ActorRef parent = getContext().getParent();
if (enforcement.shouldFilterCommandResponse(commandResponse)) {
if (null != policyEnforcer) {
try {
final CompletionStage<R> filteredResponseStage =
enforcement.filterResponse(commandResponse, policyEnforcer);
filteredResponseStage.whenComplete((filteredResponse, throwable) -> {
if (null != filteredResponse) {
log.withCorrelationId(filteredResponse)
.info("Completed filtering of command response type <{}>", filteredResponse.getType());
sender.tell(filteredResponse, parent);
} else if (null != throwable) {
final DittoRuntimeException dittoRuntimeException =
DittoRuntimeException.asDittoRuntimeException(throwable, t ->
DittoInternalErrorException.newBuilder()
.cause(t)
.dittoHeaders(commandResponse.getDittoHeaders())
.build()
);
log.withCorrelationId(dittoRuntimeException)
.info("Exception during filtering of command response type <{}> and headers: <{}>",
commandResponse.getType(), commandResponse.getDittoHeaders());
sender.tell(dittoRuntimeException, parent);
} else {
log.withCorrelationId(commandResponse)
.error("Neither filteredResponse nor throwable were present during filtering of " +
"commandResponse: <{}>", commandResponse);
}
});
} catch (final DittoRuntimeException dittoRuntimeException) {
log.withCorrelationId(dittoRuntimeException)
.info("Exception during filtering of command response type <{}> and headers: <{}>",
commandResponse.getType(), commandResponse.getDittoHeaders());
sender.tell(dittoRuntimeException, parent);
}
} else {
log.withCorrelationId(commandResponse)
.error("Could not filter command response because policyEnforcer was missing");
}
} else {
sender.tell(commandResponse, parent);
}
}
/**
* Control message for the enforcer actor.
*/
public enum Control {
/**
* Initialization is done, enforcement can be performed.
*/
INIT_DONE
}
private static final class InvalidateCachedNamespaces {
final ORSet<String> namespaces;
private InvalidateCachedNamespaces(final ORSet<String> namespaces) {
this.namespaces = namespaces;
}
}
}