-
Notifications
You must be signed in to change notification settings - Fork 119
feat: Add DnsJavaResolver and related test #2193
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e11faa8
feat: Add DnsJavaResolver and related test
panavenue 8007470
add additional unit test for multi-strings TXT record
panavenue c628535
fix unit test to throw NameNotFoundException
panavenue b40f1b4
Merge branch 'main' into feat-add-dnsjava
panavenue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ target/ | |
| **/.project | ||
| **/.settings/ | ||
| **/.classpath | ||
| .vscode/ | ||
|
|
||
| # direnv | ||
| .envrc | ||
|
|
||
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
core/src/main/java/com/google/cloud/sql/core/DnsJavaResolver.java
This file contains hidden or 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,109 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.sql.core; | ||
|
|
||
| import java.net.UnknownHostException; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.stream.Collectors; | ||
| import javax.naming.NameNotFoundException; | ||
| import org.xbill.DNS.Lookup; | ||
| import org.xbill.DNS.Record; | ||
| import org.xbill.DNS.SimpleResolver; | ||
| import org.xbill.DNS.TXTRecord; | ||
| import org.xbill.DNS.TextParseException; | ||
| import org.xbill.DNS.Type; | ||
|
|
||
| /** DnsJavaResolver is a DnsResolver that uses the dnsjava library to perform DNS lookups. */ | ||
| public class DnsJavaResolver implements DnsResolver { | ||
|
|
||
| private final SimpleResolver resolver; | ||
|
|
||
| /** Creates a resolver using the system's default DNS settings. */ | ||
| public DnsJavaResolver() { | ||
| this.resolver = null; // dnsjava's Lookup uses default resolver if null. | ||
| } | ||
|
|
||
| /** | ||
| * Creates a DNS resolver that uses a specific DNS server. | ||
| * | ||
| * @param dnsServer the DNS server hostname | ||
| * @param port the DNS server port (DNS servers usually use port 53) | ||
| */ | ||
| public DnsJavaResolver(String dnsServer, int port) { | ||
| try { | ||
| this.resolver = new SimpleResolver(dnsServer); | ||
| this.resolver.setPort(port); | ||
| } catch (UnknownHostException e) { | ||
| throw new IllegalArgumentException("Unknown DNS server host: " + dnsServer, e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns DNS TXT records for a domain name, sorted alphabetically. | ||
| * | ||
| * @param domainName the domain name to lookup | ||
| * @return the list of records | ||
| * @throws NameNotFoundException when the domain name does not resolve. | ||
| */ | ||
| @Override | ||
| public Collection<String> resolveTxt(String domainName) throws NameNotFoundException { | ||
| try { | ||
| // 1. Create a Lookup object for the TXT record type. | ||
| Lookup lookup = new Lookup(domainName, Type.TXT); | ||
|
|
||
| // 2. Set the custom resolver if one was provided in the constructor. | ||
| if (this.resolver != null) { | ||
| lookup.setResolver(this.resolver); | ||
| } | ||
|
|
||
| // 3. Execute the DNS query. | ||
| lookup.run(); | ||
|
|
||
| // 4. Check the result of the lookup. | ||
| int resultCode = lookup.getResult(); | ||
| if (resultCode == Lookup.HOST_NOT_FOUND) { | ||
| throw new NameNotFoundException("DNS record not found for " + domainName); | ||
| } | ||
| if (resultCode == Lookup.TYPE_NOT_FOUND) { | ||
| throw new NameNotFoundException("DNS record type TXT not found for " + domainName); | ||
| } | ||
| if (resultCode != Lookup.SUCCESSFUL) { | ||
| throw new RuntimeException( | ||
| "DNS lookup failed for " + domainName + ": " + lookup.getErrorString()); | ||
| } | ||
|
|
||
| // 5. Process the records, sort them, and return. | ||
| Record[] records = lookup.getAnswers(); | ||
| if (records == null || records.length == 0) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| // A single TXT record can contain multiple strings, so we use flatMap. | ||
| return Arrays.stream(records) | ||
| .map(r -> (TXTRecord) r) | ||
| .flatMap(txtRecord -> txtRecord.getStrings().stream()) | ||
| .sorted() // sort multiple records alphabetically | ||
| .collect(Collectors.toList()); | ||
|
|
||
| } catch (TextParseException e) { | ||
| // This happens if the domainName is not a valid format. | ||
| throw new RuntimeException("Invalid domain name format: " + domainName, e); | ||
| } | ||
| } | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
core/src/test/java/com/google/cloud/sql/core/DnsJavaResolverTest.java
This file contains hidden or 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,76 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.sql.core; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import java.util.Collection; | ||
| import javax.naming.NameNotFoundException; | ||
| import org.junit.Test; | ||
|
|
||
| public class DnsJavaResolverTest { | ||
|
|
||
| private static final String VALID_DOMAIN_NAME = "invalid-san-test.csqlconnectortest.com"; | ||
| private static final String MULTI_STRING_RECORD_DOMAIN_NAME = | ||
| "test-multi-string-txt-record.csqlconnectortest.com"; | ||
| private static final String VALID_DOMAIN_NAME_DATA = | ||
| "cloud-sql-connector-testing:us-central1:postgres-customer-cas-test"; | ||
| private static final String INVALID_DOMAIN_NAME = "not-a-real-domain.com"; | ||
|
|
||
| @Test | ||
| public void testResolveTxt_validDomainName() throws NameNotFoundException { | ||
| DnsJavaResolver resolver = new DnsJavaResolver(); | ||
| Collection<String> records = resolver.resolveTxt(VALID_DOMAIN_NAME); | ||
| assertThat(records).isNotEmpty(); | ||
|
|
||
| // Check that the record contains the expected IP address. | ||
| assertThat(records).contains(VALID_DOMAIN_NAME_DATA); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResolveTxt_multiStringRecord() throws NameNotFoundException { | ||
| DnsJavaResolver resolver = new DnsJavaResolver(); | ||
| Collection<String> records = resolver.resolveTxt(MULTI_STRING_RECORD_DOMAIN_NAME); | ||
| assertThat(records).isNotEmpty(); | ||
| assertThat(records).containsExactly("string1", "string2"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResolveTxt_notFound() { | ||
| DnsJavaResolver resolver = new DnsJavaResolver(); | ||
| assertThrows(NameNotFoundException.class, () -> resolver.resolveTxt(INVALID_DOMAIN_NAME)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDnsJavaResolver_invalidDnsServer() { | ||
| IllegalArgumentException ex = | ||
| assertThrows( | ||
| IllegalArgumentException.class, () -> new DnsJavaResolver("not-a-real-dns-server", 53)); | ||
| assertThat(ex).hasMessageThat().contains("Unknown DNS server host"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDnsJavaResolver_validDnsServer() throws NameNotFoundException { | ||
| // Use a public DNS server to resolve a real domain. | ||
| // Note: "8.8.8.8" is Google's public DNS Server | ||
| DnsJavaResolver resolver = new DnsJavaResolver("8.8.8.8", 53); | ||
| Collection<String> records = resolver.resolveTxt(VALID_DOMAIN_NAME); | ||
| assertThat(records).isNotEmpty(); | ||
| assertThat(records).contains(VALID_DOMAIN_NAME_DATA); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.