-
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.
Apache channel supports proxy credentials (#383)
Apache channel supports proxy credentials
- Loading branch information
1 parent
3e7b359
commit 20e3469
Showing
10 changed files
with
337 additions
and
6 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,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Apache channel supports proxy credentials | ||
links: | ||
- https://github.com/palantir/dialogue/pull/383 |
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
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
28 changes: 28 additions & 0 deletions
28
...ogue-apache-hc4-client/src/test/java/com/palantir/dialogue/hc4/ApacheProxyConfigTest.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,28 @@ | ||
/* | ||
* (c) Copyright 2020 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.hc4; | ||
|
||
import com.palantir.conjure.java.api.config.service.UserAgent; | ||
import com.palantir.conjure.java.client.config.ClientConfiguration; | ||
import com.palantir.dialogue.AbstractProxyConfigTest; | ||
import com.palantir.dialogue.Channel; | ||
|
||
public final class ApacheProxyConfigTest extends AbstractProxyConfigTest { | ||
@Override | ||
protected Channel create(ClientConfiguration config, UserAgent agent) { | ||
return ApacheHttpClientChannels.create(config, agent); | ||
} | ||
} |
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
177 changes: 177 additions & 0 deletions
177
dialogue-client-test-lib/src/main/java/com/palantir/dialogue/AbstractProxyConfigTest.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,177 @@ | ||
/* | ||
* (c) Copyright 2020 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; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.palantir.conjure.java.api.config.service.BasicCredentials; | ||
import com.palantir.conjure.java.api.config.service.UserAgent; | ||
import com.palantir.conjure.java.api.config.ssl.SslConfiguration; | ||
import com.palantir.conjure.java.client.config.ClientConfiguration; | ||
import com.palantir.conjure.java.client.config.ClientConfigurations; | ||
import com.palantir.conjure.java.config.ssl.SslSocketFactories; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.net.ProxySelector; | ||
import java.net.SocketAddress; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Map; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okhttp3.mockwebserver.RecordedRequest; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public abstract class AbstractProxyConfigTest { | ||
|
||
protected static final UserAgent AGENT = UserAgent.of(UserAgent.Agent.of("test", "0.0.1")); | ||
private static final SslConfiguration SSL_CONFIG = SslConfiguration.of( | ||
Paths.get("../dialogue-client-test-lib/src/main/resources/trustStore.jks"), | ||
Paths.get("../dialogue-client-test-lib/src/main/resources/keyStore.jks"), | ||
"keystore"); | ||
|
||
private static final Request request = Request.builder() | ||
.body(new RequestBody() { | ||
@Override | ||
public void writeTo(OutputStream output) throws IOException { | ||
output.write("Hello, World".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
@Override | ||
public String contentType() { | ||
return "text/plain"; | ||
} | ||
}) | ||
.build(); | ||
|
||
@Rule | ||
public final MockWebServer server = new MockWebServer(); | ||
|
||
@Rule | ||
public final MockWebServer proxyServer = new MockWebServer(); | ||
|
||
protected abstract Channel create(ClientConfiguration config, UserAgent agent); | ||
|
||
@Test | ||
public void testDirectVersusProxyConnection() throws Exception { | ||
server.enqueue(new MockResponse().setBody("server")); | ||
proxyServer.enqueue(new MockResponse().setBody("proxyServer")); | ||
|
||
Channel directChannel = create(createTestConfig("http://localhost:" + server.getPort()), AGENT); | ||
ClientConfiguration proxiedConfig = ClientConfiguration.builder() | ||
.from(createTestConfig("http://localhost:" + server.getPort())) | ||
.proxy(createProxySelector("localhost", proxyServer.getPort())) | ||
.build(); | ||
Channel proxiedChannel = create(proxiedConfig, AGENT); | ||
|
||
try (Response response = | ||
directChannel.execute(FakeEndpoint.INSTANCE, request).get()) { | ||
assertThat(response.code()).isEqualTo(200); | ||
assertThat(response.body()).hasContent("server"); | ||
} | ||
try (Response response = | ||
proxiedChannel.execute(FakeEndpoint.INSTANCE, request).get()) { | ||
assertThat(response.code()).isEqualTo(200); | ||
assertThat(response.body()).hasContent("proxyServer"); | ||
} | ||
RecordedRequest proxyRequest = proxyServer.takeRequest(); | ||
assertThat(proxyRequest.getHeader("Host")).isEqualTo("localhost:" + server.getPort()); | ||
} | ||
|
||
@Test | ||
public void testAuthenticatedProxy() throws Exception { | ||
proxyServer.enqueue(new MockResponse() | ||
.addHeader("Proxy-Authenticate", "Basic realm=test") | ||
.setResponseCode(407)); // indicates authenticated proxy | ||
proxyServer.enqueue(new MockResponse().setBody("proxyServer")); | ||
|
||
ClientConfiguration proxiedConfig = ClientConfiguration.builder() | ||
.from(createTestConfig("http://localhost:" + server.getPort())) | ||
.proxy(createProxySelector("localhost", proxyServer.getPort())) | ||
.proxyCredentials(BasicCredentials.of("fakeUser", "fakePassword")) | ||
.build(); | ||
Channel proxiedChannel = create(proxiedConfig, AGENT); | ||
|
||
try (Response response = | ||
proxiedChannel.execute(FakeEndpoint.INSTANCE, request).get()) { | ||
assertThat(response.code()).isEqualTo(200); | ||
assertThat(response.body()).hasContent("proxyServer"); | ||
} | ||
RecordedRequest firstRequest = proxyServer.takeRequest(); | ||
assertThat(firstRequest.getHeader("Proxy-Authorization")).isNull(); | ||
RecordedRequest secondRequest = proxyServer.takeRequest(); | ||
assertThat(secondRequest.getHeader("Proxy-Authorization")).isEqualTo("Basic ZmFrZVVzZXI6ZmFrZVBhc3N3b3Jk"); | ||
} | ||
|
||
protected static ClientConfiguration createTestConfig(String... uri) { | ||
return ClientConfiguration.builder() | ||
.from(ClientConfigurations.of( | ||
ImmutableList.copyOf(uri), | ||
SslSocketFactories.createSslSocketFactory(SSL_CONFIG), | ||
SslSocketFactories.createX509TrustManager(SSL_CONFIG))) | ||
.maxNumRetries(0) | ||
.build(); | ||
} | ||
|
||
private static ProxySelector createProxySelector(String host, int port) { | ||
return new ProxySelector() { | ||
@Override | ||
public List<Proxy> select(URI _uri) { | ||
InetSocketAddress addr = new InetSocketAddress(host, port); | ||
return ImmutableList.of(new Proxy(Proxy.Type.HTTP, addr)); | ||
} | ||
|
||
@Override | ||
public void connectFailed(URI _uri, SocketAddress _sa, IOException _ioe) {} | ||
}; | ||
} | ||
|
||
private enum FakeEndpoint implements Endpoint { | ||
INSTANCE; | ||
|
||
@Override | ||
public void renderPath(Map<String, String> _params, UrlBuilder url) { | ||
url.pathSegment("/string"); | ||
} | ||
|
||
@Override | ||
public HttpMethod httpMethod() { | ||
return HttpMethod.POST; | ||
} | ||
|
||
@Override | ||
public String serviceName() { | ||
return "service"; | ||
} | ||
|
||
@Override | ||
public String endpointName() { | ||
return "endpoint"; | ||
} | ||
|
||
@Override | ||
public String version() { | ||
return "1.0.0"; | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.