bbende commented on a change in pull request #4986: URL: https://github.com/apache/nifi/pull/4986#discussion_r619313393
########## File path: nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/util/SecureNiFiConfigUtil.java ########## @@ -0,0 +1,197 @@ +/* + * 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.bootstrap.util; + +import org.apache.commons.lang3.StringUtils; +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.util.NiFiProperties; +import org.bouncycastle.util.IPAddress; +import org.slf4j.Logger; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.security.GeneralSecurityException; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; + +public class SecureNiFiConfigUtil { + + private static final int CERT_DURATION_DAYS = 60; + private static final String LOCALHOST_NAME = "localhost"; + private static final String PROPERTY_VALUE_PATTERN = "%s=%s"; + + private SecureNiFiConfigUtil() { + + } + + private static boolean fileExists(String filename) { + return StringUtils.isNotEmpty(filename) && Paths.get(filename).toFile().exists(); + } + + private static boolean isHttpsSecurityConfigured(final Properties nifiProperties) { + if (StringUtils.isEmpty(nifiProperties.getProperty(NiFiProperties.WEB_HTTPS_PORT, StringUtils.EMPTY))) { + return false; + } + + String keystorePath = nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE, StringUtils.EMPTY); + String truststorePath = nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE, StringUtils.EMPTY); + if (StringUtils.isEmpty(keystorePath) || StringUtils.isEmpty(truststorePath)) { + return false; + } + + return true; + } + + /** + * If HTTPS is enabled (nifi.web.https.port is set), but the keystore file specified in nifi.security.keystore + * does not exist, this will generate a key pair and self-signed certificate, generate the associated keystore + * and truststore and write them to disk under the configured filepaths, generate a secure random keystore password + * and truststore password, and write these to the nifi.properties file. + * @param nifiPropertiesFilename The filename of the nifi.properties file + * @param cmdLogger The bootstrap logger + * @throws IOException can be thrown when writing keystores to disk + * @throws RuntimeException indicates a security exception while generating keystores + */ + public static void configureSecureNiFiProperties(String nifiPropertiesFilename, Logger cmdLogger) throws IOException, RuntimeException { + final File propertiesFile = new File(nifiPropertiesFilename); + final Properties nifiProperties = loadProperties(propertiesFile); + + if (!isHttpsSecurityConfigured(nifiProperties)) { + cmdLogger.debug("No HTTPS configuration detected: skipping Apache Nifi certificate generation."); + return; + } + + String keystorePath = nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE, StringUtils.EMPTY); + String truststorePath = nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE, StringUtils.EMPTY); + boolean keystoreExists = fileExists(keystorePath); + boolean truststoreExists = fileExists(truststorePath); + + if (!keystoreExists && !truststoreExists) { Review comment: @gresockj @exceptionfactory Do you think we'll run into cases where someone was intending to use their own keystore/truststore, but accidentally put the wrong path or something, so it ends up looking like they didn't exist and we proceed to generate stuff, the app starts up fine, and they have no idea its actually not using their own? The only options I could think of to protect against this are... 1) Adding an explicit property like `nifi.security.tls.auto.generate` (or some better name), that would default to false, and they'd have to specifically enable it, and then if its enabled and the files exist already, it'd be a failure. 2) We could so that we only do the auto-generation when only the two file paths are populated, meaning the passwords and types also need to be blank, and the files don't exist, making it a stronger indicator that they wanted auto generated certs. It loses the ability to provide your own passwords, but maybe doesn't matter if this is intended for helping with local default security? What do you guys think? -- 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: [email protected]
