Skip to content

Commit

Permalink
Use auth service info to set up login redirect path in auth handler s…
Browse files Browse the repository at this point in the history
…ettings
  • Loading branch information
martin-traverse committed Dec 8, 2024
1 parent 3b8043c commit 0289fee
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -92,17 +92,14 @@ public List<Route> 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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<String, Descriptors.ServiceDescriptor> 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<ServiceInfo> buildServiceInfo(PlatformConfig platformConfig) {

var services = new ArrayList<ServiceInfo>();

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<ServiceInfo>();
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) {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 0289fee

Please sign in to comment.