Repository: metron Updated Branches: refs/heads/master f34affdb2 -> 174964832
METRON-1131: The Stellar REPL rejects valid hostnames for zookeeper in its CLI options this closes apache/incubator-metron#715 Project: http://git-wip-us.apache.org/repos/asf/metron/repo Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/17496483 Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/17496483 Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/17496483 Branch: refs/heads/master Commit: 174964832eafba927ae4e7aa407d2a1100a8b776 Parents: f34affd Author: cstella <ceste...@gmail.com> Authored: Fri Aug 25 09:17:48 2017 -0400 Committer: cstella <ceste...@gmail.com> Committed: Fri Aug 25 09:17:48 2017 -0400 ---------------------------------------------------------------------- .../shell/StellarShellOptionsValidator.java | 24 ++++++++++++++------ .../shell/StellarShellOptionsValidatorTest.java | 9 ++++---- 2 files changed, 22 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metron/blob/17496483/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java index 411c288..97f5b70 100644 --- a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java +++ b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java @@ -21,21 +21,31 @@ package org.apache.metron.stellar.common.shell; import java.io.File; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.cli.CommandLine; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.validator.routines.InetAddressValidator; public class StellarShellOptionsValidator { private static final Pattern validPortPattern = Pattern.compile("(^.*)[:](\\d+)$"); - private static final Pattern validHostNamePattern; + private static final Predicate<String> hostnameValidator = hostname -> { + if(StringUtils.isEmpty(hostname)) { + return false; + } + try { + InetAddress add = InetAddress.getByName(hostname); + return true; + } catch (UnknownHostException e) { + return false; + } + }; + - static { - validHostNamePattern = Pattern.compile( - "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\\\-]*[a-zA-Z0-9])\\\\.)" - + "*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\\\-]*[A-Za-z0-9])$"); - } private static final InetAddressValidator inetAddressValidator = InetAddressValidator .getInstance(); @@ -79,7 +89,7 @@ public class StellarShellOptionsValidator { String name = matcher.group(1); Integer port = Integer.parseInt(matcher.group(2)); - if (!validHostNamePattern.matcher(name).matches() && !inetAddressValidator.isValid(name)) { + if (!hostnameValidator.test(name) && !inetAddressValidator.isValid(name)) { throw new IllegalArgumentException( String.format("Zookeeper Option %s is not a valid host name or ip address %s", name, z)); } http://git-wip-us.apache.org/repos/asf/metron/blob/17496483/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java index 4f74308..52fef96 100644 --- a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java +++ b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java @@ -28,6 +28,7 @@ import org.junit.Assert; import org.junit.Test; import java.io.File; +import java.util.regex.Pattern; import static org.junit.Assert.*; @@ -35,15 +36,15 @@ public class StellarShellOptionsValidatorTest { @Test public void validateOptions() throws Exception { - String[] validZHostArg = new String[]{"-z", "host1:8888"}; + String[] validZHostArg = new String[]{"-z", "localhost:8888"}; String[] validZIPArg = new String[]{"-z", "10.10.10.3:9999"}; - String[] invalidZNoPortArg = new String[]{"-z", "host1"}; + String[] invalidZNoPortArg = new String[]{"-z", "youtube.com"}; String[] invalidZIPNoPortArg = new String[]{"-z", "10.10.10.3"}; String[] invalidZNameArg = new String[]{"-z", "!!!@!!@!:8882"}; String[] invalidZIPArg = new String[]{"-z", "11111.22222.10.3:3332"}; String[] invalidZMissingNameArg = new String[]{"-z", ":8882"}; - String[] invalidZZeroPortArg = new String[]{"-z", "host1:0"}; - String[] invalidZHugePortArg = new String[]{"-z", "host1:75565"}; + String[] invalidZZeroPortArg = new String[]{"-z", "youtube.com:0"}; + String[] invalidZHugePortArg = new String[]{"-z", "youtube.com:75565"}; String existingFileName = "./target/existsFile";