-
Notifications
You must be signed in to change notification settings - Fork 215
/
PolicyCommandEnforcement.java
289 lines (257 loc) · 13.4 KB
/
PolicyCommandEnforcement.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
/*
* 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.policies.service.enforcement;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
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.CommandToExceptionRegistry;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObject;
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.PolicyEnforcer;
import org.eclipse.ditto.policies.model.Label;
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.PolicyEntry;
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.PolicyCommand;
import org.eclipse.ditto.policies.model.signals.commands.PolicyCommandResponse;
import org.eclipse.ditto.policies.model.signals.commands.actions.PolicyActionCommand;
import org.eclipse.ditto.policies.model.signals.commands.actions.TopLevelPolicyActionCommand;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyCommandToAccessExceptionRegistry;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyCommandToActionsExceptionRegistry;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyCommandToModifyExceptionRegistry;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyNotAccessibleException;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy;
import org.eclipse.ditto.policies.model.signals.commands.modify.PolicyModifyCommand;
import org.eclipse.ditto.policies.model.signals.commands.query.PolicyQueryCommandResponse;
/**
* Authorizes {@link PolicyCommand}s and filters {@link PolicyCommandResponse}s.
*/
public final class PolicyCommandEnforcement
extends AbstractEnforcementReloaded<Signal<?>, PolicyCommandResponse<?>> {
/**
* Json fields that are always shown regardless of authorization.
*/
private static final JsonFieldSelector POLICY_QUERY_COMMAND_RESPONSE_ALLOWLIST =
JsonFactory.newFieldSelector(Policy.JsonFields.ID);
@Override
public CompletionStage<Signal<?>> authorizeSignal(final Signal<?> signal,
final PolicyEnforcer policyEnforcer) {
if (signal instanceof Command<?> command &&
command.getCategory() == Command.Category.QUERY && !command.getDittoHeaders().isResponseRequired()) {
// ignore query command with response-required=false
return CompletableFuture.completedFuture(null);
}
final Enforcer enforcer = policyEnforcer.getEnforcer();
final var policyResourceKey = PoliciesResourceType.policyResource(signal.getResourcePath());
final var authorizationContext = signal.getDittoHeaders().getAuthorizationContext();
final Signal<?> authorizedCommand;
if (signal instanceof CreatePolicy createPolicy) {
authorizedCommand = authorizeCreatePolicy(enforcer, createPolicy, policyResourceKey, authorizationContext);
} else if (signal instanceof PolicyActionCommand) {
authorizedCommand = authorizeActionCommand(policyEnforcer, signal, policyResourceKey,
authorizationContext).orElseThrow(() -> errorForPolicyCommand(signal));
} else if (signal instanceof PolicyModifyCommand) {
if (hasUnrestrictedWritePermission(enforcer, policyResourceKey, authorizationContext)) {
authorizedCommand = signal;
} else {
throw errorForPolicyCommand(signal);
}
} else {
final String permission = Permission.READ;
if (enforcer.hasPartialPermissions(policyResourceKey,
authorizationContext,
permission)) {
authorizedCommand = signal;
} else {
throw errorForPolicyCommand(signal);
}
}
return CompletableFuture.completedFuture(authorizedCommand);
}
private PolicyCommand<?> authorizeCreatePolicy(final Enforcer enforcer,
final CreatePolicy createPolicy,
final ResourceKey policyResourceKey,
final AuthorizationContext authorizationContext) {
if (hasUnrestrictedWritePermission(enforcer, policyResourceKey, authorizationContext) ||
createPolicy.getDittoHeaders().isAllowPolicyLockout()) {
return createPolicy;
} else {
throw errorForPolicyCommand(createPolicy);
}
}
@Override
public CompletionStage<Signal<?>> authorizeSignalWithMissingEnforcer(final Signal<?> command) {
final PolicyId policyId;
if (command instanceof WithEntityId withEntityId) {
policyId = PolicyId.of(withEntityId.getEntityId());
} else {
LOGGER.warn("Processed signal which does not have an entityId: {}", command);
throw DittoInternalErrorException.newBuilder()
.dittoHeaders(command.getDittoHeaders())
.build();
}
throw PolicyNotAccessibleException.newBuilder(policyId)
.dittoHeaders(command.getDittoHeaders())
.build();
}
@Override
public boolean shouldFilterCommandResponse(final PolicyCommandResponse<?> commandResponse) {
return commandResponse instanceof PolicyQueryCommandResponse<?>;
}
@Override
public CompletionStage<PolicyCommandResponse<?>> filterResponse(final PolicyCommandResponse<?> commandResponse,
final PolicyEnforcer policyEnforcer) {
final PolicyCommandResponse<?> result;
if (commandResponse instanceof PolicyQueryCommandResponse<?> policyQueryCommandResponse) {
try {
result = buildJsonViewForPolicyQueryCommandResponse(policyQueryCommandResponse,
policyEnforcer.getEnforcer());
} catch (final RuntimeException e) {
throw reportError("Error after building JsonView", e, commandResponse.getDittoHeaders());
}
} else {
// no filtering required for non PolicyQueryCommandResponses:
result = commandResponse;
}
return CompletableFuture.completedFuture(result);
}
@SuppressWarnings("unchecked")
private <T extends Signal<?>> Optional<T> authorizeActionCommand(
final PolicyEnforcer enforcer,
final T command, final ResourceKey resourceKey, final AuthorizationContext authorizationContext) {
if (command instanceof TopLevelPolicyActionCommand topLevelPolicyActionCommand) {
return (Optional<T>) authorizeTopLevelAction(enforcer, topLevelPolicyActionCommand, authorizationContext);
} else {
return authorizeEntryLevelAction(enforcer.getEnforcer(), command, resourceKey, authorizationContext);
}
}
private <T extends Signal<?>> Optional<T> authorizeEntryLevelAction(final Enforcer enforcer,
final T command, final ResourceKey resourceKey, final AuthorizationContext authorizationContext) {
return enforcer.hasUnrestrictedPermissions(resourceKey, authorizationContext, Permission.EXECUTE) ?
Optional.of(command) : Optional.empty();
}
private Optional<TopLevelPolicyActionCommand> authorizeTopLevelAction(
final PolicyEnforcer policyEnforcer,
final TopLevelPolicyActionCommand command,
final AuthorizationContext authorizationContext) {
final var enforcer = policyEnforcer.getEnforcer();
final List<Label> labels = getLabelsFromPolicyEnforcer(policyEnforcer);
final var authorizedLabels = labels.stream()
.filter(label -> enforcer.hasUnrestrictedPermissions(asResourceKey(label, command),
authorizationContext, Permission.EXECUTE))
.toList();
if (authorizedLabels.isEmpty()) {
return Optional.empty();
} else {
final var adjustedCommand =
TopLevelPolicyActionCommand.of(command.getPolicyActionCommand(), authorizedLabels);
return Optional.of(adjustedCommand);
}
}
private static List<Label> getLabelsFromPolicyEnforcer(final PolicyEnforcer policyEnforcer) {
return policyEnforcer.getPolicy()
.map(policy -> policy.getEntriesSet().stream()
.map(PolicyEntry::getLabel)
.toList()).orElse(List.of());
}
private boolean hasUnrestrictedWritePermission(final Enforcer enforcer,
final ResourceKey policyResourceKey,
final AuthorizationContext authorizationContext) {
return enforcer.hasUnrestrictedPermissions(policyResourceKey, authorizationContext, Permission.WRITE);
}
/**
* Limit view on entity of {@code PolicyQueryCommandResponse} by enforcer.
*
* @param response the response.
* @param enforcer the enforcer.
* @return a {@code CompletionStage} containing the response with view on entity restricted by enforcer.
*/
public <T extends PolicyQueryCommandResponse<T>> T buildJsonViewForPolicyQueryCommandResponse(
final PolicyQueryCommandResponse<T> response,
final Enforcer enforcer) {
final JsonValue entity = response.getEntity();
final T result;
if (entity.isObject()) {
final JsonObject filteredView =
getJsonViewForPolicyQueryCommandResponse(entity.asObject(), response, enforcer);
result = response.setEntity(filteredView);
} else {
result = response.setEntity(entity);
}
return result;
}
private JsonObject getJsonViewForPolicyQueryCommandResponse(final JsonObject responseEntity,
final PolicyQueryCommandResponse<?> response,
final Enforcer enforcer) {
final var resourceKey = ResourceKey.newInstance(PolicyCommand.RESOURCE_TYPE, response.getResourcePath());
final var authorizationContext = response.getDittoHeaders().getAuthorizationContext();
return enforcer.buildJsonView(resourceKey, responseEntity, authorizationContext,
POLICY_QUERY_COMMAND_RESPONSE_ALLOWLIST, Permissions.newInstance(Permission.READ));
}
/**
* Create error due to failing to execute a policy-command in the expected way.
*
* @param policySignal the signal.
* @return the error.
*/
private static DittoRuntimeException errorForPolicyCommand(final Signal<?> policySignal) {
if (policySignal instanceof PolicyCommand<?> policyCommand) {
final CommandToExceptionRegistry<PolicyCommand<?>, DittoRuntimeException> registry;
if (policyCommand instanceof PolicyActionCommand) {
registry = PolicyCommandToActionsExceptionRegistry.getInstance();
} else if (policyCommand instanceof PolicyModifyCommand) {
registry = PolicyCommandToModifyExceptionRegistry.getInstance();
} else {
registry = PolicyCommandToAccessExceptionRegistry.getInstance();
}
return registry.exceptionFrom(policyCommand);
} else if (policySignal instanceof WithEntityId withEntityId) {
return PolicyNotAccessibleException.newBuilder(PolicyId.of(withEntityId.getEntityId()))
.dittoHeaders(policySignal.getDittoHeaders())
.build();
} else {
LOGGER.error("Received signal for which no DittoRuntimeException due to lack of access " +
"could be determined: {}", policySignal);
return DittoInternalErrorException.newBuilder()
.dittoHeaders(policySignal.getDittoHeaders())
.build();
}
}
/**
* Convert a policy entry label for a top-level policy action into a resource path for authorization check.
*
* @param label the policy entry label.
* @param command the top-level policy action command.
* @return the resource key.
*/
private static ResourceKey asResourceKey(final Label label, final PolicyCommand<?> command) {
return ResourceKey.newInstance(PoliciesResourceType.POLICY,
Policy.JsonFields.ENTRIES.getPointer().addLeaf(JsonKey.of(label)).append(command.getResourcePath()));
}
}