Skip to content

Remove IP literal check for InetAddressResolver #1120

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
May 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public InetSocketAddress getSocketAddress() {

@Override
public List<InetSocketAddress> getSocketAddresses() {
if (resolver == null || isIpLiteral()) {
if (resolver == null) {
return super.getSocketAddresses();
}
try {
Expand All @@ -74,11 +74,6 @@ public List<InetSocketAddress> getSocketAddresses() {
}
}

// If this returns true, it's either an IP literal or a malformed hostname. But either way, skip lookup via resolver
private boolean isIpLiteral() {
return getHost().charAt(0) == '[' || Character.digit(getHost().charAt(0), 16) != -1 || (getHost().charAt(0) == ':');
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface InetAddressResolver {
* Given the name of a host, returns a list of IP addresses of the requested
* address family associated with a provided hostname.
*
* <p>The host name can be an IP literal, as with {@link InetAddress#getAllByName(String)}</p>
*
* <p>Implementations are encouraged to implement their own caching policies, as there is
* no guarantee that the caller will implement a cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,22 @@
import com.mongodb.MongoException;
import com.mongodb.MongoSocketException;
import com.mongodb.ServerAddress;
import com.mongodb.connection.ServerConnectionState;
import com.mongodb.connection.ServerDescription;
import com.mongodb.event.ClusterDescriptionChangedEvent;
import com.mongodb.event.ClusterListener;
import com.mongodb.spi.dns.DnsClient;
import com.mongodb.spi.dns.DnsException;
import com.mongodb.spi.dns.InetAddressResolver;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.net.UnknownHostException;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import static com.mongodb.ClusterFixture.getConnectionString;
import static com.mongodb.ClusterFixture.getServerApi;
import static com.mongodb.ClusterFixture.isStandalone;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

@SuppressWarnings("try")
public abstract class AbstractDnsConfigurationTest {
Expand Down Expand Up @@ -81,41 +73,6 @@ public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event
}
}

@ParameterizedTest(name = "InetAddressResolver should not be used to resolve IP literal {0}")
@ValueSource(strings = {"127.0.0.1", "::1", "[0:0:0:0:0:0:0:1]"})
public void testInetAddressResolverDoesNotResolveIpLiteral(final String ipLiteral) throws InterruptedException, ExecutionException,
TimeoutException {
assumeTrue(isStandalone());
assumeTrue(getServerApi() == null);

// should not be invoked for IP literals
InetAddressResolver resolver = host -> {
throw new UnknownHostException();
};

CompletableFuture<Boolean> serverConnectedFuture = new CompletableFuture<>();
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(getConnectionString())
.applyToClusterSettings(builder ->
builder.hosts(Collections.singletonList(new ServerAddress(ipLiteral)))
.addClusterListener(new ClusterListener() {
@Override
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
ServerDescription serverDescription = event.getNewDescription().getServerDescriptions().get(0);
// If the resolver that throws an exception is invoked, the state will not be CONNECTED
if (serverDescription.getState() == ServerConnectionState.CONNECTED) {
serverConnectedFuture.complete(true);
}
}
}))
.inetAddressResolver(resolver)
.build();

try (MongoClient ignored = createMongoClient(settings)) {
assertTrue(serverConnectedFuture.get(1, SECONDS));
}
}

@Test
public void testDnsClientConfiguration() throws InterruptedException, ExecutionException, TimeoutException {
DnsException exception = new DnsException("", new Exception());
Expand Down