diff --git a/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/auth/AuthHandlerSettings.java b/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/auth/AuthHandlerSettings.java index 0bce7acd7..46d04b57c 100644 --- a/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/auth/AuthHandlerSettings.java +++ b/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/auth/AuthHandlerSettings.java @@ -23,12 +23,14 @@ import org.finos.tracdap.config.AuthenticationConfig; import org.finos.tracdap.config.PlatformConfig; import org.finos.tracdap.config.RoutingTarget; +import org.finos.tracdap.gateway.builders.ServiceInfo; import java.time.Duration; public class AuthHandlerSettings { + public static final String PUBLIC_LOGIN_PREFIX = "/login/"; public static final String RETURN_PATH_VARIABLE = "${returnPath}"; public static final int REFRESH_TIMEOUT_MILLIS = 500; @@ -47,16 +49,17 @@ public AuthHandlerSettings(PlatformConfig platformConfig) { this.authTarget = RoutingUtils.serviceTarget(platformConfig, ConfigKeys.AUTHENTICATION_SERVICE_KEY); this.authConfig = platformConfig.getAuthentication(); - // TODO: Get these from config and tie into routing setup - var authPrefix = "/trac-auth/"; - var loginPrefix = "/login/"; + var authServiceInfo = ServiceInfo.buildServiceInfo(platformConfig, ConfigKeys.AUTHENTICATION_SERVICE_KEY); + var authPrefix = authServiceInfo != null + ? authServiceInfo.httpPrefix() + : ServiceInfo.SERVICE_PREFIX_DEFAULTS.get(ConfigKeys.AUTHENTICATION_SERVICE_KEY); var loginPath = ConfigDefaults.readOrDefault(authConfig.getLoginPath(), ConfigDefaults.DEFAULT_LOGIN_PATH); var refreshPath = ConfigDefaults.readOrDefault(authConfig.getRefreshPath(), ConfigDefaults.DEFAULT_REFRESH_PATH); var returnPath = ConfigDefaults.readOrDefault(authConfig.getReturnPath(), ConfigDefaults.DEFAULT_RETURN_PATH); var jwtRefresh = ConfigDefaults.readOrDefault(authConfig.getJwtRefresh(), ConfigDefaults.DEFAULT_JWT_REFRESH); - this.publicLoginPrefix = joinPathSections(authPrefix, loginPrefix); + this.publicLoginPrefix = joinPathSections(authPrefix, PUBLIC_LOGIN_PREFIX); this.publicLoginUrl = joinPathSections(authPrefix, loginPath); this.publicReturnPath = returnPath; this.refreshPath = refreshPath; diff --git a/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/RouteBuilder.java b/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/RouteBuilder.java index f0385fdac..1dcbdc041 100644 --- a/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/RouteBuilder.java +++ b/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/RouteBuilder.java @@ -17,11 +17,11 @@ package org.finos.tracdap.gateway.builders; -import org.finos.tracdap.common.exception.EUnexpected; import org.finos.tracdap.common.util.RoutingUtils; import org.finos.tracdap.config.*; import org.finos.tracdap.gateway.exec.IRouteMatcher; import org.finos.tracdap.gateway.exec.Route; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -92,17 +92,14 @@ public List buildRoutes(PlatformConfig platformConfig) { private Route buildGrpcServiceRoute(PlatformConfig platformConfig, ServiceInfo serviceInfo) { - if (serviceInfo.descriptor == null) - throw new EUnexpected(); - var routeIndex = nextRouteIndex++; - var routeName = serviceInfo.serviceName; + var routeName = serviceInfo.serviceName(); var routeType = RoutingProtocol.GRPC; - var grpcPath = '/' + serviceInfo.descriptor.getFullName() + "/"; + var grpcPath = '/' + serviceInfo.descriptor().getFullName() + "/"; var matcher = (IRouteMatcher) (method, url) -> url.getPath().startsWith(grpcPath); var protocols = List.of(RoutingProtocol.GRPC, RoutingProtocol.GRPC_WEB); - var routing = RoutingUtils.serviceTarget(platformConfig, serviceInfo.serviceKey); + var routing = RoutingUtils.serviceTarget(platformConfig, serviceInfo.serviceKey()); var match = RoutingMatch.newBuilder() .setPath(grpcPath); @@ -125,20 +122,17 @@ private Route buildGrpcServiceRoute(PlatformConfig platformConfig, ServiceInfo s private Route buildRestServiceRoute(PlatformConfig platformConfig, ServiceInfo serviceInfo) { - if (serviceInfo.descriptor == null || serviceInfo.restPrefix == null) - throw new EUnexpected(); - var routeIndex = nextRouteIndex++; - var routeName = serviceInfo.serviceName; + var routeName = serviceInfo.serviceName(); var routeType = RoutingProtocol.REST; - var restPath = serviceInfo.restPrefix; + var restPath = serviceInfo.restPrefix(); var matcher = (IRouteMatcher) (method, url) -> url.getPath().startsWith(restPath); var protocols = List.of(RoutingProtocol.REST); - var routing = RoutingUtils.serviceTarget(platformConfig, serviceInfo.serviceKey); + var routing = RoutingUtils.serviceTarget(platformConfig, serviceInfo.serviceKey()); var restMethodPrefix = restPath.endsWith("/") ? restPath.substring(0, restPath.length() - 1) : restPath; - var restMethods = RestApiBuilder.buildAllMethods(serviceInfo.descriptor, restMethodPrefix, API_CLASSLOADER); + var restMethods = RestApiBuilder.buildAllMethods(serviceInfo.descriptor(), restMethodPrefix, API_CLASSLOADER); var match = RoutingMatch.newBuilder() .setPath(restPath); @@ -161,17 +155,14 @@ private Route buildRestServiceRoute(PlatformConfig platformConfig, ServiceInfo s private Route buildHttpServiceRoute(PlatformConfig platformConfig, ServiceInfo serviceInfo) { - if (serviceInfo.httpPrefix == null) - throw new EUnexpected(); - var routeIndex = nextRouteIndex++; - var routeName = serviceInfo.serviceName; + var routeName = serviceInfo.serviceName(); var routeType = RoutingProtocol.HTTP; - var httpPath = serviceInfo.httpPrefix; + var httpPath = serviceInfo.httpPrefix(); var matcher = (IRouteMatcher) (method, url) -> url.getPath().startsWith(httpPath); var protocols = List.of(RoutingProtocol.HTTP); - var routing = RoutingUtils.serviceTarget(platformConfig, serviceInfo.serviceKey); + var routing = RoutingUtils.serviceTarget(platformConfig, serviceInfo.serviceKey()); var match = RoutingMatch.newBuilder() .setPath(httpPath); diff --git a/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/ServiceInfo.java b/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/ServiceInfo.java index 41e0c53bc..c49b3afe0 100644 --- a/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/ServiceInfo.java +++ b/tracdap-services/tracdap-gateway/src/main/java/org/finos/tracdap/gateway/builders/ServiceInfo.java @@ -17,7 +17,6 @@ package org.finos.tracdap.gateway.builders; -import com.google.protobuf.Descriptors; import org.finos.tracdap.api.Data; import org.finos.tracdap.api.Metadata; import org.finos.tracdap.api.Orchestrator; @@ -28,6 +27,8 @@ import org.finos.tracdap.config.PlatformConfig; import org.finos.tracdap.config.ServiceConfig; +import com.google.protobuf.Descriptors; + import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -49,48 +50,85 @@ public class ServiceInfo { Map.entry(ConfigKeys.ORCHESTRATOR_SERVICE_KEY, "/trac-orch/"), Map.entry(ConfigKeys.WEB_SERVER_SERVICE_KEY, "/trac-web/")); + private static final Map SERVICE_DESCRIPTORS = Map.ofEntries( + Map.entry(ConfigKeys.METADATA_SERVICE_KEY, serviceDescriptor(Metadata.getDescriptor(), "TracMetadataApi")), + Map.entry(ConfigKeys.DATA_SERVICE_KEY, serviceDescriptor(Data.getDescriptor(), "TracDataApi")), + Map.entry(ConfigKeys.ORCHESTRATOR_SERVICE_KEY, serviceDescriptor(Orchestrator.getDescriptor(), "TracOrchestratorApi"))); + private static final String API_V1_PREFIX = "api/v1/"; - final String serviceKey; - final String serviceName; - final ServiceConfig config; - final Descriptors.ServiceDescriptor descriptor; + private final String serviceKey; + private final String serviceName; + private final ServiceConfig config; + private final Descriptors.ServiceDescriptor descriptor; - final String httpPrefix; - final String restPrefix; + private final String httpPrefix; + private final String restPrefix; public static List buildServiceInfo(PlatformConfig platformConfig) { + var services = new ArrayList(); + + for (var serviceKey : SERVICE_NAMES.keySet()) { + var serviceInfo = buildServiceInfo(platformConfig, serviceKey); + if (serviceInfo != null) + services.add(serviceInfo); + } + + return services; + } + + public static ServiceInfo buildServiceInfo(PlatformConfig platformConfig, String serviceKey) { + var defaultServiceConfig = ServiceConfig.newBuilder().setEnabled(false).build(); - var authConfig = platformConfig.getServicesOrDefault(ConfigKeys.AUTHENTICATION_SERVICE_KEY, defaultServiceConfig); - var metaConfig = platformConfig.getServicesOrDefault(ConfigKeys.METADATA_SERVICE_KEY, defaultServiceConfig); - var dataConfig = platformConfig.getServicesOrDefault(ConfigKeys.DATA_SERVICE_KEY, defaultServiceConfig); - var orchConfig = platformConfig.getServicesOrDefault(ConfigKeys.ORCHESTRATOR_SERVICE_KEY, defaultServiceConfig); - var webConfig = platformConfig.getServicesOrDefault(ConfigKeys.WEB_SERVER_SERVICE_KEY, defaultServiceConfig); + var serviceConfig = platformConfig.getServicesOrDefault(serviceKey, defaultServiceConfig); - var metaDescriptor = serviceDescriptor(Metadata.getDescriptor(), "TracMetadataApi"); - var dataDescriptor = serviceDescriptor(Data.getDescriptor(), "TracDataApi"); - var orchDescriptor = serviceDescriptor(Orchestrator.getDescriptor(), "TracOrchestratorApi"); + if (!isEnabled(serviceConfig)) + return null; - var services = new ArrayList(); + if (SERVICE_DESCRIPTORS.containsKey(serviceKey)) { + var descriptor = SERVICE_DESCRIPTORS.get(serviceKey); + return new ServiceInfo(serviceKey, serviceConfig, descriptor, API_V1_PREFIX); + } + else + return new ServiceInfo(serviceKey, serviceConfig); + } - if (isEnabled(authConfig)) - services.add(new ServiceInfo(ConfigKeys.AUTHENTICATION_SERVICE_KEY, authConfig)); + public boolean hasGrpc() { + return descriptor != null; + } - if (isEnabled(metaConfig)) - services.add(new ServiceInfo(ConfigKeys.METADATA_SERVICE_KEY, metaConfig, metaDescriptor, API_V1_PREFIX)); + public boolean hasRest() { + return restPrefix != null; + } - if (isEnabled(dataConfig)) - services.add(new ServiceInfo(ConfigKeys.DATA_SERVICE_KEY, dataConfig, dataDescriptor, API_V1_PREFIX)); + public boolean hasHttp() { + return httpPrefix != null; + } - if (isEnabled(orchConfig)) - services.add(new ServiceInfo(ConfigKeys.ORCHESTRATOR_SERVICE_KEY, orchConfig, orchDescriptor, API_V1_PREFIX)); + public String serviceKey() { + return serviceKey; + } - if (isEnabled(webConfig)) - services.add(new ServiceInfo(ConfigKeys.WEB_SERVER_SERVICE_KEY, webConfig)); + public String serviceName() { + return serviceName; + } - return services; + public ServiceConfig config() { + return config; + } + + public Descriptors.ServiceDescriptor descriptor() { + return descriptor; + } + + public String httpPrefix() { + return httpPrefix; + } + + public String restPrefix() { + return restPrefix; } private static boolean isEnabled(ServiceConfig serviceConfig) { @@ -146,16 +184,4 @@ private ServiceInfo( this.httpPrefix = null; this.restPrefix = httpPrefix + restPrefix; } - - public boolean hasGrpc() { - return descriptor != null; - } - - public boolean hasRest() { - return restPrefix != null; - } - - public boolean hasHttp() { - return httpPrefix != null; - } }