pvillard31 commented on code in PR #11653:
URL: https://github.com/apache/nifi/pull/11653#discussion_r3967431605


##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/framework/ssl/SecurityStoreMonitorCommandTest.java:
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.framework.ssl;
+
+import org.apache.nifi.security.ssl.KeyManagerBuilder;
+import org.apache.nifi.security.ssl.KeyManagerListener;
+import org.apache.nifi.security.ssl.TrustManagerBuilder;
+import org.apache.nifi.security.ssl.TrustManagerListener;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Set;
+import javax.net.ssl.X509ExtendedKeyManager;
+import javax.net.ssl.X509ExtendedTrustManager;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class SecurityStoreMonitorCommandTest {
+    private static final String KEY_STORE_FILE_NAME = "keystore.p12";
+
+    private static final String INITIAL_CONTENT = "initial-store";
+
+    private static final String UPDATED_CONTENT = "updated-store";
+
+    private static final String RELOADED_CONTENT = "reloaded-store";
+
+    @TempDir
+    private Path tempDir;
+
+    @Mock
+    private KeyManagerListener keyManagerListener;
+
+    @Mock
+    private KeyManagerBuilder keyManagerBuilder;
+
+    @Mock
+    private TrustManagerListener trustManagerListener;
+
+    @Mock
+    private TrustManagerBuilder trustManagerBuilder;
+
+    @Mock
+    private X509ExtendedKeyManager keyManager;
+
+    @Mock
+    private X509ExtendedTrustManager trustManager;
+
+    @Test
+    void testRunStoreUnchanged() throws IOException {
+        final Path keyStorePath = writeStore(INITIAL_CONTENT);
+        final SecurityStoreMonitorCommand command = newCommand(keyStorePath);
+
+        command.run();
+
+        verifyNoInteractions(keyManagerBuilder, keyManagerListener, 
trustManagerBuilder, trustManagerListener);
+    }
+
+    @Test
+    void testRunStoreRewrittenReloadsManagers() throws IOException {
+        final Path keyStorePath = writeStore(INITIAL_CONTENT);
+        final SecurityStoreMonitorCommand command = newCommand(keyStorePath);
+        setManagers();
+
+        writeStore(UPDATED_CONTENT);
+        command.run();
+
+        verify(keyManagerListener).setKeyManager(keyManager);
+        verify(trustManagerListener).setTrustManager(trustManager);
+    }
+
+    @Test
+    void testRunStoreRewrittenWithIdenticalContent() throws IOException {
+        final Path keyStorePath = writeStore(INITIAL_CONTENT);
+        final SecurityStoreMonitorCommand command = newCommand(keyStorePath);
+
+        writeStore(INITIAL_CONTENT);
+        command.run();
+
+        verifyNoInteractions(keyManagerBuilder, keyManagerListener, 
trustManagerBuilder, trustManagerListener);
+    }
+
+    @Test
+    void testRunReloadFailureDoesNotPropagate() throws IOException {
+        final Path keyStorePath = writeStore(INITIAL_CONTENT);
+        final SecurityStoreMonitorCommand command = newCommand(keyStorePath);
+
+        doThrow(new IllegalStateException("Key Store loading 
failed")).when(keyManagerBuilder).build();
+        writeStore(UPDATED_CONTENT);
+        command.run();
+
+        verifyNoInteractions(keyManagerListener, trustManagerListener);
+
+        doReturn(keyManager).when(keyManagerBuilder).build();
+        doReturn(trustManager).when(trustManagerBuilder).build();
+        writeStore(RELOADED_CONTENT);
+        command.run();
+
+        verify(keyManagerListener).setKeyManager(keyManager);
+        verify(trustManagerListener).setTrustManager(trustManager);
+    }
+
+    @Test
+    void testRunDeletedStoreDoesNotPropagate() throws IOException {
+        final Path keyStorePath = writeStore(INITIAL_CONTENT);
+        final SecurityStoreMonitorCommand command = newCommand(keyStorePath);
+
+        Files.delete(keyStorePath);
+        command.run();
+
+        verifyNoInteractions(keyManagerBuilder, keyManagerListener, 
trustManagerBuilder, trustManagerListener);
+    }

Review Comment:
   Can we add a regression test that replaces a symbolic link target and 
verifies that the managers reload?



##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/framework/ssl/SecurityStoreMonitorCommand.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.framework.ssl;
+
+import org.apache.nifi.security.ssl.KeyManagerBuilder;
+import org.apache.nifi.security.ssl.KeyManagerListener;
+import org.apache.nifi.security.ssl.TrustManagerBuilder;
+import org.apache.nifi.security.ssl.TrustManagerListener;
+import org.apache.nifi.util.security.MessageDigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import javax.net.ssl.X509ExtendedKeyManager;
+import javax.net.ssl.X509ExtendedTrustManager;
+
+/**
+ * Runnable command that digests configured Key Store and Trust Store paths 
and reloads managers when content changes
+ */
+public class SecurityStoreMonitorCommand implements Runnable {
+    private static final Logger logger = 
LoggerFactory.getLogger(SecurityStoreMonitorCommand.class);
+
+    private final Set<Path> storePaths;
+
+    private final Map<Path, byte[]> storeDigests;
+
+    private final KeyManagerListener keyManagerListener;
+
+    private final KeyManagerBuilder keyManagerBuilder;
+
+    private final TrustManagerListener trustManagerListener;
+
+    private final TrustManagerBuilder trustManagerBuilder;
+
+    /**
+     * Security Store Monitor Command reloads Key Manager and Trust Manager 
when a configured store path digest changes
+     *
+     * @param storePaths Key Store and Trust Store Paths to digest
+     * @param keyManagerListener Key Manager Listener for handling updated Key 
Manager
+     * @param keyManagerBuilder Key Manager Builder for creating new Key 
Manager instances
+     * @param trustManagerListener Trust Manager Listener for handling updated 
Trust Manager
+     * @param trustManagerBuilder Trust Manager Builder for creating new Trust 
Manager instances
+     */
+    public SecurityStoreMonitorCommand(
+            final Set<Path> storePaths,
+            final KeyManagerListener keyManagerListener,
+            final KeyManagerBuilder keyManagerBuilder,
+            final TrustManagerListener trustManagerListener,
+            final TrustManagerBuilder trustManagerBuilder
+    ) {
+        this.storePaths = Set.copyOf(Objects.requireNonNull(storePaths, "Store 
Paths required"));
+        this.keyManagerListener = Objects.requireNonNull(keyManagerListener, 
"Key Manager Listener required");
+        this.keyManagerBuilder = Objects.requireNonNull(keyManagerBuilder, 
"Key Manager Builder required");
+        this.trustManagerListener = 
Objects.requireNonNull(trustManagerListener, "Trust Manager Listener required");
+        this.trustManagerBuilder = Objects.requireNonNull(trustManagerBuilder, 
"Trust Manager Builder required");
+        this.storeDigests = new HashMap<>();
+
+        for (final Path storePath : this.storePaths) {
+            try {
+                storeDigests.put(storePath, getDigest(storePath));
+            } catch (final IOException e) {
+                logger.warn("Digest calculation failed for Security Store 
[{}]", storePath, e);
+            }
+        }
+    }
+
+    /**
+     * Digest configured store paths and reload Key Manager and Trust Manager 
when content changes
+     */
+    @Override
+    public void run() {
+        final List<Path> changedPaths = new ArrayList<>();
+
+        for (final Path storePath : storePaths) {
+            try {
+                final byte[] currentDigest = getDigest(storePath);

Review Comment:
   Should the stored digest advance only after the managers reload 
successfully, so an unchanged failed update is retried?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to