gresockj commented on a change in pull request #4991:
URL: https://github.com/apache/nifi/pull/4991#discussion_r613116435



##########
File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-ssl-autoloading-utils/src/test/java/org/apache/nifi/autoload/TestSSLContextFactoryAutoLoaderTask.java
##########
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.autoload;
+
+import org.apache.nifi.security.util.KeyStoreUtils;
+import org.apache.nifi.security.util.StandardTlsConfiguration;
+import org.apache.nifi.security.util.TlsConfiguration;
+import org.apache.nifi.security.util.TlsException;
+import org.apache.nifi.util.NiFiProperties;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.WatchService;
+import java.security.GeneralSecurityException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableEntryException;
+
+public class TestSSLContextFactoryAutoLoaderTask {
+
+    private static final Path KEYSTORE = new 
File("src/test/resources/keystore.jks").toPath();
+    private static final String KEYSTORE_PASSWORD = "testtesttest";
+    private static final String KEY_PASSWORD = "testtesttest";
+    private static final String KEYSTORE_TYPE = "JKS";
+
+    private static final String DEFAULT_KEY_ALIAS = "nifi-key";
+    private static final String DEFAULT_CERT_DN = "CN=localhost, OU=NIFI";
+
+    private SSLContextFactoryAutoLoaderTask task;
+    private NiFiProperties nifiProperties;
+
+    @Before
+    public void init() throws GeneralSecurityException, IOException {
+        this.createKeystore(DEFAULT_KEY_ALIAS, DEFAULT_CERT_DN);
+
+        nifiProperties = Mockito.mock(NiFiProperties.class);
+        
Mockito.when(nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD)).thenReturn(KEYSTORE_PASSWORD);
+        
Mockito.when(nifiProperties.getProperty(NiFiProperties.SECURITY_KEY_PASSWD)).thenReturn(KEY_PASSWORD);
+        
Mockito.when(nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_TYPE)).thenReturn(KEYSTORE_TYPE);
+
+        task = new SSLContextFactoryAutoLoaderTask.Builder()
+                .keystorePath(KEYSTORE)
+                .keystoreWatchService(Mockito.mock(WatchService.class))
+                .truststorePath(Mockito.mock(Path.class))
+                .autoLoader(Mockito.mock(SSLContextFactoryAutoLoader.class))
+                .nifiProperties(nifiProperties)
+                .pollIntervalMillis(100)
+                .build();
+    }
+
+    @Test
+    public void testIsReloadAllowed_true() throws IOException, 
GeneralSecurityException {
+        this.createKeystore(DEFAULT_KEY_ALIAS, DEFAULT_CERT_DN); // Creates a 
keystore with the same DNs and alias, so this is allowed
+        Assert.assertTrue(task.isReloadAllowed());
+    }
+
+    @Test
+    public void testIsReloadAllowed_differentAlias() throws IOException, 
GeneralSecurityException {
+        this.createKeystore("different-alias", DEFAULT_CERT_DN);
+        Assert.assertFalse(task.isReloadAllowed());
+    }
+
+    @Test
+    public void testIsReloadAllowed_differentSubjectDN() throws IOException, 
GeneralSecurityException {
+        this.createKeystore(DEFAULT_KEY_ALIAS, "CN=different");
+        Assert.assertFalse(task.isReloadAllowed());
+    }
+
+    @Test
+    public void testIsReloadAllowed_differentIssuerSerialNumber() throws 
IOException, UnrecoverableEntryException, NoSuchAlgorithmException, 
KeyStoreException, TlsException {
+        // This is a keystore with the same key alias and cert DN, but with a 
different issuer cert in the cert chain,
+        // showing that we disallow cert updates with the same issuer DN but 
different actual issuer serial number
+        Files.copy(new File("src/test/resources/test-keystore.jks").toPath(), 
KEYSTORE, StandardCopyOption.REPLACE_EXISTING);
+
+        Assert.assertFalse(task.isReloadAllowed());
+    }
+
+    private void createKeystore(String keyAlias, String certDn) throws 
IOException, GeneralSecurityException {
+        TlsConfiguration tlsConfiguration = 
KeyStoreUtils.createTlsConfigAndNewKeystoreTruststore(new 
StandardTlsConfiguration(
+                null, KEYSTORE_PASSWORD, KEY_PASSWORD, KEYSTORE_TYPE,
+                null, KEYSTORE_PASSWORD, KEYSTORE_TYPE), keyAlias, 
"nifi-cert", certDn);
+        Files.move(new File(tlsConfiguration.getKeystorePath()).toPath(), 
KEYSTORE, StandardCopyOption.ATOMIC_MOVE);

Review comment:
       Nice, this also helped me find ATOMIC_MOVE in ConfigEncryptionTool.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to