Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue with type conversions #4

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/io/krakens/grok/api/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ private Map<String, Object> capture(boolean flattened ) throws GrokException {
}
} else if (!isKeepEmptyCaptures()) {
return;
} else {
// Extract key to remove the type conversion suffix from the key. See: https://github.com/Graylog2/graylog2-server/issues/18883
key = Converter.extractKey(key);
}

if (capture.containsKey(key)) {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/io/krakens/grok/api/GrokTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,29 @@ public void testNamedGroupWithUnderscore() {
String result = (String) grok.match(testString).capture().get(grokPatternName);
assertEquals("test", result);
}

@Test
public void testConversion() {
// The Match#capture method had a bug where it didn't remove the type conversion part of the field name when
// there was no match in the tested string. In this example it put a "packets:long" field into the capture map
// instead of a "packets" field.
// See:
// - https://github.com/Graylog2/graylog2-server/issues/18883
// - https://github.com/Graylog2/graylog2-server/pull/18898
final Grok grok = compiler.compile("%{DATA:vendor_attack} against (?:server )?%{IP:destination_ip} (from %{IP:source_ip} )?detected(. %{NONNEGINT:packets:long})?");

final Map<String, Object> match1 = grok.match("DDOS against server 10.0.1.34 detected.").capture();

assertEquals("DDOS", match1.get("vendor_attack"));
assertEquals("10.0.1.34", match1.get("destination_ip"));
assertTrue("Should have \"packets\" field", match1.containsKey("packets"));
assertNull(match1.get("packets"));

final Map<String, Object> match2 = grok.match("DDOS against server 10.0.1.34 detected. 1234567").capture();

assertEquals("DDOS", match2.get("vendor_attack"));
assertEquals("10.0.1.34", match2.get("destination_ip"));
assertTrue("Should have \"packets\" field", match2.containsKey("packets"));
assertEquals(1234567L, match2.get("packets"));
}
}