-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for routing through an HTTPS proxy (#2279)
<!-- User-facing outcomes this PR delivers -->
- Loading branch information
1 parent
ea604e7
commit 510e16b
Showing
6 changed files
with
173 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: feature | ||
feature: | ||
description: Add support for routing through HTTPS proxies in the apache client | ||
when configured | ||
links: | ||
- https://github.com/palantir/dialogue/pull/2279 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...che-hc5-client/src/main/java/com/palantir/dialogue/hc5/HttpsProxyDefaultRoutePlanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.dialogue.hc5; | ||
|
||
import com.palantir.conjure.java.client.config.HttpsProxies; | ||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.net.ProxySelector; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
import javax.annotation.CheckForNull; | ||
import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner; | ||
import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner; | ||
import org.apache.hc.core5.http.HttpException; | ||
import org.apache.hc.core5.http.HttpHost; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
|
||
/** | ||
* Identical to {@link SystemDefaultRoutePlanner} but adds support for connecting to an HTTPS proxy. | ||
* Original version is licensed under Apache License, Version 2.0. The original source code can be found | ||
* <a href="https://github.com/apache/httpcomponents-client/blob/rel/v5.3.1/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/SystemDefaultRoutePlanner.java">here</a>. | ||
* <p> | ||
* For future changes and updates in the {@link SystemDefaultRoutePlanner} implementation, refer to the linked version | ||
* that this implementation was based off of provided to determine if this class requires changes. | ||
*/ | ||
final class HttpsProxyDefaultRoutePlanner extends DefaultRoutePlanner { | ||
private final ProxySelector proxySelector; | ||
|
||
HttpsProxyDefaultRoutePlanner(ProxySelector proxySelector) { | ||
super(null); | ||
this.proxySelector = proxySelector; | ||
} | ||
|
||
@Override | ||
@CheckForNull | ||
public HttpHost determineProxy(final HttpHost target, final HttpContext _context) throws HttpException { | ||
final URI targetUri; | ||
try { | ||
targetUri = new URI(target.toURI()); | ||
} catch (final URISyntaxException ex) { | ||
throw new HttpException("Cannot convert host to URI: " + target, ex); | ||
} | ||
ProxySelector proxySelectorInstance = this.proxySelector; | ||
if (proxySelectorInstance == null) { | ||
proxySelectorInstance = ProxySelector.getDefault(); | ||
} | ||
if (proxySelectorInstance == null) { | ||
// The proxy selector can be "unset", so we must be able to deal with a null selector | ||
return null; | ||
} | ||
final List<Proxy> proxies = proxySelectorInstance.select(targetUri); | ||
final Proxy p = chooseProxy(proxies); | ||
HttpHost result = null; | ||
if (p.type() == Proxy.Type.HTTP) { | ||
// convert the socket address to an HttpHost | ||
if (!(p.address() instanceof InetSocketAddress)) { | ||
throw new HttpException("Unable to handle non-Inet proxy address: " + p.address()); | ||
} | ||
final InetSocketAddress isa = (InetSocketAddress) p.address(); | ||
String scheme = HttpsProxies.isHttps(p) ? "https" : "http"; | ||
result = new HttpHost(scheme, isa.getAddress(), isa.getHostString(), isa.getPort()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private Proxy chooseProxy(final List<Proxy> proxies) { | ||
Proxy result = null; | ||
// check the list for one we can use | ||
for (int i = 0; (result == null) && (i < proxies.size()); i++) { | ||
final Proxy p = proxies.get(i); | ||
switch (p.type()) { | ||
case DIRECT: | ||
case HTTP: | ||
result = p; | ||
break; | ||
|
||
case SOCKS: | ||
// SOCKS hosts are not handled on the route level. | ||
// The socket may make use of the SOCKS host though. | ||
break; | ||
} | ||
} | ||
if (result == null) { | ||
// @@@ log as warning or info that only a socks proxy is available? | ||
// result can only be null if all proxies are socks proxies | ||
// socks proxies are not handled on the route planning level | ||
result = Proxy.NO_PROXY; | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import com.google.common.util.concurrent.Uninterruptibles; | ||
import com.palantir.conjure.java.api.config.service.BasicCredentials; | ||
import com.palantir.conjure.java.client.config.ClientConfiguration; | ||
import com.palantir.conjure.java.client.config.HttpsProxies; | ||
import com.palantir.conjure.java.config.ssl.SslSocketFactories; | ||
import io.undertow.Undertow; | ||
import io.undertow.server.HttpHandler; | ||
|
@@ -48,6 +49,9 @@ | |
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Parameterized tests are incompatible with BeforeEach, so the tests must be duplicated to test the two proxy types. | ||
*/ | ||
public abstract class AbstractProxyConfigTlsTest { | ||
|
||
private static final String REQUEST_BODY = "Hello, World"; | ||
|
@@ -79,6 +83,7 @@ public void close() {} | |
private volatile HttpHandler proxyHandler; | ||
|
||
private int proxyPort; | ||
private int httpsProxyPort; | ||
private Undertow proxyServer; | ||
|
||
protected abstract Channel create(ClientConfiguration config); | ||
|
@@ -95,9 +100,21 @@ public void beforeEach() { | |
proxyServer = Undertow.builder() | ||
.setHandler(exchange -> proxyHandler.handleRequest(exchange)) | ||
.addHttpListener(0, null) | ||
.addHttpsListener(0, null, sslContext) | ||
.build(); | ||
proxyServer.start(); | ||
proxyPort = getPort(proxyServer); | ||
proxyPort = getProxyPort("http", "No HTTP listener"); | ||
httpsProxyPort = getProxyPort("https", "No HTTPS listener"); | ||
} | ||
|
||
private Integer getProxyPort(String scheme, String errorMessage) { | ||
return proxyServer.getListenerInfo().stream() | ||
.filter(info -> info.getProtcol().equals(scheme)) | ||
.map(Undertow.ListenerInfo::getAddress) | ||
.map(InetSocketAddress.class::cast) | ||
.map(InetSocketAddress::getPort) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException(errorMessage)); | ||
} | ||
|
||
private static int getPort(Undertow undertow) { | ||
|
@@ -116,7 +133,16 @@ public void afterEach() { | |
} | ||
|
||
@Test | ||
public void testDirectVersusProxyReadTimeout() throws Exception { | ||
public void testDirectVersusProxyReadTimeout_httpProxy() throws Exception { | ||
testDirectVersusProxyReadTimeout(createProxySelector("localhost", proxyPort, false)); | ||
} | ||
|
||
@Test | ||
public void testDirectProxyReadTimeout_httpsProxy() throws Exception { | ||
testDirectVersusProxyReadTimeout(createProxySelector("localhost", httpsProxyPort, true)); | ||
} | ||
|
||
private void testDirectVersusProxyReadTimeout(ProxySelector proxySelector) throws Exception { | ||
serverHandler = new BlockingHandler(exchange -> { | ||
Uninterruptibles.sleepUninterruptibly(Duration.ofSeconds(2)); | ||
exchange.getResponseSender().send("server"); | ||
|
@@ -132,7 +158,7 @@ public void testDirectVersusProxyReadTimeout() throws Exception { | |
Channel directChannel = create(directConfig); | ||
ClientConfiguration proxiedConfig = ClientConfiguration.builder() | ||
.from(directConfig) | ||
.proxy(createProxySelector("localhost", proxyPort)) | ||
.proxy(proxySelector) | ||
.build(); | ||
Channel proxiedChannel = create(proxiedConfig); | ||
|
||
|
@@ -149,7 +175,16 @@ public void testDirectVersusProxyReadTimeout() throws Exception { | |
} | ||
|
||
@Test | ||
public void testAuthenticatedProxy() throws Exception { | ||
public void testAuthenticatedProxy_httpProxy() throws Exception { | ||
testAuthenticatedProxy(createProxySelector("localhost", proxyPort, false)); | ||
} | ||
|
||
@Test | ||
public void testAuthenticatedProxy_httpsProxy() throws Exception { | ||
testAuthenticatedProxy(createProxySelector("localhost", httpsProxyPort, true)); | ||
} | ||
|
||
private void testAuthenticatedProxy(ProxySelector proxySelector) throws Exception { | ||
AtomicInteger requestIndex = new AtomicInteger(); | ||
proxyHandler = exchange -> { | ||
HeaderMap requestHeaders = exchange.getRequestHeaders(); | ||
|
@@ -180,7 +215,7 @@ public void testAuthenticatedProxy() throws Exception { | |
ClientConfiguration proxiedConfig = ClientConfiguration.builder() | ||
.from(TestConfigurations.create("https://localhost:" + serverPort)) | ||
.maxNumRetries(0) | ||
.proxy(createProxySelector("localhost", proxyPort)) | ||
.proxy(proxySelector) | ||
.proxyCredentials(BasicCredentials.of("[email protected]", "fake:Password")) | ||
.build(); | ||
Channel proxiedChannel = create(proxiedConfig); | ||
|
@@ -192,12 +227,12 @@ public void testAuthenticatedProxy() throws Exception { | |
} | ||
} | ||
|
||
private static ProxySelector createProxySelector(String host, int port) { | ||
private static ProxySelector createProxySelector(String host, int port, boolean httpsProxy) { | ||
return new ProxySelector() { | ||
@Override | ||
public List<Proxy> select(URI _uri) { | ||
InetSocketAddress addr = InetSocketAddress.createUnresolved(host, port); | ||
return ImmutableList.of(new Proxy(Proxy.Type.HTTP, addr)); | ||
return ImmutableList.of(httpsProxy ? HttpsProxies.create(addr) : new Proxy(Proxy.Type.HTTP, addr)); | ||
} | ||
|
||
@Override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters