Use HTTPS when proxy settings are set (#1986)

* Use HTTPS when proxy settings are set

Fixes #1965

* Adding test
This commit is contained in:
Ryan Baxter
2021-10-20 11:12:42 -04:00
committed by GitHub
parent 41e50e345d
commit 998a4cadf2
4 changed files with 64 additions and 4 deletions

View File

@@ -30,6 +30,7 @@
<spring-cloud-commons.version>3.0.5-SNAPSHOT</spring-cloud-commons.version>
<aws-java-sdk.version>1.11.903</aws-java-sdk.version>
<google-api-services-iam.version>v1-rev20201112-1.30.10</google-api-services-iam.version>
<wiremock.version>2.31.0</wiremock.version>
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError>
<maven-checkstyle-plugin.failsOnViolation>true
</maven-checkstyle-plugin.failsOnViolation>
@@ -88,6 +89,11 @@
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.22.0</version>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>${wiremock.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>

View File

@@ -145,6 +145,11 @@
<version>0.7.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>

View File

@@ -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);
}
}

View File

@@ -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<ProxyHostProperties.ProxyForScheme, ProxyHostProperties> 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