Skip to content

Commit d878160

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 d878160

5 files changed

Lines changed: 59 additions & 1 deletion

File tree

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

Lines changed: 8 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,18 @@ 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+
Hashtable contextProps = new Hashtable<>();
64+
contextProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
65+
contextProps.put(Context.OBJECT_FACTORIES, "com.sun.jndi.url.dns.dnsURLContextFactory");
5966
Attribute attr =
60-
new InitialDirContext()
67+
new InitialDirContext(contextProps)
6168
.getAttributes(jndiPrefix + domainName, new String[] {"TXT"})
6269
.get("TXT");
6370
// 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
]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"resources":[
33
{"pattern":"\\Qcom.google.cloud.sql/project.properties\\E"},
44
{"pattern":"\\QMETA-INF/services/java.sql.Driver\\E"},
5+
{"pattern":"\\QMETA-INF/services/java.sql.Driver\\E"},
56
{"pattern":"\\Qcom/mysql/cj/util/TimeZoneMapping.properties\\E"},
67
{"pattern":"jni/.*\\.so"},
78
{"pattern":"jni/.*\\.dll"}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.sql.core;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.util.Collection;
22+
import org.junit.Test;
23+
24+
public class JndiDnsResolverTest {
25+
@Test
26+
public void testResolveName() throws Exception {
27+
JndiDnsResolver r = new JndiDnsResolver();
28+
Collection<String> got = r.resolveTxt("valid-san-test.csqlconnectortest.com");
29+
assertThat(got)
30+
.containsExactly("cloud-sql-connector-testing:us-central1:postgres-customer-cas-test");
31+
}
32+
}

0 commit comments

Comments
 (0)