Skip to content

Commit 2a3d372

Browse files
authored
Introduced timeouts for MSAL calls. (#2562)
* Introduced timeouts for MSAL calls. * Fixed indentation issues. * Added unit tests * Added a max wait duration of 20 seconds to MSAL calls - Added more tests - Improved test to check for specific error message * Added Timeout Exception catch clause for one of the auth methods * Replaced lock with tryLock. - Replaced lock with tryLock to avoid potential long waiting for other threads while one thread is taking long to complete. * Replaced lock with semaphore for beter readablility. - Added detailed comment for the usage of semaphore. * Renamed semAcquired to isSemAcquired * Fixed indentation for an existing code line * Change to use Mono::timeout method * Updated TOKEN_WAIT_DURATION_MS to correct value. * Improved error messages
1 parent 5bb3353 commit 2a3d372

File tree

4 files changed

+166
-44
lines changed

4 files changed

+166
-44
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6110,10 +6110,11 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
61106110
}
61116111

61126112
while (true) {
6113+
int millisecondsRemaining = timerRemaining(timerExpire);
61136114
if (authenticationString.equalsIgnoreCase(SqlAuthentication.ACTIVE_DIRECTORY_PASSWORD.toString())) {
61146115
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthToken(fedAuthInfo, user,
61156116
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.PASSWORD.toString()),
6116-
authenticationString);
6117+
authenticationString, millisecondsRemaining);
61176118

61186119
// Break out of the retry loop in successful case.
61196120
break;
@@ -6125,12 +6126,12 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
61256126

61266127
if (null != managedIdentityClientId && !managedIdentityClientId.isEmpty()) {
61276128
fedAuthToken = SQLServerSecurityUtility.getManagedIdentityCredAuthToken(fedAuthInfo.spn,
6128-
managedIdentityClientId);
6129+
managedIdentityClientId, millisecondsRemaining);
61296130
break;
61306131
}
61316132

61326133
fedAuthToken = SQLServerSecurityUtility.getManagedIdentityCredAuthToken(fedAuthInfo.spn,
6133-
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()));
6134+
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()), millisecondsRemaining);
61346135

61356136
// Break out of the retry loop in successful case.
61366137
break;
@@ -6141,12 +6142,12 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
61416142
if (aadPrincipalID != null && !aadPrincipalID.isEmpty() && aadPrincipalSecret != null
61426143
&& !aadPrincipalSecret.isEmpty()) {
61436144
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenPrincipal(fedAuthInfo, aadPrincipalID,
6144-
aadPrincipalSecret, authenticationString);
6145+
aadPrincipalSecret, authenticationString, millisecondsRemaining);
61456146
} else {
61466147
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenPrincipal(fedAuthInfo,
61476148
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.USER.toString()),
61486149
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.PASSWORD.toString()),
6149-
authenticationString);
6150+
authenticationString, millisecondsRemaining);
61506151
}
61516152

61526153
// Break out of the retry loop in successful case.
@@ -6159,7 +6160,7 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
61596160
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.USER.toString()),
61606161
servicePrincipalCertificate,
61616162
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.PASSWORD.toString()),
6162-
servicePrincipalCertificateKey, servicePrincipalCertificatePassword, authenticationString);
6163+
servicePrincipalCertificateKey, servicePrincipalCertificatePassword, authenticationString, millisecondsRemaining);
61636164

61646165
// Break out of the retry loop in successful case.
61656166
break;
@@ -6194,7 +6195,7 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
61946195
throw new SQLServerException(form.format(msgArgs), null);
61956196
}
61966197

6197-
int millisecondsRemaining = timerRemaining(timerExpire);
6198+
millisecondsRemaining = timerRemaining(timerExpire);
61986199
if (ActiveDirectoryAuthentication.GET_ACCESS_TOKEN_TRANSIENT_ERROR != errorCategory
61996200
|| timerHasExpired(timerExpire) || (fedauthSleepInterval >= millisecondsRemaining)) {
62006201

@@ -6240,15 +6241,15 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
62406241
Object[] msgArgs = {SQLServerDriver.AUTH_DLL_NAME, authenticationString};
62416242
throw new SQLServerException(form.format(msgArgs), null, 0, null);
62426243
}
6243-
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenIntegrated(fedAuthInfo, authenticationString);
6244+
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenIntegrated(fedAuthInfo, authenticationString, millisecondsRemaining);
62446245
}
62456246
// Break out of the retry loop in successful case.
62466247
break;
62476248
} else if (authenticationString
62486249
.equalsIgnoreCase(SqlAuthentication.ACTIVE_DIRECTORY_INTERACTIVE.toString())) {
62496250
// interactive flow
62506251
fedAuthToken = SQLServerMSAL4JUtils.getSqlFedAuthTokenInteractive(fedAuthInfo, user,
6251-
authenticationString);
6252+
authenticationString, millisecondsRemaining);
62526253

62536254
// Break out of the retry loop in successful case.
62546255
break;
@@ -6258,12 +6259,12 @@ private SqlAuthenticationToken getFedAuthToken(SqlFedAuthInfo fedAuthInfo) throw
62586259

62596260
if (null != managedIdentityClientId && !managedIdentityClientId.isEmpty()) {
62606261
fedAuthToken = SQLServerSecurityUtility.getDefaultAzureCredAuthToken(fedAuthInfo.spn,
6261-
managedIdentityClientId);
6262+
managedIdentityClientId, millisecondsRemaining);
62626263
break;
62636264
}
62646265

62656266
fedAuthToken = SQLServerSecurityUtility.getDefaultAzureCredAuthToken(fedAuthInfo.spn,
6266-
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()));
6267+
activeConnectionProperties.getProperty(SQLServerDriverStringProperty.MSI_CLIENT_ID.toString()), millisecondsRemaining);
62676268

62686269
break;
62696270
}

0 commit comments

Comments
 (0)