|
10 | 10 | package com.mirth.connect.server.controllers; |
11 | 11 |
|
12 | 12 | import static org.junit.Assert.assertEquals; |
| 13 | +import static org.junit.Assert.assertFalse; |
13 | 14 | import static org.junit.Assert.assertTrue; |
14 | 15 | import static org.junit.Assert.fail; |
15 | 16 | import static org.mockito.ArgumentMatchers.any; |
|
24 | 25 | import java.io.StringReader; |
25 | 26 | import java.io.StringWriter; |
26 | 27 | import java.io.Writer; |
| 28 | +import java.lang.reflect.Field; |
| 29 | +import java.lang.reflect.InvocationTargetException; |
| 30 | +import java.lang.reflect.Method; |
| 31 | +import java.security.KeyStore; |
| 32 | +import java.security.Provider; |
27 | 33 | import java.util.ArrayList; |
28 | 34 | import java.util.Arrays; |
29 | 35 | import java.util.List; |
30 | 36 | import java.util.Properties; |
31 | 37 |
|
| 38 | +import javax.crypto.KeyGenerator; |
| 39 | +import javax.crypto.SecretKey; |
32 | 40 | import javax.xml.XMLConstants; |
33 | 41 | import javax.xml.transform.Source; |
34 | 42 | import javax.xml.transform.TransformerFactory; |
35 | 43 | import javax.xml.transform.stream.StreamResult; |
36 | 44 | import javax.xml.transform.stream.StreamSource; |
37 | 45 |
|
38 | 46 | import org.apache.commons.io.FileUtils; |
| 47 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
39 | 48 | import org.junit.BeforeClass; |
40 | 49 | import org.junit.Test; |
41 | 50 | import org.mockito.invocation.Invocation; |
|
46 | 55 | import com.google.inject.Injector; |
47 | 56 | import com.mirth.connect.client.core.ControllerException; |
48 | 57 | import com.mirth.connect.model.DriverInfo; |
| 58 | +import com.mirth.connect.model.EncryptionSettings; |
49 | 59 |
|
50 | 60 | public class DefaultConfigurationControllerTest { |
51 | 61 |
|
@@ -275,6 +285,114 @@ public void testParseDbdriversXmlWithExternalDtd() { |
275 | 285 | assertTrue(exceptionCaught); |
276 | 286 | } |
277 | 287 |
|
| 288 | + /* |
| 289 | + * The following tests cover the keystore "dirty" tracking added so that initializeSecuritySettings() |
| 290 | + * only rewrites the keystore file when something actually changed. configureEncryption() and |
| 291 | + * generateDefaultCertificate() are private, so they are exercised via reflection; both live in the |
| 292 | + * same package as this test. Each returns true only when it mutated the keystore. |
| 293 | + */ |
| 294 | + |
| 295 | + @Test |
| 296 | + public void configureEncryption_returnsTrue_andStoresKey_whenKeyMissing() throws Exception { |
| 297 | + setEncryptionConfig(new EncryptionSettings(new Properties())); |
| 298 | + |
| 299 | + Provider provider = new BouncyCastleProvider(); |
| 300 | + KeyStore keyStore = newEmptyKeyStore(); |
| 301 | + char[] keyPassword = KEYSTORE_PASSWORD; |
| 302 | + |
| 303 | + assertFalse(keyStore.containsAlias(DefaultConfigurationController.SECRET_KEY_ALIAS)); |
| 304 | + |
| 305 | + boolean dirtied = invokeConfigureEncryption(provider, keyStore, keyPassword); |
| 306 | + |
| 307 | + assertTrue("A newly generated encryption key should mark the keystore dirty", dirtied); |
| 308 | + assertTrue("The encryption key should now be present in the keystore", keyStore.containsAlias(DefaultConfigurationController.SECRET_KEY_ALIAS)); |
| 309 | + } |
| 310 | + |
| 311 | + @Test |
| 312 | + public void configureEncryption_returnsFalse_whenKeyAlreadyPresent() throws Exception { |
| 313 | + setEncryptionConfig(new EncryptionSettings(new Properties())); |
| 314 | + |
| 315 | + Provider provider = new BouncyCastleProvider(); |
| 316 | + KeyStore keyStore = newEmptyKeyStore(); |
| 317 | + char[] keyPassword = KEYSTORE_PASSWORD; |
| 318 | + |
| 319 | + // Seed the keystore with an existing secret key so the "found" branch is taken. |
| 320 | + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); |
| 321 | + keyGenerator.init(128); |
| 322 | + SecretKey existingKey = keyGenerator.generateKey(); |
| 323 | + keyStore.setEntry(DefaultConfigurationController.SECRET_KEY_ALIAS, new KeyStore.SecretKeyEntry(existingKey), new KeyStore.PasswordProtection(keyPassword)); |
| 324 | + |
| 325 | + boolean dirtied = invokeConfigureEncryption(provider, keyStore, keyPassword); |
| 326 | + |
| 327 | + assertFalse("Reusing an existing encryption key should not mark the keystore dirty", dirtied); |
| 328 | + } |
| 329 | + |
| 330 | + @Test |
| 331 | + public void generateDefaultCertificate_returnsTrue_andStoresCert_whenCertMissing() throws Exception { |
| 332 | + Provider provider = new BouncyCastleProvider(); |
| 333 | + KeyStore keyStore = newEmptyKeyStore(); |
| 334 | + char[] keyPassword = KEYSTORE_PASSWORD; |
| 335 | + |
| 336 | + assertFalse(keyStore.containsAlias(CERTIFICATE_ALIAS)); |
| 337 | + |
| 338 | + boolean dirtied = invokeGenerateDefaultCertificate(provider, keyStore, keyPassword); |
| 339 | + |
| 340 | + assertTrue("A newly generated certificate should mark the keystore dirty", dirtied); |
| 341 | + assertTrue("The certificate should now be present in the keystore", keyStore.containsAlias(CERTIFICATE_ALIAS)); |
| 342 | + } |
| 343 | + |
| 344 | + @Test |
| 345 | + public void generateDefaultCertificate_returnsFalse_whenCertAlreadyPresent() throws Exception { |
| 346 | + Provider provider = new BouncyCastleProvider(); |
| 347 | + KeyStore keyStore = newEmptyKeyStore(); |
| 348 | + char[] keyPassword = KEYSTORE_PASSWORD; |
| 349 | + |
| 350 | + // First call generates and stores the certificate. |
| 351 | + assertTrue(invokeGenerateDefaultCertificate(provider, keyStore, keyPassword)); |
| 352 | + |
| 353 | + // Second call should find the existing certificate and leave the keystore untouched. |
| 354 | + boolean dirtied = invokeGenerateDefaultCertificate(provider, keyStore, keyPassword); |
| 355 | + |
| 356 | + assertFalse("Reusing an existing certificate should not mark the keystore dirty", dirtied); |
| 357 | + } |
| 358 | + |
| 359 | + private static final char[] KEYSTORE_PASSWORD = "testpass".toCharArray(); |
| 360 | + private static final String CERTIFICATE_ALIAS = "mirthconnect"; |
| 361 | + |
| 362 | + private KeyStore newEmptyKeyStore() throws Exception { |
| 363 | + KeyStore keyStore = KeyStore.getInstance("JCEKS"); |
| 364 | + keyStore.load(null, KEYSTORE_PASSWORD); |
| 365 | + return keyStore; |
| 366 | + } |
| 367 | + |
| 368 | + private void setEncryptionConfig(EncryptionSettings encryptionConfig) throws Exception { |
| 369 | + Field field = DefaultConfigurationController.class.getDeclaredField("encryptionConfig"); |
| 370 | + field.setAccessible(true); |
| 371 | + field.set(null, encryptionConfig); |
| 372 | + } |
| 373 | + |
| 374 | + private boolean invokeConfigureEncryption(Provider provider, KeyStore keyStore, char[] keyPassword) throws Exception { |
| 375 | + return invokePrivateDirtyMethod("configureEncryption", provider, keyStore, keyPassword); |
| 376 | + } |
| 377 | + |
| 378 | + private boolean invokeGenerateDefaultCertificate(Provider provider, KeyStore keyStore, char[] keyPassword) throws Exception { |
| 379 | + return invokePrivateDirtyMethod("generateDefaultCertificate", provider, keyStore, keyPassword); |
| 380 | + } |
| 381 | + |
| 382 | + private boolean invokePrivateDirtyMethod(String methodName, Provider provider, KeyStore keyStore, char[] keyPassword) throws Exception { |
| 383 | + Method method = DefaultConfigurationController.class.getDeclaredMethod(methodName, Provider.class, KeyStore.class, char[].class); |
| 384 | + method.setAccessible(true); |
| 385 | + try { |
| 386 | + return (boolean) method.invoke(new DefaultConfigurationController(), provider, keyStore, keyPassword); |
| 387 | + } catch (InvocationTargetException e) { |
| 388 | + Throwable cause = e.getCause(); |
| 389 | + if (cause instanceof Exception) { |
| 390 | + throw (Exception) cause; |
| 391 | + } |
| 392 | + throw e; |
| 393 | + } |
| 394 | + } |
| 395 | + |
278 | 396 | private void assertDefaultDrivers(List<DriverInfo> drivers, boolean includeODBC) { |
279 | 397 | assertEquals(includeODBC ? 7 : 6, drivers.size()); |
280 | 398 | int i = 0; |
|
0 commit comments