forked from eclipse-ditto/ditto
-
Notifications
You must be signed in to change notification settings - Fork 7
/
PoliciesRoute.java
executable file
·227 lines (201 loc) · 11.3 KB
/
PoliciesRoute.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
/*
* Copyright (c) 2017 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.services.gateway.endpoints.routes.policies;
import static org.eclipse.ditto.model.base.exceptions.DittoJsonException.wrapJsonRuntimeException;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.jwt.JsonWebToken;
import org.eclipse.ditto.model.policies.Label;
import org.eclipse.ditto.model.policies.PoliciesModelFactory;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.model.policies.SubjectId;
import org.eclipse.ditto.protocoladapter.HeaderTranslator;
import org.eclipse.ditto.services.gateway.endpoints.routes.AbstractRoute;
import org.eclipse.ditto.services.gateway.security.authentication.AuthenticationResult;
import org.eclipse.ditto.services.gateway.security.authentication.jwt.JwtAuthenticationResult;
import org.eclipse.ditto.services.gateway.util.config.endpoints.CommandConfig;
import org.eclipse.ditto.services.gateway.util.config.endpoints.HttpConfig;
import org.eclipse.ditto.signals.commands.policies.actions.ActivateTokenIntegration;
import org.eclipse.ditto.signals.commands.policies.actions.DeactivateTokenIntegration;
import org.eclipse.ditto.signals.commands.policies.actions.TopLevelActionCommand;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyActionFailedException;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyIdNotExplicitlySettableException;
import org.eclipse.ditto.signals.commands.policies.modify.DeletePolicy;
import org.eclipse.ditto.signals.commands.policies.modify.ModifyPolicy;
import org.eclipse.ditto.signals.commands.policies.query.RetrievePolicy;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.http.javadsl.server.PathMatchers;
import akka.http.javadsl.server.RequestContext;
import akka.http.javadsl.server.Route;
/**
* Builder for creating Akka HTTP routes for {@code /policies}.
*/
public final class PoliciesRoute extends AbstractRoute {
private static final String PATH_ACTIONS = "actions";
private static final String ACTIVATE_TOKEN_INTEGRATION = ActivateTokenIntegration.NAME;
private static final String DEACTIVATE_TOKEN_INTEGRATION = DeactivateTokenIntegration.NAME;
private static final String PATH_POLICIES = "policies";
private static final String PATH_ENTRIES = "entries";
private static final Label DUMMY_LABEL = Label.of("-");
private final PolicyEntriesRoute policyEntriesRoute;
private final TokenIntegrationSubjectIdFactory tokenIntegrationSubjectIdFactory;
/**
* Constructs the {@code /policies} route builder.
*
* @param proxyActor an actor selection of the command delegating actor.
* @param actorSystem the ActorSystem to use.
* @param httpConfig the configuration settings of the Gateway service's HTTP endpoint.
* @param commandConfig the configuration settings of the Gateway service's incoming command processing.
* @param headerTranslator translates headers from external sources or to external sources.
* @param tokenIntegrationSubjectIdFactory factory of resolvers for placeholders.
* @throws NullPointerException if any argument is {@code null}.
*/
public PoliciesRoute(final ActorRef proxyActor,
final ActorSystem actorSystem,
final HttpConfig httpConfig,
final CommandConfig commandConfig,
final HeaderTranslator headerTranslator,
final TokenIntegrationSubjectIdFactory tokenIntegrationSubjectIdFactory) {
super(proxyActor, actorSystem, httpConfig, commandConfig, headerTranslator);
this.tokenIntegrationSubjectIdFactory = tokenIntegrationSubjectIdFactory;
policyEntriesRoute =
new PolicyEntriesRoute(proxyActor, actorSystem, httpConfig, commandConfig, headerTranslator,
tokenIntegrationSubjectIdFactory);
}
/**
* Builds the {@code /policies} route.
*
* @return the {@code /policies} route.
*/
public Route buildPoliciesRoute(final RequestContext ctx, final DittoHeaders dittoHeaders,
final AuthenticationResult authenticationResult) {
return rawPathPrefix(PathMatchers.slash().concat(PATH_POLICIES), () ->
rawPathPrefix(PathMatchers.slash().concat(PathMatchers.segment()), policyId ->
// /policies/<policyId>
policyRoute(ctx, dittoHeaders, PolicyId.of(policyId), authenticationResult)
)
);
}
private Route policyRoute(final RequestContext ctx, final DittoHeaders dittoHeaders, final PolicyId policyId,
final AuthenticationResult authenticationResult) {
return concat(
policyEntry(ctx, dittoHeaders, policyId),
policyEntryEntries(ctx, dittoHeaders, policyId, authenticationResult),
policyActions(ctx, dittoHeaders, policyId, authenticationResult)
);
}
/*
* Describes {@code /policies/<policyId>} route.
* @return {@code /policies/<policyId>} route.
*/
private Route policyEntry(final RequestContext ctx, final DittoHeaders dittoHeaders, final PolicyId policyId) {
return pathEndOrSingleSlash(() ->
concat(
get(() -> // GET /policies/<policyId>
handlePerRequest(ctx, RetrievePolicy.of(policyId, dittoHeaders))
),
put(() -> // PUT /policies/<policyId>
ensureMediaTypeJsonWithFallbacksThenExtractDataBytes(ctx, dittoHeaders,
payloadSource ->
handlePerRequest(ctx, dittoHeaders, payloadSource,
policyJson -> ModifyPolicy
.of(policyId, PoliciesModelFactory.newPolicy(
createPolicyJsonObjectForPut(policyJson,
policyId)),
dittoHeaders)
)
)
),
delete(() -> // DELETE /policies/<policyId>
handlePerRequest(ctx, DeletePolicy.of(policyId, dittoHeaders))
)
)
);
}
private static JsonObject createPolicyJsonObjectForPut(final String jsonString, final PolicyId policyId) {
final JsonObject policyJsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
policyJsonObject.getValue(Policy.JsonFields.ID.getPointer())
.ifPresent(policyIdJsonValue -> {
if (!policyIdJsonValue.isString() || !policyId.toString().equals(policyIdJsonValue.asString())) {
throw PolicyIdNotExplicitlySettableException.newBuilder().build();
}
});
return policyJsonObject.setValue(Policy.JsonFields.ID.getPointer(), JsonValue.of(policyId));
}
/*
* Describes {@code /policies/<policyId>/entries} route.
*
* @return {@code /policies/<policyId>/entries} route.
*/
private Route policyEntryEntries(final RequestContext ctx, final DittoHeaders dittoHeaders, final PolicyId policyId,
final AuthenticationResult authResult) {
return rawPathPrefix(PathMatchers.slash().concat(PATH_ENTRIES), () -> // /policies/<policyId>/entries
policyEntriesRoute.buildPolicyEntriesRoute(ctx, dittoHeaders, policyId, authResult)
);
}
/*
* Describes{@code /policies/<policyId>/actions} route.
*/
private Route policyActions(final RequestContext ctx, final DittoHeaders dittoHeaders, final PolicyId policyId,
final AuthenticationResult authenticationResult) {
return rawPathPrefix(PathMatchers.slash().concat(PATH_ACTIONS), () -> concat(// /policies/<policyId>/actions
rawPathPrefix(PathMatchers.slash().concat(ACTIVATE_TOKEN_INTEGRATION), () ->
pathEndOrSingleSlash(() ->
extractJwt(dittoHeaders, authenticationResult, ACTIVATE_TOKEN_INTEGRATION, jwt ->
post(() -> handlePerRequest(ctx,
activatePolicyTokenIntegration(dittoHeaders, policyId, jwt)))))),
rawPathPrefix(PathMatchers.slash().concat(DEACTIVATE_TOKEN_INTEGRATION), () ->
pathEndOrSingleSlash(() ->
extractJwt(dittoHeaders, authenticationResult, DEACTIVATE_TOKEN_INTEGRATION, jwt ->
post(() -> handlePerRequest(ctx,
deactivatePolicyTokenIntegration(dittoHeaders, policyId, jwt))))))
));
}
private TopLevelActionCommand activatePolicyTokenIntegration(final DittoHeaders dittoHeaders,
final PolicyId policyId, final JsonWebToken jwt) {
final SubjectId subjectId = tokenIntegrationSubjectIdFactory.getSubjectId(dittoHeaders, jwt);
final Instant expiry = jwt.getExpirationTime();
final ActivateTokenIntegration activateTokenIntegration =
ActivateTokenIntegration.of(policyId, DUMMY_LABEL, subjectId, expiry, dittoHeaders);
return TopLevelActionCommand.of(activateTokenIntegration, List.of());
}
private TopLevelActionCommand deactivatePolicyTokenIntegration(final DittoHeaders dittoHeaders,
final PolicyId policyId, final JsonWebToken jwt) {
final SubjectId subjectId = tokenIntegrationSubjectIdFactory.getSubjectId(dittoHeaders, jwt);
final DeactivateTokenIntegration deactivateTokenIntegration =
DeactivateTokenIntegration.of(policyId, DUMMY_LABEL, subjectId, dittoHeaders);
return TopLevelActionCommand.of(deactivateTokenIntegration, List.of());
}
static Route extractJwt(final DittoHeaders dittoHeaders,
final AuthenticationResult authResult,
final String action,
final Function<JsonWebToken, Route> inner) {
if (authResult instanceof JwtAuthenticationResult) {
final Optional<JsonWebToken> jwtOptional = ((JwtAuthenticationResult) authResult).getJwt();
if (jwtOptional.isPresent()) {
return inner.apply(jwtOptional.get());
}
}
throw PolicyActionFailedException.newBuilderForInappropriateAuthenticationMethod(action)
.dittoHeaders(dittoHeaders)
.build();
}
}