Skip to content
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

chore: Use domain name from JDBC URL for Postgres and Mysql, Part of #2043 #2050

Merged
merged 1 commit into from
Jan 23, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SocketFactory extends ConfigurableSocketFactory {
}

private Configuration conf;
private String host;

/** Instantiate a socket factory. */
public SocketFactory() {}
Expand All @@ -45,13 +46,14 @@ public SocketFactory() {}
public void setConfiguration(Configuration conf, String host) {
// Ignore the hostname
this.conf = conf;
this.host = host;
}

@Override
public Socket createSocket() throws IOException {
try {
return InternalConnectorRegistry.getInstance()
.connect(ConnectionConfig.fromConnectionProperties(conf.nonMappedOptions()));
.connect(ConnectionConfig.fromConnectionProperties(conf.nonMappedOptions(), host));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public <T extends Closeable> T connect(
T socket =
(T)
InternalConnectorRegistry.getInstance()
.connect(ConnectionConfig.fromConnectionProperties(props));
.connect(ConnectionConfig.fromConnectionProperties(props, host));
return socket;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2025 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
*
* https://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.mysql;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import com.google.cloud.sql.ConnectorConfig;
import com.google.cloud.sql.ConnectorRegistry;
import com.google.common.collect.ImmutableList;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class JdbcMysqlJ8DomainNameIntegrationTests {

private static final String CONNECTION_NAME = System.getenv("MYSQL_CONNECTION_NAME");
private static final String DB_NAME = System.getenv("MYSQL_DB");
private static final String DB_USER = System.getenv("MYSQL_USER");
private static final String DB_PASSWORD = System.getenv("MYSQL_PASS");
private static final ImmutableList<String> requiredEnvVars =
ImmutableList.of("MYSQL_USER", "MYSQL_PASS", "MYSQL_DB", "MYSQL_CONNECTION_NAME");
@Rule public Timeout globalTimeout = new Timeout(80, TimeUnit.SECONDS);
private HikariDataSource connectionPool;

@BeforeClass
public static void checkEnvVars() {
// Check that required env vars are set
requiredEnvVars.forEach(
(varName) ->
assertWithMessage(
String.format(
"Environment variable '%s' must be set to perform these tests.", varName))
.that(System.getenv(varName))
.isNotEmpty());
}

@Before
public void setUpPool() throws SQLException {
// Set up URL parameters
String jdbcURL = String.format("jdbc:mysql://db.example.com/%s", DB_NAME);
Properties connProps = new Properties();
connProps.setProperty("user", DB_USER);
connProps.setProperty("password", DB_PASSWORD);
connProps.setProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");

// Register a resolver that resolves `db.example.com` to the connection name
connProps.setProperty("cloudSqlNamedConnector", "resolver-test");
ConnectorRegistry.register(
"resolver-test",
new ConnectorConfig.Builder()
.withInstanceNameResolver((n) -> "db.example.com".equals(n) ? CONNECTION_NAME : null)
.build());

// Initialize connection pool
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcURL);
config.setDataSourceProperties(connProps);
config.setConnectionTimeout(10000); // 10s

this.connectionPool = new HikariDataSource(config);
}

@Test
public void pooledConnectionTest() throws SQLException {

List<Timestamp> rows = new ArrayList<>();
try (Connection conn = connectionPool.getConnection()) {
try (PreparedStatement selectStmt = conn.prepareStatement("SELECT NOW() as TS")) {
ResultSet rs = selectStmt.executeQuery();
while (rs.next()) {
rows.add(rs.getTimestamp("TS"));
}
}
}
assertThat(rows.size()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void checkEnvVars() {
@Before
public void setUpPool() throws SQLException {
// Set up URL parameters
String jdbcURL = String.format("jdbc:mysql:///%s", DB_NAME);
String jdbcURL = String.format("jdbc:mysql://db.example.com/%s", DB_NAME);
Properties connProps = new Properties();
connProps.setProperty("user", DB_USER);
connProps.setProperty("password", DB_PASSWORD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class SocketFactory extends javax.net.SocketFactory {
private static final String DEPRECATED_SOCKET_ARG = "SocketFactoryArg";
private static final String POSTGRES_SUFFIX = "/.s.PGSQL.5432";

/** The connection property containing the hostname from the JDBC url. */
private static final String POSTGRES_HOST_PROP = "PGHOST";

private final Properties props;

static {
Expand Down Expand Up @@ -83,7 +86,9 @@ private static Properties createDefaultProperties(String instanceName) {
public Socket createSocket() throws IOException {
try {
return InternalConnectorRegistry.getInstance()
.connect(ConnectionConfig.fromConnectionProperties(props));
.connect(
ConnectionConfig.fromConnectionProperties(
props, props.getProperty(POSTGRES_HOST_PROP)));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2025 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
*
* https://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.postgres;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import com.google.cloud.sql.ConnectorConfig;
import com.google.cloud.sql.ConnectorRegistry;
import com.google.common.collect.ImmutableList;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class JdbcPostgresDomainNameIntegrationTests {

private static final String CONNECTION_NAME = System.getenv("POSTGRES_CONNECTION_NAME");
private static final String DB_NAME = System.getenv("POSTGRES_DB");
private static final String DB_USER = System.getenv("POSTGRES_USER");
private static final String DB_PASSWORD = System.getenv("POSTGRES_PASS");
private static final ImmutableList<String> requiredEnvVars =
ImmutableList.of("POSTGRES_USER", "POSTGRES_PASS", "POSTGRES_DB", "POSTGRES_CONNECTION_NAME");
@Rule public Timeout globalTimeout = new Timeout(80, TimeUnit.SECONDS);

private HikariDataSource connectionPool;

@BeforeClass
public static void checkEnvVars() {
// Check that required env vars are set
requiredEnvVars.forEach(
(varName) ->
assertWithMessage(
String.format(
"Environment variable '%s' must be set to perform these tests.", varName))
.that(System.getenv(varName))
.isNotEmpty());
}

@Before
public void setUpPool() throws SQLException {
// Set up URL parameters
String jdbcURL = String.format("jdbc:postgresql://db.example.com/%s", DB_NAME);
Properties connProps = new Properties();
connProps.setProperty("user", DB_USER);
connProps.setProperty("password", DB_PASSWORD);
connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory");
connProps.setProperty("cloudSqlNamedConnector", "resolver-test");

ConnectorRegistry.register(
"resolver-test",
new ConnectorConfig.Builder()
.withInstanceNameResolver((n) -> "db.example.com".equals(n) ? CONNECTION_NAME : null)
.build());

// Initialize connection pool
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcURL);
config.setDataSourceProperties(connProps);
config.setConnectionTimeout(10000); // 10s

this.connectionPool = new HikariDataSource(config);
}

@Test
public void pooledConnectionTest() throws SQLException {

List<Timestamp> rows = new ArrayList<>();
try (Connection conn = connectionPool.getConnection()) {
try (PreparedStatement selectStmt = conn.prepareStatement("SELECT NOW() as TS")) {
ResultSet rs = selectStmt.executeQuery();
while (rs.next()) {
rows.add(rs.getTimestamp("TS"));
}
}
}
assertThat(rows.size()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void checkEnvVars() {
@Before
public void setUpPool() throws SQLException {
// Set up URL parameters
String jdbcURL = String.format("jdbc:postgresql:///%s", DB_NAME);
String jdbcURL = String.format("jdbc:postgresql://db.example.com/%s", DB_NAME);
Properties connProps = new Properties();
connProps.setProperty("user", DB_USER);
connProps.setProperty("password", DB_PASSWORD);
Expand Down
Loading