Skip to content

chore: Create a new DnsResolver class that wraps the Java DNS API #2045

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 1 commit into from
Aug 30, 2024
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
24 changes: 24 additions & 0 deletions core/src/main/java/com/google/cloud/sql/core/DnsResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024 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.util.Collection;
import javax.naming.NameNotFoundException;

interface DnsResolver {
Collection<String> resolveTxt(String domainName) throws NameNotFoundException;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are using dnsName in other places, is domainName the same thing or is it a different meaning? If they are one and the same we should consolidate usage to one naming pattern

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go connector uses DomainName as the configuration field.

I considered fqdn, dnsName, domainName, host, and hostname. FQDN or FullyQualifiedDomainName is too wordy, and dnsName is an expands to "domain name system name", which is self-redundant. I was tempted host, or hostname because that is commonly what it is called as part of the a URL RFC 1738. I decided against it because I wanted to be specific that it needs to contain the domain name.

Go connector uses DNSInstanceConnectionNameResolver for the resolver class name, and DNSResolver for the singleton instance. This refers to the fact that it uses the DNS system to resolve the DomainName into an instance connection name. In retrospect, this felt too wordy, so the class name is DNSResolver in java.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 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.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.InitialDirContext;

/** Implements DnsResolver using the Java JNDI built-in DNS directory. */
class JndiDnsResolver implements DnsResolver {
private final String jndiPrefix;

/** Creates a resolver using the system DNS settings. */
JndiDnsResolver() {
this.jndiPrefix = "dns:";
}

/**
* 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)
*/
JndiDnsResolver(String dnsServer, int port) {
this.jndiPrefix = "dns://" + dnsServer + ":" + port + "/";
}

/**
* Returns DNS records for a domain name, sorted alphabetically.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a domain name, we should only get 1 TXT record if one exists. Why do we have Collection for the return type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect exactly 1 DNS record for an instance. However, it is possible that the connector could receive 0, 1, or many DNS records. Other application logic will gracefully handle cases where the number of responses is not 1 as expected. This class is only responsible for resolving DNS and reporting the results. Thus its data model is in line with the DNS protocol, returning a collection that may contain 0 or more responses.

*
* @param domainName the domain name to lookup
* @return the list of record
* @throws javax.naming.NameNotFoundException when the domain name did not resolve.
*/
@Override
public Collection<String> resolveTxt(String domainName)
throws javax.naming.NameNotFoundException {
try {
// Notice: This is old Java 1.2 style code. It uses the ancient JNDI DNS Provider api.
// See https://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html
Attribute attr =
new InitialDirContext()
.getAttributes(jndiPrefix + domainName, new String[] {"TXT"})
.get("TXT");
// attr.getAll() returns a Vector containing strings, one for each record returned by dns.
return Collections.list(attr.getAll()).stream()
.map((Object v) -> (String) v)
.sorted() // sort multiple records alphabetically
.collect(Collectors.toList());
} catch (NameNotFoundException e) {
throw e;
} catch (NamingException e) {
throw new RuntimeException("Unable to look up domain name " + domainName, e);
}
}
}
Loading