-
Notifications
You must be signed in to change notification settings - Fork 214
/
CreationRestrictionEnforcer.java
159 lines (132 loc) · 6.45 KB
/
CreationRestrictionEnforcer.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
/*
* 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_enforcement;
import java.util.List;
import java.util.Set;
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.headers.DittoHeadersSettable;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
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 akka.actor.ActorSystem;
/**
* PreEnforcer for evaluating if the entity creation should be restricted.
*/
@Immutable
public class CreationRestrictionEnforcer implements PreEnforcer {
private static final ThreadSafeDittoLogger log = DittoLoggerFactory.getThreadSafeLogger(CreationRestrictionEnforcer.class);
private final EntityCreationConfig config;
private final ExistenceChecker existenceChecker;
public CreationRestrictionEnforcer(final ActorSystem actorSystem) {
config = DefaultEntityCreationConfig.of(DefaultScopedConfig.dittoScoped(actorSystem.settings().config()));
existenceChecker = ExistenceChecker.get(actorSystem);
}
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<DittoHeadersSettable<?>> apply(final DittoHeadersSettable<?> dittoHeadersSettable) {
final Signal<?> messageAsSignal = getMessageAsSignal(dittoHeadersSettable);
final WithEntityId withEntityId = getMessageAsWithEntityId(dittoHeadersSettable);
final NamespacedEntityId entityId = getEntityIdAsNamespacedEntityId(withEntityId.getEntityId());
final var context = new Context(messageAsSignal.getResourceType(), entityId.getNamespace(), messageAsSignal.getDittoHeaders());
return existenceChecker.checkExistence(messageAsSignal).thenApply(exists -> {
if (Boolean.TRUE.equals(exists) || canCreate(context)) {
return dittoHeadersSettable;
} else {
throw EntityNotCreatableException.newBuilder(withEntityId.getEntityId())
.dittoHeaders(messageAsSignal.getDittoHeaders())
.build();
}
});
}
/**
* The context for evaluating if an entity may be created or not.
*/
record Context(String resourceType, String namespace, DittoHeaders headers) {}
}