/
AbstractEnforcement.java
343 lines (299 loc) · 12.6 KB
/
AbstractEnforcement.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.concierge.enforcement;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Set;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.BiConsumer;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.WithDittoHeaders;
import org.eclipse.ditto.model.enforcers.Enforcer;
import org.eclipse.ditto.model.policies.ResourceKey;
import org.eclipse.ditto.services.models.concierge.EntityId;
import org.eclipse.ditto.services.models.policies.Permission;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.services.utils.akka.controlflow.Consume;
import org.eclipse.ditto.services.utils.akka.controlflow.WithSender;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayInternalErrorException;
import org.eclipse.ditto.signals.commands.things.ThingCommand;
import akka.NotUsed;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.event.DiagnosticLoggingAdapter;
import akka.pattern.AskTimeoutException;
import akka.stream.Graph;
import akka.stream.SinkShape;
/**
* Contains self-type requirements for aspects of enforcer actor dealing with specific commands.
* Implementations only need to implement {@link #enforce(Signal, ActorRef, DiagnosticLoggingAdapter)} in which they
* check if the passed in {@link Signal} is authorized and forward it accordingly or respond with an error to the passed
* in {@code sender}.
* <p>
* Do NOT call the methods outside this package.
* </p>
*/
public abstract class AbstractEnforcement<T extends Signal> {
private final Context context;
protected AbstractEnforcement(final Context context) {
this.context = context;
}
/**
* Performs authorization enforcement for the passed {@code signal}.
* If the signal is authorized, the implementation chooses to which target to forward. If it is not authorized, the
* passed {@code sender} will get an authorization error response.
*
* @param signal the signal to authorize.
* @param sender sender of the signal.
* @param log the logger to use for logging.
* @return future after enforcement was performed.
*/
public abstract CompletionStage<Void> enforce(T signal, ActorRef sender, DiagnosticLoggingAdapter log);
Graph<SinkShape<WithSender<T>>, NotUsed> toGraph() {
return Consume.of((signal, sender) ->
enforce(signal, sender, context.log).whenComplete(handleEnforcementCompletion(signal, sender)));
}
private BiConsumer<Void, Throwable> handleEnforcementCompletion(final T signal, final ActorRef sender) {
return (_void, throwable) -> {
if (throwable != null) {
final Throwable error = throwable instanceof CompletionException
? throwable.getCause()
: throwable;
reportError("Error thrown during enforcement", sender, error, signal.getDittoHeaders());
}
};
}
/**
* Reply a message to sender.
*
* @param message message to forward.
* @param sender whom to reply to.
* @return true.
*/
protected boolean replyToSender(final Object message, final ActorRef sender) {
sender.tell(message, self());
return true;
}
/**
* Report unexpected error or unknown response.
*/
protected void reportUnexpectedErrorOrResponse(final String hint,
final ActorRef sender,
final Object response,
final Throwable error,
final DittoHeaders dittoHeaders) {
if (error != null) {
reportUnexpectedError(hint, sender, error, dittoHeaders);
} else {
reportUnknownResponse(hint, sender, response, dittoHeaders);
}
}
/**
* Reports an error differently based on type of the error. If the error is of type
* {@link org.eclipse.ditto.model.base.exceptions.DittoRuntimeException}, it is send to the {@code sender}
* without modification, otherwise it is wrapped inside a {@link GatewayInternalErrorException}.
*/
protected void reportError(final String hint, final ActorRef sender, final Throwable error,
final DittoHeaders dittoHeaders) {
if (error instanceof DittoRuntimeException) {
log(dittoHeaders).error(error, hint);
sender.tell(error, self());
} else {
reportUnexpectedError(hint, sender, error, dittoHeaders);
}
}
/**
* Report unexpected error.
*/
protected void reportUnexpectedError(final String hint, final ActorRef sender, final Throwable error,
final DittoHeaders dittoHeaders) {
log(dittoHeaders).error(error, "Unexpected error {}", hint);
sender.tell(mapToExternalException(error, dittoHeaders), self());
}
/**
* Report unknown response.
*/
protected void reportUnknownResponse(final String hint, final ActorRef sender, final Object response,
final DittoHeaders dittoHeaders) {
log(dittoHeaders).error("Unexpected response {}: <{}>", hint, response);
sender.tell(GatewayInternalErrorException.newBuilder().dittoHeaders(dittoHeaders).build(), self());
}
private DittoRuntimeException mapToExternalException(final Throwable error,
final DittoHeaders dittoHeaders) {
if (error instanceof GatewayInternalErrorException) {
return (GatewayInternalErrorException) error;
} else {
log(dittoHeaders).error(error, "Unexpected non-DittoRuntimeException error - responding with " +
"GatewayInternalErrorException: {} {}", error.getClass().getSimpleName(), error.getMessage());
return GatewayInternalErrorException.newBuilder()
.cause(error)
.dittoHeaders(dittoHeaders)
.build();
}
}
/**
* Extend a signal by read-subjects header given by an enforcer for the resource type {@code things}.
*
* @param signal the signal to extend.
* @param enforcer the enforcer.
* @return the extended signal.
*/
protected static <T extends Signal> T addReadSubjectsToThingSignal(final Signal<T> signal,
final Enforcer enforcer) {
return addReadSubjectsToSignal(signal, getThingsReadSubjects(signal, enforcer));
}
/**
* Extend a signal by read-subjects header given explicitly.
*
* @param <T> type of the signal.
* @param signal the signal to extend.
* @param readSubjects explicitly-given read subjects.
* @return the extended signal.
*/
protected static <T extends Signal> T addReadSubjectsToSignal(final Signal<T> signal,
final Set<String> readSubjects) {
final DittoHeaders newHeaders = signal.getDittoHeaders()
.toBuilder()
.readSubjects(readSubjects)
.build();
return signal.setDittoHeaders(newHeaders);
}
/**
* Get read subjects from an enforcer for the resource type {@code things}.
*
* @param signal the signal to get read subjects for.
* @param enforcer the enforcer.
* @return read subjects of the signal.
*/
protected static Set<String> getThingsReadSubjects(final Signal<?> signal, final Enforcer enforcer) {
final ResourceKey resourceKey =
ResourceKey.newInstance(ThingCommand.RESOURCE_TYPE, signal.getResourcePath());
return enforcer.getSubjectIdsWithPermission(resourceKey, Permission.READ).getGranted();
}
/**
* Check whether response or error from a future is {@code AskTimeoutException}.
*
* @param response response from a future.
* @param error error thrown in a future.
* @return whether either is {@code AskTimeoutException}.
*/
protected static boolean isAskTimeoutException(final Object response, final Throwable error) {
return error instanceof AskTimeoutException || response instanceof AskTimeoutException;
}
/**
* @return Timeout duration for asking entity shard regions.
*/
protected Duration getAskTimeout() {
return context.askTimeout;
}
/**
* @return the entity ID.
*/
protected EntityId entityId() {
return context.entityId;
}
/**
* @param withPotentialDittoHeaders the object which potentially contains DittoHeaders from which a
* {@code correlation-id} can be extracted in order to enhance the returned DiagnosticLoggingAdapter
* @return the diagnostic logging adapter.
*/
protected DiagnosticLoggingAdapter log(final Object withPotentialDittoHeaders) {
if (withPotentialDittoHeaders instanceof WithDittoHeaders) {
return log(((WithDittoHeaders<?>) withPotentialDittoHeaders).getDittoHeaders());
}
return context.log;
}
/**
* @param dittoHeaders the DittoHeaders from which a {@code correlation-id} can be extracted in order to enhance
* the returned DiagnosticLoggingAdapter.
* @return the diagnostic logging adapter.
*/
protected DiagnosticLoggingAdapter log(final DittoHeaders dittoHeaders) {
if (context.log != null) {
LogUtil.enhanceLogWithCorrelationId(context.log, dittoHeaders);
}
return context.log;
}
/**
* @return Akka pubsub mediator.
*/
protected ActorRef pubSubMediator() {
return context.pubSubMediator;
}
/**
* @return actor reference of the enforcer actor this object belongs to.
*/
protected ActorRef self() {
return context.self;
}
protected ActorRef conciergeForwarder() { return context.conciergeForwarder;}
/**
* Holds context information required by implementations of {@link AbstractEnforcement}.
*/
public static final class Context {
private final ActorRef pubSubMediator;
private final Duration askTimeout;
@Nullable
private final EntityId entityId;
@Nullable
private final DiagnosticLoggingAdapter log;
@Nullable
private final ActorRef self;
private final ActorRef conciergeForwarder;
Context(
final ActorRef pubSubMediator,
final Duration askTimeout,
final ActorRef conciergeForwarder) {
this(pubSubMediator, askTimeout, conciergeForwarder, null, null, null);
}
Context(
final ActorRef pubSubMediator,
final Duration askTimeout,
@Nullable final ActorRef conciergeForwarder,
@Nullable final EntityId entityId,
@Nullable final DiagnosticLoggingAdapter log,
@Nullable final ActorRef self) {
this.pubSubMediator = pubSubMediator;
this.askTimeout = askTimeout;
this.conciergeForwarder = conciergeForwarder;
this.entityId = entityId;
this.log = log;
this.self = self;
}
/**
* Creates a new {@link Context} from this instance with the given parameters.
*
* @param actorContext the actor context.
* @param log the logger.
* @return the created instance.
*/
public Context with(final AbstractActor.ActorContext actorContext, final DiagnosticLoggingAdapter log) {
final ActorRef contextSelf = actorContext.self();
return new Context(pubSubMediator, askTimeout, conciergeForwarder, decodeEntityId(contextSelf), log,
contextSelf);
}
private static EntityId decodeEntityId(final ActorRef self) {
final String name = self.path().name();
try {
final String typeWithPath = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
return EntityId.readFrom(typeWithPath);
} catch (final UnsupportedEncodingException e) {
throw new IllegalStateException("Unsupported encoding", e);
}
}
}
}