NIFI-2466: Added unit test to verify changes; fixed validation

This commit is contained in:
Mark Payne 2016-08-03 13:58:28 -04:00
parent aa4d4189c4
commit c5d521a1df
3 changed files with 48 additions and 1 deletions

View File

@ -233,6 +233,9 @@ public class StandardSSLContextService extends AbstractControllerService impleme
private void verifySslConfig(final ValidationContext validationContext) throws ProcessException { private void verifySslConfig(final ValidationContext validationContext) throws ProcessException {
final String protocol = validationContext.getProperty(SSL_ALGORITHM).getValue(); final String protocol = validationContext.getProperty(SSL_ALGORITHM).getValue();
try { try {
final PropertyValue keyPasswdProp = configContext.getProperty(KEY_PASSWORD);
final char[] keyPassword = keyPasswdProp.isSet() ? keyPasswdProp.getValue().toCharArray() : null;
final String keystoreFile = validationContext.getProperty(KEYSTORE).getValue(); final String keystoreFile = validationContext.getProperty(KEYSTORE).getValue();
if (keystoreFile == null) { if (keystoreFile == null) {
SslContextFactory.createTrustSslContext( SslContextFactory.createTrustSslContext(
@ -247,6 +250,7 @@ public class StandardSSLContextService extends AbstractControllerService impleme
SslContextFactory.createSslContext( SslContextFactory.createSslContext(
validationContext.getProperty(KEYSTORE).getValue(), validationContext.getProperty(KEYSTORE).getValue(),
validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(), validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(),
keyPassword,
validationContext.getProperty(KEYSTORE_TYPE).getValue(), validationContext.getProperty(KEYSTORE_TYPE).getValue(),
protocol); protocol);
return; return;
@ -255,6 +259,7 @@ public class StandardSSLContextService extends AbstractControllerService impleme
SslContextFactory.createSslContext( SslContextFactory.createSslContext(
validationContext.getProperty(KEYSTORE).getValue(), validationContext.getProperty(KEYSTORE).getValue(),
validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(), validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(),
keyPassword,
validationContext.getProperty(KEYSTORE_TYPE).getValue(), validationContext.getProperty(KEYSTORE_TYPE).getValue(),
validationContext.getProperty(TRUSTSTORE).getValue(), validationContext.getProperty(TRUSTSTORE).getValue(),
validationContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(), validationContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(),

View File

@ -109,7 +109,7 @@ public class SSLContextServiceTest {
runner.assertValid(service); runner.assertValid(service);
service = (SSLContextService) runner.getProcessContext().getControllerServiceLookup().getControllerService("test-good1"); service = (SSLContextService) runner.getProcessContext().getControllerServiceLookup().getControllerService("test-good1");
Assert.assertNotNull(service); Assert.assertNotNull(service);
SSLContextService sslService = (SSLContextService) service; SSLContextService sslService = service;
sslService.createSSLContext(ClientAuth.REQUIRED); sslService.createSSLContext(ClientAuth.REQUIRED);
sslService.createSSLContext(ClientAuth.WANT); sslService.createSSLContext(ClientAuth.WANT);
sslService.createSSLContext(ClientAuth.NONE); sslService.createSSLContext(ClientAuth.NONE);
@ -160,4 +160,46 @@ public class SSLContextServiceTest {
} }
} }
@Test
public void testDifferentKeyPassword() {
try {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final SSLContextService service = new StandardSSLContextService();
final Map<String, String> properties = new HashMap<String, String>();
properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/diffpass-ks.jks");
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "storepassword");
properties.put(StandardSSLContextService.KEY_PASSWORD.getName(), "keypassword");
properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
runner.addControllerService("test-diff-keys", service, properties);
runner.enableControllerService(service);
runner.setProperty("SSL Context Svc ID", "test-diff-keys");
runner.assertValid();
Assert.assertNotNull(service);
Assert.assertTrue(service instanceof StandardSSLContextService);
SSLContextService sslService = service;
sslService.createSSLContext(ClientAuth.NONE);
} catch (Exception e) {
System.out.println(e);
Assert.fail("Should not have thrown a exception " + e.getMessage());
}
}
@Test
public void testDifferentKeyPasswordWithoutSpecifyingPassword() {
try {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final SSLContextService service = new StandardSSLContextService();
final Map<String, String> properties = new HashMap<String, String>();
properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/diffpass-ks.jks");
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "storepassword");
properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
runner.addControllerService("test-diff-keys", service, properties);
runner.assertNotValid(service);
} catch (Exception e) {
System.out.println(e);
Assert.fail("Should not have thrown a exception " + e.getMessage());
}
}
} }