NIFI-2944 Check remote input hostname at startup.

Added unit test cases.

This closes #1379.

Signed-off-by: Andy LoPresto <alopresto@apache.org>
This commit is contained in:
Pierre Villard 2016-12-30 18:32:44 +01:00 committed by Andy LoPresto
parent 67cbef5df3
commit be6bcf20ad
No known key found for this signature in database
GPG Key ID: 3C6EF65B2F7DEF69
3 changed files with 72 additions and 9 deletions

View File

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

View File

@ -16,24 +16,25 @@
*/ */
package org.apache.nifi.util; package org.apache.nifi.util;
import org.junit.Assert; import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.io.File; import java.io.File;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.junit.Assert;
import static org.junit.Assert.assertEquals; import org.junit.Test;
public class NiFiPropertiesTest { public class NiFiPropertiesTest {
@Test @Test
public void testProperties() { public void testProperties() {
NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.properties"); NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.properties", null);
assertEquals("UI Banner Text", properties.getBannerText()); assertEquals("UI Banner Text", properties.getBannerText());
@ -55,7 +56,7 @@ public class NiFiPropertiesTest {
@Test @Test
public void testMissingProperties() { 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(); List<Path> directories = properties.getNarLibraryDirectories();
@ -69,7 +70,7 @@ public class NiFiPropertiesTest {
@Test @Test
public void testBlankProperties() { 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(); 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; String realPath = null;
try{ try{
realPath = NiFiPropertiesTest.class.getResource(propsPath).toURI().getPath(); realPath = NiFiPropertiesTest.class.getResource(propsPath).toURI().getPath();
}catch(final URISyntaxException ex){ }catch(final URISyntaxException ex){
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
return NiFiProperties.createBasicNiFiProperties(realPath, null); return NiFiProperties.createBasicNiFiProperties(realPath, additionalProperties);
} }
} }

View File

@ -39,6 +39,7 @@ import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.apache.nifi.documentation.DocGenerator; import org.apache.nifi.documentation.DocGenerator;
import org.apache.nifi.nar.ExtensionManager; import org.apache.nifi.nar.ExtensionManager;
import org.apache.nifi.nar.ExtensionMapping; import org.apache.nifi.nar.ExtensionMapping;
@ -259,6 +260,7 @@ public class NiFi {
try { try {
final ClassLoader bootstrap = createBootstrapClassLoader(); final ClassLoader bootstrap = createBootstrapClassLoader();
NiFiProperties properties = initializeProperties(args, bootstrap); NiFiProperties properties = initializeProperties(args, bootstrap);
properties.validate();
new NiFi(properties); new NiFi(properties);
} catch (final Throwable t) { } catch (final Throwable t) {
LOGGER.error("Failure to launch NiFi due to " + t, t); LOGGER.error("Failure to launch NiFi due to " + t, t);