-
Notifications
You must be signed in to change notification settings - Fork 215
/
GatewayRootActor.java
executable file
·340 lines (291 loc) · 18.4 KB
/
GatewayRootActor.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* 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.gateway.service.starter;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import org.eclipse.ditto.base.model.headers.DittoHeadersSizeChecker;
import org.eclipse.ditto.base.model.headers.translator.HeaderTranslator;
import org.eclipse.ditto.base.service.RootChildActorStarter;
import org.eclipse.ditto.base.service.actors.DittoRootActor;
import org.eclipse.ditto.base.service.config.limits.LimitsConfig;
import org.eclipse.ditto.connectivity.api.ConnectivityMessagingConstants;
import org.eclipse.ditto.edge.service.dispatching.EdgeCommandForwarderActor;
import org.eclipse.ditto.edge.service.dispatching.ShardRegions;
import org.eclipse.ditto.edge.service.dispatching.SignalTransformer;
import org.eclipse.ditto.gateway.service.endpoints.directives.auth.DevopsAuthenticationDirective;
import org.eclipse.ditto.gateway.service.endpoints.directives.auth.DevopsAuthenticationDirectiveFactory;
import org.eclipse.ditto.gateway.service.endpoints.directives.auth.GatewayAuthenticationDirectiveFactory;
import org.eclipse.ditto.gateway.service.endpoints.routes.CustomApiRoutesProvider;
import org.eclipse.ditto.gateway.service.endpoints.routes.HttpBindFlowProvider;
import org.eclipse.ditto.gateway.service.endpoints.routes.RootRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.RouteBaseProperties;
import org.eclipse.ditto.gateway.service.endpoints.routes.cloudevents.CloudEventsRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.devops.DevOpsRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.health.CachingHealthRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.policies.OAuthTokenIntegrationSubjectIdFactory;
import org.eclipse.ditto.gateway.service.endpoints.routes.policies.PoliciesRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.sse.ThingsSseRouteBuilder;
import org.eclipse.ditto.gateway.service.endpoints.routes.stats.StatsRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.status.OverallStatusRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.things.ThingsRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.thingsearch.ThingSearchRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.websocket.WebSocketRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.whoami.WhoamiRoute;
import org.eclipse.ditto.gateway.service.endpoints.utils.GatewaySignalEnrichmentProvider;
import org.eclipse.ditto.gateway.service.health.DittoStatusAndHealthProviderFactory;
import org.eclipse.ditto.gateway.service.health.GatewayHttpReadinessCheck;
import org.eclipse.ditto.gateway.service.health.StatusAndHealthProvider;
import org.eclipse.ditto.gateway.service.proxy.actors.GatewayProxyActor;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthenticationFactory;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthenticationResultProvider;
import org.eclipse.ditto.gateway.service.streaming.actors.StreamingActor;
import org.eclipse.ditto.gateway.service.util.config.GatewayConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.HttpConfig;
import org.eclipse.ditto.gateway.service.util.config.health.HealthCheckConfig;
import org.eclipse.ditto.gateway.service.util.config.security.AuthenticationConfig;
import org.eclipse.ditto.gateway.service.util.config.security.DevOpsConfig;
import org.eclipse.ditto.gateway.service.util.config.security.OAuthConfig;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.internal.utils.cluster.ClusterStatusSupplier;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.cluster.ShardRegionProxyActorFactory;
import org.eclipse.ditto.internal.utils.cluster.config.ClusterConfig;
import org.eclipse.ditto.internal.utils.config.LocalHostAddressSupplier;
import org.eclipse.ditto.internal.utils.health.DefaultHealthCheckingActorFactory;
import org.eclipse.ditto.internal.utils.health.HealthCheckingActorOptions;
import org.eclipse.ditto.internal.utils.health.cluster.ClusterStatus;
import org.eclipse.ditto.internal.utils.health.routes.StatusRoute;
import org.eclipse.ditto.internal.utils.http.DefaultHttpClientFacade;
import org.eclipse.ditto.internal.utils.http.HttpClientFacade;
import org.eclipse.ditto.internal.utils.protocol.ProtocolAdapterProvider;
import org.eclipse.ditto.internal.utils.pubsubthings.DittoProtocolSub;
import akka.actor.ActorRef;
import akka.actor.ActorRefFactory;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.cluster.Cluster;
import akka.event.DiagnosticLoggingAdapter;
import akka.http.javadsl.Http;
import akka.http.javadsl.ServerBinding;
import akka.http.javadsl.server.Route;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.Materializer;
import akka.stream.SystemMaterializer;
/**
* The Root Actor of the API Gateway's Akka ActorSystem.
*/
public final class GatewayRootActor extends DittoRootActor {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "gatewayRoot";
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final CompletionStage<ServerBinding> httpBinding;
@SuppressWarnings("unused")
private GatewayRootActor(final GatewayConfig gatewayConfig, final ActorRef pubSubMediator) {
final ActorSystem actorSystem = context().system();
final var clusterConfig = gatewayConfig.getClusterConfig();
final int numberOfShards = clusterConfig.getNumberOfShards();
final AuthenticationConfig authenticationConfig = gatewayConfig.getAuthenticationConfig();
final CacheConfig publicKeysConfig = gatewayConfig.getCachesConfig().getPublicKeysConfig();
final HealthCheckConfig healthCheckConfig = gatewayConfig.getHealthCheckConfig();
final HttpConfig httpConfig = gatewayConfig.getHttpConfig();
final ShardRegions shardRegions = ShardRegions.of(actorSystem, clusterConfig);
final SignalTransformer signalTransformer = SignalTransformer.get(context().system());
final var edgeCommandForwarder = startChildActor(EdgeCommandForwarderActor.ACTOR_NAME,
EdgeCommandForwarderActor.props(pubSubMediator, shardRegions, signalTransformer));
final var proxyActor = startProxyActor(actorSystem, pubSubMediator, edgeCommandForwarder);
pubSubMediator.tell(DistPubSubAccess.put(getSelf()), getSelf());
final DittoProtocolSub dittoProtocolSub = DittoProtocolSub.get(actorSystem);
final DefaultHttpClientFacade httpClient =
DefaultHttpClientFacade.getInstance(actorSystem, authenticationConfig.getHttpProxyConfig());
final OAuthConfig oAuthConfig = authenticationConfig.getOAuthConfig();
final JwtAuthenticationFactory jwtAuthenticationFactory =
JwtAuthenticationFactory.newInstance(oAuthConfig, publicKeysConfig, httpClient, actorSystem);
final JwtAuthenticationResultProvider jwtAuthenticationResultProvider =
JwtAuthenticationResultProvider.get(actorSystem);
final DevOpsConfig devOpsConfig = authenticationConfig.getDevOpsConfig();
final DevopsAuthenticationDirectiveFactory devopsAuthenticationDirectiveFactory =
getDevopsAuthenticationDirectiveFactory(httpClient, publicKeysConfig, devOpsConfig, actorSystem);
final ProtocolAdapterProvider protocolAdapterProvider =
ProtocolAdapterProvider.load(gatewayConfig.getProtocolConfig(), actorSystem);
final HeaderTranslator headerTranslator = protocolAdapterProvider.getHttpHeaderTranslator();
final ActorRef streamingActor = startChildActor(StreamingActor.ACTOR_NAME,
StreamingActor.props(dittoProtocolSub,
proxyActor,
jwtAuthenticationFactory.getJwtValidator(),
jwtAuthenticationResultProvider,
gatewayConfig.getStreamingConfig(),
headerTranslator,
pubSubMediator,
edgeCommandForwarder));
RootChildActorStarter.get(actorSystem).execute(getContext());
final ActorRef healthCheckActor = createHealthCheckActor(healthCheckConfig);
final var hostname = getHostname(httpConfig);
final Route rootRoute = createRoute(actorSystem, gatewayConfig, proxyActor, streamingActor,
healthCheckActor, pubSubMediator, healthCheckConfig, jwtAuthenticationFactory,
devopsAuthenticationDirectiveFactory, protocolAdapterProvider, headerTranslator);
httpBinding = Http.get(actorSystem)
.newServerAt(hostname, httpConfig.getPort())
.bindFlow(HttpBindFlowProvider.get(actorSystem).getFlow(rootRoute))
.thenApply(theBinding -> {
log.info("Serving HTTP requests on port <{}> ...", theBinding.localAddress().getPort());
return theBinding.addToCoordinatedShutdown(httpConfig.getCoordinatedShutdownTimeout(), actorSystem);
})
.exceptionally(failure -> {
log.error(failure,
"Could not create the server binding for the Gateway route because of: <{}>",
failure.getMessage());
log.error("Terminating the actor system");
actorSystem.terminate();
return null;
});
}
/**
* Creates Akka configuration object Props for this actor.
*
* @param gatewayConfig the configuration settings of this service.
* @param pubSubMediator the pub-sub mediator.
* @return the Akka configuration Props object.
*/
public static Props props(final GatewayConfig gatewayConfig, final ActorRef pubSubMediator) {
return Props.create(GatewayRootActor.class, gatewayConfig, pubSubMediator);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.matchEquals(GatewayHttpReadinessCheck.READINESS_ASK_MESSAGE, msg -> {
final var sender = getSender();
httpBinding.thenAccept(
binding -> sender.tell(GatewayHttpReadinessCheck.READINESS_ASK_MESSAGE_RESPONSE,
ActorRef.noSender()));
})
.build()
.orElse(super.createReceive());
}
private static Route createRoute(final ActorSystem actorSystem,
final GatewayConfig gatewayConfig,
final ActorRef proxyActor,
final ActorRef streamingActor,
final ActorRef healthCheckingActor,
final ActorRef pubSubMediator,
final HealthCheckConfig healthCheckConfig,
final JwtAuthenticationFactory jwtAuthenticationFactory,
final DevopsAuthenticationDirectiveFactory devopsAuthenticationDirectiveFactory,
final ProtocolAdapterProvider protocolAdapterProvider,
final HeaderTranslator headerTranslator) {
final AuthenticationConfig authConfig = gatewayConfig.getAuthenticationConfig();
final Materializer materializer = SystemMaterializer.get(actorSystem).materializer();
final GatewayAuthenticationDirectiveFactory authenticationDirectiveFactory =
GatewayAuthenticationDirectiveFactory.get(actorSystem);
final DevopsAuthenticationDirective devopsAuthenticationDirective =
devopsAuthenticationDirectiveFactory.devops();
final DevopsAuthenticationDirective statusAuthenticationDirective =
devopsAuthenticationDirectiveFactory.status();
final Supplier<ClusterStatus> clusterStateSupplier = new ClusterStatusSupplier(Cluster.get(actorSystem));
final StatusAndHealthProvider statusAndHealthProvider =
DittoStatusAndHealthProviderFactory.of(actorSystem, clusterStateSupplier, healthCheckConfig);
final LimitsConfig limitsConfig = gatewayConfig.getLimitsConfig();
final DittoHeadersSizeChecker dittoHeadersSizeChecker =
DittoHeadersSizeChecker.of(limitsConfig.getHeadersMaxSize(), limitsConfig.getAuthSubjectsMaxCount());
final HttpConfig httpConfig = gatewayConfig.getHttpConfig();
final var streamingConfig = gatewayConfig.getStreamingConfig();
final var signalEnrichmentProvider = GatewaySignalEnrichmentProvider.get(actorSystem);
final var commandConfig = gatewayConfig.getCommandConfig();
final var connectivityShardRegionProxyActor =
getConnectivityShardRegionProxyActor(actorSystem, gatewayConfig.getClusterConfig());
final var routeBaseProperties = RouteBaseProperties.newBuilder()
.actorSystem(actorSystem)
.proxyActor(proxyActor)
.httpConfig(httpConfig)
.commandConfig(commandConfig)
.headerTranslator(headerTranslator)
.connectivityShardRegionProxy(connectivityShardRegionProxyActor)
.build();
final var customApiRoutesProvider = CustomApiRoutesProvider.get(actorSystem);
return RootRoute.getBuilder(httpConfig)
.statsRoute(new StatsRoute(routeBaseProperties, devopsAuthenticationDirective))
.statusRoute(new StatusRoute(clusterStateSupplier, healthCheckingActor, actorSystem))
.overallStatusRoute(new OverallStatusRoute(clusterStateSupplier,
statusAndHealthProvider,
statusAuthenticationDirective))
.cachingHealthRoute(new CachingHealthRoute(statusAndHealthProvider,
gatewayConfig.getPublicHealthConfig()))
.devopsRoute(new DevOpsRoute(routeBaseProperties, devopsAuthenticationDirective))
.policiesRoute(new PoliciesRoute(routeBaseProperties,
OAuthTokenIntegrationSubjectIdFactory.of(authConfig.getOAuthConfig())))
.sseThingsRoute(
ThingsSseRouteBuilder.getInstance(actorSystem, streamingActor, streamingConfig, pubSubMediator)
.withProxyActor(proxyActor)
.withSignalEnrichmentProvider(signalEnrichmentProvider))
.thingsRoute(new ThingsRoute(routeBaseProperties,
gatewayConfig.getMessageConfig(),
gatewayConfig.getClaimMessageConfig()))
.thingSearchRoute(new ThingSearchRoute(routeBaseProperties))
.whoamiRoute(new WhoamiRoute(routeBaseProperties))
.cloudEventsRoute(new CloudEventsRoute(routeBaseProperties, gatewayConfig.getCloudEventsConfig()))
.websocketRoute(WebSocketRoute.getInstance(actorSystem, streamingActor, streamingConfig, materializer)
.withSignalEnrichmentProvider(signalEnrichmentProvider)
.withHeaderTranslator(headerTranslator))
.supportedSchemaVersions(httpConfig.getSupportedSchemaVersions())
.protocolAdapterProvider(protocolAdapterProvider)
.headerTranslator(headerTranslator)
.httpAuthenticationDirective(
authenticationDirectiveFactory.buildHttpAuthentication(jwtAuthenticationFactory))
.wsAuthenticationDirective(
authenticationDirectiveFactory.buildWsAuthentication(jwtAuthenticationFactory))
.dittoHeadersSizeChecker(dittoHeadersSizeChecker)
.customApiRoutesProvider(customApiRoutesProvider, routeBaseProperties)
.build();
}
private ActorRef createHealthCheckActor(final HealthCheckConfig healthCheckConfig) {
final HealthCheckingActorOptions healthCheckingActorOptions =
HealthCheckingActorOptions.getBuilder(healthCheckConfig.isEnabled(), healthCheckConfig.getInterval())
.build();
return startChildActor(DefaultHealthCheckingActorFactory.ACTOR_NAME,
DefaultHealthCheckingActorFactory.props(healthCheckingActorOptions, null));
}
private static ActorRef getConnectivityShardRegionProxyActor(final ActorSystem actorSystem,
final ClusterConfig clusterConfig) {
final var shardRegionProxyActorFactory = ShardRegionProxyActorFactory.newInstance(actorSystem, clusterConfig);
return shardRegionProxyActorFactory.getShardRegionProxyActor(ConnectivityMessagingConstants.CLUSTER_ROLE,
ConnectivityMessagingConstants.SHARD_REGION);
}
private ActorRef startProxyActor(final ActorRefFactory actorSystem, final ActorRef pubSubMediator,
final ActorRef edgeCommandForwarder) {
final ActorSelection devOpsCommandsActor =
actorSystem.actorSelection(DevOpsRoute.DEVOPS_COMMANDS_ACTOR_SELECTION);
return startChildActor(GatewayProxyActor.ACTOR_NAME,
GatewayProxyActor.props(pubSubMediator, devOpsCommandsActor, edgeCommandForwarder));
}
private static DevopsAuthenticationDirectiveFactory getDevopsAuthenticationDirectiveFactory(
final HttpClientFacade httpClient,
final CacheConfig publicKeysConfig,
final DevOpsConfig devOpsConfig,
final ActorSystem actorSystem) {
final OAuthConfig devopsOauthConfig = devOpsConfig.getOAuthConfig();
final JwtAuthenticationFactory devopsJwtAuthenticationFactory =
JwtAuthenticationFactory.newInstance(devopsOauthConfig, publicKeysConfig, httpClient, actorSystem);
return DevopsAuthenticationDirectiveFactory.newInstance(devopsJwtAuthenticationFactory, devOpsConfig);
}
private String getHostname(final HttpConfig httpConfig) {
String hostname = httpConfig.getHostname();
if (hostname.isEmpty()) {
hostname = LocalHostAddressSupplier.getInstance().get();
log.info("No explicit hostname configured, using HTTP hostname <{}>.", hostname);
}
return hostname;
}
}