Skip to content

Commit 6bbfb99

Browse files
authored
Fix an issue with type conversions (#4)
The Match#capture method had a bug where it didn't remove the type conversion suffix of the field name when there was no match in the tested string. The Matcher now correctly removes the type suffix from the field name when there is no match for the sub-pattern. Refs Graylog2/graylog2-server#18883 Refs Graylog2/graylog2-server#18898
1 parent 99f6e69 commit 6bbfb99

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/main/java/io/krakens/grok/api/Match.java

+3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ private Map<String, Object> capture(boolean flattened ) throws GrokException {
162162
}
163163
} else if (!isKeepEmptyCaptures()) {
164164
return;
165+
} else {
166+
// Extract key to remove the type conversion suffix from the key. See: https://github.com/Graylog2/graylog2-server/issues/18883
167+
key = Converter.extractKey(key);
165168
}
166169

167170
if (capture.containsKey(key)) {

src/test/java/io/krakens/grok/api/GrokTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -681,4 +681,29 @@ public void testNamedGroupWithUnderscore() {
681681
String result = (String) grok.match(testString).capture().get(grokPatternName);
682682
assertEquals("test", result);
683683
}
684+
685+
@Test
686+
public void testConversion() {
687+
// The Match#capture method had a bug where it didn't remove the type conversion part of the field name when
688+
// there was no match in the tested string. In this example it put a "packets:long" field into the capture map
689+
// instead of a "packets" field.
690+
// See:
691+
// - https://github.com/Graylog2/graylog2-server/issues/18883
692+
// - https://github.com/Graylog2/graylog2-server/pull/18898
693+
final Grok grok = compiler.compile("%{DATA:vendor_attack} against (?:server )?%{IP:destination_ip} (from %{IP:source_ip} )?detected(. %{NONNEGINT:packets:long})?");
694+
695+
final Map<String, Object> match1 = grok.match("DDOS against server 10.0.1.34 detected.").capture();
696+
697+
assertEquals("DDOS", match1.get("vendor_attack"));
698+
assertEquals("10.0.1.34", match1.get("destination_ip"));
699+
assertTrue("Should have \"packets\" field", match1.containsKey("packets"));
700+
assertNull(match1.get("packets"));
701+
702+
final Map<String, Object> match2 = grok.match("DDOS against server 10.0.1.34 detected. 1234567").capture();
703+
704+
assertEquals("DDOS", match2.get("vendor_attack"));
705+
assertEquals("10.0.1.34", match2.get("destination_ip"));
706+
assertTrue("Should have \"packets\" field", match2.containsKey("packets"));
707+
assertEquals(1234567L, match2.get("packets"));
708+
}
684709
}

0 commit comments

Comments
 (0)