-
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
Apache channel supports proxy credentials #383
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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); | ||
} | ||
} |
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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these paths seem a little off There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to reuse this config in other tests too, but avoided modifying them here. It's a bit odd, but it would allow us to check in a single ssl config with our abstract tests and reuse it in all client implementations. We have a lot of boilerplate to implement a test. |
||
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"; | ||
} | ||
} | ||
} |
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.
oh my boilerplate. Why do we need to provide null implementations?
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 want to avoid dealing with credentials in all possible cases so we don't have to deal with folks getting used to client-specific behavior.
When proxy credentials are provided I want to make sure they can only be used by proxies by disabling the target auth impl, and all schemes except basic to match okhttp.