forked from hivemq/hivemq-community-edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.hivemq.security.ssl; | ||
|
||
import java.util.Map; | ||
|
||
public class DnsResolver { | ||
|
||
private final Map<String, String> dnsMap; | ||
|
||
DnsResolver(final Map<String, String> dnsMap) { | ||
this.dnsMap = dnsMap; | ||
} | ||
|
||
String resolve(final String domain) { | ||
String alias = dnsMap.get(domain); | ||
if (alias != null) { | ||
return alias; | ||
} | ||
|
||
int index = domain.indexOf('.'); | ||
while (index >= 0) { | ||
final String wildcardDomain = "*" + domain.substring(index); | ||
alias = dnsMap.get(wildcardDomain); | ||
if (alias != null) { | ||
return alias; | ||
} | ||
index = domain.indexOf('.', index + 1); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/com/hivemq/security/ssl/DnsResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.hivemq.security.ssl; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class DnsResolverTest { | ||
public static final String ALIAS_1 = "alias1"; | ||
public static final String TEST_EXAMPLE_COM = "test.example.com"; | ||
|
||
@Test | ||
public void test_resolve_simple_dns_name() { | ||
final DnsResolver dnsResolver = new DnsResolver(Map.of(TEST_EXAMPLE_COM, ALIAS_1)); | ||
|
||
final String resolve = dnsResolver.resolve(TEST_EXAMPLE_COM); | ||
|
||
assertNotNull(resolve); | ||
assertEquals(ALIAS_1, resolve); | ||
} | ||
|
||
@Test | ||
public void test_resolve_non_matching_dns_name() { | ||
final DnsResolver dnsResolver = new DnsResolver(Map.of(TEST_EXAMPLE_COM, ALIAS_1)); | ||
|
||
final String resolve = dnsResolver.resolve("other.example.com"); | ||
|
||
assertNull(resolve); | ||
} | ||
|
||
@Test | ||
public void test_resolve_wildcard_dns_name() { | ||
final DnsResolver dnsResolver = new DnsResolver(Map.of("*.example.com", ALIAS_1)); | ||
|
||
final String resolve = dnsResolver.resolve(TEST_EXAMPLE_COM); | ||
|
||
assertNotNull(resolve); | ||
assertEquals(ALIAS_1, resolve); | ||
} | ||
|
||
@Test | ||
public void test_resolve_nested_wildcard_dns_name() { | ||
final DnsResolver dnsResolver = new DnsResolver(Map.of("*.example.com", ALIAS_1)); | ||
|
||
final String resolve = dnsResolver.resolve("sub.test.example.com"); | ||
|
||
assertNotNull(resolve); | ||
assertEquals(ALIAS_1, resolve); | ||
} | ||
} |