Skip to content

Commit 201dd71

Browse files
committed
chore: Use domain name from JDBC URL for SQL Server, Part of #2043.
1 parent 096f5e2 commit 201dd71

File tree

6 files changed

+75
-40
lines changed

6 files changed

+75
-40
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
/**
2525
* This class parses the different parts of a Cloud SQL Connection Name to allow users to easily
2626
* fetch the projectId, regionId, and instanceId.
27+
*
28+
* <p>INTERNAL USE ONLY! This API may change without notice.
2729
*/
28-
class CloudSqlInstanceName {
30+
public class CloudSqlInstanceName {
2931

3032
// Unique identifier for each Cloud SQL instance in the format "PROJECT:REGION:INSTANCE"
3133
// Some legacy project ids are domain-scoped (e.g. "example.com:PROJECT:REGION:INSTANCE")
@@ -36,6 +38,17 @@ class CloudSqlInstanceName {
3638
private final String instanceId;
3739
private final String connectionName;
3840

41+
/**
42+
* Checks if a string is a well-formed instance name.
43+
*
44+
* @param connectionName the value to check
45+
* @return true if it is a well-formed instance name.
46+
*/
47+
public static boolean isValidInstanceName(String connectionName) {
48+
Matcher matcher = CONNECTION_NAME.matcher(connectionName);
49+
return matcher.matches();
50+
}
51+
3952
/**
4053
* Initializes a new CloudSqlInstanceName class.
4154
*

jdbc/sqlserver/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@
114114
</usedDependencies>
115115
</configuration>
116116
</plugin>
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-compiler-plugin</artifactId>
120+
<configuration>
121+
<source>11</source>
122+
<target>11</target>
123+
</configuration>
124+
</plugin>
117125
</plugins>
118126
</build>
119127

jdbc/sqlserver/src/main/java/com/google/cloud/sql/sqlserver/SocketFactory.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import com.google.cloud.sql.core.CloudSqlInstanceName;
1920
import com.google.cloud.sql.core.ConnectionConfig;
2021
import com.google.cloud.sql.core.InternalConnectorRegistry;
2122
import com.google.common.annotations.VisibleForTesting;
@@ -25,7 +26,6 @@
2526
import java.net.InetAddress;
2627
import java.net.Socket;
2728
import java.net.URLDecoder;
28-
import java.nio.charset.StandardCharsets;
2929
import java.util.List;
3030
import java.util.Properties;
3131

@@ -43,14 +43,20 @@ public class SocketFactory extends javax.net.SocketFactory {
4343

4444
// props are protected, not private, so that they can be accessed from unit tests
4545
@VisibleForTesting protected Properties props = new Properties();
46+
@VisibleForTesting protected String domainName;
4647

4748
/**
4849
* Implements the {@link SocketFactory} constructor, which can be used to create authenticated
4950
* connections to a Cloud SQL instance.
5051
*/
5152
public SocketFactory(String socketFactoryConstructorArg) throws UnsupportedEncodingException {
5253
List<String> s = Splitter.on('?').splitToList(socketFactoryConstructorArg);
53-
this.props.setProperty(ConnectionConfig.CLOUD_SQL_INSTANCE_PROPERTY, s.get(0));
54+
final String instanceOrDomainName = s.get(0);
55+
if (CloudSqlInstanceName.isValidInstanceName(instanceOrDomainName)) {
56+
this.props.setProperty(ConnectionConfig.CLOUD_SQL_INSTANCE_PROPERTY, instanceOrDomainName);
57+
} else {
58+
domainName = instanceOrDomainName;
59+
}
5460
if (s.size() == 2 && s.get(1).length() > 0) {
5561
Iterable<String> queryParams = Splitter.on('&').split(s.get(1));
5662
for (String param : queryParams) {
@@ -62,8 +68,8 @@ public SocketFactory(String socketFactoryConstructorArg) throws UnsupportedEncod
6268
String.format("Malformed query param in socketFactoryConstructorArg : %s", param));
6369
}
6470
this.props.setProperty(
65-
URLDecoder.decode(splitParam.get(0), StandardCharsets.UTF_8.name()),
66-
URLDecoder.decode(splitParam.get(1), StandardCharsets.UTF_8.name()));
71+
URLDecoder.decode(splitParam.get(0), "utf-8"),
72+
URLDecoder.decode(splitParam.get(1), "utf-8"));
6773
}
6874
} else if (s.size() > 2) {
6975
throw new IllegalArgumentException(
@@ -75,7 +81,7 @@ public SocketFactory(String socketFactoryConstructorArg) throws UnsupportedEncod
7581
public Socket createSocket() throws IOException {
7682
try {
7783
return InternalConnectorRegistry.getInstance()
78-
.connect(ConnectionConfig.fromConnectionProperties(props));
84+
.connect(ConnectionConfig.fromConnectionProperties(props, domainName));
7985
} catch (InterruptedException e) {
8086
throw new RuntimeException(e);
8187
}

jdbc/sqlserver/src/test/java/com/google/cloud/sql/sqlserver/JdbcSqlServerIntegrationTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public void setUpPool() throws SQLException {
7474
"socketFactoryClass", "com.google.cloud.sql.sqlserver.SocketFactory");
7575
config.addDataSourceProperty("socketFactoryConstructorArg", CONNECTION_NAME);
7676
config.addDataSourceProperty("encrypt", "false");
77-
config.setConnectionTimeout(10000); // 10s
77+
config.setConnectionTimeout(30000); // 30s
78+
config.setInitializationFailTimeout(10000);
79+
config.setValidationTimeout(10000);
7880

7981
this.connectionPool = new HikariDataSource(config);
8082
}

jdbc/sqlserver/src/test/java/com/google/cloud/sql/sqlserver/JdbcSqlServerUnitTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
public class JdbcSqlServerUnitTests {
2929

3030
private static final String CONNECTION_NAME = "my-project:my-region:my-instance";
31+
private static final String DOMAIN_NAME = "mydatabase.example.com";
3132

3233
@Test
3334
public void checkConnectionStringNoQueryParams() throws UnsupportedEncodingException {
@@ -46,6 +47,23 @@ public void checkConnectionStringWithQueryParam() throws UnsupportedEncodingExce
4647
assertThat(socketFactory.props.get("ipTypes")).isEqualTo("PRIVATE");
4748
}
4849

50+
@Test
51+
public void checkConnectionStringDomainNameNoQueryParams() throws UnsupportedEncodingException {
52+
SocketFactory socketFactory = new SocketFactory(DOMAIN_NAME);
53+
assertThat(socketFactory.props.get(ConnectionConfig.CLOUD_SQL_INSTANCE_PROPERTY)).isNull();
54+
assertThat(socketFactory.domainName).isEqualTo(DOMAIN_NAME);
55+
}
56+
57+
@Test
58+
public void checkConnectionStringDomainNameWithQueryParam() throws UnsupportedEncodingException {
59+
String socketFactoryConstructorArg =
60+
String.format("%s?%s=%s", DOMAIN_NAME, "ipTypes", "PRIVATE");
61+
SocketFactory socketFactory = new SocketFactory(socketFactoryConstructorArg);
62+
assertThat(socketFactory.props.get(ConnectionConfig.CLOUD_SQL_INSTANCE_PROPERTY)).isNull();
63+
assertThat(socketFactory.domainName).isEqualTo(DOMAIN_NAME);
64+
assertThat(socketFactory.props.get("ipTypes")).isEqualTo("PRIVATE");
65+
}
66+
4967
@Test
5068
public void checkConnectionStringWithEmptyQueryParam() throws UnsupportedEncodingException {
5169
String socketFactoryConstructorArg = String.format("%s?", CONNECTION_NAME);

pom.xml

+21-33
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@
408408
<artifactId>maven-compiler-plugin</artifactId>
409409
<version>3.13.0</version>
410410
<configuration>
411-
<source>8</source>
412-
<target>8</target>
411+
<source>11</source>
412+
<target>11</target>
413413
<fork>true</fork>
414414
<compilerArgs>
415415
<arg>-XDcompilePolicy=simple</arg>
@@ -516,6 +516,24 @@
516516
</plugin>
517517

518518
</plugins>
519+
520+
<pluginManagement>
521+
<plugins>
522+
<plugin>
523+
<groupId>org.apache.maven.plugins</groupId>
524+
<artifactId>maven-compiler-plugin</artifactId>
525+
<version>3.13.0</version>
526+
<configuration>
527+
<source>11</source>
528+
<target>11</target>
529+
<encoding>UTF-8</encoding>
530+
<compilerArgument>-Xlint:unchecked</compilerArgument>
531+
<compilerArgument>-Xlint:deprecation</compilerArgument>
532+
<showDeprecation>true</showDeprecation>
533+
</configuration>
534+
</plugin>
535+
</plugins>
536+
</pluginManagement>
519537
</build>
520538

521539
<profiles>
@@ -677,36 +695,6 @@
677695
</build>
678696
</profile>
679697

680-
<!-- using github.com/google/error-prone-javac is required when running on JDK 8 -->
681-
<profile>
682-
<id>jdk8</id>
683-
<activation>
684-
<jdk>1.8</jdk>
685-
</activation>
686-
<build>
687-
<plugins>
688-
<plugin>
689-
<groupId>org.apache.maven.plugins</groupId>
690-
<artifactId>maven-compiler-plugin</artifactId>
691-
<configuration>
692-
<fork>true</fork>
693-
<compilerArgs combine.self="override">
694-
<arg>-XDcompilePolicy=simple</arg>
695-
<arg>-Xplugin:ErrorProne</arg>
696-
<arg>
697-
-J-Xbootclasspath/p:${settings.localRepository}/com/google/errorprone/javac/${javac.version}/javac-${javac.version}.jar
698-
</arg>
699-
</compilerArgs>
700-
<excludes>
701-
<!-- Native image does not support Java 8 -->
702-
<exclude>com/google/cloud/sql/nativeimage/*</exclude>
703-
</excludes>
704-
</configuration>
705-
</plugin>
706-
</plugins>
707-
</build>
708-
</profile>
709-
710698
<profile>
711699
<!-- This profile is used to enable GraalVM native image testing
712700
(.kokoro/graalvm-native/linux) -->
@@ -835,7 +823,7 @@
835823
<artifactId>maven-javadoc-plugin</artifactId>
836824
<version>3.11.2</version>
837825
<configuration>
838-
<source>8</source>
826+
<source>11</source>
839827
<sourcepath>src/main/java</sourcepath>
840828
</configuration>
841829
<executions>

0 commit comments

Comments
 (0)