-
Notifications
You must be signed in to change notification settings - Fork 14
Added https support and a docker file #45
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| FROM eclipse-temurin:17-jdk-jammy as builder | ||
|
|
||
| RUN apt update && apt-get install maven -y | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| RUN keytool -genkey -alias sitename -keyalg RSA -keystore keystore.jks -keysize 2048 -storepass 123456 -dname "CN=DD, OU=DD, O=DD, L=DD, S=DD, C=DD" | ||
|
|
||
| COPY pom.xml ./ | ||
| COPY src/ ./src | ||
|
|
||
| RUN mvn clean install | ||
|
|
||
| FROM eclipse-temurin:17-jdk-jammy | ||
|
|
||
| COPY --from=builder /app/target/httpbin-1.3.1-SNAPSHOT-jar-with-dependencies.jar httpbin.jar | ||
| COPY --from=builder /app/keystore.jks /keystore.jks | ||
|
|
||
| CMD ["java", "-jar", "httpbin.jar", "-keystore", "/keystore.jks" ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,36 +17,72 @@ | |
|
|
||
| package org.gaul.httpbin; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.eclipse.jetty.server.Connector; | ||
| import org.eclipse.jetty.server.HttpConfiguration; | ||
| import org.eclipse.jetty.server.HttpConnectionFactory; | ||
| import org.eclipse.jetty.server.SecureRequestCustomizer; | ||
| import org.eclipse.jetty.server.Server; | ||
| import org.eclipse.jetty.server.ServerConnector; | ||
| import org.eclipse.jetty.server.SslConnectionFactory; | ||
| import org.eclipse.jetty.util.ssl.SslContextFactory; | ||
|
|
||
| /** | ||
| * Reimplementation of HttpBin https://httpbin.org/ suitable for offline unit | ||
| * tests. | ||
| */ | ||
| public final class HttpBin { | ||
| private final Server server; | ||
| private final int mHTTPPort; | ||
| private final int mHTTPsPort; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not use the |
||
|
|
||
| public HttpBin(URI endpoint) throws Exception { | ||
| this(endpoint, new HttpBinHandler()); | ||
| public HttpBin(String ip, int httpPort, int httpsPort, String keystore) throws Exception { | ||
| this(ip, httpPort, httpsPort, keystore, new HttpBinHandler()); | ||
| } | ||
|
|
||
| public HttpBin(URI endpoint, HttpBinHandler handler) throws Exception { | ||
| requireNonNull(endpoint); | ||
| public HttpBin(String ip, int httpPort, int httpsPort, String keystore, HttpBinHandler handler) throws Exception { | ||
|
|
||
| server = new Server(); | ||
| HttpConnectionFactory httpConnectionFactory = | ||
| new HttpConnectionFactory(); | ||
| ServerConnector connector = new ServerConnector(server, | ||
|
|
||
| mHTTPPort = httpPort; | ||
| mHTTPsPort = httpsPort; | ||
| List<Connector> connectors = new ArrayList<Connector>(); | ||
| if (httpPort != 0) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to support the 0 port for automatic assignment and use something like -1 to mean do not use HTTP? |
||
| ServerConnector connector = new ServerConnector(server, | ||
| httpConnectionFactory); | ||
| connector.setHost(endpoint.getHost()); | ||
| connector.setPort(endpoint.getPort()); | ||
| server.addConnector(connector); | ||
| connector.setHost(ip); | ||
| connector.setPort(httpPort); | ||
| connectors.add(connector); | ||
| } | ||
|
|
||
| if (httpsPort != 0) { | ||
| HttpConfiguration https = new HttpConfiguration(); | ||
| SecureRequestCustomizer src = new SecureRequestCustomizer(); | ||
| src.setSniHostCheck(false); | ||
| https.addCustomizer(src); | ||
| SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); | ||
| sslContextFactory.setKeyStorePath(keystore); | ||
| sslContextFactory.setKeyStorePassword("123456"); | ||
| sslContextFactory.setKeyManagerPassword("123456"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the best way to configure the keystore? Shouldn't it come from some external source like S3Proxy does? |
||
|
|
||
| ServerConnector sslConnector = new ServerConnector(server, | ||
| new SslConnectionFactory(sslContextFactory, "http/1.1"), | ||
| new HttpConnectionFactory(https)); | ||
| sslConnector.setHost(ip); | ||
| sslConnector.setPort(httpsPort); | ||
| connectors.add(sslConnector); | ||
| } | ||
|
|
||
| if (connectors.size() == 0) { | ||
| throw new Exception("At least one of ports must be set"); | ||
| } | ||
| Connector[] customs = new Connector[connectors.size()]; | ||
| connectors.toArray(customs); | ||
| server.setConnectors(customs); | ||
| server.setHandler(handler); | ||
| } | ||
|
|
||
|
|
@@ -58,7 +94,11 @@ public void stop() throws Exception { | |
| server.stop(); | ||
| } | ||
|
|
||
| public int getPort() { | ||
| return ((ServerConnector) server.getConnectors()[0]).getLocalPort(); | ||
| public int getHTTPPort() { | ||
| return mHTTPPort; | ||
| } | ||
|
|
||
| public int getHTTPsPort() { | ||
| return mHTTPsPort; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,18 +17,54 @@ | |
|
|
||
| package org.gaul.httpbin; | ||
|
|
||
| import java.net.URI; | ||
| import org.apache.commons.cli.CommandLine; | ||
| import org.apache.commons.cli.CommandLineParser; | ||
| import org.apache.commons.cli.DefaultParser; | ||
| import org.apache.commons.cli.HelpFormatter; | ||
| import org.apache.commons.cli.Option; | ||
| import org.apache.commons.cli.Options; | ||
| import org.apache.commons.cli.ParseException; | ||
|
|
||
| public final class Main { | ||
| private Main() { | ||
| throw new AssertionError("intentionally not implemented"); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| // TODO: configurable | ||
| URI httpBinEndpoint = URI.create("http://127.0.0.1:8080"); | ||
| Options options = new Options(); | ||
| options.addOption(Option.builder("p") | ||
| .longOpt("port") | ||
| .hasArg() | ||
| .desc("http port") | ||
| .build()); | ||
| options.addOption(Option.builder("s") | ||
| .longOpt("tls-port") | ||
| .hasArg() | ||
| .desc("https port") | ||
| .build()); | ||
| options.addOption(Option.builder("ip") | ||
| .hasArg() | ||
| .desc("ip to listen on") | ||
| .build()); | ||
| options.addOption(Option.builder("keystore") | ||
| .hasArg() | ||
| .required() | ||
| .build()); | ||
| CommandLineParser parser = new DefaultParser(); | ||
| try { | ||
| CommandLine cmd = parser.parse(options, args); | ||
| int httpPort = Integer.parseInt(cmd.getOptionValue("port", "8080")); | ||
| int httpsPort = Integer.parseInt(cmd.getOptionValue("tls-port", "8443")); | ||
| String ip = cmd.getOptionValue("ip", "0.0.0.0"); | ||
| String keystore = cmd.getOptionValue("keystore"); | ||
|
|
||
| HttpBin httpBin = new HttpBin(httpBinEndpoint); | ||
| httpBin.start(); | ||
| HttpBin httpBin = new HttpBin(ip, httpPort, httpsPort, keystore); | ||
| httpBin.start(); | ||
| } catch (ParseException e) { | ||
| System.err.println("Error parsing command line options: " + e.getMessage()); | ||
| HelpFormatter formatter = new HelpFormatter(); | ||
| formatter.printHelp("myapp", options); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should "myapp" be "httpbin" or |
||
| System.exit(1); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,13 +43,13 @@ public final class HttpBinTest { | |
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| httpBin = new HttpBin(httpBinEndpoint); | ||
| httpBin = new HttpBin("127.0.0.1", 8001, 0, ""); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the intent here? Previously the tests used a zero port which selects a free port so it does not conflict with other running services. Please revert. |
||
| httpBin.start(); | ||
|
|
||
| // reset endpoint to handle zero port | ||
| httpBinEndpoint = new URI(httpBinEndpoint.getScheme(), | ||
| httpBinEndpoint.getUserInfo(), httpBinEndpoint.getHost(), | ||
| httpBin.getPort(), httpBinEndpoint.getPath(), | ||
| httpBin.getHTTPPort(), httpBinEndpoint.getPath(), | ||
| httpBinEndpoint.getQuery(), httpBinEndpoint.getFragment()); | ||
| logger.debug("HttpBin listening on {}", httpBinEndpoint); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a Dockerfile in a separate PR. But I am concerned with using such an old JDK instead of something more recent and streamlined like S3Proxy does:
https://github.com/gaul/s3proxy/blob/master/Dockerfile