From ba190be21be9f5d97669444ecac7175852ee127a Mon Sep 17 00:00:00 2001 From: ShubhamGupta29 Date: Thu, 9 Apr 2020 05:15:35 +0530 Subject: [PATCH 1/2] Adding the provision of redirecting HTTP requests to HTTPS enabled endpoints --- app/Global.java | 62 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/app/Global.java b/app/Global.java index 9b346e0b5..0246f16e6 100644 --- a/app/Global.java +++ b/app/Global.java @@ -14,17 +14,21 @@ * the License. */ +import com.google.common.base.Strings; import com.linkedin.drelephant.DrElephant; +import com.linkedin.drelephant.ElephantContext; import com.sun.security.sasl.util.AbstractSaslImpl; - -import play.Application; -import play.GlobalSettings; -import play.Logger; - import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.logging.Level; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import play.Application; +import play.GlobalSettings; +import play.Logger; +import play.api.mvc.Handler; +import play.mvc.Http; /** @@ -34,11 +38,24 @@ public class Global extends GlobalSettings { DrElephant _drElephant; + private final String INVALID_PORT = "-1"; + private final String HTTPS_PROTOCOL = "https://"; + private final String HTTP_PORT_CONFIG_NAME = "http_port"; + private final String HTTPS_PORT_CONFIG_NAME = "https_port"; + + private String HTTP_PORT; + private String HTTPS_PORT; + public void onStart(Application app) { Logger.info("Starting Application..."); fixJavaKerberos(); + HTTP_PORT = ElephantContext.instance().getElephnatConf() + .getProperty(HTTP_PORT_CONFIG_NAME); + HTTPS_PORT = ElephantContext.instance().getElephnatConf() + .getProperty(HTTPS_PORT_CONFIG_NAME); + try { _drElephant = new DrElephant(); _drElephant.start(); @@ -80,6 +97,27 @@ private static void fixJavaKerberos() { } + /** + * This method is overridden to re-direct all the requests to HTTP endpoint of Dr.Elephant to + * the respective HTTPS enabled endpoint(If such endpoints exists) + * @param requestHeader Request Header + * @return The Handler to appropriately handle the given request + */ + @Override + public Handler onRouteRequest(Http.RequestHeader requestHeader) { + if (!Strings.isNullOrEmpty(HTTP_PORT) && !Strings.isNullOrEmpty(HTTPS_PORT)) { + String port = INVALID_PORT; + if (!Strings.isNullOrEmpty(requestHeader.host())) { + port = getPortFromHostAddress(requestHeader.host()); + } + if (!port.equals(INVALID_PORT) && port.equals(HTTP_PORT)) { + return controllers.Default.redirect(HTTPS_PROTOCOL + requestHeader.host().replace(port, HTTPS_PORT) + + requestHeader.uri()); + } + } + return super.onRouteRequest(requestHeader); + } + static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); @@ -87,4 +125,18 @@ static void setFinalStatic(Field field, Object newValue) throws Exception { modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } + + /** + * + * @param hostAddress The hostAddress string which will be of format "elephant.abc.com:8080" + * @return Return the port of the of the hostAddress if exists else return INVALID_PORT + */ + private String getPortFromHostAddress(String hostAddress) { + String patterForPort = "(.+):([\\d]{1,5})"; + Matcher matcher = Pattern.compile(patterForPort).matcher(hostAddress); + if (matcher.find()) { + return matcher.group(2); + } + return INVALID_PORT; + } } From 4f00af32b62a36d149d6fdb28b6ebb494a319f58 Mon Sep 17 00:00:00 2001 From: ShubhamGupta29 Date: Fri, 10 Apr 2020 20:23:36 +0530 Subject: [PATCH 2/2] Addressing review comments --- app/Global.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/app/Global.java b/app/Global.java index 0246f16e6..de8ad5e01 100644 --- a/app/Global.java +++ b/app/Global.java @@ -15,6 +15,7 @@ */ import com.google.common.base.Strings; +import com.google.common.net.HostAndPort; import com.linkedin.drelephant.DrElephant; import com.linkedin.drelephant.ElephantContext; import com.sun.security.sasl.util.AbstractSaslImpl; @@ -108,7 +109,9 @@ public Handler onRouteRequest(Http.RequestHeader requestHeader) { if (!Strings.isNullOrEmpty(HTTP_PORT) && !Strings.isNullOrEmpty(HTTPS_PORT)) { String port = INVALID_PORT; if (!Strings.isNullOrEmpty(requestHeader.host())) { - port = getPortFromHostAddress(requestHeader.host()); + HostAndPort hostAndPort = HostAndPort.fromString(requestHeader.host()) + .withDefaultPort(Integer.parseInt(HTTP_PORT)); + port = String.valueOf(hostAndPort.getPort()); } if (!port.equals(INVALID_PORT) && port.equals(HTTP_PORT)) { return controllers.Default.redirect(HTTPS_PROTOCOL + requestHeader.host().replace(port, HTTPS_PORT) @@ -125,18 +128,4 @@ static void setFinalStatic(Field field, Object newValue) throws Exception { modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } - - /** - * - * @param hostAddress The hostAddress string which will be of format "elephant.abc.com:8080" - * @return Return the port of the of the hostAddress if exists else return INVALID_PORT - */ - private String getPortFromHostAddress(String hostAddress) { - String patterForPort = "(.+):([\\d]{1,5})"; - Matcher matcher = Pattern.compile(patterForPort).matcher(hostAddress); - if (matcher.find()) { - return matcher.group(2); - } - return INVALID_PORT; - } }