Skip to content

Commit e7d9d0f

Browse files
committed
RANGER-5390:Enable and Improve Test Cases for KMS Module
1 parent 27f9656 commit e7d9d0f

27 files changed

+963
-320
lines changed

kms/src/test/java/org/apache/hadoop/crypto/key/RangerKMSDBTest.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
package org.apache.hadoop.crypto.key;
1919

2020
import org.apache.hadoop.conf.Configuration;
21+
import org.apache.ranger.kms.dao.DaoManager;
2122
import org.junit.jupiter.api.AfterEach;
2223
import org.junit.jupiter.api.BeforeEach;
23-
import org.junit.jupiter.api.Disabled;
2424
import org.junit.jupiter.api.Test;
2525
import org.junit.jupiter.api.extension.ExtendWith;
2626
import org.mockito.junit.jupiter.MockitoExtension;
@@ -40,7 +40,6 @@
4040
import static org.junit.jupiter.api.Assertions.assertTrue;
4141

4242
@ExtendWith(MockitoExtension.class)
43-
@Disabled
4443
public class RangerKMSDBTest {
4544
private static final String PROPERTY_PREFIX = "ranger.ks.";
4645
private static final String DB_DIALECT = "jpa.jdbc.dialect";
@@ -72,21 +71,17 @@ public class RangerKMSDBTest {
7271
public void setUp() throws Exception {
7372
conf = new Configuration();
7473

75-
// Set basic database properties required for RangerKMSDB constructor
7674
conf.set(PROPERTY_PREFIX + DB_DIALECT, "org.eclipse.persistence.platform.database.H2Platform");
7775
conf.set(PROPERTY_PREFIX + DB_DRIVER, "org.h2.Driver");
7876
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:h2:mem:testdb");
7977
conf.set(PROPERTY_PREFIX + DB_USER, "test");
8078
conf.set(PROPERTY_PREFIX + DB_PASSWORD, "test");
8179

82-
// Save original system properties
8380
originalSystemProperties = new Properties();
8481
originalSystemProperties.putAll(System.getProperties());
8582

86-
// Create temporary files for testing
8783
createTempFiles();
8884

89-
// Get private method and field using reflection
9085
updateDBSSLURLMethod = RangerKMSDB.class.getDeclaredMethod("updateDBSSLURL");
9186
updateDBSSLURLMethod.setAccessible(true);
9287

@@ -96,10 +91,8 @@ public void setUp() throws Exception {
9691

9792
@AfterEach
9893
public void tearDown() {
99-
// Restore original system properties
10094
System.setProperties(originalSystemProperties);
10195

102-
// Clean up temporary files
10396
cleanupTempFiles();
10497

10598
if (rangerKMSDB != null) {
@@ -112,30 +105,26 @@ public void testUpdateDBSSLURL_NullConfiguration() throws Exception {
112105
Configuration nullConf = null;
113106
rangerKMSDB = new RangerKMSDB(nullConf) {
114107
@Override
115-
public org.apache.ranger.kms.dao.DaoManager getDaoManager() {
108+
public DaoManager getDaoManager() {
116109
return null;
117110
}
118111
};
119112

120-
// Should not throw exception
121113
assertDoesNotThrow(() -> updateDBSSLURLMethod.invoke(rangerKMSDB));
122114
}
123115

124116
@Test
125117
public void testUpdateDBSSLURL_NoSSLEnabledProperty() throws Exception {
126-
// Don't set DB_SSL_ENABLED property
127118
createRangerKMSDBWithoutSSL();
128119

129120
String originalUrl = conf.get(PROPERTY_PREFIX + DB_URL);
130121
updateDBSSLURLMethod.invoke(rangerKMSDB);
131122

132-
// URL should remain unchanged
133123
assertEquals(originalUrl, conf.get(PROPERTY_PREFIX + DB_URL));
134124
}
135125

136126
@Test
137127
public void testUpdateDBSSLURL_MySQLSSLEnabled_NoQueryParams() throws Exception {
138-
// Setup MySQL configuration
139128
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
140129
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger");
141130
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -157,7 +146,6 @@ public void testUpdateDBSSLURL_MySQLSSLEnabled_NoQueryParams() throws Exception
157146

158147
@Test
159148
public void testUpdateDBSSLURL_MySQLSSLEnabled_WithQueryParams() throws Exception {
160-
// Setup MySQL configuration with existing query parameters
161149
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
162150
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger?charset=utf8");
163151
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -166,13 +154,11 @@ public void testUpdateDBSSLURL_MySQLSSLEnabled_WithQueryParams() throws Exceptio
166154
updateDBSSLURLMethod.invoke(rangerKMSDB);
167155

168156
String updatedUrl = conf.get(PROPERTY_PREFIX + DB_URL);
169-
// Should not modify URL if it already has query parameters
170157
assertEquals("jdbc:mysql://localhost:3306/ranger?charset=utf8", updatedUrl);
171158
}
172159

173160
@Test
174161
public void testUpdateDBSSLURL_MySQLSSLDisabled() throws Exception {
175-
// Setup MySQL configuration with SSL disabled
176162
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
177163
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger");
178164
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "false");
@@ -186,7 +172,6 @@ public void testUpdateDBSSLURL_MySQLSSLDisabled() throws Exception {
186172

187173
@Test
188174
public void testUpdateDBSSLURL_PostgreSQLSSLEnabled_WithCertificateFile() throws Exception {
189-
// Setup PostgreSQL configuration
190175
conf.set(PROPERTY_PREFIX + DB_DIALECT, "postgresql");
191176
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:postgresql://localhost:5432/ranger");
192177
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -203,7 +188,6 @@ public void testUpdateDBSSLURL_PostgreSQLSSLEnabled_WithCertificateFile() throws
203188

204189
@Test
205190
public void testUpdateDBSSLURL_PostgreSQLSSLEnabled_WithVerification_NoCertFile() throws Exception {
206-
// Setup PostgreSQL configuration
207191
conf.set(PROPERTY_PREFIX + DB_DIALECT, "postgresql");
208192
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:postgresql://localhost:5432/ranger");
209193
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -220,7 +204,6 @@ public void testUpdateDBSSLURL_PostgreSQLSSLEnabled_WithVerification_NoCertFile(
220204

221205
@Test
222206
public void testUpdateDBSSLURL_PostgreSQLSSLEnabled_NoVerification() throws Exception {
223-
// Setup PostgreSQL configuration
224207
conf.set(PROPERTY_PREFIX + DB_DIALECT, "postgresql");
225208
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:postgresql://localhost:5432/ranger");
226209
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -237,7 +220,6 @@ public void testUpdateDBSSLURL_PostgreSQLSSLEnabled_NoVerification() throws Exce
237220

238221
@Test
239222
public void testUpdateDBSSLURL_PostgreSQLSSLDisabled() throws Exception {
240-
// Setup PostgreSQL configuration with SSL disabled
241223
conf.set(PROPERTY_PREFIX + DB_DIALECT, "postgresql");
242224
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:postgresql://localhost:5432/ranger");
243225
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "false");
@@ -247,13 +229,11 @@ public void testUpdateDBSSLURL_PostgreSQLSSLDisabled() throws Exception {
247229
updateDBSSLURLMethod.invoke(rangerKMSDB);
248230

249231
String updatedUrl = conf.get(PROPERTY_PREFIX + DB_URL);
250-
// PostgreSQL URL should not be modified when SSL is disabled
251232
assertEquals(originalUrl, updatedUrl);
252233
}
253234

254235
@Test
255236
public void testUpdateDBSSLURL_OracleDatabase() throws Exception {
256-
// Setup Oracle configuration (should not modify URL)
257237
conf.set(PROPERTY_PREFIX + DB_DIALECT, "oracle");
258238
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:oracle:thin:@localhost:1521:ranger");
259239
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -263,13 +243,11 @@ public void testUpdateDBSSLURL_OracleDatabase() throws Exception {
263243
updateDBSSLURLMethod.invoke(rangerKMSDB);
264244

265245
String updatedUrl = conf.get(PROPERTY_PREFIX + DB_URL);
266-
// Oracle URL should not be modified
267246
assertEquals(originalUrl, updatedUrl);
268247
}
269248

270249
@Test
271250
public void testUpdateDBSSLURL_KeystoreAndTruststoreSetup() throws Exception {
272-
// Setup MySQL configuration with SSL verification and keystore/truststore
273251
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
274252
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger");
275253
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -284,7 +262,6 @@ public void testUpdateDBSSLURL_KeystoreAndTruststoreSetup() throws Exception {
284262
createRangerKMSDBWithoutSSL();
285263
updateDBSSLURLMethod.invoke(rangerKMSDB);
286264

287-
// Verify system properties are set
288265
assertEquals(tempKeystore.getAbsolutePath(), System.getProperty("javax.net.ssl.keyStore"));
289266
assertEquals("keystore-password", System.getProperty("javax.net.ssl.keyStorePassword"));
290267
assertEquals(tempTruststore.getAbsolutePath(), System.getProperty("javax.net.ssl.trustStore"));
@@ -293,7 +270,6 @@ public void testUpdateDBSSLURL_KeystoreAndTruststoreSetup() throws Exception {
293270

294271
@Test
295272
public void testUpdateDBSSLURL_OneWaySSL() throws Exception {
296-
// Setup MySQL configuration with 1-way SSL (should not set keystore)
297273
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
298274
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger");
299275
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -307,15 +283,12 @@ public void testUpdateDBSSLURL_OneWaySSL() throws Exception {
307283
createRangerKMSDBWithoutSSL();
308284
updateDBSSLURLMethod.invoke(rangerKMSDB);
309285

310-
// Verify keystore is not set for 1-way SSL
311286
assertNull(System.getProperty("javax.net.ssl.keyStore"));
312-
// But truststore should still be set
313287
assertEquals(tempTruststore.getAbsolutePath(), System.getProperty("javax.net.ssl.trustStore"));
314288
}
315289

316290
@Test
317291
public void testUpdateDBSSLURL_NonExistentKeystoreFile() throws Exception {
318-
// Setup configuration with non-existent keystore file
319292
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
320293
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger");
321294
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -327,13 +300,11 @@ public void testUpdateDBSSLURL_NonExistentKeystoreFile() throws Exception {
327300
createRangerKMSDBWithoutSSL();
328301
updateDBSSLURLMethod.invoke(rangerKMSDB);
329302

330-
// Should not set system property for non-existent file
331303
assertNull(System.getProperty("javax.net.ssl.keyStore"));
332304
}
333305

334306
@Test
335307
public void testUpdateDBSSLURL_EmptyKeystoreProperty() throws Exception {
336-
// Setup configuration with empty keystore property
337308
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
338309
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger");
339310
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -344,13 +315,11 @@ public void testUpdateDBSSLURL_EmptyKeystoreProperty() throws Exception {
344315
createRangerKMSDBWithoutSSL();
345316
updateDBSSLURLMethod.invoke(rangerKMSDB);
346317

347-
// Should not set system property for empty keystore
348318
assertNull(System.getProperty("javax.net.ssl.keyStore"));
349319
}
350320

351321
@Test
352322
public void testUpdateDBSSLURL_VariousBooleanValues() throws Exception {
353-
// Test various boolean value formats
354323
String[] trueValues = {"true", "TRUE", "True"};
355324
String[] falseValues = {"false", "FALSE", "False", "", null, "invalid"};
356325

@@ -392,7 +361,6 @@ public void testUpdateDBSSLURL_VariousBooleanValues() throws Exception {
392361

393362
@Test
394363
public void testUpdateDBSSLURL_SQLServerDatabase() throws Exception {
395-
// Test SQL Server (should not modify URL for SSL)
396364
conf.set(PROPERTY_PREFIX + DB_DIALECT, "sqlserver");
397365
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:sqlserver://localhost:1433;database=ranger");
398366
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -402,13 +370,11 @@ public void testUpdateDBSSLURL_SQLServerDatabase() throws Exception {
402370
updateDBSSLURLMethod.invoke(rangerKMSDB);
403371

404372
String updatedUrl = conf.get(PROPERTY_PREFIX + DB_URL);
405-
// SQL Server URL should not be modified
406373
assertEquals(originalUrl, updatedUrl);
407374
}
408375

409376
@Test
410377
public void testUpdateDBSSLURL_PostgreSQLSSLRequired() throws Exception {
411-
// Setup PostgreSQL configuration with SSL required
412378
conf.set(PROPERTY_PREFIX + DB_DIALECT, "postgresql");
413379
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:postgresql://localhost:5432/ranger");
414380
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -425,7 +391,6 @@ public void testUpdateDBSSLURL_PostgreSQLSSLRequired() throws Exception {
425391

426392
@Test
427393
public void testUpdateDBSSLURL_ComplexScenario() throws Exception {
428-
// Test complex scenario with multiple properties set
429394
conf.set(PROPERTY_PREFIX + DB_DIALECT, "mysql");
430395
conf.set(PROPERTY_PREFIX + DB_URL, "jdbc:mysql://localhost:3306/ranger");
431396
conf.set(PROPERTY_PREFIX + DB_SSL_ENABLED, "true");
@@ -445,7 +410,6 @@ public void testUpdateDBSSLURL_ComplexScenario() throws Exception {
445410
assertTrue(updatedUrl.contains("requireSSL=false"));
446411
assertTrue(updatedUrl.contains("verifyServerCertificate=false"));
447412

448-
// System properties should not be set when verification is false
449413
assertNull(System.getProperty("javax.net.ssl.keyStore"));
450414
assertNull(System.getProperty("javax.net.ssl.trustStore"));
451415
}
@@ -455,7 +419,6 @@ private void createTempFiles() throws IOException {
455419
tempTruststore = File.createTempFile("test-truststore", ".jks");
456420
tempCertificate = File.createTempFile("test-cert", ".pem");
457421

458-
// Write some dummy content to make files readable
459422
Files.write(tempKeystore.toPath(), "dummy content".getBytes());
460423
Files.write(tempTruststore.toPath(), "dummy content".getBytes());
461424
Files.write(tempCertificate.toPath(), "dummy content".getBytes());
@@ -476,7 +439,6 @@ private void cleanupTempFiles() {
476439
private void createRangerKMSDBWithoutSSL() {
477440
try {
478441
rangerKMSDB = new RangerKMSDB(conf) {
479-
// Override to prevent actual DB connection
480442
@Override
481443
public org.apache.ranger.kms.dao.DaoManager getDaoManager() {
482444
return null;

kms/src/test/java/org/apache/hadoop/crypto/key/TestDBToKeySecure.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.hadoop.conf.Configuration;
2020
import org.junit.jupiter.api.AfterAll;
2121
import org.junit.jupiter.api.BeforeAll;
22-
import org.junit.jupiter.api.Disabled;
2322
import org.junit.jupiter.api.MethodOrderer;
2423
import org.junit.jupiter.api.Test;
2524
import org.junit.jupiter.api.TestMethodOrder;
@@ -36,7 +35,6 @@
3635

3736
@ExtendWith(MockitoExtension.class)
3837
@TestMethodOrder(MethodOrderer.MethodName.class)
39-
@Disabled
4038
public class TestDBToKeySecure {
4139
private static SecurityManager originalSecurityManager;
4240

kms/src/test/java/org/apache/hadoop/crypto/key/TestHSM2DBMKUtil.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.junit.jupiter.api.AfterEach;
2020
import org.junit.jupiter.api.BeforeEach;
21-
import org.junit.jupiter.api.Disabled;
2221
import org.junit.jupiter.api.MethodOrderer;
2322
import org.junit.jupiter.api.Test;
2423
import org.junit.jupiter.api.TestMethodOrder;
@@ -35,7 +34,6 @@
3534

3635
@ExtendWith(MockitoExtension.class)
3736
@TestMethodOrder(MethodOrderer.MethodName.class)
38-
@Disabled
3937
public class TestHSM2DBMKUtil {
4038
private final PrintStream originalOut = System.out;
4139
private final PrintStream originalErr = System.err;

0 commit comments

Comments
 (0)