Skip to content

Commit 70a5a83

Browse files
authored
feat: add iam authn example for mysql (GoogleCloudPlatform#7485)
1 parent df21ac1 commit 70a5a83

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

cloud-sql/mysql/servlet/src/main/java/com/example/cloudsql/ConnectionPoolContextListener.java

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public void contextInitialized(ServletContextEvent event) {
4949
if (pool == null) {
5050
if (System.getenv("INSTANCE_HOST") != null) {
5151
pool = TcpConnectionPoolFactory.createConnectionPool();
52+
} else if (System.getenv("DB_IAM_USER") != null) {
53+
pool = ConnectorIamAuthnConnectionPoolFactory.createConnectionPool();
5254
} else {
5355
pool = ConnectorConnectionPoolFactory.createConnectionPool();
5456
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2022 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.example.cloudsql;
18+
19+
// [START cloud_sql_mysql_servlet_auto_iam_authn]
20+
import com.zaxxer.hikari.HikariConfig;
21+
import com.zaxxer.hikari.HikariDataSource;
22+
import javax.sql.DataSource;
23+
24+
public class ConnectorIamAuthnConnectionPoolFactory extends ConnectionPoolFactory {
25+
26+
// Note: Saving credentials in environment variables is convenient, but not
27+
// secure - consider a more secure solution such as
28+
// Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
29+
// keep secrets safe.
30+
private static final String INSTANCE_CONNECTION_NAME =
31+
System.getenv("INSTANCE_CONNECTION_NAME");
32+
private static final String INSTANCE_UNIX_SOCKET = System.getenv("INSTANCE_UNIX_SOCKET");
33+
private static final String DB_IAM_USER = System.getenv("DB_IAM_USER");
34+
private static final String DB_NAME = System.getenv("DB_NAME");
35+
36+
37+
public static DataSource createConnectionPool() {
38+
// The configuration object specifies behaviors for the connection pool.
39+
HikariConfig config = new HikariConfig();
40+
41+
// The following URL is equivalent to setting the config options below:
42+
// jdbc:mysql:///<DB_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&
43+
// socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<DB_USER>&password=<DB_PASS>
44+
// See the link below for more info on building a JDBC URL for the Cloud SQL JDBC Socket Factory
45+
// https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url
46+
47+
// Configure which instance and what database user to connect with.
48+
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
49+
50+
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
51+
config.addDataSourceProperty("cloudSqlInstance", INSTANCE_CONNECTION_NAME);
52+
53+
// If connecting using automatic database authentication, follow the instructions for
54+
// connecting using the connector, but set the DB_IAM_USER value to an IAM user or
55+
// service account that has been given access to the database.
56+
// See https://cloud.google.com/sql/docs/postgres/iam-logins for more details.
57+
config.addDataSourceProperty("enableIamAuth", "true");
58+
config.addDataSourceProperty("user", DB_IAM_USER);
59+
// Explicitly set sslmode to disable to prevent driver from hanging.
60+
// The Java Connector will handle SSL so it is unneccesary to enable it at the driver level.
61+
config.addDataSourceProperty("sslmode", "disable");
62+
63+
64+
// ... Specify additional connection properties here.
65+
// [START_EXCLUDE]
66+
configureConnectionPool(config);
67+
// [END_EXCLUDE]
68+
69+
// Initialize the connection pool using the configuration object.
70+
return new HikariDataSource(config);
71+
}
72+
}
73+
// [END cloud_sql_mysql_servlet_auto_iam_authn]

0 commit comments

Comments
 (0)