-
Notifications
You must be signed in to change notification settings - Fork 214
/
CreationRestrictionPreEnforcer.java
183 lines (151 loc) · 7.08 KB
/
CreationRestrictionPreEnforcer.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
/*
* 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.enforcement.pre;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.regex.Pattern;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.entity.id.NamespacedEntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.EntityNotCreatableException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.policies.enforcement.config.CreationRestrictionConfig;
import org.eclipse.ditto.policies.enforcement.config.DefaultEntityCreationConfig;
import org.eclipse.ditto.policies.enforcement.config.EntityCreationConfig;
import com.typesafe.config.Config;
import akka.actor.ActorSystem;
/**
* Pre-Enforcer for evaluating if creation of new entities should be restricted.
*/
@Immutable
public final class CreationRestrictionPreEnforcer implements PreEnforcer {
private static final ThreadSafeDittoLogger LOG =
DittoLoggerFactory.getThreadSafeLogger(CreationRestrictionPreEnforcer.class);
private final EntityCreationConfig config;
/**
* Constructs a new instance of CreationRestrictionPreEnforcer extension.
*
* @param actorSystem the actor system in which to load the extension.
* @param config the configuration for this extension.
*/
@SuppressWarnings("unused")
public CreationRestrictionPreEnforcer(final ActorSystem actorSystem, final Config config) {
// explicitly use the ActorSystem config instead of the PreEnforcer config - as the config is loaded from
// file "ditto-entity-creation.conf" and extending with system properties of that file should not be broken
this.config = DefaultEntityCreationConfig.of(actorSystem.settings().config());
}
boolean canCreate(final Context context) {
return matchesList(this.config.getGrant(), context)
&& !matchesList(this.config.getRevoke(), context);
}
private boolean matchesList(final List<CreationRestrictionConfig> list, final Context context) {
return list.stream().anyMatch(entry -> matches(entry, context));
}
private static boolean matches(final CreationRestrictionConfig entry, final Context context) {
return matchesResourceType(entry.getResourceTypes(), context)
&& matchesAuthSubjectPattern(entry.getAuthSubject(), context)
&& matchesNamespacePattern(entry.getNamespace(), context);
}
private static boolean matchesResourceType(final Set<String> resourceTypes, final Context context) {
if (resourceTypes.isEmpty()) {
// no restriction -> pass
LOG.withCorrelationId(context.headers()).debug("No resource type restriction: pass");
return true;
}
if (resourceTypes.contains(context.resourceType())) {
return true;
}
LOG.withCorrelationId(context.headers()).debug("No resource type match: reject");
// no match, but non-empty list -> reject
return false;
}
private static boolean matchesAuthSubjectPattern(final List<Pattern> authSubjectPatterns, final Context context) {
if (authSubjectPatterns.isEmpty()) {
// no restrictions -> pass
LOG.withCorrelationId(context.headers()).debug("No auth subject restriction: pass");
return true;
}
for (final var subject : context.headers().getAuthorizationContext().getAuthorizationSubjectIds()) {
for (final var authSubjectPattern : authSubjectPatterns) {
if (authSubjectPattern.matcher(subject).matches()) {
LOG.withCorrelationId(context.headers()).debug("Matched auth subject {}: pass", subject);
// entry found -> pass
return true;
}
}
}
LOG.withCorrelationId(context.headers()).debug("No auth subject match: reject");
// no match, but non-empty list -> reject
return false;
}
private static boolean matchesNamespacePattern(final List<Pattern> namespacePatterns, final Context context) {
if (namespacePatterns.isEmpty()) {
LOG.withCorrelationId(context.headers()).debug("No namespace restriction: pass");
// no restrictions -> pass
return true;
}
var namespace = context.namespace();
for (final var namespacePattern : namespacePatterns) {
if (namespacePattern.matcher(namespace).matches()) {
LOG.withCorrelationId(context.headers()).debug("Namespace '{}' matched {}: pass", namespace,
namespacePattern);
return true;
}
}
LOG.withCorrelationId(context.headers()).debug("No namespace match: reject");
// no match, but non-empty list -> reject
return false;
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"config=" + config +
']';
}
@Override
public CompletionStage<Signal<?>> apply(final Signal<?> signal) {
final CompletionStage<Signal<?>> result;
if (isCreatingCommand(signal)) {
result = CompletableFuture.completedStage(handleCreatingCommand(signal));
} else {
result = CompletableFuture.completedFuture(signal);
}
return result;
}
private static boolean isCreatingCommand(final Signal<?> signal) {
return signal instanceof Command<?> command && command.getCategory().equals(Command.Category.CREATE);
}
private Signal<?> handleCreatingCommand(final Signal<?> signal) {
final WithEntityId withEntityId = getMessageAsWithEntityId(signal);
final NamespacedEntityId entityId = getEntityIdAsNamespacedEntityId(withEntityId.getEntityId());
final var context = new Context(signal.getResourceType(), entityId.getNamespace(),
signal.getDittoHeaders());
if (canCreate(context)) {
return signal;
} else {
throw EntityNotCreatableException.newBuilder(withEntityId.getEntityId())
.dittoHeaders(signal.getDittoHeaders())
.build();
}
}
/**
* The context for evaluating if an entity may be created or not.
*/
record Context(String resourceType, String namespace, DittoHeaders headers) {}
}