Skip to content

Commit

Permalink
Updated Jaas config for test
Browse files Browse the repository at this point in the history
  • Loading branch information
muskan124947 committed Jan 29, 2025
1 parent c92be4a commit a466021
Showing 1 changed file with 21 additions and 38 deletions.
59 changes: 21 additions & 38 deletions src/test/java/com/microsoft/sqlserver/jdbc/KerberosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@ public class KerberosTest extends AbstractTest {

@BeforeAll
public static void setupTests() throws Exception {
configureJaas();
setJaasConfiguration();
setConnection();
}

/**
* Configures JAAS for the test environment.
*/
private static void configureJaas() {
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,
options);
Map<String, AppConfigurationEntry[]> configurationEntries = new HashMap<>();
configurationEntries.put("SQLJDBCDriver", new AppConfigurationEntry[] {kerberosConfigurationEntry});
Configuration.setConfiguration(new InternalConfiguration(configurationEntries));
private static void setJaasConfiguration() {
AppConfigurationEntry[] entries = new AppConfigurationEntry[]{
new AppConfigurationEntry(
"com.sun.security.auth.module.Krb5LoginModule",
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
new HashMap<String, Object>() {{
put("useTicketCache", "true");
put("renewTGT", "true");
}}
)
};
Configuration.setConfiguration(new Configuration() {
@Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
if ("SQLJDBCDriver".equals(name)) {
return entries;
}
return null;
}
});
}

@Test
Expand Down Expand Up @@ -109,28 +114,6 @@ 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.
*/
Expand Down

0 comments on commit a466021

Please sign in to comment.