From 998a4cadf252eef04159b7c8e4385ee2aa6c4424 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 20 Oct 2021 11:12:42 -0400 Subject: [PATCH] Use HTTPS when proxy settings are set (#1986) * Use HTTPS when proxy settings are set Fixes #1965 * Adding test --- pom.xml | 6 +++ spring-cloud-config-server/pom.xml | 5 ++ .../server/proxy/SchemeBasedRoutePlanner.java | 8 +-- .../server/support/HttpClientSupportTest.java | 49 +++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index bacd9189..63aff70a 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,7 @@ 3.0.5-SNAPSHOT 1.11.903 v1-rev20201112-1.30.10 + 2.31.0 true true @@ -88,6 +89,11 @@ google-auth-library-oauth2-http 0.22.0 + + com.github.tomakehurst + wiremock-jre8 + ${wiremock.version} + diff --git a/spring-cloud-config-server/pom.xml b/spring-cloud-config-server/pom.xml index d2597543..924e5700 100644 --- a/spring-cloud-config-server/pom.xml +++ b/spring-cloud-config-server/pom.xml @@ -145,6 +145,11 @@ 0.7.3 test + + com.github.tomakehurst + wiremock-jre8 + test + diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/proxy/SchemeBasedRoutePlanner.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/proxy/SchemeBasedRoutePlanner.java index be06b7ad..008055e2 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/proxy/SchemeBasedRoutePlanner.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/proxy/SchemeBasedRoutePlanner.java @@ -38,15 +38,15 @@ public class SchemeBasedRoutePlanner extends DefaultRoutePlanner { @Override protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) { - return "https".equals(target.getSchemeName()) ? determineProxy(this.httpsProxy) - : determineProxy(this.httpProxy); + return "https".equals(target.getSchemeName()) ? determineProxy(this.httpsProxy, "https") + : determineProxy(this.httpProxy, HttpHost.DEFAULT_SCHEME_NAME); } - private HttpHost determineProxy(ProxyHostProperties properties) { + private HttpHost determineProxy(ProxyHostProperties properties, String scheme) { if (properties == null) { return null; } - return new HttpHost(properties.getHost(), properties.getPort()); + return new HttpHost(properties.getHost(), properties.getPort(), scheme); } } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/support/HttpClientSupportTest.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/support/HttpClientSupportTest.java index 03107fd0..138508e0 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/support/HttpClientSupportTest.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/support/HttpClientSupportTest.java @@ -19,7 +19,12 @@ package org.springframework.cloud.config.server.support; import java.io.IOException; import java.net.SocketTimeoutException; import java.security.GeneralSecurityException; +import java.util.HashMap; +import java.util.Map; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.junit.Rule; @@ -32,11 +37,19 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.cloud.config.server.environment.JGitEnvironmentProperties; +import org.springframework.cloud.config.server.proxy.ProxyHostProperties; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.isA; import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause; @@ -64,6 +77,42 @@ public class HttpClientSupportTest { httpClient.execute(new HttpGet(String.format("http://127.0.0.1:%s/test/endpoint", this.localServerPort))); } + @Test + public void httpsProxy() throws GeneralSecurityException, IOException { + WireMockServer wireMockProxyServer = new WireMockServer( + options().httpDisabled(true).dynamicHttpsPort().enableBrowserProxying(true).trustAllProxyTargets(true)); + WireMockServer wireMockServer = new WireMockServer(options().httpDisabled(true).dynamicHttpsPort()); + wireMockProxyServer.start(); + wireMockServer.start(); + WireMock.configureFor("https", "localhost", wireMockServer.httpsPort()); + stubFor(get("/test/proxy").willReturn(aResponse().withStatus(200))); + + JGitEnvironmentProperties properties = new JGitEnvironmentProperties(); + Map proxy = new HashMap<>(); + ProxyHostProperties hostProperties = new ProxyHostProperties(); + hostProperties.setHost("localhost"); + hostProperties.setPort(wireMockProxyServer.httpsPort()); + proxy.put(ProxyHostProperties.ProxyForScheme.HTTPS, hostProperties); + properties.setProxy(proxy); + properties.setSkipSslValidation(true); + CloseableHttpClient httpClient = null; + CloseableHttpResponse response = null; + try { + httpClient = HttpClientSupport.builder(properties).build(); + response = httpClient + .execute(new HttpGet("https://localhost:" + wireMockServer.httpsPort() + "/test/proxy")); + } + finally { + if (response != null) { + response.close(); + } + if (httpClient != null) { + httpClient.close(); + } + verify(1, getRequestedFor(urlEqualTo("/test/proxy"))); + } + } + @SpringBootConfiguration @EnableWebMvc @EnableAutoConfiguration