diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerADAL4JUtils.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerADAL4JUtils.java index a94ca3cc5..197649b70 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerADAL4JUtils.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerADAL4JUtils.java @@ -41,7 +41,11 @@ static SqlFedAuthToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String use return new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate()); } catch (MalformedURLException | InterruptedException e) { throw new SQLServerException(e.getMessage(), e); - } catch (ExecutionException e) { + } catch (ExecutionException | AuthenticationException e) { + if (adal4jLogger.isLoggable(Level.SEVERE)) { + adal4jLogger.fine(adal4jLogger.toString() + " ADAL exception:" + e.getMessage()); + } + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution")); Object[] msgArgs = {user, authenticationString}; @@ -49,17 +53,22 @@ static SqlFedAuthToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String use * the cause error message uses \\n\\r which does not give correct format change it to \r\n to provide * correct format */ - String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n"); - AuthenticationException correctedAuthenticationException = new AuthenticationException( - correctedErrorMessage); + if (null == e.getCause() || null == e.getCause().getMessage()) { + throw new SQLServerException(form.format(msgArgs), null); + } else { + String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n"); + AuthenticationException correctedAuthenticationException = new AuthenticationException( + correctedErrorMessage); - /* - * SQLServerException is caused by ExecutionException, which is caused by AuthenticationException to match - * the exception tree before error message correction - */ - ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException); + /* + * SQLServerException is caused by ExecutionException, which is caused by AuthenticationException to + * match the exception tree before error message correction + */ + ExecutionException correctedExecutionException = new ExecutionException( + correctedAuthenticationException); - throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException); + throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException); + } } finally { executorService.shutdown(); } @@ -90,7 +99,11 @@ static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo, return new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate()); } catch (InterruptedException | IOException e) { throw new SQLServerException(e.getMessage(), e); - } catch (ExecutionException e) { + } catch (ExecutionException | AuthenticationException e) { + if (adal4jLogger.isLoggable(Level.SEVERE)) { + adal4jLogger.fine(adal4jLogger.toString() + " ADAL exception:" + e.getMessage()); + } + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution")); Object[] msgArgs = {"", authenticationString}; diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConcurrentLoginTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConcurrentLoginTest.java index dbd7b32ae..b637000d2 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConcurrentLoginTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConcurrentLoginTest.java @@ -95,7 +95,7 @@ public void testConcurrentLogin() throws Exception { t1.start(); t2.start(); - if (isWindows && enableADIntegrated) { + if (enableADIntegrated) { Thread t3 = new Thread(r3); t3.setUncaughtExceptionHandler(handler); t3.start(); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConnectionSuspensionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConnectionSuspensionTest.java index d7d847784..5cbda65da 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConnectionSuspensionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConnectionSuspensionTest.java @@ -42,7 +42,7 @@ public void testAccessTokenExpiredThenCreateNewStatementADPassword() throws SQLE @Test public void testAccessTokenExpiredThenCreateNewStatementADIntegrated() throws SQLException { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); testAccessTokenExpiredThenCreateNewStatement(SqlAuthentication.ActiveDirectoryIntegrated); } @@ -107,7 +107,7 @@ public void testAccessTokenExpiredThenExecuteUsingSameStatementADPassword() thro @Test public void testAccessTokenExpiredThenExecuteUsingSameStatementADIntegrated() throws SQLException { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); testAccessTokenExpiredThenExecuteUsingSameStatement(SqlAuthentication.ActiveDirectoryIntegrated); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ErrorMessageTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ErrorMessageTest.java index 49d472eea..719ff8884 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ErrorMessageTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ErrorMessageTest.java @@ -26,7 +26,7 @@ @Tag(Constants.fedAuth) public class ErrorMessageTest extends FedauthCommon { - String userName = "abc" + azureUserName; + String badUserName = "abc" + azureUserName; String connectionUrl = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase; @Test @@ -214,13 +214,14 @@ public void testSQLPasswordWithUntrustedSqlDB() throws SQLException { @Test public void testADPasswordUnregisteredUserWithConnectionStringUserName() throws SQLException { - try (Connection connection = DriverManager.getConnection(connectionUrl + ";userName=" + userName + ";password=" - + azurePassword + ";Authentication=" + SqlAuthentication.ActiveDirectoryPassword.toString())) { + try (Connection connection = DriverManager + .getConnection(connectionUrl + ";userName=" + badUserName + ";password=" + azurePassword + + ";Authentication=" + SqlAuthentication.ActiveDirectoryPassword.toString())) { fail(EXPECTED_EXCEPTION_NOT_THROWN); } catch (SQLServerException e) { assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(), e.getMessage() - .contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + userName + .contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName + " in Active Directory (Authentication=ActiveDirectoryPassword).") && e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD)); } @@ -232,7 +233,7 @@ public void testADPasswordUnregisteredUserWithDatasource() throws SQLException { SQLServerDataSource ds = new SQLServerDataSource(); ds.setServerName(azureServer); ds.setDatabaseName(azureDatabase); - ds.setUser(userName); + ds.setUser(badUserName); ds.setPassword(azurePassword); ds.setAuthentication(SqlAuthentication.ActiveDirectoryPassword.toString()); @@ -241,7 +242,7 @@ public void testADPasswordUnregisteredUserWithDatasource() throws SQLException { } catch (SQLServerException e) { assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(), e.getMessage() - .contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + userName + .contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName + " in Active Directory (Authentication=ActiveDirectoryPassword).") && e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD)); } @@ -249,13 +250,13 @@ public void testADPasswordUnregisteredUserWithDatasource() throws SQLException { @Test public void testADPasswordUnregisteredUserWithConnectionStringUser() throws SQLException { - try (Connection connection = DriverManager.getConnection(connectionUrl + ";user=" + userName + ";password=" + try (Connection connection = DriverManager.getConnection(connectionUrl + ";user=" + badUserName + ";password=" + azurePassword + ";Authentication=" + SqlAuthentication.ActiveDirectoryPassword.toString())) { fail(EXPECTED_EXCEPTION_NOT_THROWN); } catch (SQLServerException e) { assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(), e.getMessage() - .contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + userName + .contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName + " in Active Directory (Authentication=ActiveDirectoryPassword).") && e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD)); } @@ -268,20 +269,20 @@ public void testAuthenticationAgainstSQLServerWithActivedirectorypassword() thro info.put("Authentication", SqlAuthentication.ActiveDirectoryPassword.toString()); try (Connection connection = DriverManager - .getConnection(connectionUrl + ";user=" + userName + ";password=" + azurePassword, info)) { + .getConnection(connectionUrl + ";user=" + badUserName + ";password=" + azurePassword, info)) { fail(EXPECTED_EXCEPTION_NOT_THROWN); } catch (Exception e) { if (!(e instanceof SQLServerException)) { fail(EXPECTED_EXCEPTION_NOT_THROWN); } assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(), e.getMessage().contains(ERR_MSG_FAILED_AUTHENTICATE - + " the user " + userName + " in Active Directory (Authentication=ActiveDirectoryPassword).")); + + " the user " + badUserName + " in Active Directory (Authentication=ActiveDirectoryPassword).")); } } @Test public void testAuthenticationAgainstSQLServerWithActivedirectoryIntegrated() throws SQLException { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); java.util.Properties info = new Properties(); info.put("TrustServerCertificate", "true"); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthCommon.java b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthCommon.java index 675fa4341..6a9427a78 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthCommon.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthCommon.java @@ -116,7 +116,7 @@ public static void getConfigs() throws Exception { azureGroupUserName = getConfiguredProperty("azureGroupUserName"); String prop = getConfiguredProperty("enableADIntegrated"); - enableADIntegrated = (isWindows && null != prop && prop.equalsIgnoreCase("true")) ? true : false; + enableADIntegrated = (null != prop && prop.equalsIgnoreCase("true")) ? true : false; adPasswordConnectionStr = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";user=" + azureUserName + ";password=" + azurePassword + ";Authentication=" @@ -158,7 +158,12 @@ void testUserName(Connection conn, String user, SqlAuthentication authentication if (SqlAuthentication.ActiveDirectoryIntegrated != authentication) { assertTrue(user.equals(rs.getString(1))); } else { - assertTrue(rs.getString(1).contains(System.getProperty("user.name"))); + if (isWindows) { + assertTrue(rs.getString(1).contains(System.getProperty("user.name"))); + } else { + // cannot verify user in kerberos tickets so just check it's not empty + assertTrue(!rs.getString(1).isEmpty()); + } } } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthTest.java index ac383935a..45308fbae 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthTest.java @@ -106,7 +106,7 @@ public void testActiveDirectoryPasswordDS() throws Exception { @Test public void testActiveDirectoryIntegratedDS() throws Exception { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); SQLServerDataSource ds = new SQLServerDataSource(); ds.setServerName(azureServer); @@ -173,7 +173,7 @@ public void testNotValidSqlPassword() throws SQLException { @Test public void testNotValidActiveDirectoryIntegrated() throws SQLException { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); testNotValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), false, true); testNotValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), true, true); @@ -200,7 +200,7 @@ public void testValidSqlPassword() throws SQLException { @Test public void testValidActiveDirectoryIntegrated() throws SQLException { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); testValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), false, true); testValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), true, true); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/PooledConnectionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/PooledConnectionTest.java index c4d31c483..9183b6683 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/PooledConnectionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/fedauth/PooledConnectionTest.java @@ -58,7 +58,7 @@ public void testPooledConnectionAccessTokenExpiredThenReconnectADPassword() thro @Test public void testPooledConnectionAccessTokenExpiredThenReconnectADIntegrated() throws SQLException { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); // suspend 5 mins testPooledConnectionAccessTokenExpiredThenReconnect((long) 5 * 60, SqlAuthentication.ActiveDirectoryIntegrated); @@ -132,7 +132,7 @@ public void testPooledConnectionMultiThreadADPassword() throws SQLException { @Test public void testPooledConnectionMultiThreadADIntegrated() throws SQLException { - org.junit.Assume.assumeTrue(isWindows && enableADIntegrated); + org.junit.Assume.assumeTrue(enableADIntegrated); testPooledConnectionMultiThread(secondsBeforeExpiration, SqlAuthentication.ActiveDirectoryIntegrated); }