-
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.
Dialogue client channel creation may target a specific address (#2134)
Dialogue client channel creation may target a specific address
- Loading branch information
1 parent
9d4b185
commit 0a8e6df
Showing
11 changed files
with
433 additions
and
3 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
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,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Dialogue client channel creation may target a specific address | ||
links: | ||
- https://github.com/palantir/dialogue/pull/2134 |
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
94 changes: 94 additions & 0 deletions
94
dialogue-apache-hc5-client/src/main/java/com/palantir/dialogue/hc5/DialogueRoutePlanner.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,94 @@ | ||
/* | ||
* (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 java.net.InetAddress; | ||
import java.net.ProxySelector; | ||
import javax.annotation.Nullable; | ||
import org.apache.hc.client5.http.HttpRoute; | ||
import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner; | ||
import org.apache.hc.client5.http.routing.HttpRoutePlanner; | ||
import org.apache.hc.core5.http.HttpException; | ||
import org.apache.hc.core5.http.HttpHost; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
|
||
/** | ||
* This implementation wraps the default route planner, but allows a specific pre-resolved address to be used. | ||
* Client instances are shared between all URIs, so this route planner cannot know resolved addresses at the | ||
* moment it's created, and instead must rely on callers passing the resolved address using the | ||
* {@link HttpContext}. | ||
*/ | ||
final class DialogueRoutePlanner implements HttpRoutePlanner { | ||
private static final String ATTRIBUTE = "dialogueResolvedAddress"; | ||
private final HttpRoutePlanner delegate; | ||
|
||
DialogueRoutePlanner(ProxySelector proxySelector) { | ||
delegate = new SystemDefaultRoutePlanner(proxySelector); | ||
} | ||
|
||
@Override | ||
public HttpRoute determineRoute(HttpHost host, HttpContext context) throws HttpException { | ||
HttpRoute route = delegate.determineRoute(host, context); | ||
if (route.getTargetHost().getAddress() == null) { | ||
InetAddress resolvedAddress = get(context); | ||
if (resolvedAddress != null) { | ||
return withResolvedAddress(route, resolvedAddress); | ||
} | ||
} | ||
return route; | ||
} | ||
|
||
private static HttpRoute withResolvedAddress(HttpRoute route, InetAddress resolvedAddress) { | ||
HttpHost targetHost = route.getTargetHost(); | ||
return new HttpRoute( | ||
new HttpHost( | ||
targetHost.getSchemeName(), resolvedAddress, targetHost.getHostName(), targetHost.getPort()), | ||
route.getLocalAddress(), | ||
// We don't really expect proxies to be used with pre-resolved addresses, however | ||
// that takes place at a different layer of the implementation. | ||
extractProxies(route), | ||
route.isSecure(), | ||
route.getTunnelType(), | ||
route.getLayerType()); | ||
} | ||
|
||
@Nullable | ||
private static HttpHost[] extractProxies(HttpRoute route) { | ||
int hops = route.getHopCount(); | ||
if (hops > 1) { | ||
HttpHost[] proxies = new HttpHost[hops - 1]; | ||
for (int i = 0; i < hops - 1; i++) { | ||
proxies[i] = route.getHopTarget(i); | ||
} | ||
return proxies; | ||
} | ||
return null; | ||
} | ||
|
||
static void set(HttpContext context, InetAddress resolvedAddress) { | ||
context.setAttribute(ATTRIBUTE, resolvedAddress); | ||
} | ||
|
||
@Nullable | ||
private static InetAddress get(HttpContext context) { | ||
Object value = context.getAttribute(ATTRIBUTE); | ||
if (value instanceof InetAddress) { | ||
return (InetAddress) value; | ||
} | ||
return null; | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
dialogue-apache-hc5-client/src/test/java/com/palantir/dialogue/hc5/ResolvedAddressTest.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,141 @@ | ||
/* | ||
* (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 static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.Iterables; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.palantir.conjure.java.client.config.ClientConfiguration; | ||
import com.palantir.dialogue.Channel; | ||
import com.palantir.dialogue.Request; | ||
import com.palantir.dialogue.Response; | ||
import com.palantir.dialogue.TestConfigurations; | ||
import com.palantir.dialogue.TestEndpoint; | ||
import com.palantir.dialogue.TestOnlyCertificates; | ||
import com.palantir.dialogue.core.DialogueChannelFactory.ChannelArgs; | ||
import com.palantir.dialogue.hc5.ApacheHttpClientChannels.CloseableClient; | ||
import io.undertow.Undertow; | ||
import io.undertow.server.handlers.ResponseCodeHandler; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.time.Duration; | ||
import java.util.UUID; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLPeerUnverifiedException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class ResolvedAddressTest { | ||
|
||
@Test | ||
void testPlainHttp() throws Exception { | ||
Undertow undertow = Undertow.builder() | ||
.addHttpListener(0, null, new ResponseCodeHandler(204)) | ||
.build(); | ||
undertow.start(); | ||
try (CloseableClient client = | ||
ApacheHttpClientChannels.createCloseableHttpClient(TestConfigurations.create(), "test")) { | ||
int port = port(undertow); | ||
String hostname = UUID.randomUUID().toString(); | ||
String uri = "http://" + hostname + ':' + port; | ||
InetAddress resolved = InetAddress.getByAddress(hostname, new byte[] {127, 0, 0, 1}); | ||
Channel channel = ApacheHttpClientChannels.createSingleUri( | ||
ChannelArgs.builder().uri(uri).resolvedAddress(resolved).build(), client); | ||
ListenableFuture<Response> future = | ||
channel.execute(TestEndpoint.GET, Request.builder().build()); | ||
try (Response response = future.get()) { | ||
assertThat(response.code()).isEqualTo(204); | ||
} | ||
} finally { | ||
undertow.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
void testTls() throws Exception { | ||
String hostname = UUID.randomUUID().toString(); | ||
|
||
TestOnlyCertificates.GeneratedKeyPair keyPair = TestOnlyCertificates.generate(hostname); | ||
|
||
SSLContext context = TestOnlyCertificates.toContext(keyPair, true); | ||
|
||
ClientConfiguration config = ClientConfiguration.builder() | ||
.from(TestConfigurations.create()) | ||
.sslSocketFactory(context.getSocketFactory()) | ||
.trustManager(TestOnlyCertificates.toTrustManager(keyPair)) | ||
.build(); | ||
|
||
Undertow undertow = Undertow.builder() | ||
.addHttpsListener(0, null, context, new ResponseCodeHandler(204)) | ||
.build(); | ||
undertow.start(); | ||
try (CloseableClient client = ApacheHttpClientChannels.createCloseableHttpClient(config, "test")) { | ||
int port = port(undertow); | ||
String uri = "https://" + hostname + ':' + port; | ||
InetAddress resolved = InetAddress.getByAddress(hostname, new byte[] {127, 0, 0, 1}); | ||
Channel channel = ApacheHttpClientChannels.createSingleUri( | ||
ChannelArgs.builder().uri(uri).resolvedAddress(resolved).build(), client); | ||
ListenableFuture<Response> future = | ||
channel.execute(TestEndpoint.GET, Request.builder().build()); | ||
try (Response response = future.get()) { | ||
assertThat(response.code()).isEqualTo(204); | ||
} | ||
} finally { | ||
undertow.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
void testTlsWithUnexpectedHostname() throws Exception { | ||
String hostname = UUID.randomUUID().toString(); | ||
TestOnlyCertificates.GeneratedKeyPair keyPair = TestOnlyCertificates.generate("localhost"); | ||
|
||
SSLContext context = TestOnlyCertificates.toContext(keyPair, true); | ||
|
||
ClientConfiguration config = ClientConfiguration.builder() | ||
.from(TestConfigurations.create()) | ||
.sslSocketFactory(context.getSocketFactory()) | ||
.trustManager(TestOnlyCertificates.toTrustManager(keyPair)) | ||
.build(); | ||
|
||
Undertow undertow = Undertow.builder() | ||
.addHttpsListener(0, null, context, new ResponseCodeHandler(204)) | ||
.build(); | ||
undertow.start(); | ||
try (CloseableClient client = ApacheHttpClientChannels.createCloseableHttpClient(config, "test")) { | ||
int port = port(undertow); | ||
String uri = "https://" + hostname + ':' + port; | ||
InetAddress resolved = InetAddress.getByAddress(hostname, new byte[] {127, 0, 0, 1}); | ||
Channel channel = ApacheHttpClientChannels.createSingleUri( | ||
ChannelArgs.builder().uri(uri).resolvedAddress(resolved).build(), client); | ||
ListenableFuture<Response> future = | ||
channel.execute(TestEndpoint.GET, Request.builder().build()); | ||
assertThat(future) | ||
.failsWithin(Duration.ofSeconds(5)) | ||
.withThrowableThat() | ||
.withRootCauseInstanceOf(SSLPeerUnverifiedException.class); | ||
} finally { | ||
undertow.stop(); | ||
} | ||
} | ||
|
||
private static int port(Undertow undertow) { | ||
return ((InetSocketAddress) | ||
Iterables.getOnlyElement(undertow.getListenerInfo()).getAddress()) | ||
.getPort(); | ||
} | ||
} |
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.