Skip to content

Commit 3f9635a

Browse files
Feature | Active Directory MSI Authentication support (#838)
1 parent 33bbc84 commit 3f9635a

File tree

11 files changed

+369
-97
lines changed

11 files changed

+369
-97
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ static boolean isDllLoaded() {
5959
enabled = true;
6060
} catch (UnsatisfiedLinkError e) {
6161
temp = e;
62-
authLogger.warning("Failed to load the sqljdbc_auth.dll cause : " + e.getMessage());
6362
// This is not re-thrown on purpose - the constructor will terminate the properly with the appropriate error
6463
// string
6564
} finally {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ final class TDS {
103103
static final int TDS_FEDAUTH_LIBRARY_RESERVED = 0x7F;
104104
static final byte ADALWORKFLOW_ACTIVEDIRECTORYPASSWORD = 0x01;
105105
static final byte ADALWORKFLOW_ACTIVEDIRECTORYINTEGRATED = 0x02;
106+
static final byte ADALWORKFLOW_ACTIVEDIRECTORYMSI = 0x03;
106107
static final byte FEDAUTH_INFO_ID_STSURL = 0x01; // FedAuthInfoData is token endpoint URL from which to acquire fed
107108
// auth token
108109
static final byte FEDAUTH_INFO_ID_SPN = 0x02; // FedAuthInfoData is the SPN to use for acquiring fed auth token

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,4 +805,19 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
805805
* indicates whether Bulk Copy API should be used for Batch Insert operations.
806806
*/
807807
public void setUseBulkCopyForBatchInsert(boolean useBulkCopyForBatchInsert);
808+
809+
/**
810+
* Sets the client id to be used to retrieve access token from MSI EndPoint.
811+
*
812+
* @param msiClientId
813+
* Client ID of User Assigned Managed Identity
814+
*/
815+
public void setMSIClientId(String msiClientId);
816+
817+
/**
818+
* Returns the value for the connection property 'msiClientId'.
819+
*
820+
* @return msiClientId property value
821+
*/
822+
public String getMSIClientId();
808823
}

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,26 @@ static SqlFedAuthToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String use
3737
ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID, user, password, null);
3838

3939
AuthenticationResult authenticationResult = future.get();
40-
SqlFedAuthToken fedAuthToken = new SqlFedAuthToken(authenticationResult.getAccessToken(),
41-
authenticationResult.getExpiresOnDate());
4240

43-
return fedAuthToken;
41+
return new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
4442
} catch (MalformedURLException | InterruptedException e) {
4543
throw new SQLServerException(e.getMessage(), e);
4644
} catch (ExecutionException e) {
4745
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution"));
4846
Object[] msgArgs = {user, authenticationString};
4947

50-
// the cause error message uses \\n\\r which does not give correct format
51-
// change it to \r\n to provide correct format
48+
/*
49+
* the cause error message uses \\n\\r which does not give correct format change it to \r\n to provide
50+
* correct format
51+
*/
5252
String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
5353
AuthenticationException correctedAuthenticationException = new AuthenticationException(
5454
correctedErrorMessage);
5555

56-
// SQLServerException is caused by ExecutionException, which is caused by
57-
// AuthenticationException
58-
// to match the exception tree before error message correction
56+
/*
57+
* SQLServerException is caused by ExecutionException, which is caused by AuthenticationException to match
58+
* the exception tree before error message correction
59+
*/
5960
ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException);
6061

6162
throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException);
@@ -69,8 +70,10 @@ static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo,
6970
ExecutorService executorService = Executors.newFixedThreadPool(1);
7071

7172
try {
72-
// principal name does not matter, what matters is the realm name
73-
// it gets the username in principal_name@realm_name format
73+
/*
74+
* principal name does not matter, what matters is the realm name it gets the username in
75+
* principal_name@realm_name format
76+
*/
7477
KerberosPrincipal kerberosPrincipal = new KerberosPrincipal("username");
7578
String username = kerberosPrincipal.getName();
7679

@@ -83,10 +86,8 @@ static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo,
8386
ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID, username, null, null);
8487

8588
AuthenticationResult authenticationResult = future.get();
86-
SqlFedAuthToken fedAuthToken = new SqlFedAuthToken(authenticationResult.getAccessToken(),
87-
authenticationResult.getExpiresOnDate());
8889

89-
return fedAuthToken;
90+
return new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
9091
} catch (InterruptedException | IOException e) {
9192
throw new SQLServerException(e.getMessage(), e);
9293
} catch (ExecutionException e) {
@@ -97,15 +98,18 @@ static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo,
9798
// the case when Future's outcome has no AuthenticationResult but exception
9899
throw new SQLServerException(form.format(msgArgs), null);
99100
} else {
100-
// the cause error message uses \\n\\r which does not give correct format
101-
// change it to \r\n to provide correct format
101+
/*
102+
* the cause error message uses \\n\\r which does not give correct format change it to \r\n to provide
103+
* correct format
104+
*/
102105
String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
103106
AuthenticationException correctedAuthenticationException = new AuthenticationException(
104107
correctedErrorMessage);
105108

106-
// SQLServerException is caused by ExecutionException, which is caused by
107-
// AuthenticationException
108-
// to match the exception tree before error message correction
109+
/*
110+
* SQLServerException is caused by ExecutionException, which is caused by AuthenticationException to
111+
* match the exception tree before error message correction
112+
*/
109113
ExecutionException correctedExecutionException = new ExecutionException(
110114
correctedAuthenticationException);
111115

0 commit comments

Comments
 (0)