-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a new connection property "useDefaultJaasConfig" to coexist wi…
…th solutions that overwrite the system JAAS (#2147) * Added a new connection property "useDefaultJaasConfig" * New option to allow the JDBC driver to perform Kerberos authentication using its builtin JaasConfiguration(), to easily coexist with an external JAAS configuration that does not provide a SQLJDBCDriver Login module configuration. * Warning is printed if the jaasConfigurationName is non-default at the same time as useDefaultJaasConfig, as the jaasConfigurationName will not be used. * Added tests for useDefaultJaasConfig * PR comments --------- Co-authored-by: Terry Chow <[email protected]> Co-authored-by: Terry Chow <[email protected]>
- Loading branch information
1 parent
5ac4e61
commit 3a7daf3
Showing
18 changed files
with
197 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/com/microsoft/sqlserver/jdbc/KerberosTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.microsoft.sqlserver.jdbc; | ||
|
||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
import com.microsoft.sqlserver.testframework.Constants; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.security.auth.login.AppConfigurationEntry; | ||
import javax.security.auth.login.Configuration; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class KerberosTest extends AbstractTest { | ||
|
||
private static String kerberosAuth = "KERBEROS"; | ||
|
||
@BeforeAll | ||
public static void setupTests() throws Exception { | ||
setConnection(); | ||
} | ||
|
||
@Tag(Constants.Kerberos) | ||
@Test | ||
public void testUseDefaultJaasConfigConnectionStringPropertyTrue() throws Exception { | ||
String connectionStringUseDefaultJaasConfig = connectionStringKerberos + ";useDefaultJaasConfig=true;"; | ||
|
||
// Initial connection should succeed with default JAAS config | ||
try (SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionStringUseDefaultJaasConfig)) { | ||
ResultSet rs = conn.createStatement().executeQuery("select auth_scheme from sys.dm_exec_connections where session_id=@@spid"); | ||
rs.next(); | ||
Assertions.assertEquals(kerberosAuth, rs.getString(1)); | ||
} | ||
|
||
// Attempt to overwrite JAAS config. Since useDefaultJaasConfig=true, this should have no effect | ||
// and subsequent connections should succeed. | ||
overwriteJaasConfig(); | ||
|
||
// New connection should successfully connect and continue to use the default JAAS config. | ||
try (SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionStringUseDefaultJaasConfig)) { | ||
ResultSet rs = conn.createStatement().executeQuery("select auth_scheme from sys.dm_exec_connections where session_id=@@spid"); | ||
rs.next(); | ||
Assertions.assertEquals(kerberosAuth, rs.getString(1)); | ||
} | ||
} | ||
|
||
@Tag(Constants.Kerberos) | ||
@Test | ||
public void testUseDefaultJaasConfigConnectionStringPropertyFalse() throws Exception { | ||
|
||
// useDefaultJaasConfig=false by default | ||
// Initial connection should succeed with default JAAS config | ||
try (SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionStringKerberos)) { | ||
ResultSet rs = conn.createStatement().executeQuery("select auth_scheme from sys.dm_exec_connections where session_id=@@spid"); | ||
rs.next(); | ||
Assertions.assertEquals(kerberosAuth, rs.getString(1)); | ||
} | ||
|
||
// Overwrite JAAS config. Since useDefaultJaasConfig=false, overwriting should succeed and have an effect. | ||
// Subsequent connections will fail. | ||
overwriteJaasConfig(); | ||
|
||
// New connection should fail as it is attempting to connect using an overwritten JAAS config. | ||
try (SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionStringKerberos)) { | ||
Assertions.fail(TestResource.getResource("R_expectedExceptionNotThrown")); | ||
} catch (SQLServerException e) { | ||
Assertions.assertTrue(e.getMessage() | ||
.contains(TestResource.getResource("R_noLoginModulesConfiguredForJdbcDriver"))); | ||
} | ||
} | ||
|
||
/** | ||
* Overwrites the default JAAS config. Call before making a connection. | ||
*/ | ||
private static void overwriteJaasConfig() { | ||
AppConfigurationEntry kafkaClientConfigurationEntry = new AppConfigurationEntry( | ||
"com.sun.security.auth.module.Krb5LoginModule", | ||
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, | ||
new HashMap<>()); | ||
Map<String, AppConfigurationEntry[]> configurationEntries = new HashMap<>(); | ||
configurationEntries.put("KAFKA_CLIENT_CONTEXT_NAME", | ||
new AppConfigurationEntry[] { kafkaClientConfigurationEntry }); | ||
Configuration.setConfiguration(new InternalConfiguration(configurationEntries)); | ||
} | ||
|
||
private static class InternalConfiguration extends Configuration { | ||
private final Map<String, AppConfigurationEntry[]> configurationEntries; | ||
|
||
InternalConfiguration(Map<String, AppConfigurationEntry[]> configurationEntries) { | ||
this.configurationEntries = configurationEntries; | ||
} | ||
|
||
@Override | ||
public AppConfigurationEntry[] getAppConfigurationEntry(String name) { | ||
return this.configurationEntries.get(name); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.