Skip to content

Commit 3625712

Browse files
authored
chore: split out Java connector IAM AuthN sample and region tags (GoogleCloudPlatform#7206)
1 parent 3f9be37 commit 3625712

File tree

3 files changed

+76
-21
lines changed

3 files changed

+76
-21
lines changed

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

Lines changed: 2 additions & 0 deletions
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
}

cloud-sql/postgres/servlet/src/main/java/com/example/cloudsql/ConnectorConnectionPoolFactory.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,6 @@ public static DataSource createConnectionPool() {
7272
config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE");
7373
// [START cloud_sql_postgres_servlet_connect_unix]
7474

75-
// [END cloud_sql_postgres_servlet_connect_connector]
76-
// [END cloud_sql_postgres_servlet_connect_unix]
77-
// [START cloud_sql_postgres_servlet_auto_iam_authn]
78-
// If connecting using automatic database authentication, follow the instructions for
79-
// connecting using the connector, but set the DB_IAM_USER value to an IAM user or
80-
// service account that has been given access to the database.
81-
// See https://cloud.google.com/sql/docs/postgres/iam-logins for more details.
82-
String dbIamUser = System.getenv("DB_IAM_USER");
83-
if (dbIamUser != null) {
84-
config.addDataSourceProperty("enableIamAuth", "true");
85-
config.addDataSourceProperty("user", dbIamUser);
86-
// Password must be set to a nonempty value to bypass driver validation errors.
87-
config.addDataSourceProperty("password", "password");
88-
// Explicitly set sslmode to disable to prevent driver from hanging.
89-
// The Java Connector will handle SSL so it is unneccesary to enable it at the driver level.
90-
config.addDataSourceProperty("sslmode", "disable");
91-
}
92-
// [END cloud_sql_postgres_servlet_auto_iam_authn]
93-
// [START cloud_sql_postgres_servlet_connect_connector]
94-
// [START cloud_sql_postgres_servlet_connect_unix]
95-
9675

9776
// ... Specify additional connection properties here.
9877
// [START_EXCLUDE]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_postgres_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 DB_IAM_USER = System.getenv("DB_IAM_USER");
33+
private static final String DB_NAME = System.getenv("DB_NAME");
34+
35+
public static DataSource createConnectionPool() {
36+
// The configuration object specifies behaviors for the connection pool.
37+
HikariConfig config = new HikariConfig();
38+
39+
// The following URL is equivalent to setting the config options below:
40+
// jdbc:postgresql:///<DB_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&
41+
// socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<DB_IAM_USER>&
42+
// password=password
43+
// See the link below for more info on building a JDBC URL for the Cloud SQL JDBC Socket Factory
44+
// https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url
45+
46+
// Configure which instance and what database to connect with.
47+
config.setJdbcUrl(String.format("jdbc:postgresql:///%s", DB_NAME));
48+
49+
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql_postgres.SocketFactory");
50+
config.addDataSourceProperty("cloudSqlInstance", INSTANCE_CONNECTION_NAME);
51+
52+
// If connecting using automatic database authentication, follow the instructions for
53+
// connecting using the connector, but set the DB_IAM_USER value to an IAM user or
54+
// service account that has been given access to the database.
55+
// See https://cloud.google.com/sql/docs/postgres/iam-logins for more details.
56+
config.addDataSourceProperty("enableIamAuth", "true");
57+
config.addDataSourceProperty("user", DB_IAM_USER);
58+
// Password must be set to a nonempty value to bypass driver validation errors.
59+
config.addDataSourceProperty("password", "password");
60+
// Explicitly set sslmode to disable to prevent driver from hanging.
61+
// The Java Connector will handle SSL so it is unneccesary to enable it at the driver level.
62+
config.addDataSourceProperty("sslmode", "disable");
63+
64+
65+
// ... Specify additional connection properties here.
66+
// [START_EXCLUDE]
67+
configureConnectionPool(config);
68+
// [END_EXCLUDE]
69+
70+
// Initialize the connection pool using the configuration object.
71+
return new HikariDataSource(config);
72+
}
73+
}
74+
// [END cloud_sql_postgres_servlet_auto_iam_authn]

0 commit comments

Comments
 (0)