You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by al...@apache.org on 2017/01/03 04:25:14 UTC

nifi git commit: NIFI-2944 Check remote input hostname at startup. Added unit test cases.

Repository: nifi
Updated Branches:
  refs/heads/master 67cbef5df -> be6bcf20a


NIFI-2944 Check remote input hostname at startup.
Added unit test cases.

This closes #1379.

Signed-off-by: Andy LoPresto <al...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/be6bcf20
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/be6bcf20
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/be6bcf20

Branch: refs/heads/master
Commit: be6bcf20adea6798c528a2e3b8824b48a5a9772e
Parents: 67cbef5
Author: Pierre Villard <pi...@gmail.com>
Authored: Fri Dec 30 18:32:44 2016 +0100
Committer: Andy LoPresto <al...@apache.org>
Committed: Mon Jan 2 20:25:03 2017 -0800

----------------------------------------------------------------------
 .../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(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/be6bcf20/nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/NiFiProperties.java
----------------------------------------------------------------------
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 554bae5..933d62d 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...
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/nifi/blob/be6bcf20/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java
----------------------------------------------------------------------
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 e174d14..48b1b1a 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<Path> 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<Path> 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<String, String> 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<String, String> 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);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/nifi/blob/be6bcf20/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-runtime/src/main/java/org/apache/nifi/NiFi.java
----------------------------------------------------------------------
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 bb2a2f6..0488823 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);