Skip to content

Commit e63814e

Browse files
authored
IBM Semeru Runtime Certified Edition for z/OS, Kerberos and mssql-jdbc don't work together #2576 (#2581)
* IBM Semeru Runtime Certified Edition for z/OS, Kerberos and mssql-jdbc don't work together #2576 * Added test case * Comment * Updated test * Updated the configuration name JAAS * Testing purpose * removed local changes * Added error string and useIbmModule boolean flag * Update isIBM() * Updated the logic
1 parent 83072af commit e63814e

File tree

5 files changed

+90
-16
lines changed

5 files changed

+90
-16
lines changed

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,36 @@ public class JaasConfiguration extends Configuration {
1919
private final Configuration delegate;
2020
private AppConfigurationEntry[] defaultValue;
2121

22-
private static AppConfigurationEntry[] generateDefaultConfiguration() {
23-
if (Util.isIBM()) {
22+
private static AppConfigurationEntry[] generateDefaultConfiguration() throws SQLServerException {
23+
try {
24+
if (Util.isIBM()) {
25+
return loadIbmModule();
26+
}
27+
Class.forName("com.sun.security.auth.module.Krb5LoginModule");
28+
Map<String, String> confDetails = new HashMap<>();
29+
confDetails.put("useTicketCache", "true");
30+
return new AppConfigurationEntry[] {
31+
new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
32+
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, confDetails)};
33+
} catch (ClassNotFoundException e) {
34+
throw new SQLServerException(SQLServerException.getErrString("R_moduleNotFound"), null);
35+
}
36+
}
37+
38+
private static AppConfigurationEntry[] loadIbmModule() throws SQLServerException {
39+
try {
40+
Class.forName("com.ibm.security.auth.module.Krb5LoginModule");
2441
Map<String, String> confDetailsWithoutPassword = new HashMap<>();
2542
confDetailsWithoutPassword.put("useDefaultCcache", "true");
2643
Map<String, String> confDetailsWithPassword = new HashMap<>();
27-
// We generated a two configurations fallback that is suitable for password and password-less authentication
28-
// See
29-
// https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.security.component.80.doc/security-component/jgssDocs/jaas_login_user.html
3044
final String ibmLoginModule = "com.ibm.security.auth.module.Krb5LoginModule";
3145
return new AppConfigurationEntry[] {
3246
new AppConfigurationEntry(ibmLoginModule, AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT,
3347
confDetailsWithoutPassword),
3448
new AppConfigurationEntry(ibmLoginModule, AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT,
3549
confDetailsWithPassword)};
36-
} else {
37-
Map<String, String> confDetails = new HashMap<>();
38-
confDetails.put("useTicketCache", "true");
39-
return new AppConfigurationEntry[] {
40-
new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
41-
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, confDetails)};
50+
} catch (ClassNotFoundException ex) {
51+
throw new SQLServerException(SQLServerException.getErrString("R_ibmModuleNotFound"), null);
4252
}
4353
}
4454

@@ -47,8 +57,10 @@ private static AppConfigurationEntry[] generateDefaultConfiguration() {
4757
*
4858
* @param delegate
4959
* a possibly null delegate
60+
* @throws SQLServerException
61+
* if neither Kerberos module is found: com.sun.security.auth.module.Krb5LoginModule or com.ibm.security.auth.module.Krb5LoginModule
5062
*/
51-
JaasConfiguration(Configuration delegate) {
63+
JaasConfiguration(Configuration delegate) throws SQLServerException {
5264
this.delegate = delegate;
5365
this.defaultValue = generateDefaultConfiguration();
5466
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ final class KerbAuthentication extends SSPIAuthentication {
4343
private GSSContext peerContext = null;
4444

4545
static {
46-
// Overrides the default JAAS configuration loader.
47-
// This one will forward to the default one in all cases but the default configuration is empty.
48-
Configuration.setConfiguration(new JaasConfiguration(Configuration.getConfiguration()));
46+
try {
47+
// Overrides the default JAAS configuration loader.
48+
// This one will forward to the default one in all cases but the default configuration is empty.
49+
Configuration.setConfiguration(new JaasConfiguration(Configuration.getConfiguration()));
50+
} catch (SQLServerException e) {
51+
throw new RuntimeException("Failed to set JAAS configuration: " + e.getMessage(), e);
52+
}
4953
}
5054

5155
/**

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,8 @@ protected Object[][] getContents() {
557557
{"R_InvalidRuleFormat", "Wrong number of parameters supplied to rule. Number of parameters: {0}, expected: 2 or 3."},
558558
{"R_InvalidRetryInterval", "Current retry interval: {0}, is longer than queryTimeout: {1}."},
559559
{"R_UnableToFindClass", "Unable to locate specified class: {0}"},
560+
{"R_ibmModuleNotFound", "com.ibm.security.auth.module.Krb5LoginModule module was not found."},
561+
{"R_moduleNotFound", "Neither com.sun.security.auth.module.Krb5LoginModule nor com.ibm.security.auth.module.Krb5LoginModule was found."},
560562
};
561563
}
562564
// @formatter:on

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,26 @@ private Util() {
4747
static final String SYSTEM_JRE = System.getProperty("java.vendor") + " " + System.getProperty("java.version");
4848
private static final Lock LOCK = new ReentrantLock();
4949

50+
private static Boolean isIBM = null;
51+
5052
static boolean isIBM() {
53+
if (isIBM != null) {
54+
return isIBM;
55+
}
56+
5157
String vmName = System.getProperty("java.vm.name");
52-
return SYSTEM_JRE.startsWith("IBM") && vmName.startsWith("IBM");
58+
if (vmName != null && vmName.startsWith("IBM")) {
59+
isIBM = true;
60+
return isIBM;
61+
}
62+
63+
try {
64+
Class.forName("com.ibm.security.auth.module.Krb5LoginModule");
65+
isIBM = true;
66+
} catch (ClassNotFoundException ex) {
67+
isIBM = false;
68+
}
69+
return isIBM;
5370
}
5471

5572
static String getJVMArchOnWindows() {

src/test/java/com/microsoft/sqlserver/jdbc/KerberosTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,45 @@ private static void createKerberosConnection(String connectionString) throws Exc
9292
}
9393
}
9494

95+
/**
96+
* Test to verify the Kerberos module used
97+
*/
98+
@Test
99+
public void testKerberosConnectionWithDefaultJaasConfig() {
100+
try {
101+
// Set a mock JAAS configuration using the existing method
102+
overwriteJaasConfig();
103+
104+
String connectionString = connectionStringKerberos + ";useDefaultJaasConfig=true;";
105+
createKerberosConnection(connectionString);
106+
107+
Configuration config = Configuration.getConfiguration();
108+
AppConfigurationEntry[] entries = config.getAppConfigurationEntry("CLIENT_CONTEXT_NAME");
109+
Assertions.assertNotNull(entries);
110+
Assertions.assertTrue(entries.length > 0);
111+
if (Util.isIBM()) {
112+
Assertions.assertEquals("com.ibm.security.auth.module.Krb5LoginModule", entries[0].getLoginModuleName());
113+
} else {
114+
Assertions.assertEquals("com.sun.security.auth.module.Krb5LoginModule", entries[0].getLoginModuleName());
115+
}
116+
} catch (Exception e) {
117+
Assertions.fail("Exception was thrown: " + e.getMessage());
118+
}
119+
}
120+
121+
/**
122+
* Test to verify the JaasConfiguration constructor
123+
*/
124+
@Test
125+
public void testJaasConfigurationConstructor() {
126+
try {
127+
JaasConfiguration config = new JaasConfiguration(Configuration.getConfiguration());
128+
Assertions.assertNotNull(config);
129+
} catch (SQLServerException e) {
130+
Assertions.fail("Exception was thrown: " + e.getMessage());
131+
}
132+
}
133+
95134
/**
96135
* Overwrites the default JAAS config. Call before making a connection.
97136
*/

0 commit comments

Comments
 (0)