exceptionfactory commented on code in PR #11653: URL: https://github.com/apache/nifi/pull/11653#discussion_r3967855757
########## 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: This is a tradeoff, but it is expected. With this approach, if the reload fails, it will not continue to warn as opposed to continue to retry. -- 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]
