Repository: incubator-metron
Updated Branches:
  refs/heads/master 23ff817eb -> 812fe75c3


METRON-424 ability to validate ip addresses against both IPV4 and IPV6 closes 
apache/incubator-metron#303


Project: http://git-wip-us.apache.org/repos/asf/incubator-metron/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-metron/commit/812fe75c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-metron/tree/812fe75c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-metron/diff/812fe75c

Branch: refs/heads/master
Commit: 812fe75c30a826c460b59e9fc056c69d0d91b876
Parents: 23ff817
Author: Otto Fowler <ofow...@industrialdefender.com>
Authored: Mon Oct 17 09:47:21 2016 -0400
Committer: cstella <ceste...@gmail.com>
Committed: Mon Oct 17 09:47:21 2016 -0400

----------------------------------------------------------------------
 metron-platform/metron-common/README.md         |  2 +-
 .../field/validation/network/IPValidation.java  | 29 +++++++++++-------
 .../validation/network/IPValidationTest.java    | 31 ++++++++++++++++++++
 .../src/main/config/zookeeper/global.json       |  2 +-
 4 files changed, 51 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/812fe75c/metron-platform/metron-common/README.md
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/README.md 
b/metron-platform/metron-common/README.md
index c4099ed..42354d7 100644
--- a/metron-platform/metron-common/README.md
+++ b/metron-platform/metron-common/README.md
@@ -600,7 +600,7 @@ structured like so:
 * `config` : A String to Object map for validation configuration.  This is 
optional if the validation function requires no configuration.
 * `validation` : The validation function to be used.  This is one of
    * `STELLAR` : Execute a Stellar Language statement.  Expects the query 
string in the `condition` field of the config.
-   * `IP` : Validates that the input fields are an IP address.  By default, if 
no configuration is set, it assumes `IPV4`, but you can specify the type by 
passing in the config by passing in `type` with either `IPV6` or `IPV4`.
+   * `IP` : Validates that the input fields are an IP address.  By default, if 
no configuration is set, it assumes `IPV4`, but you can specify the type by 
passing in the config by passing in `type` with either `IPV6` or `IPV4` or by 
passing in a list [`IPV4`,`IPV6`] in which case the input(s) will be validated 
against both.
    * `DOMAIN` : Validates that the fields are all domains.
    * `EMAIL` : Validates that the fields are all email addresses
    * `URL` : Validates that the fields are all URLs

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/812fe75c/metron-platform/metron-common/src/main/java/org/apache/metron/common/field/validation/network/IPValidation.java
----------------------------------------------------------------------
diff --git 
a/metron-platform/metron-common/src/main/java/org/apache/metron/common/field/validation/network/IPValidation.java
 
b/metron-platform/metron-common/src/main/java/org/apache/metron/common/field/validation/network/IPValidation.java
index 3f27d19..d14684c 100644
--- 
a/metron-platform/metron-common/src/main/java/org/apache/metron/common/field/validation/network/IPValidation.java
+++ 
b/metron-platform/metron-common/src/main/java/org/apache/metron/common/field/validation/network/IPValidation.java
@@ -24,8 +24,7 @@ import org.apache.metron.common.dsl.Predicate2StellarFunction;
 import org.apache.metron.common.dsl.Stellar;
 import org.apache.metron.common.field.validation.FieldValidation;
 
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.function.Predicate;
 
 public class IPValidation implements FieldValidation, Predicate<List<Object>> {
@@ -34,7 +33,7 @@ public class IPValidation implements FieldValidation, 
Predicate<List<Object>> {
           , description = "Determine if an string is an IP or not."
           , params = {
               "ip - An object which we wish to test is an ip"
-             ,"type (optional) - one of IPV4 or IPV6.  The default is IPV4."
+             ,"type (optional) - Object of string or collection type (e.g. 
list) one of IPV4 or IPV6 or both.  The default is IPV4."
                      }
           , returns = "True if the string is an IP and false otherwise.")
   public static class IS_IP extends Predicate2StellarFunction {
@@ -76,12 +75,15 @@ public class IPValidation implements FieldValidation, 
Predicate<List<Object>> {
     Config(String key) {
       this.key = key;
     }
-    public <T> T get(Map<String, Object> config, Class<T> clazz) {
+    public List get(Map<String, Object> config ) {
       Object o = config.get(key);
       if(o == null) {
-        return null;
+        return Collections.singletonList("DEFAULT");
       }
-      return clazz.cast(o);
+      if( o instanceof ArrayList){
+        return (ArrayList)o;
+      }
+      return Collections.singletonList(o);
     }
   }
 
@@ -115,19 +117,24 @@ public class IPValidation implements FieldValidation, 
Predicate<List<Object>> {
     }
     return type.isValid(ip.toString());
   }
+
   @Override
   public boolean isValid( Map<String, Object> input
                         , Map<String, Object> validationConfig
                         , Map<String, Object> globalConfig
                         , Context context
                         ) {
-    IPType type = IPType.get(Config.TYPE.get(validationConfig, String.class));
-    for(Object o : input.values()) {
-      if(o != null && !type.isValid(o.toString())) {
-        return false;
+    List types = Config.TYPE.get(validationConfig);
+
+    for(Object typeObject : types) {
+      IPType type = IPType.get(typeObject.toString());
+      for (Object o : input.values()) {
+        if(o == null || type.isValid(o.toString())) {
+          return true;
+        }
       }
     }
-    return true;
+    return false;
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/812fe75c/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java
----------------------------------------------------------------------
diff --git 
a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java
 
b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java
index 437f61b..b088b3d 100644
--- 
a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java
+++ 
b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java
@@ -59,6 +59,25 @@ public class IPValidationTest extends BaseValidationTest {
   public static String validWithMultipleFields;
   public static String validWithMultipleFields_MQL = "IS_IP(field1, 'IPV4') && 
IS_IP(field2, 'IPV4')";
 
+  /**
+   {
+   "fieldValidations" : [
+   {
+   "input" : [ "field1", "field2" ]
+   ,"validation" : "IP"
+   ,"config" : {
+   "type" : ["IPV4","IPV6"]
+   }
+   }
+   ]
+   }
+   */
+  @Multiline
+  public static String validWithMultipleFieldsMultipleTypes;
+  public static String validWithMultipleFieldsMultipleTypes_MQL = 
"IS_IP(field1, '[IPV4,IPV6]') && IS_IP(field2, '[IPV4,IPV6]')";
+
+
+
   @Test
   public void positiveTest_single() throws IOException {
     Assert.assertTrue(execute(validWithSingleField, ImmutableMap.of("field1", 
"127.0.0.1")));
@@ -82,4 +101,16 @@ public class IPValidationTest extends BaseValidationTest {
     Assert.assertFalse(execute(validWithMultipleFields, 
ImmutableMap.of("field1", 1, "field2", "192.168.1")));
     Assert.assertFalse(runPredicate(validWithMultipleFields_MQL, 
ImmutableMap.of("field1", 1, "field2", "192.168.1")));
   }
+
+  @Test
+  public void positiveTest_multiplex2() throws IOException {
+    Assert.assertTrue(execute(validWithMultipleFieldsMultipleTypes, 
ImmutableMap.of("field1", "192.168.0.1", "field2", "127.0.0.2")));
+    Assert.assertTrue(runPredicate(validWithMultipleFieldsMultipleTypes_MQL, 
ImmutableMap.of("field1", "192.168.0.1", "field2", "127.0.0.2")));
+  }
+
+  @Test
+  public void negativeTest_multiplex2() throws IOException {
+    Assert.assertFalse(execute(validWithMultipleFieldsMultipleTypes, 
ImmutableMap.of("field1", 1, "field2", "192.168.1")));
+    Assert.assertFalse(runPredicate(validWithMultipleFieldsMultipleTypes_MQL, 
ImmutableMap.of("field1", 1, "field2", "192.168.1")));
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/812fe75c/metron-platform/metron-integration-test/src/main/config/zookeeper/global.json
----------------------------------------------------------------------
diff --git 
a/metron-platform/metron-integration-test/src/main/config/zookeeper/global.json 
b/metron-platform/metron-integration-test/src/main/config/zookeeper/global.json
index 266badf..3f2ee58 100644
--- 
a/metron-platform/metron-integration-test/src/main/config/zookeeper/global.json
+++ 
b/metron-platform/metron-integration-test/src/main/config/zookeeper/global.json
@@ -11,7 +11,7 @@
 
   "fieldValidations" : [
     {
-      "input" : [ "src_ip_addr", "dst_ip_addr"],
+      "input" : [ "ip_src_addr", "ip_dst_addr"],
       "validation" : "IP"
     }
   ],

Reply via email to