Skip to content

Commit

Permalink
Update isIBM()
Browse files Browse the repository at this point in the history
  • Loading branch information
muskan124947 committed Jan 29, 2025
1 parent 7da553c commit 6187602
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ public class JaasConfiguration extends Configuration {
private final Configuration delegate;
private AppConfigurationEntry[] defaultValue;

private static boolean useIbmModule = false;

private static AppConfigurationEntry[] generateDefaultConfiguration() throws SQLServerException {
try {
if (useIbmModule) {
if (Util.isIBM()) {
return loadIbmModule();

Check warning on line 25 in src/main/java/com/microsoft/sqlserver/jdbc/JaasConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/JaasConfiguration.java#L25

Added line #L25 was not covered by tests
}
Class.forName("com.sun.security.auth.module.Krb5LoginModule");
Expand All @@ -40,7 +38,6 @@ private static AppConfigurationEntry[] generateDefaultConfiguration() throws SQL
private static AppConfigurationEntry[] loadIbmModule() throws SQLServerException {
try {
Class.forName("com.ibm.security.auth.module.Krb5LoginModule");

Check warning on line 40 in src/main/java/com/microsoft/sqlserver/jdbc/JaasConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/JaasConfiguration.java#L40

Added line #L40 was not covered by tests
useIbmModule = true;
Map<String, String> confDetailsWithoutPassword = new HashMap<>();
confDetailsWithoutPassword.put("useDefaultCcache", "true");
Map<String, String> confDetailsWithPassword = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ final class KerbAuthentication extends SSPIAuthentication {
private GSSContext peerContext = null;

static {
// Overrides the default JAAS configuration loader.
// This one will forward to the default one in all cases but the default configuration is empty.
try {
// Overrides the default JAAS configuration loader.
// This one will forward to the default one in all cases but the default configuration is empty.
Configuration.setConfiguration(new JaasConfiguration(Configuration.getConfiguration()));
} catch (SQLServerException e) {
e.printStackTrace();
throw new RuntimeException("Failed to set JAAS configuration: " + e.getMessage(), e);
}

Check warning on line 52 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#L49-L52

Added lines #L49 - L52 were not covered by tests
}

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,26 @@ private Util() {
static final String SYSTEM_JRE = System.getProperty("java.vendor") + " " + System.getProperty("java.version");
private static final Lock LOCK = new ReentrantLock();

private static Boolean isIBM = null;

static boolean isIBM() {
if (isIBM != null) {
return isIBM;
}

String vmName = System.getProperty("java.vm.name");
return SYSTEM_JRE.startsWith("IBM") && vmName.startsWith("IBM");
if (vmName != null && vmName.startsWith("IBM")) {
isIBM = true;
return isIBM;

Check warning on line 60 in src/main/java/com/microsoft/sqlserver/jdbc/Util.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/Util.java#L59-L60

Added lines #L59 - L60 were not covered by tests
}

try {
Class.forName("com.ibm.security.auth.module.Krb5LoginModule");
isIBM = true;

Check warning on line 65 in src/main/java/com/microsoft/sqlserver/jdbc/Util.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/Util.java#L64-L65

Added lines #L64 - L65 were not covered by tests
} catch (ClassNotFoundException ex) {
isIBM = false;
}

Check warning on line 68 in src/main/java/com/microsoft/sqlserver/jdbc/Util.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/Util.java#L68

Added line #L68 was not covered by tests
return isIBM;
}

static String getJVMArchOnWindows() {
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/com/microsoft/sqlserver/jdbc/KerberosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,29 @@ public void testKerberosConnectionWithDefaultJaasConfig() {
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());
if (Util.isIBM()) {
Assertions.assertEquals("com.ibm.security.auth.module.Krb5LoginModule", entries[0].getLoginModuleName());
} else {
Assertions.assertEquals("com.sun.security.auth.module.Krb5LoginModule", entries[0].getLoginModuleName());
}
} catch (Exception e) {
Assertions.fail("Exception was thrown: " + e.getMessage());
}
}

/**
* Test to verify the JaasConfiguration constructor
*/
@Test
public void testJaasConfigurationConstructor() {
try {
JaasConfiguration config = new JaasConfiguration(Configuration.getConfiguration());
Assertions.assertNotNull(config);
} catch (SQLServerException e) {
Assertions.fail("Exception was thrown: " + e.getMessage());
}
}

/**
* Overwrites the default JAAS config. Call before making a connection.
*/
Expand Down

0 comments on commit 6187602

Please sign in to comment.