| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | package controllers;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  | import static auth.AuthUtils.ACTOR;
 | 
					
						
							|  |  |  | import static auth.AuthUtils.SESSION_COOKIE_GMS_TOKEN_NAME;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import akka.actor.ActorSystem;
 | 
					
						
							|  |  |  | import akka.stream.ActorMaterializer;
 | 
					
						
							|  |  |  | import akka.stream.Materializer;
 | 
					
						
							|  |  |  | import akka.util.ByteString;
 | 
					
						
							|  |  |  | import auth.Authenticator;
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  | import com.datahub.authentication.AuthenticationConstants;
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  | import com.fasterxml.jackson.databind.JsonNode;
 | 
					
						
							|  |  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import com.linkedin.util.Pair;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import com.typesafe.config.Config;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  | import java.io.InputStream;
 | 
					
						
							| 
									
										
										
										
											2024-01-22 11:46:04 -06:00
										 |  |  | import java.net.URI;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  | import java.time.Duration;
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  | import java.time.Instant;
 | 
					
						
							| 
									
										
										
										
											2022-09-09 23:13:30 +02:00
										 |  |  | import java.util.List;
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import java.util.Map;
 | 
					
						
							|  |  |  | import java.util.Optional;
 | 
					
						
							|  |  |  | import java.util.concurrent.CompletableFuture;
 | 
					
						
							|  |  |  | import java.util.concurrent.ExecutionException;
 | 
					
						
							|  |  |  | import java.util.stream.Collectors;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  | import javax.annotation.Nonnull;
 | 
					
						
							|  |  |  | import javax.annotation.Nullable;
 | 
					
						
							|  |  |  | import javax.inject.Inject;
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  | import org.slf4j.Logger;
 | 
					
						
							|  |  |  | import org.slf4j.LoggerFactory;
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  | import play.Environment;
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import play.http.HttpEntity;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  | import play.libs.Json;
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import play.libs.ws.InMemoryBodyWritable;
 | 
					
						
							|  |  |  | import play.libs.ws.StandaloneWSClient;
 | 
					
						
							|  |  |  | import play.libs.ws.ahc.StandaloneAhcWSClient;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import play.mvc.Controller;
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import play.mvc.Http;
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  | import play.mvc.Http.Cookie;
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import play.mvc.ResponseHeader;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import play.mvc.Result;
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | import play.mvc.Security;
 | 
					
						
							|  |  |  | import play.shaded.ahc.org.asynchttpclient.AsyncHttpClient;
 | 
					
						
							|  |  |  | import play.shaded.ahc.org.asynchttpclient.AsyncHttpClientConfig;
 | 
					
						
							|  |  |  | import play.shaded.ahc.org.asynchttpclient.DefaultAsyncHttpClient;
 | 
					
						
							|  |  |  | import play.shaded.ahc.org.asynchttpclient.DefaultAsyncHttpClientConfig;
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  | import utils.ConfigUtil;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | public class Application extends Controller {
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |   private static final Logger logger = LoggerFactory.getLogger(Application.class.getName());
 | 
					
						
							|  |  |  |   private final Config config;
 | 
					
						
							|  |  |  |   private final StandaloneWSClient ws;
 | 
					
						
							|  |  |  |   private final Environment environment;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @Inject
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |   public Application(Environment environment, @Nonnull Config config) {
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     this.config = config;
 | 
					
						
							|  |  |  |     ws = createWsClient();
 | 
					
						
							|  |  |  |     this.environment = environment;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Serves the build output index.html for any given path
 | 
					
						
							|  |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * @param path takes a path string, which essentially is ignored routing is managed client side
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |    * @return {Result} build output index.html resource
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   @Nonnull
 | 
					
						
							|  |  |  |   private Result serveAsset(@Nullable String path) {
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  |     try {
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |       InputStream indexHtml = environment.resourceAsStream("public/index.html");
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |       return ok(indexHtml).withHeader("Cache-Control", "no-cache").as("text/html");
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  |     } catch (Exception e) {
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |       logger.warn("Cannot load public/index.html resource. Static assets or assets jar missing?");
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |       return notFound().withHeader("Cache-Control", "no-cache").as("text/html");
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @Nonnull
 | 
					
						
							|  |  |  |   public Result healthcheck() {
 | 
					
						
							|  |  |  |     return ok("GOOD");
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * index Action proxies to serveAsset
 | 
					
						
							|  |  |  |    *
 | 
					
						
							|  |  |  |    * @param path takes a path string which is either index.html or the path segment after /
 | 
					
						
							|  |  |  |    * @return {Result} response from serveAsset method
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   @Nonnull
 | 
					
						
							|  |  |  |   public Result index(@Nullable String path) {
 | 
					
						
							|  |  |  |     return serveAsset("");
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 10:53:26 -07:00
										 |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  |    * Proxies requests to the Metadata Service
 | 
					
						
							|  |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * <p>TODO: Investigate using mutual SSL authentication to call Metadata Service.
 | 
					
						
							| 
									
										
										
										
											2019-09-05 10:53:26 -07:00
										 |  |  |    */
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  |   @Security.Authenticated(Authenticator.class)
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |   public CompletableFuture<Result> proxy(String path, Http.Request request)
 | 
					
						
							|  |  |  |       throws ExecutionException, InterruptedException {
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |     final String authorizationHeaderValue = getAuthorizationHeaderValueToProxy(request);
 | 
					
						
							|  |  |  |     final String resolvedUri = mapPath(request.uri());
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |     final String metadataServiceHost =
 | 
					
						
							|  |  |  |         ConfigUtil.getString(
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |             config,
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |             ConfigUtil.METADATA_SERVICE_HOST_CONFIG_PATH,
 | 
					
						
							|  |  |  |             ConfigUtil.DEFAULT_METADATA_SERVICE_HOST);
 | 
					
						
							|  |  |  |     final int metadataServicePort =
 | 
					
						
							|  |  |  |         ConfigUtil.getInt(
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |             config,
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |             ConfigUtil.METADATA_SERVICE_PORT_CONFIG_PATH,
 | 
					
						
							|  |  |  |             ConfigUtil.DEFAULT_METADATA_SERVICE_PORT);
 | 
					
						
							|  |  |  |     final boolean metadataServiceUseSsl =
 | 
					
						
							|  |  |  |         ConfigUtil.getBoolean(
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |             config,
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |             ConfigUtil.METADATA_SERVICE_USE_SSL_CONFIG_PATH,
 | 
					
						
							|  |  |  |             ConfigUtil.DEFAULT_METADATA_SERVICE_USE_SSL);
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // TODO: Fully support custom internal SSL.
 | 
					
						
							|  |  |  |     final String protocol = metadataServiceUseSsl ? "https" : "http";
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |     final Map<String, List<String>> headers = request.getHeaders().toMap();
 | 
					
						
							| 
									
										
										
										
											2022-09-09 23:13:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |     if (headers.containsKey(Http.HeaderNames.HOST)
 | 
					
						
							|  |  |  |         && !headers.containsKey(Http.HeaderNames.X_FORWARDED_HOST)) {
 | 
					
						
							|  |  |  |       headers.put(Http.HeaderNames.X_FORWARDED_HOST, headers.get(Http.HeaderNames.HOST));
 | 
					
						
							| 
									
										
										
										
											2022-09-09 23:13:30 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-22 11:46:04 -06:00
										 |  |  |     if (!headers.containsKey(Http.HeaderNames.X_FORWARDED_PROTO)) {
 | 
					
						
							|  |  |  |       final String schema =
 | 
					
						
							|  |  |  |           Optional.ofNullable(URI.create(request.uri()).getScheme()).orElse("http");
 | 
					
						
							|  |  |  |       headers.put(Http.HeaderNames.X_FORWARDED_PROTO, List.of(schema));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  |     // Get the current time to measure the duration of the request
 | 
					
						
							|  |  |  |     Instant start = Instant.now();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     return ws.url(
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |             String.format(
 | 
					
						
							|  |  |  |                 "%s://%s:%s%s", protocol, metadataServiceHost, metadataServicePort, resolvedUri))
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |         .setMethod(request.method())
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |         .setHeaders(
 | 
					
						
							|  |  |  |             headers.entrySet().stream()
 | 
					
						
							|  |  |  |                 // Remove X-DataHub-Actor to prevent malicious delegation.
 | 
					
						
							|  |  |  |                 .filter(
 | 
					
						
							|  |  |  |                     entry ->
 | 
					
						
							|  |  |  |                         !AuthenticationConstants.LEGACY_X_DATAHUB_ACTOR_HEADER.equalsIgnoreCase(
 | 
					
						
							|  |  |  |                             entry.getKey()))
 | 
					
						
							|  |  |  |                 .filter(entry -> !Http.HeaderNames.CONTENT_LENGTH.equalsIgnoreCase(entry.getKey()))
 | 
					
						
							|  |  |  |                 .filter(entry -> !Http.HeaderNames.CONTENT_TYPE.equalsIgnoreCase(entry.getKey()))
 | 
					
						
							|  |  |  |                 .filter(entry -> !Http.HeaderNames.AUTHORIZATION.equalsIgnoreCase(entry.getKey()))
 | 
					
						
							|  |  |  |                 // Remove Host s.th. service meshes do not route to wrong host
 | 
					
						
							|  |  |  |                 .filter(entry -> !Http.HeaderNames.HOST.equalsIgnoreCase(entry.getKey()))
 | 
					
						
							|  |  |  |                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |         .addHeader(Http.HeaderNames.AUTHORIZATION, authorizationHeaderValue)
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |         .addHeader(
 | 
					
						
							|  |  |  |             AuthenticationConstants.LEGACY_X_DATAHUB_ACTOR_HEADER, getDataHubActorHeader(request))
 | 
					
						
							|  |  |  |         .setBody(
 | 
					
						
							|  |  |  |             new InMemoryBodyWritable(
 | 
					
						
							|  |  |  |                 ByteString.fromByteBuffer(request.body().asBytes().asByteBuffer()),
 | 
					
						
							| 
									
										
										
										
											2024-06-22 09:10:11 -05:00
										 |  |  |                 request.contentType().orElse("application/json")))
 | 
					
						
							| 
									
										
										
										
											2022-03-04 16:10:25 -08:00
										 |  |  |         .setRequestTimeout(Duration.ofSeconds(120))
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  |         .execute()
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |         .thenApply(
 | 
					
						
							|  |  |  |             apiResponse -> {
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |               // Log the query if it takes longer than the configured threshold and verbose logging
 | 
					
						
							|  |  |  |               // is enabled
 | 
					
						
							|  |  |  |               boolean verboseGraphQLLogging = config.getBoolean("graphql.verbose.logging");
 | 
					
						
							|  |  |  |               int verboseGraphQLLongQueryMillis = config.getInt("graphql.verbose.slowQueryMillis");
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  |               Instant finish = Instant.now();
 | 
					
						
							|  |  |  |               long timeElapsed = Duration.between(start, finish).toMillis();
 | 
					
						
							|  |  |  |               if (verboseGraphQLLogging && timeElapsed >= verboseGraphQLLongQueryMillis) {
 | 
					
						
							|  |  |  |                 logSlowQuery(request, resolvedUri, timeElapsed);
 | 
					
						
							|  |  |  |               }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |               final ResponseHeader header =
 | 
					
						
							|  |  |  |                   new ResponseHeader(
 | 
					
						
							|  |  |  |                       apiResponse.getStatus(),
 | 
					
						
							|  |  |  |                       apiResponse.getHeaders().entrySet().stream()
 | 
					
						
							|  |  |  |                           .filter(
 | 
					
						
							|  |  |  |                               entry ->
 | 
					
						
							|  |  |  |                                   !Http.HeaderNames.CONTENT_LENGTH.equalsIgnoreCase(entry.getKey()))
 | 
					
						
							|  |  |  |                           .filter(
 | 
					
						
							|  |  |  |                               entry ->
 | 
					
						
							|  |  |  |                                   !Http.HeaderNames.CONTENT_TYPE.equalsIgnoreCase(entry.getKey()))
 | 
					
						
							|  |  |  |                           .map(entry -> Pair.of(entry.getKey(), String.join(";", entry.getValue())))
 | 
					
						
							|  |  |  |                           .collect(Collectors.toMap(Pair::getFirst, Pair::getSecond)));
 | 
					
						
							|  |  |  |               final HttpEntity body =
 | 
					
						
							|  |  |  |                   new HttpEntity.Strict(
 | 
					
						
							|  |  |  |                       apiResponse.getBodyAsBytes(),
 | 
					
						
							|  |  |  |                       Optional.ofNullable(apiResponse.getContentType()));
 | 
					
						
							|  |  |  |               return new Result(header, body);
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |         .toCompletableFuture();
 | 
					
						
							| 
									
										
										
										
											2019-09-05 10:53:26 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Creates a wrapping ObjectNode containing config information
 | 
					
						
							|  |  |  |    *
 | 
					
						
							|  |  |  |    * @return Http Result instance with app configuration attributes
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   @Nonnull
 | 
					
						
							|  |  |  |   public Result appConfig() {
 | 
					
						
							|  |  |  |     final ObjectNode config = Json.newObject();
 | 
					
						
							| 
									
										
										
										
											2022-01-20 08:21:47 +05:30
										 |  |  |     config.put("application", "datahub-frontend");
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     config.put("appVersion", this.config.getString("app.version"));
 | 
					
						
							|  |  |  |     config.put("isInternal", this.config.getBoolean("linkedin.internal"));
 | 
					
						
							|  |  |  |     config.put("shouldShowDatasetLineage", this.config.getBoolean("linkedin.show.dataset.lineage"));
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |     config.put(
 | 
					
						
							|  |  |  |         "suggestionConfidenceThreshold",
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |         Integer.valueOf(this.config.getString("linkedin.suggestion.confidence.threshold")));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     config.set("wikiLinks", wikiLinks());
 | 
					
						
							|  |  |  |     config.set("tracking", trackingInfo());
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |     // In a staging environment, we can trigger this flag to be true so that the UI can handle based
 | 
					
						
							|  |  |  |     // on
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     // such config and alert users that their changes will not affect production data
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     config.put("isStagingBanner", this.config.getBoolean("ui.show.staging.banner"));
 | 
					
						
							|  |  |  |     config.put("isLiveDataWarning", this.config.getBoolean("ui.show.live.data.banner"));
 | 
					
						
							|  |  |  |     config.put("showChangeManagement", this.config.getBoolean("ui.show.CM.banner"));
 | 
					
						
							| 
									
										
										
										
											2019-09-05 10:53:26 -07:00
										 |  |  |     // Flag to enable people entity elements
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     config.put("showPeople", this.config.getBoolean("ui.show.people"));
 | 
					
						
							|  |  |  |     config.put("changeManagementLink", this.config.getString("ui.show.CM.link"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     // Flag set in order to warn users that search is experiencing issues
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     config.put("isStaleSearch", this.config.getBoolean("ui.show.stale.search"));
 | 
					
						
							|  |  |  |     config.put("showAdvancedSearch", this.config.getBoolean("ui.show.advanced.search"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     // Flag to use the new api for browsing datasets
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     config.put("useNewBrowseDataset", this.config.getBoolean("ui.new.browse.dataset"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     // show lineage graph in relationships tabs
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     config.put("showLineageGraph", this.config.getBoolean("ui.show.lineage.graph"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     // show institutional memory for available entities
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     config.put("showInstitutionalMemory", this.config.getBoolean("ui.show.institutional.memory"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Insert properties for user profile operations
 | 
					
						
							|  |  |  |     config.set("userEntityProps", userEntityProps());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     final ObjectNode response = Json.newObject();
 | 
					
						
							|  |  |  |     response.put("status", "ok");
 | 
					
						
							|  |  |  |     response.set("config", config);
 | 
					
						
							|  |  |  |     return ok(response);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Creates a JSON object of profile / avatar properties
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |    * @return Json avatar / profile image properties
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   @Nonnull
 | 
					
						
							|  |  |  |   private ObjectNode userEntityProps() {
 | 
					
						
							|  |  |  |     final ObjectNode props = Json.newObject();
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     props.put("aviUrlPrimary", config.getString("linkedin.links.avi.urlPrimary"));
 | 
					
						
							|  |  |  |     props.put("aviUrlFallback", config.getString("linkedin.links.avi.urlFallback"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     return props;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * @return Json object with internal wiki links
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   @Nonnull
 | 
					
						
							|  |  |  |   private ObjectNode wikiLinks() {
 | 
					
						
							|  |  |  |     final ObjectNode wikiLinks = Json.newObject();
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     wikiLinks.put("appHelp", config.getString("links.wiki.appHelp"));
 | 
					
						
							|  |  |  |     wikiLinks.put("gdprPii", config.getString("links.wiki.gdprPii"));
 | 
					
						
							|  |  |  |     wikiLinks.put("tmsSchema", config.getString("links.wiki.tmsSchema"));
 | 
					
						
							|  |  |  |     wikiLinks.put("gdprTaxonomy", config.getString("links.wiki.gdprTaxonomy"));
 | 
					
						
							|  |  |  |     wikiLinks.put("staleSearchIndex", config.getString("links.wiki.staleSearchIndex"));
 | 
					
						
							|  |  |  |     wikiLinks.put("dht", config.getString("links.wiki.dht"));
 | 
					
						
							|  |  |  |     wikiLinks.put("purgePolicies", config.getString("links.wiki.purgePolicies"));
 | 
					
						
							|  |  |  |     wikiLinks.put("jitAcl", config.getString("links.wiki.jitAcl"));
 | 
					
						
							|  |  |  |     wikiLinks.put("metadataCustomRegex", config.getString("links.wiki.metadataCustomRegex"));
 | 
					
						
							|  |  |  |     wikiLinks.put("exportPolicy", config.getString("links.wiki.exportPolicy"));
 | 
					
						
							|  |  |  |     wikiLinks.put("metadataHealth", config.getString("links.wiki.metadataHealth"));
 | 
					
						
							|  |  |  |     wikiLinks.put("purgeKey", config.getString("links.wiki.purgeKey"));
 | 
					
						
							|  |  |  |     wikiLinks.put("datasetDecommission", config.getString("links.wiki.datasetDecommission"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     return wikiLinks;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * @return Json object containing the tracking configuration details
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   @Nonnull
 | 
					
						
							|  |  |  |   private ObjectNode trackingInfo() {
 | 
					
						
							|  |  |  |     final ObjectNode piwik = Json.newObject();
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     piwik.put("piwikSiteId", Integer.valueOf(config.getString("tracking.piwik.siteid")));
 | 
					
						
							|  |  |  |     piwik.put("piwikUrl", config.getString("tracking.piwik.url"));
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     final ObjectNode trackers = Json.newObject();
 | 
					
						
							|  |  |  |     trackers.set("piwik", piwik);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     final ObjectNode trackingConfig = Json.newObject();
 | 
					
						
							|  |  |  |     trackingConfig.set("trackers", trackers);
 | 
					
						
							|  |  |  |     trackingConfig.put("isEnabled", true);
 | 
					
						
							|  |  |  |     return trackingConfig;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private StandaloneWSClient createWsClient() {
 | 
					
						
							|  |  |  |     final String name = "proxyClient";
 | 
					
						
							|  |  |  |     ActorSystem system = ActorSystem.create(name);
 | 
					
						
							|  |  |  |     system.registerOnTermination(() -> System.exit(0));
 | 
					
						
							|  |  |  |     Materializer materializer = ActorMaterializer.create(system);
 | 
					
						
							|  |  |  |     AsyncHttpClientConfig asyncHttpClientConfig =
 | 
					
						
							|  |  |  |         new DefaultAsyncHttpClientConfig.Builder()
 | 
					
						
							| 
									
										
										
										
											2023-03-09 20:32:00 -08:00
										 |  |  |             .setDisableUrlEncodingForBoundRequests(true)
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  |             .setMaxRequestRetry(0)
 | 
					
						
							|  |  |  |             .setShutdownQuietPeriod(0)
 | 
					
						
							|  |  |  |             .setShutdownTimeout(0)
 | 
					
						
							|  |  |  |             .build();
 | 
					
						
							|  |  |  |     AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(asyncHttpClientConfig);
 | 
					
						
							|  |  |  |     return new StandaloneAhcWSClient(asyncHttpClient, materializer);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2021-11-08 13:37:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * Returns the value of the Authorization Header to be provided when proxying requests to the
 | 
					
						
							|  |  |  |    * downstream Metadata Service.
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * <p>Currently, the Authorization header value may be derived from
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * <p>a) The value of the "token" attribute of the Session Cookie provided by the client. This
 | 
					
						
							|  |  |  |    * value is set when creating the session token initially from a token granted by the Metadata
 | 
					
						
							|  |  |  |    * Service.
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * <p>Or if the "token" attribute cannot be found in a session cookie, then we fallback to
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * <p>b) The value of the Authorization header provided in the original request. This will be used
 | 
					
						
							|  |  |  |    * in cases where clients are making programmatic requests to Metadata Service APIs directly,
 | 
					
						
							|  |  |  |    * without providing a session cookie (ui only).
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * <p>If neither are found, an empty string is returned.
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    */
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |   private String getAuthorizationHeaderValueToProxy(Http.Request request) {
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |     // If the session cookie has an authorization token, use that. If there's an authorization
 | 
					
						
							|  |  |  |     // header provided, simply
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |     // use that.
 | 
					
						
							|  |  |  |     String value = "";
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |     if (request.session().data().containsKey(SESSION_COOKIE_GMS_TOKEN_NAME)) {
 | 
					
						
							|  |  |  |       value = "Bearer " + request.session().data().get(SESSION_COOKIE_GMS_TOKEN_NAME);
 | 
					
						
							|  |  |  |     } else if (request.getHeaders().contains(Http.HeaderNames.AUTHORIZATION)) {
 | 
					
						
							|  |  |  |       value = request.getHeaders().get(Http.HeaderNames.AUTHORIZATION).get();
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  |     return value;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * Returns the value of the legacy X-DataHub-Actor header to forward to the Metadata Service. This
 | 
					
						
							|  |  |  |    * is sent along with any requests that have a valid frontend session cookie to identify the
 | 
					
						
							|  |  |  |    * calling actor, for backwards compatibility.
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |    * <p>If Metadata Service authentication is enabled, this value is not required because Actor
 | 
					
						
							|  |  |  |    * context will most often come from the authentication credentials provided in the Authorization
 | 
					
						
							|  |  |  |    * header.
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |    */
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |   private String getDataHubActorHeader(Http.Request request) {
 | 
					
						
							|  |  |  |     String actor = request.session().data().get(ACTOR);
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:33:14 -08:00
										 |  |  |     return actor == null ? "" : actor;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 13:37:31 -08:00
										 |  |  |   private String mapPath(@Nonnull final String path) {
 | 
					
						
							|  |  |  |     // Case 1: Map legacy GraphQL path to GMS GraphQL API (for compatibility)
 | 
					
						
							|  |  |  |     if (path.equals("/api/v2/graphql")) {
 | 
					
						
							|  |  |  |       return "/api/graphql";
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Case 2: Map requests to /gms to / (Rest.li API)
 | 
					
						
							|  |  |  |     final String gmsApiPath = "/api/gms";
 | 
					
						
							|  |  |  |     if (path.startsWith(gmsApiPath)) {
 | 
					
						
							| 
									
										
										
										
											2022-09-21 14:21:55 -07:00
										 |  |  |       String newPath = path.substring(gmsApiPath.length());
 | 
					
						
							|  |  |  |       if (!newPath.startsWith("/")) {
 | 
					
						
							|  |  |  |         newPath = "/" + newPath;
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |       return newPath;
 | 
					
						
							| 
									
										
										
										
											2021-11-08 13:37:31 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Otherwise, return original path
 | 
					
						
							|  |  |  |     return path;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |    * Called if verbose logging is enabled and request takes longer that the slow query milliseconds
 | 
					
						
							|  |  |  |    * defined in the config
 | 
					
						
							|  |  |  |    *
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  |    * @param request GraphQL request that was made
 | 
					
						
							|  |  |  |    * @param resolvedUri URI that was requested
 | 
					
						
							|  |  |  |    * @param duration How long the query took to complete
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   private void logSlowQuery(Http.Request request, String resolvedUri, float duration) {
 | 
					
						
							|  |  |  |     StringBuilder jsonBody = new StringBuilder();
 | 
					
						
							|  |  |  |     Optional<Cookie> actorCookie = request.getCookie("actor");
 | 
					
						
							|  |  |  |     String actorValue = actorCookie.isPresent() ? actorCookie.get().value() : "N/A";
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 16:10:46 -04:00
										 |  |  |     // Get the JSON body
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  |     try {
 | 
					
						
							|  |  |  |       ObjectMapper mapper = new ObjectMapper();
 | 
					
						
							|  |  |  |       JsonNode jsonNode = request.body().asJson();
 | 
					
						
							|  |  |  |       ((ObjectNode) jsonNode).remove("query");
 | 
					
						
							| 
									
										
										
										
											2025-04-02 16:10:46 -04:00
										 |  |  |       jsonBody.append(mapper.writeValueAsString(jsonNode));
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     } catch (Exception e) {
 | 
					
						
							|  |  |  |       logger.info("GraphQL Request Received: {}, Unable to parse JSON body", resolvedUri);
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  |     }
 | 
					
						
							|  |  |  |     String jsonBodyStr = jsonBody.toString();
 | 
					
						
							| 
									
										
										
										
											2025-04-02 16:10:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Get the query string
 | 
					
						
							|  |  |  |     StringBuilder query = new StringBuilder();
 | 
					
						
							|  |  |  |     try {
 | 
					
						
							|  |  |  |       ObjectMapper mapper = new ObjectMapper();
 | 
					
						
							|  |  |  |       query.append(mapper.writeValueAsString(request.queryString()));
 | 
					
						
							|  |  |  |     } catch (Exception e) {
 | 
					
						
							|  |  |  |       logger.info("GraphQL Request Received: {}, Unable to parse query string", resolvedUri);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     String queryString = query.toString();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     logger.info(
 | 
					
						
							|  |  |  |         "Slow GraphQL Request Received: {}, Request query string: {}, Request actor: {}, Request JSON: {}, Request completed in {} ms",
 | 
					
						
							|  |  |  |         resolvedUri,
 | 
					
						
							| 
									
										
										
										
											2025-04-02 16:10:46 -04:00
										 |  |  |         queryString,
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |         actorValue,
 | 
					
						
							|  |  |  |         jsonBodyStr,
 | 
					
						
							|  |  |  |         duration);
 | 
					
						
							| 
									
										
										
										
											2024-09-10 13:01:35 -04:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | }
 |