From 408cd8b5752bdcbacc452ade47f489068ab984b0 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Tue, 27 Aug 2019 10:23:07 -0700 Subject: [PATCH 01/15] fix for PKC12 truststore issue --- .../microsoft/sqlserver/jdbc/SQLServerDataSource.java | 4 ++++ .../sqlserver/jdbc/SQLServerXAConnection.java | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java index 64c523fd7..4d9dff0fd 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java @@ -358,6 +358,10 @@ public void setTrustStorePassword(String trustStorePassword) { trustStorePassword); } + String getTrustStorePassword() { + return getStringProperty(connectionProps, SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString(), null); + } + @Override public void setHostNameInCertificate(String hostName) { setStringProperty(connectionProps, SQLServerDriverStringProperty.HOSTNAME_IN_CERTIFICATE.toString(), hostName); diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java index 958ffa365..829c8b6f3 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java @@ -52,6 +52,17 @@ public final class SQLServerXAConnection extends SQLServerPooledConnection imple controlConnectionProperties.setProperty(SQLServerDriverStringProperty.PASSWORD.toString(), pwd); } + // Add truststore password property for creating the control connection. This will be removed again + String trustStorePassword = ds.getTrustStorePassword(); + if (null == trustStorePassword) { + // trustStorePassword can either come from the connection string or added via SQLServerXADataSource::setTrustStorePassword. + // if trustStorePassword is null here, then it must have been provided through the connection string. + Properties urlProps = Util.parseUrl(ds.getURL(), xaLogger); + trustStorePassword = urlProps.getProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString()); + } + + controlConnectionProperties.setProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString(), trustStorePassword); + if (xaLogger.isLoggable(Level.FINER)) xaLogger.finer("Creating an internal control connection for" + toString()); physicalControlConnection = null; From 44bb3d7e0aa96e2d6b7274880a2b9174653c39e7 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Tue, 27 Aug 2019 16:06:16 -0700 Subject: [PATCH 02/15] add null check --- .../microsoft/sqlserver/jdbc/SQLServerXAConnection.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java index 829c8b6f3..ff4e65e0e 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java @@ -56,12 +56,15 @@ public final class SQLServerXAConnection extends SQLServerPooledConnection imple String trustStorePassword = ds.getTrustStorePassword(); if (null == trustStorePassword) { // trustStorePassword can either come from the connection string or added via SQLServerXADataSource::setTrustStorePassword. - // if trustStorePassword is null here, then it must have been provided through the connection string. + // if trustStorePassword is null at this point, then check the connection string. Properties urlProps = Util.parseUrl(ds.getURL(), xaLogger); trustStorePassword = urlProps.getProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString()); } - controlConnectionProperties.setProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString(), trustStorePassword); + // if trustStorePassword is still null, it wasn't provided. Do not set the property as null to avoid NPE. + if (null != trustStorePassword) { + controlConnectionProperties.setProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString(), trustStorePassword); + } if (xaLogger.isLoggable(Level.FINER)) xaLogger.finer("Creating an internal control connection for" + toString()); From 6a48400f954e310e9ad7a9698f7669b08399c969 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Mon, 23 Sep 2019 15:23:11 -0700 Subject: [PATCH 03/15] file format --- .../sqlserver/jdbc/SQLServerDataSource.java | 7 +++---- .../sqlserver/jdbc/SQLServerXAConnection.java | 12 +++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java index 4d9dff0fd..046dd7313 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java @@ -482,11 +482,10 @@ public boolean getSendTimeAsDatetime() { return getBooleanProperty(connectionProps, SQLServerDriverBooleanProperty.SEND_TIME_AS_DATETIME.toString(), SQLServerDriverBooleanProperty.SEND_TIME_AS_DATETIME.getDefaultValue()); } - + @Override public void setUseFmtOnly(boolean useFmtOnly) { - setBooleanProperty(connectionProps, SQLServerDriverBooleanProperty.USE_FMT_ONLY.toString(), - useFmtOnly); + setBooleanProperty(connectionProps, SQLServerDriverBooleanProperty.USE_FMT_ONLY.toString(), useFmtOnly); } @Override @@ -494,7 +493,7 @@ public boolean getUseFmtOnly() { return getBooleanProperty(connectionProps, SQLServerDriverBooleanProperty.USE_FMT_ONLY.toString(), SQLServerDriverBooleanProperty.USE_FMT_ONLY.getDefaultValue()); } - + /** * Sets whether string parameters are sent to the server in UNICODE format. * diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java index ff4e65e0e..fe1067775 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java @@ -55,15 +55,17 @@ public final class SQLServerXAConnection extends SQLServerPooledConnection imple // Add truststore password property for creating the control connection. This will be removed again String trustStorePassword = ds.getTrustStorePassword(); if (null == trustStorePassword) { - // trustStorePassword can either come from the connection string or added via SQLServerXADataSource::setTrustStorePassword. - // if trustStorePassword is null at this point, then check the connection string. - Properties urlProps = Util.parseUrl(ds.getURL(), xaLogger); - trustStorePassword = urlProps.getProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString()); + // trustStorePassword can either come from the connection string or added via + // SQLServerXADataSource::setTrustStorePassword. + // if trustStorePassword is null at this point, then check the connection string. + Properties urlProps = Util.parseUrl(ds.getURL(), xaLogger); + trustStorePassword = urlProps.getProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString()); } // if trustStorePassword is still null, it wasn't provided. Do not set the property as null to avoid NPE. if (null != trustStorePassword) { - controlConnectionProperties.setProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString(), trustStorePassword); + controlConnectionProperties.setProperty(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString(), + trustStorePassword); } if (xaLogger.isLoggable(Level.FINER)) From 5088fcb152012f3c27fbbdb6bbaa525a9c8e1c0f Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Wed, 25 Sep 2019 16:40:05 -0700 Subject: [PATCH 04/15] add test --- azure-pipelines.yml | 6 +- pom.xml | 3 +- .../jdbc/connection/XADataSourceTest.java | 93 +++++++++++++++++++ .../sqlserver/testframework/Constants.java | 2 + 4 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java diff --git a/azure-pipelines.yml b/azure-pipelines.yml index e80e73dae..733c1dc1f 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -33,7 +33,7 @@ jobs: displayName: 'Maven build jre12' inputs: mavenPomFile: 'pom.xml' - goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre12 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups)' + goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre12 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore)' testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre12' javaHomeOption: Path @@ -42,7 +42,7 @@ jobs: displayName: 'Maven build jre11' inputs: mavenPomFile: 'pom.xml' - goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre11 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups)' + goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre11 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore)' testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre11' javaHomeOption: Path @@ -51,7 +51,7 @@ jobs: displayName: 'Maven build jre8' inputs: mavenPomFile: 'pom.xml' - goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre8 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups)' + goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre8 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore)' testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre8' javaHomeOption: Path diff --git a/pom.xml b/pom.xml index 1b3eb944b..985db01d6 100644 --- a/pom.xml +++ b/pom.xml @@ -50,9 +50,10 @@ xAzureSQLDW - - - - For tests not compatible with Azure Data Warehouse - xAzureSQLMI - - - - For tests not compatible with Azure SQL Managed Instance NTLM - - - - - - For tests using NTLM Authentication mode (excluded by default) + XA - - - - - - - For XA tests (excluded by default) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Default testing enabled with SQL Server 2019 (SQLv14) --> - xSQLv15, NTLM + xSQLv15, NTLM, XA 1.2.1 diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java new file mode 100644 index 000000000..e2676695c --- /dev/null +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -0,0 +1,93 @@ +package com.microsoft.sqlserver.jdbc.connection; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.security.KeyStore; +import java.security.cert.CertificateFactory; +import java.util.ArrayList; +import java.util.List; + +import javax.sql.XAConnection; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +import com.microsoft.sqlserver.jdbc.SQLServerXADataSource; +import com.microsoft.sqlserver.testframework.AbstractTest; +import com.microsoft.sqlserver.testframework.Constants; + + +@RunWith(JUnitPlatform.class) +@Tag(Constants.XA) +public class XADataSourceTest extends AbstractTest { + private static String connectionUrlSSL = connectionString + "encrypt=true;trustServerCertificate=false;"; + private static List certificates = new ArrayList<>(); + + @Test + public void testPKCS12() throws Exception { + SQLServerXADataSource ds = new SQLServerXADataSource(); + + // populate certificates arraylist with certificates + // Only re-populate the truststore if need arises in the future. + + // populateCertificates(); + // String trustStore = (new TrustStore(certificates)).getFileName(); + + String trustStore = System.getProperty("pkcs12_truststore"); + String url = connectionUrlSSL + "trustStore=" + trustStore + ";"; + ds.setURL(url); + ds.setTrustStorePassword(System.getProperty("pkcs12_truststore_password")); + XAConnection connection = ds.getXAConnection(); + connection.close(); + } + + private static void populateCertificates() { + certificates.add("sql-2k8r2-sp3-1.galaxy.ad.cer"); + certificates.add("sql-2k8-sp4-1.galaxy.ad.cer"); + certificates.add("sql-2k12-sp3-2.galaxy.ad.cer"); + certificates.add("sql-2k14-2.galaxy.ad.cer"); + certificates.add("sql-2k16-01.galaxy.ad.cer"); + certificates.add("sql-2k16-02.galaxy.ad.cer"); + certificates.add("sql-2k16-04.galaxy.ad.cer"); + certificates.add("sql-2k17-01.galaxy.ad.cer"); + certificates.add("sql-2k17-03.galaxy.ad.cer"); + certificates.add("sql-2k17-04.galaxy.ad.cer"); + certificates.add("sql-2k19-01.galaxy.ad.cer"); + certificates.add("sql-2k19-02.galaxy.ad.cer"); + } + + static class TrustStore { + private File trustStoreFile; + + static final String TRUST_STORE_PWD = ""; + + TrustStore(List certificateNames) throws Exception { + trustStoreFile = File.createTempFile("myTrustStore", null, new File(".")); + // trustStoreFile.deleteOnExit(); + KeyStore ks = KeyStore.getInstance("PKCS12"); + ks.load(null, null); + + for (String certificateName : certificateNames) { + ks.setCertificateEntry(certificateName, getCertificate(certificateName)); + } + + FileOutputStream os = new FileOutputStream(trustStoreFile); + ks.store(os, TRUST_STORE_PWD.toCharArray()); + os.flush(); + os.close(); + } + + final String getFileName() throws Exception { + return trustStoreFile.getCanonicalPath(); + } + + private static java.security.cert.Certificate getCertificate(String certname) throws Exception { + FileInputStream is = new FileInputStream(certname); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + return cf.generateCertificate(is); + } + } +} diff --git a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java index b1ff0d894..0757fae87 100644 --- a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java +++ b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java @@ -25,6 +25,7 @@ private Constants() {} * xAzureSQLDW - - - - For tests not compatible with Azure Data Warehouse * xAzureSQLMI - - - - For tests not compatible with Azure SQL Managed Instance * NTLM - - - - - - - For NTLM tests + * XA - - - - - - - - For XA tests * */ public static final String xJDBC42 = "xJDBC42"; @@ -36,6 +37,7 @@ private Constants() {} public static final String xAzureSQLDW = "xAzureSQLDW"; public static final String xAzureSQLMI = "xAzureSQLMI"; public static final String NTLM = "NTLM"; + public static final String XA = "XA"; public static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current(); public static final Logger LOGGER = Logger.getLogger("AbstractTest"); From 4cd2fdbacf447b88c7079a3fe5faacefc8847712 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Wed, 25 Sep 2019 16:40:35 -0700 Subject: [PATCH 05/15] add header --- .../sqlserver/jdbc/connection/XADataSourceTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java index e2676695c..84e4a09a9 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -1,3 +1,8 @@ +/* + * Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made + * available under the terms of the MIT License. See the LICENSE file in the project root for more information. + */ + package com.microsoft.sqlserver.jdbc.connection; import java.io.File; From 3052d5451aca3595c977e06de2c297e4afb6a9fb Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 09:04:03 -0700 Subject: [PATCH 06/15] upload secure file --- azure-pipelines.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 733c1dc1f..e3416a85e 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -29,6 +29,9 @@ jobs: Get-Content .\JavaKeyStoreBase.txt | Set-Content -Encoding utf8 JavaKeyStore.txt Remove-Item –path .\JavaKeyStoreBase.txt displayName: 'PowerShell Script' + - task: DownloadSecureFile@1 + inputs: + secureFile: 'pkcs12_truststore' - task: Maven@3 displayName: 'Maven build jre12' inputs: @@ -55,4 +58,4 @@ jobs: testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre8' javaHomeOption: Path - jdkDirectory: $(JDK12) + jdkDirectory: $(JDK12) \ No newline at end of file From 432524e857ffc26aef76e9fbdc12b24547c2d8ce Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 09:16:15 -0700 Subject: [PATCH 07/15] test --- .../microsoft/sqlserver/jdbc/connection/XADataSourceTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java index 84e4a09a9..33803d538 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -45,6 +45,9 @@ public void testPKCS12() throws Exception { String url = connectionUrlSSL + "trustStore=" + trustStore + ";"; ds.setURL(url); ds.setTrustStorePassword(System.getProperty("pkcs12_truststore_password")); + System.out.println("pkcs12_truststore = " + trustStore); + System.out.println("pkcs12_truststore_password = " + System.getProperty("pkcs12_truststore_password")); + System.out.println("Working Directory = " + System.getProperty("user.dir")); XAConnection connection = ds.getXAConnection(); connection.close(); } From 0625f4358c31a97c3484fbb7163c766c12658f29 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 09:24:42 -0700 Subject: [PATCH 08/15] update secure file path --- azure-pipelines.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index e3416a85e..e9fdd665c 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -30,13 +30,15 @@ jobs: Remove-Item –path .\JavaKeyStoreBase.txt displayName: 'PowerShell Script' - task: DownloadSecureFile@1 + name: pkcs12_truststore + displayName: 'Download PKCS12 truststore file' inputs: secureFile: 'pkcs12_truststore' - task: Maven@3 displayName: 'Maven build jre12' inputs: mavenPomFile: 'pom.xml' - goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre12 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore)' + goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre12 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore.secureFilePath)' testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre12' javaHomeOption: Path @@ -45,7 +47,7 @@ jobs: displayName: 'Maven build jre11' inputs: mavenPomFile: 'pom.xml' - goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre11 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore)' + goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre11 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore.secureFilePath)' testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre11' javaHomeOption: Path @@ -54,7 +56,7 @@ jobs: displayName: 'Maven build jre8' inputs: mavenPomFile: 'pom.xml' - goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre8 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore)' + goals: 'clean -Dmssql_jdbc_test_connection_properties=jdbc:sqlserver://$(Target_SQL)$(server_domain);$(database);$(user);$(password); install -Pjre8 -DuserNTLM=$(userNTLM) -DpasswordNTLM=$(passwordNTLM) -DdomainNTLM=$(domainNTLM) -DexcludedGroups=$(Ex_Groups) -Dpkcs12_truststore_password=$(pkcs12_truststore_password) -Dpkcs12_truststore=$(pkcs12_truststore.secureFilePath)' testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre8' javaHomeOption: Path From da4583bcf8a736cbe5c8e1bf21b2a580b8e52428 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 09:39:40 -0700 Subject: [PATCH 09/15] remove debugging code --- .../microsoft/sqlserver/jdbc/connection/XADataSourceTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java index 33803d538..84e4a09a9 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -45,9 +45,6 @@ public void testPKCS12() throws Exception { String url = connectionUrlSSL + "trustStore=" + trustStore + ";"; ds.setURL(url); ds.setTrustStorePassword(System.getProperty("pkcs12_truststore_password")); - System.out.println("pkcs12_truststore = " + trustStore); - System.out.println("pkcs12_truststore_password = " + System.getProperty("pkcs12_truststore_password")); - System.out.println("Working Directory = " + System.getProperty("user.dir")); XAConnection connection = ds.getXAConnection(); connection.close(); } From 31a11314fec9b878b4cca164f2c1ead8e4105259 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 09:48:00 -0700 Subject: [PATCH 10/15] cleanup --- azure-pipelines.yml | 2 +- .../jdbc/connection/XADataSourceTest.java | 21 +++++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index e9fdd665c..45647c1e6 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -60,4 +60,4 @@ jobs: testResultsFiles: '**/TEST-*.xml' testRunTitle: 'Maven build jre8' javaHomeOption: Path - jdkDirectory: $(JDK12) \ No newline at end of file + jdkDirectory: $(JDK12) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java index 84e4a09a9..caac3dc01 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -31,6 +31,10 @@ public class XADataSourceTest extends AbstractTest { private static String connectionUrlSSL = connectionString + "encrypt=true;trustServerCertificate=false;"; private static List certificates = new ArrayList<>(); + /** + * Tests XA connection with PKCS12 truststore that is password protected. + * @throws Exception + */ @Test public void testPKCS12() throws Exception { SQLServerXADataSource ds = new SQLServerXADataSource(); @@ -50,18 +54,9 @@ public void testPKCS12() throws Exception { } private static void populateCertificates() { - certificates.add("sql-2k8r2-sp3-1.galaxy.ad.cer"); - certificates.add("sql-2k8-sp4-1.galaxy.ad.cer"); - certificates.add("sql-2k12-sp3-2.galaxy.ad.cer"); - certificates.add("sql-2k14-2.galaxy.ad.cer"); - certificates.add("sql-2k16-01.galaxy.ad.cer"); - certificates.add("sql-2k16-02.galaxy.ad.cer"); - certificates.add("sql-2k16-04.galaxy.ad.cer"); - certificates.add("sql-2k17-01.galaxy.ad.cer"); - certificates.add("sql-2k17-03.galaxy.ad.cer"); - certificates.add("sql-2k17-04.galaxy.ad.cer"); - certificates.add("sql-2k19-01.galaxy.ad.cer"); - certificates.add("sql-2k19-02.galaxy.ad.cer"); + // populate the arraylist with all the certificates of servers that are used + certificates.add(".cer"); + certificates.add(".cer"); } static class TrustStore { @@ -70,7 +65,7 @@ static class TrustStore { static final String TRUST_STORE_PWD = ""; TrustStore(List certificateNames) throws Exception { - trustStoreFile = File.createTempFile("myTrustStore", null, new File(".")); + trustStoreFile = File.createTempFile("", null, new File(".")); // trustStoreFile.deleteOnExit(); KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(null, null); From 61402e056e1c02a94dac767750bf4ec8abc458c0 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 09:51:51 -0700 Subject: [PATCH 11/15] formatting --- .../microsoft/sqlserver/jdbc/connection/XADataSourceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java index caac3dc01..6651b4f0c 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -33,6 +33,7 @@ public class XADataSourceTest extends AbstractTest { /** * Tests XA connection with PKCS12 truststore that is password protected. + * * @throws Exception */ @Test @@ -59,7 +60,7 @@ private static void populateCertificates() { certificates.add(".cer"); } - static class TrustStore { + private static class TrustStore { private File trustStoreFile; static final String TRUST_STORE_PWD = ""; From f12a2ec9624485bfdde6a987795beb1aef55011a Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 12:35:11 -0700 Subject: [PATCH 12/15] cleanup --- pom.xml | 6 +- .../microsoft/sqlserver/jdbc/TestUtils.java | 55 ++++++++++++++++++ .../jdbc/connection/XADataSourceTest.java | 58 ++----------------- .../sqlserver/testframework/Constants.java | 2 +- 4 files changed, 63 insertions(+), 58 deletions(-) diff --git a/pom.xml b/pom.xml index 985db01d6..0c4c4a729 100644 --- a/pom.xml +++ b/pom.xml @@ -49,11 +49,11 @@ xAzureSQLDB - - - - For tests not compatible with Azure SQL Database - - xAzureSQLDW - - - - For tests not compatible with Azure Data Warehouse - xAzureSQLMI - - - - For tests not compatible with Azure SQL Managed Instance - NTLM - - - - - - For tests using NTLM Authentication mode (excluded by default) - XA - - - - - - - For XA tests (excluded by default) + NTLM - - - - - - - For tests using NTLM Authentication mode (excluded by default) + reqExternalSetup - For tests requiring external setup (excluded by default) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Default testing enabled with SQL Server 2019 (SQLv14) --> - xSQLv15, NTLM, XA + xSQLv15, NTLM, reqExternalSetup 1.2.1 diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java b/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java index 1e257dec3..791b83a72 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java @@ -10,7 +10,12 @@ import java.io.ByteArrayInputStream; import java.io.CharArrayReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.net.URI; +import java.security.KeyStore; +import java.security.cert.CertificateFactory; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; @@ -21,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.List; import java.util.Locale; import java.util.ResourceBundle; @@ -827,4 +833,53 @@ public static String formatErrorMsg(String s) { public static String addOrOverrideProperty(String connectionString, String property, String value) { return connectionString + ";" + property + "=" + value + ";"; } + + /** + * Creates a truststore and returns the path of it. + * + * @param certificates + * String list of certificates + * @param tsName + * name of truststore to create + * @param tsPwd + * password of truststore to set + * @param ksType + * type of Keystore e.g PKCS12 or JKS + * @return Path of truststore that was created + * @throws Exception + */ + public static String createTrustStore(List certificates, String tsName, String tsPwd, + String ksType) throws Exception { + return (new TrustStore(certificates, tsName, tsPwd, ksType)).getFileName(); + } + + private static class TrustStore { + private File trustStoreFile; + + TrustStore(List certificateNames, String tsName, String tsPwd, String ksType) throws Exception { + trustStoreFile = File.createTempFile(tsName, null, new File(".")); + trustStoreFile.deleteOnExit(); + KeyStore ks = KeyStore.getInstance(ksType); + ks.load(null, null); + + for (String certificateName : certificateNames) { + ks.setCertificateEntry(certificateName, getCertificate(certificateName)); + } + + FileOutputStream os = new FileOutputStream(trustStoreFile); + ks.store(os, tsPwd.toCharArray()); + os.flush(); + os.close(); + } + + final String getFileName() throws Exception { + return trustStoreFile.getCanonicalPath(); + } + + private static java.security.cert.Certificate getCertificate(String certname) throws Exception { + FileInputStream is = new FileInputStream(certname); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + return cf.generateCertificate(is); + } + } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java index 6651b4f0c..714179d83 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -5,14 +5,6 @@ package com.microsoft.sqlserver.jdbc.connection; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.security.KeyStore; -import java.security.cert.CertificateFactory; -import java.util.ArrayList; -import java.util.List; - import javax.sql.XAConnection; import org.junit.jupiter.api.Tag; @@ -26,26 +18,22 @@ @RunWith(JUnitPlatform.class) -@Tag(Constants.XA) +@Tag(Constants.reqExternalSetup) public class XADataSourceTest extends AbstractTest { private static String connectionUrlSSL = connectionString + "encrypt=true;trustServerCertificate=false;"; - private static List certificates = new ArrayList<>(); /** * Tests XA connection with PKCS12 truststore that is password protected. * + * Only re-populate the truststore if need arises in the future. + * TestUtils.createTrustStore() can be used to create the truststore. + * * @throws Exception */ @Test public void testPKCS12() throws Exception { SQLServerXADataSource ds = new SQLServerXADataSource(); - // populate certificates arraylist with certificates - // Only re-populate the truststore if need arises in the future. - - // populateCertificates(); - // String trustStore = (new TrustStore(certificates)).getFileName(); - String trustStore = System.getProperty("pkcs12_truststore"); String url = connectionUrlSSL + "trustStore=" + trustStore + ";"; ds.setURL(url); @@ -53,42 +41,4 @@ public void testPKCS12() throws Exception { XAConnection connection = ds.getXAConnection(); connection.close(); } - - private static void populateCertificates() { - // populate the arraylist with all the certificates of servers that are used - certificates.add(".cer"); - certificates.add(".cer"); - } - - private static class TrustStore { - private File trustStoreFile; - - static final String TRUST_STORE_PWD = ""; - - TrustStore(List certificateNames) throws Exception { - trustStoreFile = File.createTempFile("", null, new File(".")); - // trustStoreFile.deleteOnExit(); - KeyStore ks = KeyStore.getInstance("PKCS12"); - ks.load(null, null); - - for (String certificateName : certificateNames) { - ks.setCertificateEntry(certificateName, getCertificate(certificateName)); - } - - FileOutputStream os = new FileOutputStream(trustStoreFile); - ks.store(os, TRUST_STORE_PWD.toCharArray()); - os.flush(); - os.close(); - } - - final String getFileName() throws Exception { - return trustStoreFile.getCanonicalPath(); - } - - private static java.security.cert.Certificate getCertificate(String certname) throws Exception { - FileInputStream is = new FileInputStream(certname); - CertificateFactory cf = CertificateFactory.getInstance("X.509"); - return cf.generateCertificate(is); - } - } } diff --git a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java index 0757fae87..684efea7b 100644 --- a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java +++ b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java @@ -37,7 +37,7 @@ private Constants() {} public static final String xAzureSQLDW = "xAzureSQLDW"; public static final String xAzureSQLMI = "xAzureSQLMI"; public static final String NTLM = "NTLM"; - public static final String XA = "XA"; + public static final String reqExternalSetup = "reqExternalSetup"; public static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current(); public static final Logger LOGGER = Logger.getLogger("AbstractTest"); From 419821005b92539f1839ce0f5087ee035fcff673 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 13:40:42 -0700 Subject: [PATCH 13/15] comments / semicolons --- .../microsoft/sqlserver/jdbc/connection/XADataSourceTest.java | 2 +- .../java/com/microsoft/sqlserver/testframework/Constants.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java index 714179d83..9d5958a0c 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/XADataSourceTest.java @@ -20,7 +20,7 @@ @RunWith(JUnitPlatform.class) @Tag(Constants.reqExternalSetup) public class XADataSourceTest extends AbstractTest { - private static String connectionUrlSSL = connectionString + "encrypt=true;trustServerCertificate=false;"; + private static String connectionUrlSSL = connectionString + ";encrypt=true;trustServerCertificate=false;"; /** * Tests XA connection with PKCS12 truststore that is password protected. diff --git a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java index 684efea7b..add919439 100644 --- a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java +++ b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java @@ -25,7 +25,7 @@ private Constants() {} * xAzureSQLDW - - - - For tests not compatible with Azure Data Warehouse * xAzureSQLMI - - - - For tests not compatible with Azure SQL Managed Instance * NTLM - - - - - - - For NTLM tests - * XA - - - - - - - - For XA tests + * reqExternalSetup - For tests requiring external setup * */ public static final String xJDBC42 = "xJDBC42"; From abacd974b2d35eefa4f01a11387ac3c230de7a23 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 16:03:39 -0700 Subject: [PATCH 14/15] update gradle file --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index e3adfde93..5170fb4e9 100644 --- a/build.gradle +++ b/build.gradle @@ -31,7 +31,7 @@ allprojects { test { useJUnitPlatform { - excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','NTLM') + excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','NTLM', 'reqExternalSetup') } } @@ -70,7 +70,7 @@ if(hasProperty('buildProfile') && buildProfile == "jre8") { targetCompatibility = 1.8 test { useJUnitPlatform { - excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','NTLM','xJDBC42') + excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','NTLM','xJDBC42','reqExternalSetup') } } } From 20cf09f352f359a9e760f43b43d4a121b504f6e5 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Thu, 26 Sep 2019 16:05:06 -0700 Subject: [PATCH 15/15] change spacing --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 5170fb4e9..2deb2a598 100644 --- a/build.gradle +++ b/build.gradle @@ -31,7 +31,7 @@ allprojects { test { useJUnitPlatform { - excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','NTLM', 'reqExternalSetup') + excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','reqExternalSetup','NTLM') } } @@ -70,7 +70,7 @@ if(hasProperty('buildProfile') && buildProfile == "jre8") { targetCompatibility = 1.8 test { useJUnitPlatform { - excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','NTLM','xJDBC42','reqExternalSetup') + excludeTags (hasProperty('excludedGroups') ? excludedGroups : 'xSQLv15','xGradle','NTLM','reqExternalSetup','xJDBC42') } } }