/
AbstractTest.java
631 lines (550 loc) · 24.2 KB
/
AbstractTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) 2016 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.testframework;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import com.microsoft.sqlserver.jdbc.ISQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionAzureKeyVaultProvider;
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider;
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionKeyStoreProvider;
import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
/**
* Think about following things:
* <li>Connection pool
* <li>Configured Property file instead of passing from args.
* <li>Think of different property files for different settings. / flag etc.
* <Li>Think about what kind of logging we are going use it. <B>util.logging</B> will be preference.
*
* @since 6.1.2
*/
public abstract class AbstractTest {
protected static String applicationClientID = null;
protected static String applicationKey = null;
protected static String tenantID;
protected static String[] keyIDs = null;
protected static String[] enclaveServer = null;
protected static String[] enclaveAttestationUrl = null;
protected static String[] enclaveAttestationProtocol = null;
protected static String clientCertificate = null;
protected static String clientKey = null;
protected static String clientKeyPassword = "";
protected static String trustStorePath = "";
protected static String trustServerCertificate = "";
protected static String windowsKeyPath = null;
protected static String javaKeyPath = null;
protected static String javaKeyAliases = null;
protected static SQLServerColumnEncryptionKeyStoreProvider jksProvider = null;
protected static SQLServerColumnEncryptionAzureKeyVaultProvider akvProvider = null;
static boolean isKspRegistered = false;
// properties needed for MSI
protected static String msiClientId = null;
protected static String keyStorePrincipalId = null;
protected static String keyStoreSecret = null;
protected static SQLServerConnection connection = null;
protected static ISQLServerDataSource ds = null;
protected static ISQLServerDataSource dsXA = null;
protected static ISQLServerDataSource dsPool = null;
protected static Connection connectionAzure = null;
protected static String connectionString = null;
protected static String connectionStringNTLM;
private static boolean determinedSqlAzureOrSqlServer = false;
private static boolean determinedSqlOS = false;
private static boolean isSqlAzure = false;
private static boolean isSqlAzureDW = false;
private static boolean isSqlLinux = false;
/**
* Byte Array containing streamed logging output. Content can be retrieved using toByteArray() or toString()
*/
private static ByteArrayOutputStream logOutputStream = null;
private static PrintStream logPrintStream = null;
private static Properties configProperties = null;
protected static boolean isWindows = System.getProperty("os.name").startsWith("Windows");
public static Properties properties = null;
/**
* This will take care of all initialization before running the Test Suite.
*
* @throws Exception
* when an error occurs
*/
@BeforeAll
public static void setup() throws Exception {
// Invoke fine logging...
invokeLogging();
// get Properties from config file
try (InputStream input = new FileInputStream(Constants.CONFIG_PROPERTIES_FILE)) {
configProperties = new Properties();
configProperties.load(input);
} catch (FileNotFoundException | SecurityException e) {
// no config file used
}
connectionString = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_TEST_CONNECTION_PROPERTIES);
applicationClientID = getConfiguredProperty("applicationClientID");
applicationKey = getConfiguredProperty("applicationKey");
tenantID = getConfiguredProperty("tenantID");
trustServerCertificate = getConfiguredProperty("trustServerCertificate", "true");
connectionString = TestUtils.addOrOverrideProperty(connectionString, "trustServerCertificate",
trustServerCertificate);
javaKeyPath = TestUtils.getCurrentClassPath() + Constants.JKS_NAME;
keyIDs = getConfiguredProperty("keyID", "").split(Constants.SEMI_COLON);
windowsKeyPath = getConfiguredProperty("windowsKeyPath");
String prop;
prop = getConfiguredProperty("enclaveServer", null);
if (null == prop) {
// default to server in connection string
String serverName = (connectionString.substring(Constants.JDBC_PREFIX.length())
.split(Constants.SEMI_COLON)[0]).split(":")[0];
enclaveServer = new String[1];
enclaveServer[0] = new String(serverName);
} else {
enclaveServer = prop.split(Constants.SEMI_COLON);
}
prop = getConfiguredProperty("enclaveAttestationUrl", null);
enclaveAttestationUrl = null != prop ? prop.split(Constants.SEMI_COLON) : null;
prop = getConfiguredProperty("enclaveAttestationProtocol", null);
enclaveAttestationProtocol = null != prop ? prop.split(Constants.SEMI_COLON) : null;
clientCertificate = getConfiguredProperty("clientCertificate", null);
clientKey = getConfiguredProperty("clientKey", null);
clientKeyPassword = getConfiguredProperty("clientKeyPassword", "");
trustStorePath = getConfiguredProperty("trustStore", "");
Map<String, SQLServerColumnEncryptionKeyStoreProvider> map = new HashMap<String, SQLServerColumnEncryptionKeyStoreProvider>();
if (null == jksProvider) {
jksProvider = new SQLServerColumnEncryptionJavaKeyStoreProvider(javaKeyPath,
Constants.JKS_SECRET.toCharArray());
map.put(Constants.CUSTOM_KEYSTORE_NAME, jksProvider);
}
if (null == akvProvider && null != applicationClientID && null != applicationKey) {
File file = null;
try {
file = new File(Constants.MSSQL_JDBC_PROPERTIES);
try (OutputStream os = new FileOutputStream(file);) {
Properties props = new Properties();
// Append to the list of hardcoded endpoints.
props.setProperty(Constants.AKV_TRUSTED_ENDPOINTS_KEYWORD, ";vault.azure.net");
props.store(os, "");
}
akvProvider = new SQLServerColumnEncryptionAzureKeyVaultProvider(applicationClientID, applicationKey);
map.put(Constants.AZURE_KEY_VAULT_NAME, akvProvider);
} finally {
if (null != file) {
file.delete();
}
}
}
if (!isKspRegistered) {
SQLServerConnection.registerColumnEncryptionKeyStoreProviders(map);
isKspRegistered = true;
}
// MSI properties
msiClientId = getConfiguredProperty("msiClientId");
keyStorePrincipalId = getConfiguredProperty("keyStorePrincipalId");
keyStoreSecret = getConfiguredProperty("keyStoreSecret");
}
protected static void setupConnectionString() {
connectionStringNTLM = connectionString;
// if these properties are defined then NTLM is desired, modify connection string accordingly
String domain = getConfiguredProperty("domainNTLM");
String user = getConfiguredProperty("userNTLM");
String password = getConfiguredProperty("passwordNTLM");
if (null != domain) {
connectionStringNTLM = TestUtils.addOrOverrideProperty(connectionStringNTLM, "domain", domain);
}
if (null != user) {
connectionStringNTLM = TestUtils.addOrOverrideProperty(connectionStringNTLM, "user", user);
}
if (null != password) {
connectionStringNTLM = TestUtils.addOrOverrideProperty(connectionStringNTLM, "password", password);
}
if (null != user && null != password) {
connectionStringNTLM = TestUtils.addOrOverrideProperty(connectionStringNTLM, "authenticationScheme",
"NTLM");
connectionStringNTLM = TestUtils.addOrOverrideProperty(connectionStringNTLM, "integratedSecurity", "true");
}
ds = updateDataSource(connectionString, new SQLServerDataSource());
dsXA = updateDataSource(connectionString, new SQLServerXADataSource());
dsPool = updateDataSource(connectionString, new SQLServerConnectionPoolDataSource());
}
protected static void setConnection() throws Exception {
setupConnectionString();
Assertions.assertNotNull(connectionString, TestResource.getResource("R_ConnectionStringNull"));
Class.forName(Constants.MSSQL_JDBC_PACKAGE + ".SQLServerDriver");
if (!SQLServerDriver.isRegistered()) {
SQLServerDriver.register();
}
if (null == connection || connection.isClosed()) {
connection = getConnection();
}
isSqlAzureOrAzureDW(connection);
checkSqlOS(connection);
}
/**
* Covers only connection properties required for testing. Does not cover all connection properties - add more
* properties if needed.
*
* @param ds
* DataSource to be configured
* @return ISQLServerDataSource
*/
protected static ISQLServerDataSource updateDataSource(String connectionString, ISQLServerDataSource ds) {
if (null != connectionString && connectionString.startsWith(Constants.JDBC_PREFIX)) {
String extract = connectionString.substring(Constants.JDBC_PREFIX.length());
String[] identifiers = extract.split(Constants.SEMI_COLON);
String server = identifiers[0];
// Check if serverName contains instance name
if (server.contains(Constants.BACK_SLASH)) {
int i = identifiers[0].indexOf(Constants.BACK_SLASH);
ds.setServerName(extractPort(server.substring(0, i), ds));
ds.setInstanceName(server.substring(i + 1));
} else {
ds.setServerName(extractPort(server, ds));
}
for (String prop : identifiers) {
if (prop.contains(Constants.EQUAL_TO)) {
int index = prop.indexOf(Constants.EQUAL_TO);
String name = prop.substring(0, index);
String value = prop.substring(index + 1);
switch (name.toUpperCase()) {
case Constants.INTEGRATED_SECURITY:
ds.setIntegratedSecurity(Boolean.parseBoolean(value));
break;
case Constants.USER:
case Constants.USER_NAME:
ds.setUser(value);
break;
case Constants.PORT:
case Constants.PORT_NUMBER:
ds.setPortNumber(Integer.parseInt(value));
break;
case Constants.PASSWORD:
ds.setPassword(value);
break;
case Constants.DOMAIN:
case Constants.DOMAIN_NAME:
ds.setDomain(value);
break;
case Constants.DATABASE:
case Constants.DATABASE_NAME:
ds.setDatabaseName(value);
break;
case Constants.COLUMN_ENCRYPTION_SETTING:
ds.setColumnEncryptionSetting(value);
break;
case Constants.DISABLE_STATEMENT_POOLING:
ds.setDisableStatementPooling(Boolean.parseBoolean(value));
break;
case Constants.STATEMENT_POOLING_CACHE_SIZE:
ds.setStatementPoolingCacheSize(Integer.parseInt(value));
break;
case Constants.AUTHENTICATION:
ds.setAuthentication(value);
break;
case Constants.AUTHENTICATION_SCHEME:
ds.setAuthenticationScheme(value);
break;
case Constants.CANCEL_QUERY_TIMEOUT:
ds.setCancelQueryTimeout(Integer.parseInt(value));
break;
case Constants.ENCRYPT:
ds.setEncrypt(Boolean.parseBoolean(value));
break;
case Constants.TRUST_SERVER_CERTIFICATE:
ds.setTrustServerCertificate(Boolean.parseBoolean(value));
break;
case Constants.HOST_NAME_IN_CERTIFICATE:
ds.setHostNameInCertificate(value);
break;
case Constants.ENCLAVE_ATTESTATIONURL:
ds.setEnclaveAttestationUrl(value);
break;
case Constants.ENCLAVE_ATTESTATIONPROTOCOL:
ds.setEnclaveAttestationProtocol(value);
break;
case Constants.KEYVAULTPROVIDER_CLIENTID:
ds.setKeyVaultProviderClientId(value);
break;
case Constants.KEYVAULTPROVIDER_CLIENTKEY:
ds.setKeyVaultProviderClientKey(value);
break;
case Constants.KEYSTORE_AUTHENTICATION:
ds.setKeyStoreAuthentication(value);
break;
case Constants.KEYSTORE_PRINCIPALID:
ds.setKeyStorePrincipalId(value);
break;
case Constants.KEYSTORE_SECRET:
ds.setKeyStoreSecret(value);
break;
case Constants.MSICLIENTID:
ds.setMSIClientId(value);
break;
case Constants.CLIENT_CERTIFICATE:
ds.setClientCertificate(value);
break;
case Constants.CLIENT_KEY:
ds.setClientKey(value);
break;
case Constants.CLIENT_KEY_PASSWORD:
ds.setClientKeyPassword(value);
break;
case Constants.AAD_SECURE_PRINCIPAL_ID:
ds.setAADSecurePrincipalId(value);
break;
case Constants.AAD_SECURE_PRINCIPAL_SECRET:
ds.setAADSecurePrincipalSecret(value);
break;
case Constants.SEND_TEMPORAL_DATATYPES_AS_STRING_FOR_BULK_COPY:
ds.setSendTemporalDataTypesAsStringForBulkCopy(Boolean.parseBoolean(value));
break;
case Constants.MAX_RESULT_BUFFER:
ds.setMaxResultBuffer(value);
break;
case Constants.CONNECT_RETRY_COUNT:
ds.setConnectRetryCount(Integer.parseInt(value));
break;
case Constants.CONNECT_RETRY_INTERVAL:
ds.setConnectRetryInterval(Integer.parseInt(value));
break;
default:
break;
}
}
}
}
return ds;
}
static String extractPort(String server, ISQLServerDataSource ds) {
if (server.contains(Constants.COLON)) {
ds.setPortNumber(Integer.parseInt(server.substring(server.indexOf(Constants.COLON) + 1)));
server = server.substring(0, server.indexOf(Constants.COLON));
}
return server;
}
/**
* Get the connection String
*
* @return connectionString
*/
public static String getConnectionString() {
return connectionString;
}
/**
* Retrieves connection using default configured connection string
*
* @return
* @throws SQLException
*/
protected static SQLServerConnection getConnection() throws SQLException {
return PrepUtil.getConnection(connectionString);
}
/**
* This will take care of all clean ups after running the Test Suite.
*
* @throws Exception
* when an error occurs
*/
@AfterAll
public static void teardown() throws Exception {
try {
if (null != connection && !connection.isClosed()) {
connection.close();
}
if (null != logOutputStream) {
logOutputStream.close();
}
if (null != logPrintStream) {
logPrintStream.close();
}
} finally {
connection = null;
logOutputStream = null;
logPrintStream = null;
}
}
/**
* Invoke logging.
*/
public static void invokeLogging() {
Handler handler = null;
// enable logging to stream by default for tests
String enableLogging = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_LOGGING, Boolean.TRUE.toString());
// If logging is not enable then return.
if (!Boolean.TRUE.toString().equalsIgnoreCase(enableLogging)) {
return;
}
String loggingHandler = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_LOGGING_HANDLER,
Constants.LOGGING_HANDLER_STREAM);
try {
if (Constants.LOGGING_HANDLER_CONSOLE.equalsIgnoreCase(loggingHandler)) {
handler = new ConsoleHandler();
handler.setFormatter(new SimpleFormatter());
} else if (Constants.LOGGING_HANDLER_FILE.equalsIgnoreCase(loggingHandler)) {
handler = new FileHandler(Constants.DEFAULT_DRIVER_LOG);
handler.setFormatter(new SimpleFormatter());
System.out.println("Look for Driver.log file in your classpath for detail logs");
} else if (Constants.LOGGING_HANDLER_STREAM.equalsIgnoreCase(loggingHandler)) {
logOutputStream = new ByteArrayOutputStream();
logPrintStream = new PrintStream(logOutputStream);
handler = new StreamHandler(logPrintStream, new SimpleFormatter());
}
if (handler != null) {
handler.setLevel(Level.FINEST);
Logger.getLogger(Constants.MSSQL_JDBC_LOGGING_HANDLER).addHandler(handler);
}
/*
* By default, Loggers also send their output to their parent logger. Typically the root Logger is
* configured with a set of Handlers that essentially act as default handlers for all loggers.
*/
Logger logger = Logger.getLogger(Constants.MSSQL_JDBC_PACKAGE);
logger.setLevel(Level.FINEST);
} catch (Exception e) {
System.err.println("Could not invoke logging: " + e.getMessage());
}
}
/**
* Returns if target Server is SQL Azure Database
*
* @return true/false
*/
public static boolean isSqlAzure() {
return isSqlAzure;
}
/**
* Returns if target Server is SQL Azure DW
*
* @return true/false
*/
public static boolean isSqlAzureDW() {
return isSqlAzureDW;
}
/**
* Returns if target Server is SQL Linux
*
* @return true/false
*/
public static boolean isSqlLinux() {
return isSqlLinux;
}
/**
* Determines the server's type.
*
* @param con
* connection to server
* @return void
* @throws SQLException
*/
private static void isSqlAzureOrAzureDW(Connection con) throws SQLException {
if (determinedSqlAzureOrSqlServer) {
return;
}
try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT CAST(SERVERPROPERTY('EngineEdition') as INT)")) {
rs.next();
int engineEdition = rs.getInt(1);
isSqlAzure = (engineEdition == Constants.ENGINE_EDITION_FOR_SQL_AZURE
|| engineEdition == Constants.ENGINE_EDITION_FOR_SQL_AZURE_DW);
isSqlAzureDW = (engineEdition == Constants.ENGINE_EDITION_FOR_SQL_AZURE_DW);
determinedSqlAzureOrSqlServer = true;
}
}
/**
* Determines the server's OSF
*
* @param con
* @throws SQLException
*/
private static void checkSqlOS(Connection con) throws SQLException {
if (determinedSqlOS) {
return;
}
try (Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT @@VERSION")) {
rs.next();
isSqlLinux = rs.getString(1).contains("Linux");
determinedSqlOS = true;
}
}
/**
* Read property from system or config properties file
*
* @param key
* @return property value
*/
protected static String getConfiguredProperty(String key) {
String value = System.getProperty(key);
if (null == value && null != configProperties) {
return configProperties.getProperty(key);
}
return value;
}
/**
* Read property from system or config properties file or read from env var
*
* @param key
* @return property value
*/
private static String getConfiguredPropertyOrEnv(String key) {
String value = getConfiguredProperty(key);
if (null == value) {
return System.getenv(key);
}
return value;
}
/**
* Read property from system or config properties file if not set return default value
*
* @param key
* @return property value or default value
*/
protected static String getConfiguredProperty(String key, String defaultValue) {
String value = getConfiguredProperty(key);
if (null == value) {
return defaultValue;
}
return value;
}
/**
* Read property from system or config properties file or env var if not set return default value
*
* @param key
* @return property value or default value
*/
private static String getConfiguredPropertyOrEnv(String key, String defaultValue) {
String value = getConfiguredProperty(key);
if (null == value) {
value = System.getenv(key);
}
if (null == value) {
value = defaultValue;
}
return value;
}
}