Skip to content

Commit 247c28e

Browse files
fix: IAM_DEFAULT_PORT property to override host and dialect ports. (#475)
1 parent 0219faa commit 247c28e

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

wrapper/src/main/java/software/amazon/jdbc/plugin/IamAuthConnectionPlugin.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,7 @@ private Connection connectInternal(String driverProtocol, HostSpec hostSpec, Pro
107107
host = IAM_HOST.getString(props);
108108
}
109109

110-
int port = hostSpec.getPort();
111-
if (!hostSpec.isPortSpecified()) {
112-
if (StringUtils.isNullOrEmpty(IAM_DEFAULT_PORT.getString(props))) {
113-
port = this.pluginService.getDialect().getDefaultPort();
114-
} else {
115-
port = IAM_DEFAULT_PORT.getInteger(props);
116-
if (port <= 0) {
117-
throw new IllegalArgumentException(
118-
Messages.get(
119-
"IamAuthConnectionPlugin.invalidPort",
120-
new Object[] {port}));
121-
}
122-
}
123-
}
110+
int port = getPort(props, hostSpec);
124111

125112
final String iamRegion = IAM_REGION.getString(props);
126113
final Region region = StringUtils.isNullOrEmpty(iamRegion)
@@ -245,6 +232,26 @@ public static void clearCache() {
245232
tokenCache.clear();
246233
}
247234

235+
private int getPort(Properties props, HostSpec hostSpec) {
236+
if (!StringUtils.isNullOrEmpty(IAM_DEFAULT_PORT.getString(props))) {
237+
int defaultPort = IAM_DEFAULT_PORT.getInteger(props);
238+
if (defaultPort > 0) {
239+
return defaultPort;
240+
} else {
241+
LOGGER.finest(
242+
() -> Messages.get(
243+
"IamAuthConnectionPlugin.invalidPort",
244+
new Object[] {defaultPort}));
245+
}
246+
}
247+
248+
if (hostSpec.isPortSpecified()) {
249+
return hostSpec.getPort();
250+
} else {
251+
return this.pluginService.getDialect().getDefaultPort();
252+
}
253+
}
254+
248255
private Region getRdsRegion(final String hostname) throws SQLException {
249256

250257
// Get Region

wrapper/src/main/resources/messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ HostSelector.noHostsMatchingRole=No hosts were found matching the requested ''{0
162162
IamAuthConnectionPlugin.unsupportedHostname=Unsupported AWS hostname {0}. Amazon domain name in format *.AWS-Region.rds.amazonaws.com or *.rds.AWS-Region.amazonaws.com.cn is expected.
163163
IamAuthConnectionPlugin.useCachedIamToken=Use cached IAM token = ''{0}''
164164
IamAuthConnectionPlugin.generatedNewIamToken=Generated new IAM token = ''{0}''
165-
IamAuthConnectionPlugin.invalidPort=Port number: {0} is not valid. Port number should be greater than zero.
165+
IamAuthConnectionPlugin.invalidPort=Port number: {0} is not valid. Port number should be greater than zero. Falling back to default port.
166166
IamAuthConnectionPlugin.unhandledException=Unhandled exception: ''{0}''
167167
IamAuthConnectionPlugin.connectException=Error occurred while opening a connection: ''{0}''
168168

wrapper/src/test/java/software/amazon/jdbc/plugin/IamAuthConnectionPluginTest.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,31 @@ public void testMySqlConnectValidTokenInCache() throws SQLException {
113113
}
114114

115115
@Test
116-
public void testPostgresConnectWithInvalidPort() {
117-
props.setProperty("iamDefaultPort", "0");
118-
PluginService mockPluginService = Mockito.mock(PluginService.class);
119-
final IamAuthConnectionPlugin targetPlugin = new IamAuthConnectionPlugin(mockPluginService);
116+
public void testPostgresConnectWithInvalidPortFallbacksToHostPort() throws SQLException {
117+
final String invalidIamDefaultPort = "0";
118+
props.setProperty(IamAuthConnectionPlugin.IAM_DEFAULT_PORT.name, invalidIamDefaultPort);
120119

121-
final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
122-
() -> targetPlugin.connect(PG_DRIVER_PROTOCOL, PG_HOST_SPEC, props, true, mockLambda));
120+
final String cacheKeyWithNewPort = "us-east-2:pg.testdb.us-east-2.rds.amazonaws.com:"
121+
+ PG_HOST_SPEC_WITH_PORT.getPort() + ":postgresqlUser";
122+
IamAuthConnectionPlugin.tokenCache.put(cacheKeyWithNewPort,
123+
new IamAuthConnectionPlugin.TokenInfo(TEST_TOKEN, Instant.now().plusMillis(300000)));
123124

124-
assertEquals("Port number: 0 is not valid. Port number should be greater than zero.", exception.getMessage());
125+
testTokenSetInProps(PG_DRIVER_PROTOCOL, PG_HOST_SPEC_WITH_PORT);
126+
}
127+
128+
@Test
129+
public void testPostgresConnectWithInvalidPortAndNoHostPortFallbacksToHostPort() throws SQLException {
130+
final String invalidIamDefaultPort = "0";
131+
props.setProperty(IamAuthConnectionPlugin.IAM_DEFAULT_PORT.name, invalidIamDefaultPort);
132+
133+
when(mockDialect.getDefaultPort()).thenReturn(DEFAULT_PG_PORT);
134+
135+
final String cacheKeyWithNewPort = "us-east-2:pg.testdb.us-east-2.rds.amazonaws.com:"
136+
+ DEFAULT_PG_PORT + ":postgresqlUser";
137+
IamAuthConnectionPlugin.tokenCache.put(cacheKeyWithNewPort,
138+
new IamAuthConnectionPlugin.TokenInfo(TEST_TOKEN, Instant.now().plusMillis(300000)));
139+
140+
testTokenSetInProps(PG_DRIVER_PROTOCOL, PG_HOST_SPEC);
125141
}
126142

127143
@Test
@@ -150,6 +166,18 @@ public void testConnectWithSpecifiedPort() throws SQLException {
150166
testTokenSetInProps(PG_DRIVER_PROTOCOL, PG_HOST_SPEC_WITH_PORT);
151167
}
152168

169+
@Test
170+
public void testConnectWithSpecifiedIamDefaultPort() throws SQLException {
171+
final String iamDefaultPort = "9999";
172+
props.setProperty(IamAuthConnectionPlugin.IAM_DEFAULT_PORT.name, iamDefaultPort);
173+
final String cacheKeyWithNewPort = "us-east-2:pg.testdb.us-east-2.rds.amazonaws.com:"
174+
+ iamDefaultPort + ":postgresqlUser";
175+
IamAuthConnectionPlugin.tokenCache.put(cacheKeyWithNewPort,
176+
new IamAuthConnectionPlugin.TokenInfo(TEST_TOKEN, Instant.now().plusMillis(300000)));
177+
178+
testTokenSetInProps(PG_DRIVER_PROTOCOL, PG_HOST_SPEC_WITH_PORT);
179+
}
180+
153181
@Test
154182
public void testConnectWithSpecifiedRegion() throws SQLException {
155183
final String cacheKeyWithNewRegion =

0 commit comments

Comments
 (0)