Skip to content

Commit

Permalink
Updated jaas config
Browse files Browse the repository at this point in the history
  • Loading branch information
muskan124947 committed Jan 28, 2025
1 parent 5691df0 commit ad2a067
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.logging.Level;

import javax.security.auth.Subject;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

Expand Down Expand Up @@ -108,7 +107,7 @@ private void initAuthInit() throws SQLServerException {

if (null == currentSubject) {
if (useDefaultJaas) {
lc = new LoginContext(configName, null, callback, new JaasConfiguration(null));
lc = new LoginContext(configName, null, callback);

Check warning on line 110 in src/main/java/com/microsoft/sqlserver/jdbc/KerbAuthentication.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/KerbAuthentication.java#L110

Added line #L110 was not covered by tests
} else {
lc = new LoginContext(configName, callback);
}
Expand Down
45 changes: 38 additions & 7 deletions src/test/java/com/microsoft/sqlserver/jdbc/KerberosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.HashMap;
import java.util.Map;


@Tag(Constants.kerberos)
@RunWith(JUnitPlatform.class)
public class KerberosTest extends AbstractTest {
Expand All @@ -34,11 +33,16 @@ public static void setupTests() throws Exception {
* Configures JAAS for the test environment.
*/
private static void configureJaas() {
AppConfigurationEntry kafkaClientConfigurationEntry = new AppConfigurationEntry(
Map<String, String> options = new HashMap<>();
options.put("useTicketCache", "true");
options.put("renewTGT", "true");
options.put("doNotPrompt", "false"); // Allow prompting for credentials if necessary

AppConfigurationEntry kerberosConfigurationEntry = new AppConfigurationEntry(
"com.sun.security.auth.module.Krb5LoginModule", AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
new HashMap<>());
options);
Map<String, AppConfigurationEntry[]> configurationEntries = new HashMap<>();
configurationEntries.put("SQLJDBCDriver", new AppConfigurationEntry[] {kafkaClientConfigurationEntry});
configurationEntries.put("SQLJDBCDriver", new AppConfigurationEntry[] {kerberosConfigurationEntry});
Configuration.setConfiguration(new InternalConfiguration(configurationEntries));
}

Expand Down Expand Up @@ -105,15 +109,42 @@ private static void createKerberosConnection(String connectionString) throws Exc
}
}

/**
* Test to verify the Kerberos module used
*/
@Test
public void testKerberosConnectionWithDefaultJaasConfig() {
try {
// Set a mock JAAS configuration using the existing method
overwriteJaasConfig();

String connectionString = connectionStringKerberos + ";useDefaultJaasConfig=true;";
createKerberosConnection(connectionString);

Configuration config = Configuration.getConfiguration();
AppConfigurationEntry[] entries = config.getAppConfigurationEntry("CLIENT_CONTEXT_NAME");
Assertions.assertNotNull(entries);
Assertions.assertTrue(entries.length > 0);
Assertions.assertEquals("com.sun.security.auth.module.Krb5LoginModule", entries[0].getLoginModuleName());
} catch (Exception e) {
Assertions.fail("Exception was thrown: " + e.getMessage());
}
}

/**
* Overwrites the default JAAS config. Call before making a connection.
*/
private static void overwriteJaasConfig() {
AppConfigurationEntry kafkaClientConfigurationEntry = new AppConfigurationEntry(
Map<String, String> options = new HashMap<>();
options.put("useTicketCache", "true");
options.put("renewTGT", "true");
options.put("doNotPrompt", "false"); // Allow prompting for credentials if necessary

AppConfigurationEntry kerberosConfigurationEntry = new AppConfigurationEntry(
"com.sun.security.auth.module.Krb5LoginModule", AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
new HashMap<>());
options);
Map<String, AppConfigurationEntry[]> configurationEntries = new HashMap<>();
configurationEntries.put("CLIENT_CONTEXT_NAME", new AppConfigurationEntry[] {kafkaClientConfigurationEntry});
configurationEntries.put("CLIENT_CONTEXT_NAME", new AppConfigurationEntry[] {kerberosConfigurationEntry});
Configuration.setConfiguration(new InternalConfiguration(configurationEntries));
}

Expand Down

0 comments on commit ad2a067

Please sign in to comment.