-
Notifications
You must be signed in to change notification settings - Fork 215
/
LiveSignalEnforcement.java
282 lines (248 loc) · 13.4 KB
/
LiveSignalEnforcement.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
/*
* 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 static org.eclipse.ditto.policies.api.Permission.WRITE;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.UnsupportedSignalException;
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.WithEntity;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.internal.utils.pubsub.StreamingType;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.messages.model.MessageFormatInvalidException;
import org.eclipse.ditto.messages.model.MessageSendNotAllowedException;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
import org.eclipse.ditto.messages.model.signals.commands.SendClaimMessage;
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.model.PoliciesResourceType;
import org.eclipse.ditto.policies.model.ResourceKey;
import org.eclipse.ditto.policies.model.enforcers.EffectedSubjects;
import org.eclipse.ditto.policies.model.enforcers.Enforcer;
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.exceptions.EventSendNotAllowedException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
/**
* Enforces live commands (including message commands) and live events.
*/
final class LiveSignalEnforcement
extends AbstractEnforcementReloaded<Signal<?>, CommandResponse<?>>
implements ThingEnforcementStrategy {
@Override
public boolean isApplicable(final Signal<?> signal) {
return Command.isLiveCommand(signal) || Event.isLiveEvent(signal) || CommandResponse.isLiveCommandResponse(signal);
}
@Override
public boolean responseIsApplicable(final CommandResponse<?> commandResponse) {
return CommandResponse.isLiveCommandResponse(commandResponse);
}
@Override
@SuppressWarnings("unchecked")
public EnforcementReloaded<Signal<?>, CommandResponse<?>> getEnforcement() {
return this;
}
@Override
public CompletionStage<Signal<?>> authorizeSignal(final Signal<?> liveSignal, final PolicyEnforcer policyEnforcer) {
final CompletionStage<Signal<?>> result;
final var correlationIdOpt = WithDittoHeaders.getCorrelationId(liveSignal);
if (correlationIdOpt.isPresent()) {
final Enforcer enforcer = policyEnforcer.getEnforcer();
if (liveSignal instanceof SendClaimMessage) {
// claim messages require no enforcement, publish them right away:
result = publishMessageCommand((MessageCommand<?, ?>) liveSignal, enforcer);
} else {
final var streamingType = StreamingType.fromSignal(liveSignal);
if (streamingType.isPresent()) {
result = enforceLiveSignal(streamingType.get(), liveSignal, policyEnforcer);
} else {
LOGGER.withCorrelationId(liveSignal)
.error("Unsupported Signal in LiveSignalEnforcement: <{}>", liveSignal);
throw DittoInternalErrorException.newBuilder()
.dittoHeaders(liveSignal.getDittoHeaders())
.build();
}
}
} else {
// drop live command to nonexistent things and respond with error.
final EntityId entityId =
liveSignal instanceof WithEntityId withEntityId ? withEntityId.getEntityId() : EntityId.of(
ThingConstants.ENTITY_TYPE, "unknown:unknown");
LOGGER.withCorrelationId(liveSignal)
.info("Command of type <{}> with ID <{}> could not be dispatched as no enforcer " +
"could be looked up! Answering with ThingNotAccessibleException.",
liveSignal.getType(), entityId);
throw ThingNotAccessibleException.newBuilder(ThingId.of(entityId))
.dittoHeaders(liveSignal.getDittoHeaders())
.build();
}
return result;
}
@Override
public CompletionStage<Signal<?>> authorizeSignalWithMissingEnforcer(final Signal<?> signal) {
if (signal instanceof WithEntityId withEntityId) {
throw ThingNotAccessibleException.newBuilder(ThingId.of(withEntityId.getEntityId()))
.dittoHeaders(signal.getDittoHeaders())
.build();
} else {
throw ThingNotAccessibleException.newBuilder(ThingId.of("unknown:unknown"))
.dittoHeaders(signal.getDittoHeaders())
.build();
}
}
@Override
public boolean shouldFilterCommandResponse(final CommandResponse<?> commandResponse) {
return true;
}
@Override
public CompletionStage<CommandResponse<?>> filterResponse(final CommandResponse<?> commandResponse,
final PolicyEnforcer policyEnforcer) {
if (commandResponse instanceof WithEntity<?> withEntity) {
try {
if (withEntity.getEntity().isObject()) {
final JsonObject jsonViewForCommandResponse =
ThingCommandEnforcement.getJsonViewForCommandResponse(withEntity.getEntity().asObject(),
commandResponse, policyEnforcer.getEnforcer());
final WithEntity<?> commandResponseWithEntity = withEntity.setEntity(jsonViewForCommandResponse);
return CompletableFuture.completedStage((CommandResponse<?>) commandResponseWithEntity);
} else {
return CompletableFuture.completedStage(commandResponse);
}
} catch (final RuntimeException e) {
throw reportError("Error after building JsonView", e, commandResponse.getDittoHeaders());
}
} else {
return CompletableFuture.completedStage(commandResponse);
}
}
private CompletionStage<Signal<?>> enforceLiveSignal(final StreamingType streamingType, final Signal<?> liveSignal,
final PolicyEnforcer enforcer) {
switch (streamingType) {
case MESSAGES:
return enforceMessageCommand((MessageCommand<?, ?>) liveSignal, enforcer.getEnforcer());
case LIVE_EVENTS:
return enforceLiveEvent(liveSignal, enforcer.getEnforcer());
case LIVE_COMMANDS:
ThingCommandEnforcement.authorizeByPolicyOrThrow(enforcer.getEnforcer(), (ThingCommand<?>) liveSignal);
final ThingCommand<?> withReadSubjects =
addEffectedReadSubjectsToThingLiveSignal((ThingCommand<?>) liveSignal, enforcer.getEnforcer());
LOGGER.withCorrelationId(withReadSubjects)
.info("Live Command was authorized: <{}>", withReadSubjects.getType());
return CompletableFuture.completedStage(withReadSubjects);
default:
LOGGER.withCorrelationId(liveSignal)
.warn("Ignoring unsupported command signal: <{}>", liveSignal);
throw UnsupportedSignalException.newBuilder(liveSignal.getType())
.message("The sent command is not supported as live command")
.dittoHeaders(liveSignal.getDittoHeaders())
.build();
}
}
/**
* 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 addEffectedReadSubjectsToThingLiveSignal(final Signal<T> signal,
final Enforcer enforcer) {
final var resourceKey = ResourceKey.newInstance(ThingConstants.ENTITY_TYPE, signal.getResourcePath());
final var effectedSubjects = enforcer.getSubjectsWithPermission(resourceKey, Permission.READ);
final var newHeaders = signal.getDittoHeaders()
.toBuilder()
.readGrantedSubjects(effectedSubjects.getGranted())
.readRevokedSubjects(effectedSubjects.getRevoked())
.build();
return signal.setDittoHeaders(newHeaders);
}
private CompletionStage<Signal<?>> enforceLiveEvent(final Signal<?> liveSignal,
final Enforcer enforcer) {
final boolean authorized = enforcer.hasUnrestrictedPermissions(
PoliciesResourceType.thingResource(liveSignal.getResourcePath()),
liveSignal.getDittoHeaders().getAuthorizationContext(), WRITE);
if (authorized) {
LOGGER.withCorrelationId(liveSignal)
.info("Live Event was authorized: <{}>", liveSignal);
final ThingEvent<?> withReadSubjects =
ThingCommandEnforcement.addEffectedReadSubjectsToThingSignal((ThingEvent<?>) liveSignal, enforcer);
return CompletableFuture.completedStage(withReadSubjects);
} else {
LOGGER.withCorrelationId(liveSignal)
.info("Live Event was NOT authorized: <{}>", liveSignal);
throw EventSendNotAllowedException.newBuilder(((ThingEvent<?>) liveSignal).getEntityId())
.dittoHeaders(liveSignal.getDittoHeaders())
.build();
}
}
private CompletionStage<Signal<?>> enforceMessageCommand(final MessageCommand<?, ?> command,
final Enforcer enforcer) {
if (isAuthorized(command, enforcer)) {
return publishMessageCommand(command, enforcer);
} else {
return CompletableFuture.failedStage(rejectMessageCommand(command));
}
}
private CompletionStage<Signal<?>> publishMessageCommand(final MessageCommand<?, ?> command,
final Enforcer enforcer) {
final ResourceKey resourceKey =
ResourceKey.newInstance(MessageCommand.RESOURCE_TYPE, command.getResourcePath());
final EffectedSubjects effectedSubjects = enforcer.getSubjectsWithPermission(resourceKey, Permission.READ);
final var headersWithReadSubjects = command.getDittoHeaders()
.toBuilder()
.readGrantedSubjects(effectedSubjects.getGranted())
.readRevokedSubjects(effectedSubjects.getRevoked())
.build();
final MessageCommand<?, ?> withReadSubjects = command.setDittoHeaders(headersWithReadSubjects);
return CompletableFuture.completedStage(withReadSubjects);
}
private MessageSendNotAllowedException rejectMessageCommand(final MessageCommand<?, ?> command) {
final MessageSendNotAllowedException error =
MessageSendNotAllowedException.newBuilder(command.getEntityId())
.dittoHeaders(command.getDittoHeaders())
.build();
LOGGER.withCorrelationId(command).info(
"The command <{}> was not forwarded due to insufficient rights {}: {} - AuthorizationContext: {}",
command.getType(), error.getClass().getSimpleName(), error.getMessage(),
command.getDittoHeaders().getAuthorizationContext());
return error;
}
private static boolean isAuthorized(final MessageCommand<?, ?> command, final Enforcer enforcer) {
return enforcer.hasUnrestrictedPermissions(extractMessageResourceKey(command),
command.getDittoHeaders().getAuthorizationContext(), WRITE);
}
private static ResourceKey extractMessageResourceKey(final MessageCommand<?, ?> command) {
try {
return PoliciesResourceType.messageResource(command.getResourcePath());
} catch (final IllegalArgumentException e) {
throw MessageFormatInvalidException.newBuilder(JsonFactory.nullArray())
.message("Unable to determine message resource path.")
.description("Please verify that the thing ID, message subject and direction are set correctly.")
.dittoHeaders(command.getDittoHeaders())
.build();
}
}
}