From a8ba1de612e9b1cf009f78ef3ea24e8d97355d81 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 28 Sep 2020 16:11:47 -0500 Subject: [PATCH] Issue #5362 - ProxyServletTest additions for server backend TLS Signed-off-by: Joakim Erdfelt --- .../eclipse/jetty/proxy/ProxyServletTest.java | 103 +++++++++++++++--- 1 file changed, 87 insertions(+), 16 deletions(-) diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java index eac3637571dc..d3e2de15e623 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java @@ -76,17 +76,21 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeaderValue; import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.HttpScheme; import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.StringUtil; +import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -127,6 +131,7 @@ public static Stream impls() private AbstractProxyServlet proxyServlet; private Server server; private ServerConnector serverConnector; + private ServerConnector tlsServerConnector; private void startServer(HttpServlet servlet) throws Exception { @@ -136,6 +141,16 @@ private void startServer(HttpServlet servlet) throws Exception serverConnector = new ServerConnector(server); server.addConnector(serverConnector); + SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); + String keyStorePath = MavenTestingUtils.getTestResourceFile("server_keystore.p12").getAbsolutePath(); + sslContextFactory.setKeyStorePath(keyStorePath); + sslContextFactory.setKeyStorePassword("storepwd"); + tlsServerConnector = new ServerConnector(server, new SslConnectionFactory( + sslContextFactory, + HttpVersion.HTTP_1_1.asString()), + new HttpConnectionFactory()); + server.addConnector(tlsServerConnector); + ServletContextHandler appCtx = new ServletContextHandler(server, "/", true, false); ServletHolder appServletHolder = new ServletHolder(servlet); appCtx.addServlet(appServletHolder, "/*"); @@ -730,27 +745,80 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se public static Stream transparentImpls() { return Stream.of( - ProxyServlet.Transparent.class, - AsyncProxyServlet.Transparent.class, - AsyncMiddleManServlet.Transparent.class + new ProxyServlet.Transparent() + { + @Override + protected HttpClient newHttpClient() + { + return newTrustAllClient(super.newHttpClient()); + } + + @Override + public String toString() + { + return ProxyServlet.Transparent.class.getName(); + } + }, + new AsyncProxyServlet.Transparent() + { + @Override + protected HttpClient newHttpClient() + { + return newTrustAllClient(super.newHttpClient()); + } + + @Override + public String toString() + { + return AsyncProxyServlet.Transparent.class.getName(); + } + }, + new AsyncMiddleManServlet.Transparent() + { + @Override + protected HttpClient newHttpClient() + { + return newTrustAllClient(super.newHttpClient()); + } + + @Override + public String toString() + { + return AsyncMiddleManServlet.Transparent.class.getName(); + } + } ).map(Arguments::of); } + private static HttpClient newTrustAllClient(HttpClient client) + { + SslContextFactory sslContextFactory = client.getSslContextFactory(); + sslContextFactory.setTrustAll(true); + return client; + } + + @ParameterizedTest + @MethodSource("transparentImpls") + public void testTransparentProxy(AbstractProxyServlet proxyServletClass) throws Exception + { + testTransparentProxyWithPrefix(proxyServletClass, "http", "/proxy"); + } + @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxy(Class proxyServletClass) throws Exception + public void testTransparentProxyTls(AbstractProxyServlet proxyServletClass) throws Exception { - testTransparentProxyWithPrefix(proxyServletClass, "/proxy"); + testTransparentProxyWithPrefix(proxyServletClass, "https", "/proxy"); } @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxyWithRootContext(Class proxyServletClass) throws Exception + public void testTransparentProxyWithRootContext(AbstractProxyServlet proxyServletClass) throws Exception { - testTransparentProxyWithPrefix(proxyServletClass, "/"); + testTransparentProxyWithPrefix(proxyServletClass, "http", "/"); } - private void testTransparentProxyWithPrefix(Class proxyServletClass, String prefix) throws Exception + private void testTransparentProxyWithPrefix(AbstractProxyServlet proxyServletClass, String scheme, String prefix) throws Exception { final String target = "/test"; startServer(new HttpServlet() @@ -763,7 +831,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se resp.setStatus(target.equals(req.getRequestURI()) ? 200 : 404); } }); - String proxyTo = "http://localhost:" + serverConnector.getLocalPort(); + int serverPort = serverConnector.getLocalPort(); + if (HttpScheme.HTTPS.is(scheme)) + serverPort = tlsServerConnector.getLocalPort(); + String proxyTo = scheme + "://localhost:" + serverPort; Map params = new HashMap<>(); params.put("proxyTo", proxyTo); params.put("prefix", prefix); @@ -781,33 +852,33 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxyWithQuery(Class proxyServletClass) throws Exception + public void testTransparentProxyWithQuery(AbstractProxyServlet proxyServletClass) throws Exception { testTransparentProxyWithQuery(proxyServletClass, "/foo", "/proxy", "/test"); } @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxyEmptyContextWithQuery(Class proxyServletClass) throws Exception + public void testTransparentProxyEmptyContextWithQuery(AbstractProxyServlet proxyServletClass) throws Exception { testTransparentProxyWithQuery(proxyServletClass, "", "/proxy", "/test"); } @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxyEmptyTargetWithQuery(Class proxyServletClass) throws Exception + public void testTransparentProxyEmptyTargetWithQuery(AbstractProxyServlet proxyServletClass) throws Exception { testTransparentProxyWithQuery(proxyServletClass, "/bar", "/proxy", ""); } @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxyEmptyContextEmptyTargetWithQuery(Class proxyServletClass) throws Exception + public void testTransparentProxyEmptyContextEmptyTargetWithQuery(AbstractProxyServlet proxyServletClass) throws Exception { testTransparentProxyWithQuery(proxyServletClass, "", "/proxy", ""); } - private void testTransparentProxyWithQuery(Class proxyServletClass, String proxyToContext, String prefix, String target) throws Exception + private void testTransparentProxyWithQuery(AbstractProxyServlet proxyServletClass, String proxyToContext, String prefix, String target) throws Exception { final String query = "a=1&b=2"; startServer(new HttpServlet() @@ -851,7 +922,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxyWithQueryWithSpaces(Class proxyServletClass) throws Exception + public void testTransparentProxyWithQueryWithSpaces(AbstractProxyServlet proxyServletClass) throws Exception { final String target = "/test"; final String query = "a=1&b=2&c=1234%205678&d=hello+world"; @@ -893,7 +964,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se @ParameterizedTest @MethodSource("transparentImpls") - public void testTransparentProxyWithoutPrefix(Class proxyServletClass) throws Exception + public void testTransparentProxyWithoutPrefix(AbstractProxyServlet proxyServletClass) throws Exception { final String target = "/test"; startServer(new HttpServlet()