Skip to content

Commit 1f5c014

Browse files
refactor property definitions
expose driver properties for username, password and database name fix and enable some data source integration tests
1 parent dcdc444 commit 1f5c014

File tree

16 files changed

+133
-148
lines changed

16 files changed

+133
-148
lines changed

wrapper/src/main/java/com/amazon/awslabs/jdbc/ConnectionPropertyNames.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

wrapper/src/main/java/com/amazon/awslabs/jdbc/DataSourceConnectionProvider.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
package com.amazon.awslabs.jdbc;
2020

21-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.DATABASE_PROPERTY_NAME;
22-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.PASSWORD_PROPERTY_NAME;
23-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.USER_PROPERTY_NAME;
2421
import static com.amazon.awslabs.jdbc.util.ConnectionUrlBuilder.buildUrl;
2522
import static com.amazon.awslabs.jdbc.util.StringUtils.isNullOrEmpty;
2623

@@ -92,16 +89,16 @@ public Connection connect(
9289
copy.put(this.portPropertyName, hostSpec.getPort());
9390
}
9491

95-
if (!isNullOrEmpty(this.databasePropertyName) && !isNullOrEmpty(props.getProperty(DATABASE_PROPERTY_NAME))) {
96-
copy.setProperty(this.databasePropertyName, props.getProperty(DATABASE_PROPERTY_NAME));
92+
if (!isNullOrEmpty(this.databasePropertyName) && !isNullOrEmpty(PropertyDefinition.DATABASE_NAME.getString(props))) {
93+
copy.setProperty(this.databasePropertyName, PropertyDefinition.DATABASE_NAME.getString(props));
9794
}
9895

99-
if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
100-
copy.setProperty(this.userPropertyName, props.getProperty(USER_PROPERTY_NAME));
96+
if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(PropertyDefinition.USER.getString(props))) {
97+
copy.setProperty(this.userPropertyName, PropertyDefinition.USER.getString(props));
10198
}
10299

103-
if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
104-
copy.setProperty(this.passwordPropertyName, props.getProperty(PASSWORD_PROPERTY_NAME));
100+
if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(PropertyDefinition.PASSWORD.getString(props))) {
101+
copy.setProperty(this.passwordPropertyName, PropertyDefinition.PASSWORD.getString(props));
105102
}
106103

107104
if (!isNullOrEmpty(this.urlPropertyName) && !isNullOrEmpty(props.getProperty(this.urlPropertyName))) {
@@ -122,6 +119,10 @@ public Connection connect(
122119
urlProperties));
123120
}
124121

122+
copy.remove(PropertyDefinition.DATABASE_NAME.name);
123+
copy.remove(PropertyDefinition.USER.name);
124+
copy.remove(PropertyDefinition.PASSWORD.name);
125+
125126
PropertyUtils.applyProperties(this.dataSource, copy);
126127
return this.dataSource.getConnection();
127128
}
@@ -141,18 +142,22 @@ public Connection connect(final @NonNull String url, final @NonNull Properties p
141142
copy.setProperty(this.urlPropertyName, url);
142143
}
143144

144-
if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
145-
copy.put(this.userPropertyName, props.getProperty(USER_PROPERTY_NAME));
145+
if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(PropertyDefinition.USER.getString(props))) {
146+
copy.put(this.userPropertyName, PropertyDefinition.USER.getString(props));
146147
}
147148

148-
if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
149-
copy.put(this.passwordPropertyName, props.getProperty(PASSWORD_PROPERTY_NAME));
149+
if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(PropertyDefinition.PASSWORD.getString(props))) {
150+
copy.put(this.passwordPropertyName, PropertyDefinition.PASSWORD.getString(props));
150151
}
151152

152-
if (!isNullOrEmpty(this.databasePropertyName) && !isNullOrEmpty(props.getProperty(DATABASE_PROPERTY_NAME))) {
153-
copy.put(this.databasePropertyName, props.getProperty(DATABASE_PROPERTY_NAME));
153+
if (!isNullOrEmpty(this.databasePropertyName) && !isNullOrEmpty(PropertyDefinition.DATABASE_NAME.getString(props))) {
154+
copy.put(this.databasePropertyName, PropertyDefinition.DATABASE_NAME.getString(props));
154155
}
155156

157+
copy.remove(PropertyDefinition.DATABASE_NAME.name);
158+
copy.remove(PropertyDefinition.USER.name);
159+
copy.remove(PropertyDefinition.PASSWORD.name);
160+
156161
PropertyUtils.applyProperties(this.dataSource, copy);
157162
return this.dataSource.getConnection();
158163
}

wrapper/src/main/java/com/amazon/awslabs/jdbc/Driver.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
package com.amazon.awslabs.jdbc;
2020

21-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.DATABASE_PROPERTY_NAME;
22-
2321
import com.amazon.awslabs.jdbc.util.DriverInfo;
2422
import com.amazon.awslabs.jdbc.util.StringUtils;
2523
import com.amazon.awslabs.jdbc.wrapper.ConnectionWrapper;
@@ -133,7 +131,7 @@ public boolean acceptsURL(String url) throws SQLException {
133131
if (dPos != -1) {
134132
String database = urlServer.substring(dPos + 1);
135133
if (!database.isEmpty()) {
136-
propertiesFromUrl.setProperty(DATABASE_PROPERTY_NAME, database);
134+
PropertyDefinition.DATABASE_NAME.set(propertiesFromUrl, database);
137135
}
138136
}
139137

wrapper/src/main/java/com/amazon/awslabs/jdbc/DriverConnectionProvider.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818

1919
package com.amazon.awslabs.jdbc;
2020

21-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.DATABASE_PROPERTY_NAME;
22-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.PASSWORD_PROPERTY_NAME;
23-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.USER_PROPERTY_NAME;
2421
import static com.amazon.awslabs.jdbc.util.StringUtils.isNullOrEmpty;
2522

23+
import com.amazon.awslabs.jdbc.util.PropertyUtils;
2624
import java.sql.Connection;
2725
import java.sql.SQLException;
2826
import java.util.Properties;
@@ -64,20 +62,27 @@ public Connection connect(
6462
final @NonNull Properties props)
6563
throws SQLException {
6664

67-
final String databaseName =
68-
props.getProperty(DATABASE_PROPERTY_NAME) != null ? props.getProperty(DATABASE_PROPERTY_NAME) : "";
65+
Properties copy = PropertyUtils.copyProperties(props);
66+
67+
final String databaseName = PropertyDefinition.DATABASE_NAME.getString(props) != null
68+
? PropertyDefinition.DATABASE_NAME.getString(props)
69+
: "";
6970
final StringBuilder urlBuilder = new StringBuilder();
7071
urlBuilder.append(protocol).append(hostSpec.getUrl()).append(databaseName);
7172

72-
if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
73-
props.setProperty(this.userPropertyName, props.getProperty(USER_PROPERTY_NAME));
73+
if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(PropertyDefinition.USER.getString(props))) {
74+
copy.setProperty(this.userPropertyName, PropertyDefinition.USER.getString(props));
7475
}
7576

76-
if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
77-
props.setProperty(this.passwordPropertyName, props.getProperty(PASSWORD_PROPERTY_NAME));
77+
if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(PropertyDefinition.PASSWORD.getString(props))) {
78+
copy.setProperty(this.passwordPropertyName, PropertyDefinition.PASSWORD.getString(props));
7879
}
7980

80-
return this.driver.connect(urlBuilder.toString(), props);
81+
copy.remove(PropertyDefinition.DATABASE_NAME.name);
82+
copy.remove(PropertyDefinition.USER.name);
83+
copy.remove(PropertyDefinition.PASSWORD.name);
84+
85+
return this.driver.connect(urlBuilder.toString(), copy);
8186
}
8287

8388
/**

wrapper/src/main/java/com/amazon/awslabs/jdbc/PropertyDefinition.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@
2828

2929
public class PropertyDefinition {
3030

31-
public static final ProxyDriverProperty CLUSTER_INSTANCE_HOST_PATTERN =
32-
new ProxyDriverProperty(
33-
"clusterInstanceHostPattern",
34-
null,
35-
"The cluster instance DNS pattern that will be used to build a complete instance endpoint. "
36-
+ "A \"?\" character in this pattern should be used as a placeholder for cluster instance names. "
37-
+ "This pattern is required to be specified for IP address or custom domain connections to AWS RDS "
38-
+ "clusters. Otherwise, if unspecified, the pattern will be automatically created for AWS RDS clusters.");
39-
40-
public static final ProxyDriverProperty ENABLE_CLUSTER_AWARE_FAILOVER =
41-
new ProxyDriverProperty(
42-
"enableClusterAwareFailover", "true",
43-
"Enable/disable cluster-aware failover logic");
44-
4531
public static final ProxyDriverProperty LOG_UNCLOSED_CONNECTIONS =
4632
new ProxyDriverProperty(
4733
"proxyDriverLogUnclosedConnections", "false",
@@ -65,15 +51,17 @@ public class PropertyDefinition {
6551
new ProxyDriverProperty(
6652
"proxyDriverProfileName", null, "Driver configuration profile name");
6753

68-
public static final ProxyDriverProperty USE_AWS_IAM =
54+
public static final ProxyDriverProperty USER =
55+
new ProxyDriverProperty(
56+
"proxyDriverUser", null, "Driver user name");
57+
58+
public static final ProxyDriverProperty PASSWORD =
6959
new ProxyDriverProperty(
70-
"useAwsIam", "false", "Set to true to use AWS IAM database authentication");
60+
"proxyDriverPassword", null, "Driver password");
7161

72-
public static final ProxyDriverProperty CLUSTER_ID = new ProxyDriverProperty(
73-
"clusterId", "",
74-
"A unique identifier for the cluster. "
75-
+ "Connections with the same cluster id share a cluster topology cache. "
76-
+ "If unspecified, a cluster id is automatically created for AWS RDS clusters.");
62+
public static final ProxyDriverProperty DATABASE_NAME =
63+
new ProxyDriverProperty(
64+
"proxyDriverDatabaseName", null, "Driver database name");
7765

7866
private static final Map<String, ProxyDriverProperty> PROPS_BY_NAME =
7967
new HashMap<String, ProxyDriverProperty>();

wrapper/src/main/java/com/amazon/awslabs/jdbc/ds/ProxyDriverDataSource.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818

1919
package com.amazon.awslabs.jdbc.ds;
2020

21-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.DATABASE_PROPERTY_NAME;
22-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.PASSWORD_PROPERTY_NAME;
23-
import static com.amazon.awslabs.jdbc.ConnectionPropertyNames.USER_PROPERTY_NAME;
2421
import static com.amazon.awslabs.jdbc.util.ConnectionUrlBuilder.buildUrl;
2522
import static com.amazon.awslabs.jdbc.util.ConnectionUrlParser.parsePropertiesFromUrl;
2623

2724
import com.amazon.awslabs.jdbc.DataSourceConnectionProvider;
2825
import com.amazon.awslabs.jdbc.Driver;
2926
import com.amazon.awslabs.jdbc.DriverConnectionProvider;
27+
import com.amazon.awslabs.jdbc.PropertyDefinition;
3028
import com.amazon.awslabs.jdbc.util.ConnectionUrlParser;
3129
import com.amazon.awslabs.jdbc.util.PropertyUtils;
3230
import com.amazon.awslabs.jdbc.util.SqlState;
@@ -91,11 +89,12 @@ public Connection getConnection(String username, String password) throws SQLExce
9189

9290
Properties props = PropertyUtils.copyProperties(this.targetDataSourceProperties);
9391
setCredentialProperties(props);
92+
9493
if (!isNullOrEmpty(this.targetDataSourceClassName)) {
9594
final DataSource targetDataSource = createTargetDataSource();
9695

9796
if (!isNullOrEmpty(this.databasePropertyName) && !isNullOrEmpty(props.getProperty(this.databasePropertyName))) {
98-
props.put(DATABASE_PROPERTY_NAME, props.getProperty(this.databasePropertyName));
97+
PropertyDefinition.DATABASE_NAME.set(props, props.getProperty(this.databasePropertyName));
9998
}
10099

101100
// If the url is set explicitly through setJdbcUrl or the connection properties.
@@ -145,8 +144,9 @@ public Connection getConnection(String username, String password) throws SQLExce
145144
throw new SQLException("Can't find a suitable driver for " + this.jdbcUrl);
146145
}
147146

148-
setDatabasePropertyFromUrl(props);
149147
parsePropertiesFromUrl(this.jdbcUrl, props);
148+
setCredentialProperties(props);
149+
setDatabasePropertyFromUrl(props);
150150

151151
return new ConnectionWrapper(
152152
props,
@@ -301,21 +301,21 @@ protected boolean isNullOrEmpty(final String str) {
301301

302302
private void setCredentialProperties(Properties props) {
303303
// If username was provided as a get connection parameter and a userPropertyName is set.
304-
if (!isNullOrEmpty(this.user) && !isNullOrEmpty(this.userPropertyName)) {
305-
props.setProperty(USER_PROPERTY_NAME, this.user);
304+
if (!isNullOrEmpty(this.user)) {
305+
PropertyDefinition.USER.set(props, this.user);
306306

307307
// If username was provided in targetDataSourceProperties and a userPropertyName is set.
308308
} else if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(props.getProperty(this.userPropertyName))) {
309-
props.setProperty(USER_PROPERTY_NAME, props.getProperty(this.userPropertyName));
309+
PropertyDefinition.USER.set(props, props.getProperty(this.userPropertyName));
310310
this.user = props.getProperty(this.userPropertyName);
311311
}
312312

313-
if (!isNullOrEmpty(this.password) && !isNullOrEmpty(this.passwordPropertyName)) {
314-
props.setProperty(PASSWORD_PROPERTY_NAME, this.password);
313+
if (!isNullOrEmpty(this.password)) {
314+
PropertyDefinition.PASSWORD.set(props, this.password);
315315

316316
} else if (!isNullOrEmpty(this.passwordPropertyName)
317317
&& !isNullOrEmpty(props.getProperty(this.passwordPropertyName))) {
318-
props.setProperty(PASSWORD_PROPERTY_NAME, props.getProperty(this.passwordPropertyName));
318+
PropertyDefinition.PASSWORD.set(props, props.getProperty(this.passwordPropertyName));
319319
this.password = props.getProperty(this.passwordPropertyName);
320320
}
321321
}
@@ -331,20 +331,20 @@ private DataSource createTargetDataSource() throws SQLException {
331331
private void setDatabasePropertyFromUrl(Properties props) {
332332
final String databaseName = ConnectionUrlParser.parseDatabaseFromUrl(this.jdbcUrl);
333333
if (!isNullOrEmpty(databaseName)) {
334-
props.setProperty(DATABASE_PROPERTY_NAME, databaseName);
334+
PropertyDefinition.DATABASE_NAME.set(props, databaseName);
335335
}
336336
}
337337

338338
private void setCredentialPropertiesFromUrl(Properties props) {
339339
final String userFromUrl = ConnectionUrlParser.parseUserFromUrl(this.jdbcUrl, this.userPropertyName);
340340
if (isNullOrEmpty(this.user) && !isNullOrEmpty(userFromUrl)) {
341-
props.setProperty(USER_PROPERTY_NAME, userFromUrl);
341+
PropertyDefinition.USER.set(props, userFromUrl);
342342
this.user = userFromUrl;
343343
}
344344

345345
final String passwordFromUrl = ConnectionUrlParser.parsePasswordFromUrl(this.jdbcUrl, this.passwordPropertyName);
346346
if (isNullOrEmpty(this.password) && !isNullOrEmpty(passwordFromUrl)) {
347-
props.setProperty(PASSWORD_PROPERTY_NAME, passwordFromUrl);
347+
PropertyDefinition.PASSWORD.set(props, passwordFromUrl);
348348
this.password = passwordFromUrl;
349349
}
350350
}

wrapper/src/main/java/com/amazon/awslabs/jdbc/hostlistprovider/AuroraHostListProvider.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ public class AuroraHostListProvider implements HostListProvider, DynamicHostList
5757
"Cluster topology refresh rate in millis. "
5858
+ "The cached topology for the cluster will be invalidated after the specified time, "
5959
+ "after which it will be updated during the next interaction with the connection.");
60+
61+
public static final ProxyDriverProperty CLUSTER_ID = new ProxyDriverProperty(
62+
"clusterId", "",
63+
"A unique identifier for the cluster. "
64+
+ "Connections with the same cluster id share a cluster topology cache. "
65+
+ "If unspecified, a cluster id is automatically created for AWS RDS clusters.");
66+
67+
public static final ProxyDriverProperty CLUSTER_INSTANCE_HOST_PATTERN =
68+
new ProxyDriverProperty(
69+
"clusterInstanceHostPattern",
70+
null,
71+
"The cluster instance DNS pattern that will be used to build a complete instance endpoint. "
72+
+ "A \"?\" character in this pattern should be used as a placeholder for cluster instance names. "
73+
+ "This pattern is required to be specified for IP address or custom domain connections to AWS RDS "
74+
+ "clusters. Otherwise, if unspecified, the pattern will be automatically created for AWS RDS clusters.");
75+
6076
static final String PG_RETRIEVE_TOPOLOGY_SQL =
6177
"SELECT SERVER_ID, SESSION_ID FROM aurora_replica_status() "
6278
// filter out nodes that haven't been updated in the last 5 minutes
@@ -111,14 +127,14 @@ public AuroraHostListProvider(
111127
this.originalUrl = originalUrl;
112128
this.clusterId = UUID.randomUUID().toString();
113129
this.refreshRateInMilliseconds = CLUSTER_TOPOLOGY_REFRESH_RATE_MS.getInteger(properties);
114-
this.clusterInstanceTemplate = PropertyDefinition.CLUSTER_INSTANCE_HOST_PATTERN.get(this.properties) == null
130+
this.clusterInstanceTemplate = CLUSTER_INSTANCE_HOST_PATTERN.get(this.properties) == null
115131
? new HostSpec(rdsHelper.getRdsInstanceHostPattern(originalUrl))
116-
: new HostSpec(PropertyDefinition.CLUSTER_INSTANCE_HOST_PATTERN.getString(this.properties));
132+
: new HostSpec(CLUSTER_INSTANCE_HOST_PATTERN.getString(this.properties));
117133
validateHostPatternSetting(this.clusterInstanceTemplate.getHost());
118134

119135
this.rdsUrlType = rdsHelper.identifyRdsType(originalUrl);
120136

121-
final String clusterIdSetting = PropertyDefinition.CLUSTER_ID.get(this.properties);
137+
final String clusterIdSetting = CLUSTER_ID.get(this.properties);
122138
if (!StringUtils.isNullOrEmpty(clusterIdSetting)) {
123139
this.clusterId = clusterIdSetting;
124140
} else if (rdsUrlType == RdsUrlType.RDS_PROXY) {

wrapper/src/main/java/com/amazon/awslabs/jdbc/plugin/IamAuthConnectionPlugin.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
package com.amazon.awslabs.jdbc.plugin;
2020

21-
import com.amazon.awslabs.jdbc.ConnectionPropertyNames;
2221
import com.amazon.awslabs.jdbc.HostSpec;
2322
import com.amazon.awslabs.jdbc.JdbcCallable;
23+
import com.amazon.awslabs.jdbc.PropertyDefinition;
2424
import com.amazon.awslabs.jdbc.ProxyDriverProperty;
2525
import com.amazon.awslabs.jdbc.util.StringUtils;
2626
import java.sql.Connection;
@@ -111,20 +111,20 @@ public Connection connect(
111111
}
112112

113113
final String cacheKey = getCacheKey(
114-
props.getProperty(ConnectionPropertyNames.USER_PROPERTY_NAME),
114+
PropertyDefinition.USER.getString(props),
115115
host,
116116
port,
117117
region);
118118
final TokenInfo tokenInfo = tokenCache.get(cacheKey);
119119

120120
if (tokenInfo != null && !tokenInfo.isExpired()) {
121121
LOGGER.log(Level.FINEST, "use cached IAM token = " + tokenInfo.getToken());
122-
props.setProperty(ConnectionPropertyNames.PASSWORD_PROPERTY_NAME, tokenInfo.getToken());
122+
PropertyDefinition.PASSWORD.set(props, tokenInfo.getToken());
123123
} else {
124-
final String token = generateAuthenticationToken(props.getProperty(ConnectionPropertyNames.USER_PROPERTY_NAME),
124+
final String token = generateAuthenticationToken(PropertyDefinition.USER.getString(props),
125125
hostSpec.getHost(), port, region);
126126
LOGGER.log(Level.FINEST, "generated new IAM token = " + token);
127-
props.setProperty(ConnectionPropertyNames.PASSWORD_PROPERTY_NAME, token);
127+
PropertyDefinition.PASSWORD.set(props, token);
128128
tokenCache.put(
129129
cacheKey,
130130
new TokenInfo(token, Instant.now().plus(tokenExpirationSec, ChronoUnit.SECONDS)));

0 commit comments

Comments
 (0)