Skip to content

Commit

Permalink
Allow custom configuration for the HttpBindFlowProvider extension
Browse files Browse the repository at this point in the history
Signed-off-by: Yannic Klem <yannic.klem@bosch.io>
  • Loading branch information
Yannic92 committed Jul 12, 2022
1 parent 21867b5 commit 2a724fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import org.eclipse.ditto.base.service.DittoExtensionIds;
import org.eclipse.ditto.base.service.DittoExtensionPoint;

import com.typesafe.config.Config;

import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.http.javadsl.model.HttpRequest;
Expand All @@ -42,21 +45,30 @@ public interface HttpBindFlowProvider extends DittoExtensionPoint {
* {@code ActorSystem}.
*
* @param actorSystem the actorSystem in which the {@code HttpBindFlowProvider} should be loaded.
* @param config the configuration for this extension.
* @return the {@code HttpBindFlowProvider} implementation.
* @throws NullPointerException if {@code actorSystem} is {@code null}.
*/
static HttpBindFlowProvider get(final ActorSystem actorSystem) {
static HttpBindFlowProvider get(final ActorSystem actorSystem, final Config config) {
checkNotNull(actorSystem, "actorSystem");
return ExtensionId.INSTANCE.get(actorSystem);
checkNotNull(config, "config");
final var extensionIdConfig = ExtensionId.computeConfig(config);
return DittoExtensionIds.get(actorSystem)
.computeIfAbsent(extensionIdConfig, ExtensionId::new)
.get(actorSystem);
}

final class ExtensionId extends DittoExtensionPoint.ExtensionId<HttpBindFlowProvider> {

private static final String CONFIG_PATH = "ditto.gateway.http.bind-flow-provider";
private static final ExtensionId INSTANCE = new ExtensionId(HttpBindFlowProvider.class);
private static final String CONFIG_KEY = "http-bind-flow-provider";
private static final String CONFIG_PATH = "ditto.extensions." + CONFIG_KEY;

private ExtensionId(final ExtensionIdConfig<HttpBindFlowProvider> extensionIdConfig) {
super(extensionIdConfig);
}

private ExtensionId(final Class<HttpBindFlowProvider> parentClass) {
super(parentClass);
static ExtensionIdConfig<HttpBindFlowProvider> computeConfig(final Config config) {
return ExtensionIdConfig.of(HttpBindFlowProvider.class, config, CONFIG_KEY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ private GatewayRootActor(final GatewayConfig gatewayConfig, final ActorRef pubSu
pubSubMediator,
edgeCommandForwarder));

RootChildActorStarter.get(actorSystem, ScopedConfig.dittoExtension(config)).execute(getContext());
final var dittoExtensionConfig = ScopedConfig.dittoExtension(config);
RootChildActorStarter.get(actorSystem, dittoExtensionConfig).execute(getContext());

final ActorRef healthCheckActor = createHealthCheckActor(healthCheckConfig);
final var hostname = getHostname(httpConfig);
Expand All @@ -164,9 +165,10 @@ private GatewayRootActor(final GatewayConfig gatewayConfig, final ActorRef pubSu
healthCheckActor, pubSubMediator, healthCheckConfig, jwtAuthenticationFactory,
devopsAuthenticationDirectiveFactory, protocolAdapterProvider, headerTranslator);


httpBinding = Http.get(actorSystem)
.newServerAt(hostname, httpConfig.getPort())
.bindFlow(HttpBindFlowProvider.get(actorSystem).getFlow(rootRoute))
.bindFlow(HttpBindFlowProvider.get(actorSystem, dittoExtensionConfig).getFlow(rootRoute))
.thenApply(theBinding -> {
log.info("Serving HTTP requests on port <{}> ...", theBinding.localAddress().getPort());
return theBinding.addToCoordinatedShutdown(httpConfig.getCoordinatedShutdownTimeout(), actorSystem);
Expand Down
14 changes: 7 additions & 7 deletions gateway/service/src/main/resources/gateway.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
ditto {
extensions {
jwt-authorization-subjects-provider = {
extension-class = "org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider"
extension-class = org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider
}
# The provider for JSON Web Token authentication results
jwt-authentication-result-provider = {
extension-class = "org.eclipse.ditto.gateway.service.security.authentication.jwt.DefaultJwtAuthenticationResultProvider"
extension-class = org.eclipse.ditto.gateway.service.security.authentication.jwt.DefaultJwtAuthenticationResultProvider
# The provider for JSON Web Token authorization subjects
extension-config = {
jwt-authorization-subjects-provider = {
extension-class = "org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider"
extension-class = org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider
}
}
}
Expand Down Expand Up @@ -37,6 +37,10 @@ ditto {
}
}
}

# The provider for custom HTTP bind flows.
http-bind-flow-provider = org.eclipse.ditto.gateway.service.endpoints.routes.LoggingHttpBindFlowProvider

}

service-name = "gateway"
Expand Down Expand Up @@ -97,10 +101,6 @@ ditto {
# The provider for custom API routes.
custom-api-routes-provider = "org.eclipse.ditto.gateway.service.endpoints.routes.NoopCustomApiRoutesProvider"
custom-api-routes-provider = ${?HTTP_CUSTOM_API_ROUTES_PROVIDER}

# The provider for custom HTTP bind flows.
bind-flow-provider = "org.eclipse.ditto.gateway.service.endpoints.routes.LoggingHttpBindFlowProvider"
bind-flow-provider = ${?HTTP_BIND_FLOW_PROVIDER}
}

streaming {
Expand Down
8 changes: 4 additions & 4 deletions gateway/service/src/test/resources/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,19 @@ test {
ditto {
extensions {
jwt-authorization-subjects-provider = {
extension-class = "org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider"
extension-class = org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider
}
# The provider for JSON Web Token authentication results
jwt-authentication-result-provider = {
extension-class = "org.eclipse.ditto.gateway.service.security.authentication.jwt.DefaultJwtAuthenticationResultProvider"
extension-class = org.eclipse.ditto.gateway.service.security.authentication.jwt.DefaultJwtAuthenticationResultProvider
# The provider for JSON Web Token authorization subjects
extension-config = {
jwt-authorization-subjects-provider = {
extension-class = "org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider"
extension-class = org.eclipse.ditto.gateway.service.security.authentication.jwt.DittoJwtAuthorizationSubjectsProvider
}
}
}
http-bind-flow-provider = org.eclipse.ditto.gateway.service.endpoints.routes.LoggingHttpBindFlowProvider
}

mapping-strategy.implementation = "org.eclipse.ditto.gateway.service.util.GatewayMappingStrategies"
Expand Down Expand Up @@ -261,7 +262,6 @@ ditto {
http {
request-timeout = 5s
custom-api-routes-provider = "org.eclipse.ditto.gateway.service.endpoints.routes.NoopCustomApiRoutesProvider"
bind-flow-provider = "org.eclipse.ditto.gateway.service.endpoints.routes.LoggingHttpBindFlowProvider"
}
}
}
Expand Down

0 comments on commit 2a724fe

Please sign in to comment.