Skip to content

Adds DNS resolver configuration option for ApacheHttpClient #2834

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

Merged
merged 3 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-418a15a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "Apache HTTP Client",
"contributor": "",
"type": "feature",
"description": "Add DNS resolver override support for Apache HTTP Client"
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.socket.ConnectionSocketFactory;
Expand Down Expand Up @@ -393,6 +394,11 @@ public interface Builder extends SdkHttpClient.Builder<ApacheHttpClient.Builder>
*/
Builder useIdleConnectionReaper(Boolean useConnectionReaper);

/**
* Configuration that defines a DNS resolver. If no matches are found, the default resolver is used.
*/
Builder dnsResolver(DnsResolver dnsResolver);

/**
* Configuration that defines an HTTP route planner that computes the route an HTTP request should take.
* May not be used in conjunction with {@link #proxyConfiguration(ProxyConfiguration)}.
Expand Down Expand Up @@ -441,6 +447,7 @@ private static final class DefaultBuilder implements Builder {
private Boolean expectContinueEnabled;
private HttpRoutePlanner httpRoutePlanner;
private CredentialsProvider credentialsProvider;
private DnsResolver dnsResolver;

private DefaultBuilder() {
}
Expand Down Expand Up @@ -551,6 +558,16 @@ public void setUseIdleConnectionReaper(Boolean useIdleConnectionReaper) {
useIdleConnectionReaper(useIdleConnectionReaper);
}

@Override
public Builder dnsResolver(DnsResolver dnsResolver) {
this.dnsResolver = dnsResolver;
return this;
}

public void setDnsResolver(DnsResolver dnsResolver) {
dnsResolver(dnsResolver);
}

@Override
public Builder httpRoutePlanner(HttpRoutePlanner httpRoutePlanner) {
this.httpRoutePlanner = httpRoutePlanner;
Expand Down Expand Up @@ -620,7 +637,7 @@ public HttpClientConnectionManager create(ApacheHttpClient.DefaultBuilder config
createSocketFactoryRegistry(sslsf),
null,
DefaultSchemePortResolver.INSTANCE,
null,
configuration.dnsResolver,
standardOptions.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE).toMillis(),
TimeUnit.MILLISECONDS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -112,7 +116,6 @@ public void credentialProviderCantBeUsedWithProxyCredentials_SystemProperties()
}).isInstanceOf(IllegalArgumentException.class);
}


@Test
public void credentialProviderCanBeUsedWithProxy() {
ProxyConfiguration proxyConfig = ProxyConfiguration.builder()
Expand All @@ -123,4 +126,23 @@ public void credentialProviderCanBeUsedWithProxy() {
.credentialsProvider(Mockito.mock(CredentialsProvider.class))
.build();
}

@Test
public void dnsResolverCanBeUsed() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test that if the caller explicitly sets to null we use the default resolver?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
if (host.equalsIgnoreCase("my.host.com")) {
return new InetAddress[] { InetAddress.getByName("127.0.0.1") };
} else {
return super.resolve(host);
}
}
};

ApacheHttpClient.builder()
.dnsResolver(dnsResolver)
.build()
.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.http.apache;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.any;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.mockito.Mockito.verify;
Expand All @@ -25,21 +26,30 @@
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import software.amazon.awssdk.http.HttpExecuteRequest;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.SdkHttpClientTestSuite;
import software.amazon.awssdk.http.SdkHttpFullRequest;
import software.amazon.awssdk.http.SdkHttpMethod;
import software.amazon.awssdk.http.apache.internal.ApacheHttpRequestConfig;
import software.amazon.awssdk.http.apache.internal.impl.ConnectionManagerAwareHttpClient;
import software.amazon.awssdk.utils.AttributeMap;
Expand Down Expand Up @@ -148,4 +158,69 @@ public void clear() {

mockProxyServer.verify(2, RequestPatternBuilder.allRequests());
}

@Test
public void overrideDnsResolver_WithDnsMatchingResolver_successful() throws Exception {
overrideDnsResolver("magic.local.host");
}

@Test(expected = UnknownHostException.class)
public void overrideDnsResolver_WithUnknownHost_throwsException() throws Exception {
overrideDnsResolver("sad.local.host");
}

@Test
public void overrideDnsResolver_WithLocalhost_successful() throws Exception {
overrideDnsResolver("localhost");
}

@Test
public void explicitNullDnsResolver_WithLocalhost_successful() throws Exception {
overrideDnsResolver("localhost", true);
}

private void overrideDnsResolver(String hostName) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hostName looks unused

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will take a look

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used for the URI a bit further down

overrideDnsResolver(hostName, false);
}

private void overrideDnsResolver(String hostName, boolean nullifyResolver) throws IOException {

DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
if (host.equalsIgnoreCase("magic.local.host")) {
return new InetAddress[] { InetAddress.getByName("127.0.0.1") };
} else {
return super.resolve(host);
}
}
};
if (nullifyResolver) {
dnsResolver = null;
}

SdkHttpClient client = ApacheHttpClient.builder()
.dnsResolver(dnsResolver)
.buildWithDefaults(AttributeMap.builder()
.put(TRUST_ALL_CERTIFICATES, Boolean.TRUE)
.build());

mockProxyServer.resetToDefaultMappings();
mockProxyServer.stubFor(any(urlPathEqualTo("/")).willReturn(aResponse().withStatus(HttpURLConnection.HTTP_OK)));

URI uri = URI.create("https://" + hostName + ":" + mockProxyServer.httpsPort());
SdkHttpFullRequest req = SdkHttpFullRequest.builder()
.uri(uri)
.method(SdkHttpMethod.POST)
.putHeader("Host", uri.getHost())
.build();

client.prepareRequest(HttpExecuteRequest.builder()
.request(req)
.contentStreamProvider(req.contentStreamProvider().orElse(null))
.build())
.call();

mockProxyServer.verify(1, RequestPatternBuilder.allRequests());
}
}