Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to use Reentrant locks to be Loom friendly #2223

Merged
merged 1 commit into from
Sep 28, 2023
Merged
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 @@ -24,6 +24,8 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;

import javax.security.auth.kerberos.KerberosPrincipal;
Expand Down Expand Up @@ -69,14 +71,18 @@ private SQLServerMSAL4JUtils() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

static synchronized SqlAuthenticationToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String user,
String password, String authenticationString) throws SQLServerException {
private static final Lock lock = new ReentrantLock();

static SqlAuthenticationToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String user, String password,
String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();

if (logger.isLoggable(Level.FINEST)) {
logger.finest(LOGCONTEXT + authenticationString + ": get FedAuth token for user: " + user);
}

lock.lock();

try {
final PublicClientApplication pca = PublicClientApplication
.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService)
Expand Down Expand Up @@ -104,12 +110,13 @@ static synchronized SqlAuthenticationToken getSqlFedAuthToken(SqlFedAuthInfo fed
} catch (MalformedURLException | ExecutionException e) {
throw getCorrectedException(e, user, authenticationString);
} finally {
lock.unlock();
executorService.shutdown();
}
}

static synchronized SqlAuthenticationToken getSqlFedAuthTokenPrincipal(SqlFedAuthInfo fedAuthInfo,
String aadPrincipalID, String aadPrincipalSecret, String authenticationString) throws SQLServerException {
static SqlAuthenticationToken getSqlFedAuthTokenPrincipal(SqlFedAuthInfo fedAuthInfo, String aadPrincipalID,
String aadPrincipalSecret, String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();

if (logger.isLoggable(Level.FINEST)) {
Expand All @@ -122,6 +129,8 @@ static synchronized SqlAuthenticationToken getSqlFedAuthTokenPrincipal(SqlFedAut
Set<String> scopes = new HashSet<>();
scopes.add(scope);

lock.lock();

try {
IClientCredential credential = ClientCredentialFactory.createFromSecret(aadPrincipalSecret);
ConfidentialClientApplication clientApplication = ConfidentialClientApplication
Expand All @@ -148,11 +157,12 @@ static synchronized SqlAuthenticationToken getSqlFedAuthTokenPrincipal(SqlFedAut
} catch (MalformedURLException | ExecutionException e) {
throw getCorrectedException(e, aadPrincipalID, authenticationString);
} finally {
lock.unlock();
executorService.shutdown();
}
}

static synchronized SqlAuthenticationToken getSqlFedAuthTokenPrincipalCertificate(SqlFedAuthInfo fedAuthInfo,
static SqlAuthenticationToken getSqlFedAuthTokenPrincipalCertificate(SqlFedAuthInfo fedAuthInfo,
String aadPrincipalID, String certFile, String certPassword, String certKey, String certKeyPassword,
String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Expand All @@ -168,6 +178,8 @@ static synchronized SqlAuthenticationToken getSqlFedAuthTokenPrincipalCertificat
Set<String> scopes = new HashSet<>();
scopes.add(scope);

lock.lock();

try {
ConfidentialClientApplication clientApplication = null;

Expand Down Expand Up @@ -249,11 +261,12 @@ static synchronized SqlAuthenticationToken getSqlFedAuthTokenPrincipalCertificat
throw getCorrectedException(e, aadPrincipalID, authenticationString);

} finally {
lock.unlock();
executorService.shutdown();
}
}

static synchronized SqlAuthenticationToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo,
static SqlAuthenticationToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo,
String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();

Expand All @@ -269,6 +282,8 @@ static synchronized SqlAuthenticationToken getSqlFedAuthTokenIntegrated(SqlFedAu
+ "realm name:" + kerberosPrincipal.getRealm());
}

lock.lock();

try {
final PublicClientApplication pca = PublicClientApplication
.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService)
Expand Down Expand Up @@ -296,18 +311,21 @@ static synchronized SqlAuthenticationToken getSqlFedAuthTokenIntegrated(SqlFedAu
} catch (IOException | ExecutionException e) {
throw getCorrectedException(e, user, authenticationString);
} finally {
lock.unlock();
executorService.shutdown();
}
}

static synchronized SqlAuthenticationToken getSqlFedAuthTokenInteractive(SqlFedAuthInfo fedAuthInfo, String user,
static SqlAuthenticationToken getSqlFedAuthTokenInteractive(SqlFedAuthInfo fedAuthInfo, String user,
String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();

if (logger.isLoggable(Level.FINEST)) {
logger.finest(LOGCONTEXT + authenticationString + ": get FedAuth token interactive for user: " + user);
}

lock.lock();

try {
PublicClientApplication pca = PublicClientApplication
.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService)
Expand Down Expand Up @@ -384,6 +402,7 @@ static synchronized SqlAuthenticationToken getSqlFedAuthTokenInteractive(SqlFedA
} catch (MalformedURLException | URISyntaxException | ExecutionException e) {
throw getCorrectedException(e, user, authenticationString);
} finally {
lock.unlock();
executorService.shutdown();
}
}
Expand Down
Loading