Skip to content

Commit

Permalink
improve the logic in DefaultDnsResolver to take into consideration th…
Browse files Browse the repository at this point in the history
…at 'resolvedHost' might not contain '.'
  • Loading branch information
NathanQingyangXu committed Oct 21, 2024
1 parent 540b89e commit 9535d5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ public List<String> resolveHostFromSrvRecords(final String srvHost, final String
for (String srvRecord : srvAttributeValues) {
String[] split = srvRecord.split(" ");
String resolvedHost = split[3].endsWith(".") ? split[3].substring(0, split[3].length() - 1) : split[3];
if (srvHostHasLessThanThreeParts && resolvedHost.equals(srvHost)) {
throw new MongoConfigurationException(
format("The SRV host name '%s' has less than three parts and the resolved host '%s' is identical.",
srvHost, resolvedHost)
);
String resolvedHostDomain;
if (resolvedHost.contains(".")) {
resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1);
} else {
resolvedHostDomain = "";
}
String resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1);
if (!sameParentDomain(srvHostDomainParts, resolvedHostDomain)) {
throw new MongoConfigurationException(
format("The SRV host name '%s' resolved to a host '%s 'that is not in a sub-domain of the SRV host.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void tearDown() {
"mongo.local, driver.mongo.local"
})
@DisplayName("1. Allow SRVs with fewer than 3 '.' separated parts")
void test1(final String srvHost, final String resolvedHost) {
void test_allow_SRVs_with_fewer_than_three_parts(final String srvHost, final String resolvedHost) {
doTest(srvHost, resolvedHost, false);
}

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

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

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

Expand Down

0 comments on commit 9535d5d

Please sign in to comment.