|
@@ -14,6 +14,7 @@ import java.util.Collection;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static org.hamcrest.CoreMatchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.containsInAnyOrder;
|
|
|
|
|
|
public class GrokPatternCreatorTests extends ESTestCase {
|
|
@@ -71,7 +72,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".+?%{TIMESTAMP_ISO8601:timestamp}.+?%{LOGLEVEL:loglevel}.+?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -87,7 +89,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".*?%{TOMCAT_DATESTAMP:timestamp}.+?%{LOGLEVEL:loglevel}.+?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -105,7 +108,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".+?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -120,7 +124,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".+?%{NUMBER:field}.+?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -134,7 +139,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
// It seems sensible that we don't detect these suffices as either base 10 or base 16 numbers
|
|
|
assertEquals(".+?", overallGrokPatternBuilder.toString());
|
|
@@ -150,7 +156,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".*?%{BASE16NUM:field}.*?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -163,7 +170,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
// We don't want the .1. in the middle to get detected as a hex number
|
|
|
assertEquals(".+?", overallGrokPatternBuilder.toString());
|
|
@@ -178,7 +186,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".*?%{EMAILADDRESS:email}.*?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -192,7 +201,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".*?%{URI:uri}.*?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -206,7 +216,8 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
Map<String, Integer> fieldNameCountStore = new HashMap<>();
|
|
|
StringBuilder overallGrokPatternBuilder = new StringBuilder();
|
|
|
|
|
|
- GrokPatternCreator.appendBestGrokMatchForStrings(fieldNameCountStore, overallGrokPatternBuilder, false, false, mustMatchStrings);
|
|
|
+ GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false,
|
|
|
+ false, mustMatchStrings);
|
|
|
|
|
|
assertEquals(".+?%{PATH:path}.*?", overallGrokPatternBuilder.toString());
|
|
|
}
|
|
@@ -263,4 +274,74 @@ public class GrokPatternCreatorTests extends ESTestCase {
|
|
|
"%{IP:ipaddress}.+?Authpriv.+?Info.+?sshd.+?subsystem.+?request.+?for.+?sftp.*",
|
|
|
GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples));
|
|
|
}
|
|
|
+
|
|
|
+ public void testFindBestGrokMatchFromExamplesGivenAdversarialInputRecurseDepth() {
|
|
|
+ String regex = ".*?combo.+?rpc\\.statd.+?gethostbyname.+?error.+?for.+?X.+?X.+?Z.+?Z.+?hn.+?hn.*";
|
|
|
+ // Two timestamps: one local, one UTC
|
|
|
+ Collection<String> examples = Arrays.asList(
|
|
|
+ "combo rpc.statd[1605]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22...",
|
|
|
+ "combo rpc.statd[1608]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22...",
|
|
|
+ "combo rpc.statd[1635]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" +
|
|
|
+ "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22...");
|
|
|
+ assertEquals(
|
|
|
+ ".*?combo.+?rpc\\.statd.+?%{NUMBER:field}.+?gethostbyname.+?error.+?for.+?X.+?X.+?Z.+?Z.+?hn.+?hn.+?%{NUMBER:field2}" +
|
|
|
+ ".+?%{NUMBER:field3}.+?%{NUMBER:field4}.+?%{NUMBER:field5}.+?%{NUMBER:field6}.+?%{NUMBER:field7}.+?%{NUMBER:field8}" +
|
|
|
+ ".+?%{NUMBER:field9}.+?%{NUMBER:field10}.+?%{NUMBER:field11}.*",
|
|
|
+ GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testFindBestGrokMatchFromExamplesGivenMatchAllRegex() {
|
|
|
+ String regex = ".*";
|
|
|
+ // Two timestamps: one local, one UTC
|
|
|
+ Collection<String> examples = Arrays.asList(
|
|
|
+ "Killing job [count_tweets]",
|
|
|
+ "Killing job [tweets_by_location]",
|
|
|
+ "[count_tweets] Killing job",
|
|
|
+ "[tweets_by_location] Killing job");
|
|
|
+ assertThat(GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples), equalTo(regex));
|
|
|
+ }
|
|
|
}
|