Skip to content

Commit 6231530

Browse files
authored
Merge branch 'main' into refactor-protobuf
2 parents 7ad5445 + 7151a94 commit 6231530

25 files changed

Lines changed: 304 additions & 138 deletions

File tree

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Changelog
22

3+
## [1.28.0](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/compare/v1.27.1...v1.28.0) (2026-01-13)
4+
5+
6+
### Features
7+
8+
* Use configured DNS name to lookup instance IP address ([#2244](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/2244)) ([f89e805](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/commit/f89e8057b7c9568e85c8ceb8beb7b9df9f9b9e69)), closes [#2243](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/2243)
9+
10+
11+
### Bug Fixes
12+
13+
* Update the R2DBC connectors to work with Netty 4.2+ ([#2240](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/2240)) ([efa8bb9](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/commit/efa8bb980f205b8edb5d841929cfb8602d1573eb)), closes [#2227](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/2227)
14+
15+
16+
### Dependencies
17+
18+
* Update project dependencies to latest ([#2249](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/2249)) ([6881969](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/commit/6881969e2e792c056de65c12637c09f7e4cfa993))
19+
320
## [1.27.1](https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/compare/v1.27.0...v1.27.1) (2025-12-09)
421

522

build.sh

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
SCRIPT_DIR=$(cd -P "$(dirname "$0")" >/dev/null 2>&1 && pwd)
1919
SCRIPT_FILE="${SCRIPT_DIR}/$(basename "$0")"
2020

21+
mvn_cmd=$SCRIPT_DIR/mvnw
22+
2123
##
2224
## Local Development
2325
##
@@ -26,23 +28,27 @@ SCRIPT_FILE="${SCRIPT_DIR}/$(basename "$0")"
2628

2729
## clean - Cleans the build output
2830
function clean() {
29-
mvn clean
31+
$mvn_cmd clean
3032
}
3133

3234
## build - Builds the project without running tests.
3335
function build() {
34-
mvn install -DskipTests=true
36+
$mvn_cmd install -DskipTests=true
3537
}
3638

3739
## test - Runs local unit tests.
3840
function test() {
3941
if [[ "$(uname -s)" == "Darwin" ]]; then
4042
echo "macOS detected. Setting up IP aliases for tests."
4143
echo "You may be prompted for your password to run sudo."
42-
sudo ifconfig lo0 alias 127.0.0.2 up
43-
sudo ifconfig lo0 alias 127.0.0.3 up
44+
if ! ifconfig lo0 | grep -q 127.0.0.2 ; then
45+
sudo ifconfig lo0 alias 127.0.0.2 up
46+
fi
47+
if ! ifconfig lo0 | grep -q 127.0.0.3 ; then
48+
sudo ifconfig lo0 alias 127.0.0.3 up
49+
fi
4450
fi
45-
mvn -P coverage test
51+
$mvn_cmd -P coverage test
4652
}
4753

4854
## e2e - Runs end-to-end integration tests.
@@ -66,18 +72,18 @@ function e2e_graalvm() {
6672

6773
## fix - Fixes java code format.
6874
function fix() {
69-
mvn com.coveo:fmt-maven-plugin:format
75+
$mvn_cmd com.coveo:fmt-maven-plugin:format
7076
}
7177

7278
## lint - runs the java lint
7379
function lint() {
74-
mvn -P lint install -DskipTests=true
80+
$mvn_cmd -P lint install -DskipTests=true
7581
}
7682

7783

7884
## deps - updates dependencies to the latest version
7985
function deps() {
80-
mvn versions:use-latest-versions
86+
$mvn_cmd versions:use-latest-versions
8187
find . -name 'pom.xml.versionsBackup' -print0 | xargs -0 rm -f
8288
}
8389

@@ -91,7 +97,7 @@ function write_e2e_env(){
9197
secret_vars=(
9298
MYSQL_CONNECTION_NAME=MYSQL_CONNECTION_NAME
9399
MYSQL_USER=MYSQL_USER
94-
MYSQL_USER_IAM=MYSQL_USER_IAM_GO
100+
IMPERSONATED_USER=IMPERSONATED_USER
95101
MYSQL_PASS=MYSQL_PASS
96102
MYSQL_DB=MYSQL_DB
97103
MYSQL_MCP_CONNECTION_NAME=MYSQL_MCP_CONNECTION_NAME
@@ -105,8 +111,8 @@ function write_e2e_env(){
105111
POSTGRES_CAS_PASS=POSTGRES_CAS_PASS
106112
POSTGRES_CUSTOMER_CAS_CONNECTION_NAME=POSTGRES_CUSTOMER_CAS_CONNECTION_NAME
107113
POSTGRES_CUSTOMER_CAS_PASS=POSTGRES_CUSTOMER_CAS_PASS
108-
POSTGRES_CUSTOMER_CAS_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_DOMAIN_NAME
109-
POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME
114+
POSTGRES_CUSTOMER_CAS_PASS_VALID_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_DOMAIN_NAME
115+
POSTGRES_CUSTOMER_CAS_PASS_INVALID_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME
110116
POSTGRES_MCP_CONNECTION_NAME=POSTGRES_MCP_CONNECTION_NAME
111117
POSTGRES_MCP_PASS=POSTGRES_MCP_PASS
112118
SQLSERVER_CONNECTION_NAME=SQLSERVER_CONNECTION_NAME
@@ -131,6 +137,10 @@ function write_e2e_env(){
131137
val=$(gcloud secrets versions access latest --project "$TEST_PROJECT" --secret="$secret_name")
132138
echo "export $env_var_name='$val'"
133139
done
140+
141+
echo "export MYSQL_IAM_USER='$(whoami)'"
142+
echo "export POSTGRES_IAM_USER='$(whoami)@google.com'"
143+
134144
} > "$outfile"
135145

136146
}

core/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
<parent>
2424
<groupId>com.google.cloud.sql</groupId>
2525
<artifactId>jdbc-socket-factory-parent</artifactId>
26-
<version>1.27.2-SNAPSHOT</version><!-- {x-version-update:jdbc-socket-factory-parent:current} -->
26+
<version>1.28.1-SNAPSHOT</version><!-- {x-version-update:jdbc-socket-factory-parent:current} -->
2727
</parent>
2828
<artifactId>jdbc-socket-factory-core</artifactId>
29-
<version>1.27.2-SNAPSHOT</version><!-- {x-version-update:jdbc-socket-factory-core:current} -->
29+
<version>1.28.1-SNAPSHOT</version><!-- {x-version-update:jdbc-socket-factory-core:current} -->
3030
<packaging>jar</packaging>
3131

3232
<name>Cloud SQL Core Socket Factory (Core Library, don't depend on this directly)</name>
@@ -61,13 +61,13 @@
6161
<dependency>
6262
<groupId>com.github.jnr</groupId>
6363
<artifactId>jnr-enxio</artifactId>
64-
<version>0.32.18</version>
64+
<version>0.32.19</version>
6565
</dependency>
6666

6767
<dependency>
6868
<groupId>com.github.jnr</groupId>
6969
<artifactId>jnr-unixsocket</artifactId>
70-
<version>0.38.23</version>
70+
<version>0.38.24</version>
7171
<exclusions>
7272
<exclusion>
7373
<groupId>org.ow2.asm</groupId>

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
2525
import java.io.File;
2626
import java.io.IOException;
27+
import java.net.InetAddress;
2728
import java.net.InetSocketAddress;
2829
import java.net.Socket;
30+
import java.net.UnknownHostException;
2931
import java.security.KeyPair;
32+
import java.util.List;
3033
import java.util.Timer;
3134
import java.util.concurrent.ConcurrentHashMap;
3235
import java.util.concurrent.ExecutionException;
@@ -52,6 +55,7 @@ class Connector {
5255
private final ConnectorConfig config;
5356

5457
private final InstanceConnectionNameResolver instanceNameResolver;
58+
private final DnsResolver dnsResolver;
5559
private final Timer instanceNameResolverTimer;
5660
private final ProtocolHandler mdxProtocolHandler;
5761

@@ -65,9 +69,9 @@ class Connector {
6569
long refreshTimeoutMs,
6670
int serverProxyPort,
6771
InstanceConnectionNameResolver instanceNameResolver,
72+
DnsResolver dnsResolver,
6873
ProtocolHandler mdxProtocolHandler) {
6974
this.config = config;
70-
7175
this.adminApi =
7276
connectionInfoRepositoryFactory.create(instanceCredentialFactory.create(), config);
7377
this.instanceCredentialFactory = instanceCredentialFactory;
@@ -76,6 +80,7 @@ class Connector {
7680
this.minRefreshDelayMs = minRefreshDelayMs;
7781
this.serverProxyPort = serverProxyPort;
7882
this.instanceNameResolver = instanceNameResolver;
83+
this.dnsResolver = dnsResolver;
7984
this.instanceNameResolverTimer = new Timer("InstanceNameResolverTimer", true);
8085
this.mdxProtocolHandler = mdxProtocolHandler;
8186
}
@@ -125,6 +130,40 @@ Socket connect(ConnectionConfig config, long timeoutMs) throws IOException {
125130
try {
126131
ConnectionMetadata metadata = instance.getConnectionMetadata(timeoutMs);
127132
String instanceIp = metadata.getPreferredIpAddress();
133+
134+
// If a domain name was used to connect, resolve it to an IP address
135+
if (!Strings.isNullOrEmpty(instance.getConfig().getDomainName())) {
136+
try {
137+
List<InetAddress> addrs = dnsResolver.resolveHost(instance.getConfig().getDomainName());
138+
if (addrs != null && !addrs.isEmpty()) {
139+
logger.debug(
140+
String.format(
141+
"[%s] custom DNS name %s resolved to %s, using it to connect",
142+
instance.getConfig().getCloudSqlInstance(),
143+
instance.getConfig().getDomainName(),
144+
addrs.get(0).getHostAddress()));
145+
instanceIp = addrs.get(0).getHostAddress();
146+
} else {
147+
logger.debug(
148+
String.format(
149+
"[%s] custom DNS name %s resolved but returned no entries, using %s from"
150+
+ " instance metadata",
151+
instance.getConfig().getCloudSqlInstance(),
152+
instance.getConfig().getDomainName(),
153+
instanceIp));
154+
}
155+
} catch (UnknownHostException e) {
156+
logger.debug(
157+
String.format(
158+
"[%s] custom DNS name %s did not resolve to an IP address: %s, using %s from"
159+
+ " instance metadata",
160+
instance.getConfig().getCloudSqlInstance(),
161+
instance.getConfig().getDomainName(),
162+
e.getMessage(),
163+
instanceIp));
164+
}
165+
}
166+
128167
logger.debug(String.format("[%s] Connecting to instance.", instanceIp));
129168

130169
SSLSocket socket = (SSLSocket) metadata.getSslContext().getSocketFactory().createSocket();

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
package com.google.cloud.sql.core;
1818

19+
import java.net.InetAddress;
1920
import java.net.UnknownHostException;
2021
import java.util.Arrays;
2122
import java.util.Collection;
23+
import java.util.Collections;
24+
import java.util.List;
2225
import java.util.stream.Collectors;
2326
import javax.naming.NameNotFoundException;
27+
import org.xbill.DNS.ARecord;
2428
import org.xbill.DNS.Lookup;
2529
import org.xbill.DNS.Record;
2630
import org.xbill.DNS.SimpleResolver;
@@ -105,4 +109,44 @@ public Collection<String> resolveTxt(String domainName) throws NameNotFoundExcep
105109
throw new RuntimeException("Invalid domain name format: " + domainName, e);
106110
}
107111
}
112+
113+
/**
114+
* Resolve an A record.
115+
*
116+
* @param hostName the hostname to look up
117+
* @return the resolved IP addresses
118+
* @throws UnknownHostException if no records are found.
119+
*/
120+
@Override
121+
public List<InetAddress> resolveHost(String hostName) throws UnknownHostException {
122+
try {
123+
Lookup lookup = new Lookup(hostName, Type.A);
124+
if (this.resolver != null) {
125+
lookup.setResolver(this.resolver);
126+
}
127+
lookup.run();
128+
129+
int resultCode = lookup.getResult();
130+
if (resultCode == Lookup.HOST_NOT_FOUND) {
131+
throw new UnknownHostException("DNS record not found for " + hostName);
132+
}
133+
if (resultCode != Lookup.SUCCESSFUL) {
134+
throw new UnknownHostException(
135+
"DNS lookup failed for " + hostName + ": " + lookup.getErrorString());
136+
}
137+
138+
Record[] records = lookup.getAnswers();
139+
if (records == null || records.length == 0) {
140+
return Collections.emptyList();
141+
}
142+
143+
return Arrays.stream(records)
144+
.map(r -> (ARecord) r)
145+
.map(ARecord::getAddress)
146+
.collect(Collectors.toList());
147+
148+
} catch (TextParseException e) {
149+
throw new UnknownHostException("Invalid domain name format: " + hostName);
150+
}
151+
}
108152
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616

1717
package com.google.cloud.sql.core;
1818

19+
import java.net.InetAddress;
20+
import java.net.UnknownHostException;
1921
import java.util.Collection;
22+
import java.util.List;
2023
import javax.naming.NameNotFoundException;
2124

2225
/** Wraps the Java DNS API. */
2326
interface DnsResolver {
2427
Collection<String> resolveTxt(String domainName) throws NameNotFoundException;
28+
29+
List<InetAddress> resolveHost(String hostName) throws UnknownHostException;
2530
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ private Connector createConnector(ConnectorConfig config) {
339339
connectTimeoutMs,
340340
serverProxyPort,
341341
new DnsInstanceConnectionNameResolver(new DnsJavaResolver()),
342+
new DnsJavaResolver(),
342343
this.mdxProtocolHandler);
343344
}
344345

0 commit comments

Comments
 (0)