Skip to content

Commit 9535d5d

Browse files
improve the logic in DefaultDnsResolver to take into consideration that 'resolvedHost' might not contain '.'
1 parent 540b89e commit 9535d5d

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ public List<String> resolveHostFromSrvRecords(final String srvHost, final String
8989
for (String srvRecord : srvAttributeValues) {
9090
String[] split = srvRecord.split(" ");
9191
String resolvedHost = split[3].endsWith(".") ? split[3].substring(0, split[3].length() - 1) : split[3];
92-
if (srvHostHasLessThanThreeParts && resolvedHost.equals(srvHost)) {
93-
throw new MongoConfigurationException(
94-
format("The SRV host name '%s' has less than three parts and the resolved host '%s' is identical.",
95-
srvHost, resolvedHost)
96-
);
92+
String resolvedHostDomain;
93+
if (resolvedHost.contains(".")) {
94+
resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1);
95+
} else {
96+
resolvedHostDomain = "";
9797
}
98-
String resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1);
9998
if (!sameParentDomain(srvHostDomainParts, resolvedHostDomain)) {
10099
throw new MongoConfigurationException(
101100
format("The SRV host name '%s' resolved to a host '%s 'that is not in a sub-domain of the SRV host.",

driver-core/src/test/unit/com/mongodb/internal/connection/InitialDnsSeedListDiscoveryProseTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void tearDown() {
5959
"mongo.local, driver.mongo.local"
6060
})
6161
@DisplayName("1. Allow SRVs with fewer than 3 '.' separated parts")
62-
void test1(final String srvHost, final String resolvedHost) {
62+
void test_allow_SRVs_with_fewer_than_three_parts(final String srvHost, final String resolvedHost) {
6363
doTest(srvHost, resolvedHost, false);
6464
}
6565

@@ -70,7 +70,7 @@ void test1(final String srvHost, final String resolvedHost) {
7070
"blogs.mongodb.com, blogs.evil.com"
7171
})
7272
@DisplayName("2. Throw when return address does not end with SRV domain")
73-
void test2(final String srvHost, final String resolvedHost) {
73+
void test_throw_when_return_address_doesnot_end_with_SRV_domain(final String srvHost, final String resolvedHost) {
7474
doTest(srvHost, resolvedHost, true);
7575
}
7676

@@ -79,8 +79,8 @@ void test2(final String srvHost, final String resolvedHost) {
7979
"localhost, localhost",
8080
"mongo.local, mongo.local"
8181
})
82-
@DisplayName("3. Throw when return address is identical to SRV hostname and return address does not contain '.' separating shared part of domain")
83-
void test3(final String srvHost, final String resolvedHost) {
82+
@DisplayName("3. Throw when return address is identical to SRV hostname and the SRV hostname has fewer than three `.` separated parts")
83+
void test_throw_when_return_address_is_identical_to_SRV_hostname(final String srvHost, final String resolvedHost) {
8484
doTest(srvHost, resolvedHost, true);
8585
}
8686

@@ -91,7 +91,7 @@ void test3(final String srvHost, final String resolvedHost) {
9191
"blogs.mongodb.com, cluster.testmongodb.com"
9292
})
9393
@DisplayName("4. Throw when return address does not contain '.' separating shared part of domain")
94-
void test4(final String srvHost, final String resolvedHost) {
94+
void test_throw_when_return_address_doesnot_contain_shared_part_of_domain(final String srvHost, final String resolvedHost) {
9595
doTest(srvHost, resolvedHost, true);
9696
}
9797

0 commit comments

Comments
 (0)