Skip to content
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 @@ -131,8 +131,9 @@ private Connection connectInternal(String driverProtocol, HostSpec hostSpec, Pro
port,
region);
final TokenInfo tokenInfo = tokenCache.get(cacheKey);
final boolean isCachedToken = tokenInfo != null && !tokenInfo.isExpired();

if (tokenInfo != null && !tokenInfo.isExpired()) {
if (isCachedToken) {
LOGGER.finest(
() -> Messages.get(
"IamAuthConnectionPlugin.useCachedIamToken",
Expand All @@ -154,7 +155,47 @@ private Connection connectInternal(String driverProtocol, HostSpec hostSpec, Pro
cacheKey,
new TokenInfo(token, Instant.now().plus(tokenExpirationSec, ChronoUnit.SECONDS)));
}
return connectFunc.call();

try {
return connectFunc.call();
} catch (final SQLException exception) {

LOGGER.finest(
Copy link
Contributor

@aaronchung-bitquill aaronchung-bitquill May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Would it also be worth logging the token as well? Or not necessary?

() -> Messages.get(
"IamAuthConnectionPlugin.connectException",
new Object[] {exception}));

if (!this.pluginService.isLoginException(exception) || !isCachedToken) {
throw exception;
Comment on lines +168 to +169
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with Aaron, can we log the token and isCachedToken if it is a login exception. Would be beneficial for debugging purposes

}

// Login unsuccessful with cached token
// Try to generate a new token and try to connect again

final String token = generateAuthenticationToken(
hostSpec,
props,
host,
port,
region);
LOGGER.finest(
() -> Messages.get(
"IamAuthConnectionPlugin.generatedNewIamToken",
new Object[] {token}));
PropertyDefinition.PASSWORD.set(props, token);
tokenCache.put(
cacheKey,
new TokenInfo(token, Instant.now().plus(tokenExpirationSec, ChronoUnit.SECONDS)));

return connectFunc.call();

} catch (final Exception exception) {
LOGGER.warning(
() -> Messages.get(
"IamAuthConnectionPlugin.unhandledException",
new Object[] {exception}));
throw new SQLException(exception);
}
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions wrapper/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ IamAuthConnectionPlugin.unsupportedHostname=Unsupported AWS hostname {0}. Amazon
IamAuthConnectionPlugin.useCachedIamToken=Use cached IAM token = ''{0}''
IamAuthConnectionPlugin.generatedNewIamToken=Generated new IAM token = ''{0}''
IamAuthConnectionPlugin.invalidPort=Port number: {0} is not valid. Port number should be greater than zero.
IamAuthConnectionPlugin.unhandledException=Unhandled exception: ''{0}''
IamAuthConnectionPlugin.connectException=Error occurred while opening a connection: ''{0}''

# Log Query Connection Plugin
LogQueryConnectionPlugin.executingQuery=[{0}] Executing query: {1}
Expand Down