Skip to content

Commit 3a7daf3

Browse files
edanidzerdatkyc
andauthored
Adding a new connection property "useDefaultJaasConfig" to coexist with 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]>
1 parent 5ac4e61 commit 3a7daf3

18 files changed

+197
-12
lines changed

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@
4242
xAzureSQLDB - - - - For tests not compatible with Azure SQL Database - -
4343
xAzureSQLDW - - - - For tests not compatible with Azure Data Warehouse -
4444
xAzureSQLMI - - - - For tests not compatible with Azure SQL Managed Instance
45-
NTLM - - - - - - For tests using NTLM Authentication mode (excluded by default)
46-
reqExternalSetup - For tests requiring external setup (excluded by default)
45+
NTLM - - - - - - - For tests using NTLM Authentication mode (excluded by default)
46+
Kerberos - - - - - For tests using Kerberos authentication (excluded by default)
47+
reqExternalSetup - For tests requiring external setup (excluded by default)
4748
clientCertAuth - - For tests requiring client certificate authentication
4849
setup (excluded by default) - - - - - - - - - - - - - - - - - - - - - - -
4950
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5051
Default testing enabled with SQL Server 2019 (SQLv15) -->
51-
<excludedGroups>xSQLv12,xSQLv15,NTLM,MSI,reqExternalSetup,clientCertAuth,fedAuth</excludedGroups>
52+
<excludedGroups>xSQLv12,xSQLv15,NTLM,MSI,reqExternalSetup,clientCertAuth,fedAuth,kerberos</excludedGroups>
5253
<!-- Use -preview for preview release, leave empty for official release. -->
5354
<releaseExt>-preview</releaseExt>
5455
<!-- Driver Dependencies -->

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
802802
int getSocketTimeout();
803803

804804
/**
805-
* Sets the login configuration file for Kerberos authentication. This overrides the default configuration <i>
805+
* Sets the login configuration name for Kerberos authentication. This overrides the default configuration <i>
806806
* SQLJDBCDriver </i>
807807
*
808808
* @param configurationName
@@ -814,7 +814,7 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
814814
void setJASSConfigurationName(String configurationName);
815815

816816
/**
817-
* Returns the login configuration file for Kerberos authentication.
817+
* Returns the login configuration name for Kerberos authentication.
818818
*
819819
*
820820
* @return login configuration file name
@@ -825,7 +825,7 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
825825
String getJASSConfigurationName();
826826

827827
/**
828-
* Sets the login configuration file for Kerberos authentication. This overrides the default configuration <i>
828+
* Sets the login configuration name for Kerberos authentication. This overrides the default configuration <i>
829829
* SQLJDBCDriver </i>
830830
*
831831
*
@@ -835,12 +835,27 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
835835
void setJAASConfigurationName(String configurationName);
836836

837837
/**
838-
* Returns the login configuration file for Kerberos authentication.
838+
* Returns the login configuration name for Kerberos authentication.
839839
*
840-
* @return login configuration file name
840+
* @return login configuration name
841841
*/
842842
String getJAASConfigurationName();
843843

844+
/**
845+
* Returns whether the default JAAS Configuration should be used
846+
*
847+
* @return useDefaultJaasConfig boolean value
848+
*/
849+
boolean getUseDefaultJaasConfig();
850+
851+
/**
852+
* Sets whether the default JAAS Configuration will be used. This means the system-wide JAAS configuration
853+
* is ignored to avoid conflicts with libraries that override the JAAS configuration.
854+
*
855+
* @param useDefaultJaasConfig
856+
* boolean property to use the default JAAS configuration
857+
*/
858+
void setUseDefaultJaasConfig(boolean useDefaultJaasConfig);
844859
/**
845860
* Sets whether Fips Mode should be enabled/disabled on the connection. For FIPS enabled JVM this property should be
846861
* true.

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,31 @@ private void initAuthInit() throws SQLServerException {
7373
String configName = con.activeConnectionProperties.getProperty(
7474
SQLServerDriverStringProperty.JAAS_CONFIG_NAME.toString(),
7575
SQLServerDriverStringProperty.JAAS_CONFIG_NAME.getDefaultValue());
76+
boolean useDefaultJaas = Boolean.parseBoolean(con.activeConnectionProperties.getProperty(
77+
SQLServerDriverBooleanProperty.USE_DEFAULT_JAAS_CONFIG.toString(),
78+
Boolean.toString(SQLServerDriverBooleanProperty.USE_DEFAULT_JAAS_CONFIG.getDefaultValue())));
79+
80+
if (!configName.equals(
81+
SQLServerDriverStringProperty.JAAS_CONFIG_NAME.getDefaultValue()) && useDefaultJaas) {
82+
// Reset configName to default -- useDefaultJaas setting takes priority over jaasConfigName
83+
if (authLogger.isLoggable(Level.WARNING)) {
84+
authLogger.warning(toString() + String.format(
85+
"Using default JAAS configuration, configured %s=%s will not be used.",
86+
SQLServerDriverStringProperty.JAAS_CONFIG_NAME, configName));
87+
}
88+
configName = SQLServerDriverStringProperty.JAAS_CONFIG_NAME.getDefaultValue();
89+
}
7690
Subject currentSubject;
7791
KerbCallback callback = new KerbCallback(con);
7892
try {
7993
AccessControlContext context = AccessController.getContext();
8094
currentSubject = Subject.getSubject(context);
8195
if (null == currentSubject) {
82-
lc = new LoginContext(configName, callback);
96+
if (useDefaultJaas) {
97+
lc = new LoginContext(configName, null, callback, new JaasConfiguration(null));
98+
} else {
99+
lc = new LoginContext(configName, callback);
100+
}
83101
lc.login();
84102
// per documentation LoginContext will instantiate a new subject.
85103
currentSubject = lc.getSubject();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,18 @@ public String getJAASConfigurationName() {
10301030
SQLServerDriverStringProperty.JAAS_CONFIG_NAME.getDefaultValue());
10311031
}
10321032

1033+
@Override
1034+
public boolean getUseDefaultJaasConfig() {
1035+
return getBooleanProperty(connectionProps, SQLServerDriverBooleanProperty.USE_DEFAULT_JAAS_CONFIG.toString(),
1036+
SQLServerDriverBooleanProperty.USE_DEFAULT_JAAS_CONFIG.getDefaultValue());
1037+
}
1038+
1039+
@Override
1040+
public void setUseDefaultJaasConfig(boolean useDefaultJaasConfig) {
1041+
setBooleanProperty(connectionProps, SQLServerDriverBooleanProperty.USE_DEFAULT_JAAS_CONFIG.toString(),
1042+
useDefaultJaasConfig);
1043+
}
1044+
10331045
/**
10341046
* @deprecated This method is deprecated. Use {@link SQLServerDataSource#setUser(String user)} instead.
10351047
*

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ enum SQLServerDriverBooleanProperty {
693693
USE_BULK_COPY_FOR_BATCH_INSERT("useBulkCopyForBatchInsert", false),
694694
USE_FMT_ONLY("useFmtOnly", false),
695695
SEND_TEMPORAL_DATATYPES_AS_STRING_FOR_BULK_COPY("sendTemporalDataTypesAsStringForBulkCopy", true),
696-
DELAY_LOADING_LOBS("delayLoadingLobs", true);
696+
DELAY_LOADING_LOBS("delayLoadingLobs", true),
697+
USE_DEFAULT_JAAS_CONFIG("useDefaultJaasConfig", false);
697698

698699
private final String name;
699700
private final boolean defaultValue;
@@ -892,6 +893,9 @@ public final class SQLServerDriver implements java.sql.Driver {
892893
null),
893894
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.JAAS_CONFIG_NAME.toString(),
894895
SQLServerDriverStringProperty.JAAS_CONFIG_NAME.getDefaultValue(), false, null),
896+
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.USE_DEFAULT_JAAS_CONFIG.toString(),
897+
Boolean.toString(SQLServerDriverBooleanProperty.USE_DEFAULT_JAAS_CONFIG.getDefaultValue()), false,
898+
TRUE_FALSE),
895899
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.SSL_PROTOCOL.toString(),
896900
SQLServerDriverStringProperty.SSL_PROTOCOL.getDefaultValue(), false,
897901
new String[] {SSLProtocol.TLS.toString(), SSLProtocol.TLS_V10.toString(),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ protected Object[][] getContents() {
451451
{"R_kerberosLoginFailedForUsername", "Cannot login with Kerberos principal {0}, check your credentials. {1}"},
452452
{"R_kerberosLoginFailed", "Kerberos Login failed: {0} due to {1} ({2})"},
453453
{"R_StoredProcedureNotFound", "Could not find stored procedure ''{0}''."},
454-
{"R_jaasConfigurationNamePropertyDescription", "Login configuration file for Kerberos authentication."},
454+
{"R_jaasConfigurationNamePropertyDescription", "Login configuration name for Kerberos authentication."},
455+
{"R_useDefaultJaasConfigPropertyDescription", "Use the default JAAS configuration for Kerberos authentication."},
455456
{"R_AKVKeyNotFound", "Key not found: {0}"},
456457
{"R_SQLVariantSupport", "SQL_VARIANT is not supported in versions of SQL Server before 2008."},
457458
{"R_invalidProbbytes", "SQL_VARIANT: invalid probBytes for {0} type."},

src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/BulkCopySendTemporalDataTypesAsStringAETest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
@Tag(Constants.xSQLv12)
5858
@Tag(Constants.xAzureSQLDB)
5959
@Tag(Constants.xAzureSQLDW)
60+
@Tag(Constants.reqExternalSetup)
6061
public class BulkCopySendTemporalDataTypesAsStringAETest extends AESetup {
6162
static String inputFile = "BulkCopyCSVSendTemporalDataTypesAsStringForBulkCopy.csv";
6263
static String encoding = "UTF-8";

src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/CallableStatementTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
@Tag(Constants.xSQLv12)
5454
@Tag(Constants.xAzureSQLDW)
5555
@Tag(Constants.xAzureSQLDB)
56+
@Tag(Constants.reqExternalSetup)
5657
public class CallableStatementTest extends AESetup {
5758

5859
private static String multiStatementsProcedure = AbstractSQLGenerator

src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/JDBCEncryptionDecryptionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
@Tag(Constants.xSQLv12)
6565
@Tag(Constants.xAzureSQLDW)
6666
@Tag(Constants.xAzureSQLDB)
67+
@Tag(Constants.reqExternalSetup)
6768
public class JDBCEncryptionDecryptionTest extends AESetup {
6869
private boolean nullable = false;
6970

src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/MultiUserAKVTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
@Tag(Constants.xSQLv12)
5959
@Tag(Constants.xAzureSQLDW)
6060
@Tag(Constants.xAzureSQLDB)
61+
@Tag(Constants.reqExternalSetup)
6162
public class MultiUserAKVTest extends AESetup {
6263

6364
private static Map<String, SQLServerColumnEncryptionKeyStoreProvider> requiredKeyStoreProvider = new HashMap<>();

0 commit comments

Comments
 (0)