-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add support for routing through an HTTPS proxy #2279
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c35d1fa
Add support for routing through an HTTPS proxy
edfa50a
exception type
bae15d6
Updated API
fb422b3
Bump to real dep
e16cc91
Merge remote-tracking branch 'origin/develop' into ea/https-proxy
bb53c29
Change to package private
5f2ffb9
Add generated changelog entries
svc-changelog 57b698f
Fix test
2ea7bb0
Add generated changelog entries
svc-changelog 1bc0d5c
Note original license
62d890b
Update docs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 link the original source here and describe that the original is licensed under apache 2.0.
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.
A bit confused, it is linked, did you want it linked differently? Or you want a link to http://www.apache.org/licenses/LICENSE-2.0
Added a line below "Original version is licensed under Apache License, Version 2.0."
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.
I'd like to have a link to the original, at the version used at the point in time we modified from. This way if there are future changes, we can tell whether or not they may be impactful. The javadoc link will point toward whatever version we happen to depend on at any point in time
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.
Makes sense, updated the docs