Skip to content

Commit 45689a2

Browse files
committed
fix: Add GraalVM configuration for the JDK's DNS Resolver.
This fixes the GraalVM distribution so that it can resolve DNS Names.
1 parent 88631fc commit 45689a2

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

core/src/main/java/com/google/cloud/sql/core/JndiDnsResolver.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.util.Collection;
2020
import java.util.Collections;
21+
import java.util.Hashtable;
2122
import java.util.stream.Collectors;
23+
import javax.naming.Context;
2224
import javax.naming.NameNotFoundException;
2325
import javax.naming.NamingException;
2426
import javax.naming.directory.Attribute;
@@ -51,13 +53,19 @@ class JndiDnsResolver implements DnsResolver {
5153
* @throws javax.naming.NameNotFoundException when the domain name did not resolve.
5254
*/
5355
@Override
56+
@SuppressWarnings("JdkObsolete")
5457
public Collection<String> resolveTxt(String domainName)
5558
throws javax.naming.NameNotFoundException {
5659
try {
5760
// Notice: This is old Java 1.2 style code. It uses the ancient JNDI DNS Provider api.
5861
// See https://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html
62+
63+
// Explicitly reference the JNDI DNS classes. This is required for GraalVM.
64+
Hashtable contextProps = new Hashtable<>();
65+
contextProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
66+
contextProps.put(Context.OBJECT_FACTORIES, "com.sun.jndi.url.dns.dnsURLContextFactory");
5967
Attribute attr =
60-
new InitialDirContext()
68+
new InitialDirContext(contextProps)
6169
.getAttributes(jndiPrefix + domainName, new String[] {"TXT"})
6270
.get("TXT");
6371
// attr.getAll() returns a Vector containing strings, one for each record returned by dns.

core/src/main/java/com/google/cloud/sql/nativeimage/CloudSqlFeature.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ final class CloudSqlFeature implements Feature {
4545
private static final String POSTGRES_SOCKET_CLASS = "com.google.cloud.sql.postgres.SocketFactory";
4646

4747
private static final String MYSQL_SOCKET_CLASS = "com.google.cloud.sql.mysql.SocketFactory";
48+
private static final String JNDI_DNS_FACTORY = "com.sun.jndi.dns.DnsContextFactory";
49+
private static final String JNDI_DNS_OBJECT_FACTORY = "com.sun.jndi.url.dns.dnsURLContextFactory";
4850

4951
@Override
5052
public void beforeAnalysis(BeforeAnalysisAccess access) {
@@ -55,6 +57,10 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
5557
// The Core Cloud SQL Socket
5658
NativeImageUtils.registerClassForReflection(access, CLOUD_SQL_SOCKET_CLASS);
5759

60+
// The JNDI DNS factory for looking up DNS names.
61+
NativeImageUtils.registerClassForReflection(access, JNDI_DNS_FACTORY);
62+
NativeImageUtils.registerClassForReflection(access, JNDI_DNS_OBJECT_FACTORY);
63+
5864
// Register Hikari configs if used with Cloud SQL.
5965
if (access.findClassByName("com.zaxxer.hikari.HikariConfig") != null) {
6066
NativeImageUtils.registerClassForReflection(access, "com.zaxxer.hikari.HikariConfig");

core/src/main/resources/META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/proxy-config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,17 @@
1818
"jnr.enxio.channels.Native$LibC",
1919
"jnr.ffi.provider.LoadedLibrary"
2020
]
21+
},
22+
{
23+
"name": "com.sun.jndi.dns.DnsContextFactory",
24+
"interfaces": [
25+
"javax.naming.spi.InitialContextFactory"
26+
]
27+
},
28+
{
29+
"name": "com.sun.jndi.url.dns.dnsURLContextFactory",
30+
"interfaces": [
31+
"javax.naming.spi.ObjectFactory"
32+
]
2133
}
2234
]

0 commit comments

Comments
 (0)