From be6bcf20adea6798c528a2e3b8824b48a5a9772e Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Fri, 30 Dec 2016 18:32:44 +0100 Subject: [PATCH] NIFI-2944 Check remote input hostname at startup. Added unit test cases. This closes #1379. Signed-off-by: Andy LoPresto --- .../org/apache/nifi/util/NiFiProperties.java | 14 ++++ .../apache/nifi/util/NiFiPropertiesTest.java | 65 ++++++++++++++++--- .../src/main/java/org/apache/nifi/NiFi.java | 2 + 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/NiFiProperties.java b/nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/NiFiProperties.java index 554bae55e0..933d62d668 100644 --- a/nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/NiFiProperties.java +++ b/nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/NiFiProperties.java @@ -1086,4 +1086,18 @@ public abstract class NiFiProperties { }; } + /** + * This method is used to validate the NiFi properties when the file is loaded + * for the first time. The objective is to stop NiFi startup in case a property + * is not correctly configured and could cause issues afterwards. + */ + public void validate() { + // REMOTE_INPUT_HOST should be a valid hostname + String remoteInputHost = getProperty(REMOTE_INPUT_HOST); + if(!StringUtils.isBlank(remoteInputHost) && remoteInputHost.split(":").length > 1) { // no scheme/port needed here (http://) + throw new IllegalArgumentException(remoteInputHost + " is not a correct value for " + REMOTE_INPUT_HOST + ". It should be a valid hostname without protocol or port."); + } + // Other properties to validate... + } + } diff --git a/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java index e174d140e6..48b1b1ae1c 100644 --- a/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java +++ b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java @@ -16,24 +16,25 @@ */ package org.apache.nifi.util; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.Assert.assertEquals; import java.io.File; import java.net.URISyntaxException; import java.nio.file.Path; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; - -import static org.junit.Assert.assertEquals; +import org.junit.Assert; +import org.junit.Test; public class NiFiPropertiesTest { @Test public void testProperties() { - NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.properties"); + NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.properties", null); assertEquals("UI Banner Text", properties.getBannerText()); @@ -55,7 +56,7 @@ public class NiFiPropertiesTest { @Test public void testMissingProperties() { - NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.missing.properties"); + NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.missing.properties", null); List directories = properties.getNarLibraryDirectories(); @@ -69,7 +70,7 @@ public class NiFiPropertiesTest { @Test public void testBlankProperties() { - NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties"); + NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", null); List directories = properties.getNarLibraryDirectories(); @@ -80,14 +81,60 @@ public class NiFiPropertiesTest { } - private NiFiProperties loadNiFiProperties(final String propsPath){ + @Test + public void testValidateProperties() { + // expect no error to be thrown + Map additionalProperties = new HashMap<>(); + additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "localhost"); + NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); + + try { + properties.validate(); + } catch (Throwable t) { + Assert.fail("unexpected exception: " + t.getMessage()); + } + + // expect no error to be thrown + additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, ""); + properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); + + try { + properties.validate(); + } catch (Throwable t) { + Assert.fail("unexpected exception: " + t.getMessage()); + } + + // expect no error to be thrown + additionalProperties.remove(NiFiProperties.REMOTE_INPUT_HOST); + properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); + + try { + properties.validate(); + } catch (Throwable t) { + Assert.fail("unexpected exception: " + t.getMessage()); + } + + // expected error + additionalProperties = new HashMap<>(); + additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "http://localhost"); + properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); + + try { + properties.validate(); + Assert.fail("Validation should throw an exception"); + } catch (Throwable t) { + // nothing to do + } + } + + private NiFiProperties loadNiFiProperties(final String propsPath, final Map additionalProperties){ String realPath = null; try{ realPath = NiFiPropertiesTest.class.getResource(propsPath).toURI().getPath(); }catch(final URISyntaxException ex){ throw new RuntimeException(ex); } - return NiFiProperties.createBasicNiFiProperties(realPath, null); + return NiFiProperties.createBasicNiFiProperties(realPath, additionalProperties); } } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-runtime/src/main/java/org/apache/nifi/NiFi.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-runtime/src/main/java/org/apache/nifi/NiFi.java index bb2a2f612c..0488823b8c 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-runtime/src/main/java/org/apache/nifi/NiFi.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-runtime/src/main/java/org/apache/nifi/NiFi.java @@ -39,6 +39,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; + import org.apache.nifi.documentation.DocGenerator; import org.apache.nifi.nar.ExtensionManager; import org.apache.nifi.nar.ExtensionMapping; @@ -259,6 +260,7 @@ public class NiFi { try { final ClassLoader bootstrap = createBootstrapClassLoader(); NiFiProperties properties = initializeProperties(args, bootstrap); + properties.validate(); new NiFi(properties); } catch (final Throwable t) { LOGGER.error("Failure to launch NiFi due to " + t, t);