Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the provision of redirecting HTTP requests to HTTPS #675

Open
wants to merge 2 commits into
base: tuning_20190221
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions app/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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();
Expand Down Expand Up @@ -80,11 +97,46 @@ 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");
modifiersField.setAccessible(true);
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})";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/patter/pattern/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After using Guava's HostAndPort.fromString() there was no need of the method so removed the respective method.

Matcher matcher = Pattern.compile(patterForPort).matcher(hostAddress);
if (matcher.find()) {
return matcher.group(2);
}
return INVALID_PORT;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider leveraging Guava's HostAndPort.fromString() instead of manually implementing this logic. I also don't think this regular expression will work if it's a bare IPv6 address.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big thanks Logan, I was looking for such a utility method and couldn't find it so implemented a crude one. Thanks for the review and for reporting this issue.

}