Skip to content

Commit e642a41

Browse files
berndkmerz
authored andcommitted
Switch back to a repackaged and fixed version of java-grok (#5800)
* Switch back to a repackaged and fixed version of java-grok To support underscores ("_") in Grok match group names, we had to modify the java-grok library to use the old regexp engine again. See: graylog-labs/java-grok#2 This also adds a test for the Grok extractor to make sure that using underscores works. Fixes #5704 Fixes #5563 * Fix GrokPatternService#extractPatternNames and add a test for it * Add missing license header to GrokPatternServiceTest * Add test for named group with underscore Prior to this change, there was no test for named groups with underscores in the FunctionSnippetsTest This change enhances the grok() test to run with a named group with underscore.
1 parent 0e013b8 commit e642a41

File tree

8 files changed

+82
-9
lines changed

8 files changed

+82
-9
lines changed

graylog-project-parent/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@
447447
</exclusions>
448448
</dependency>
449449
<dependency>
450-
<groupId>io.krakens</groupId>
451-
<artifactId>java-grok</artifactId>
450+
<groupId>org.graylog2.repackaged</groupId>
451+
<artifactId>grok</artifactId>
452452
<version>${grok.version}</version>
453453
</dependency>
454454
<dependency>

graylog2-server/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@
304304
</dependency>
305305

306306
<dependency>
307-
<groupId>io.krakens</groupId>
308-
<artifactId>java-grok</artifactId>
307+
<groupId>org.graylog2.repackaged</groupId>
308+
<artifactId>grok</artifactId>
309309
</dependency>
310310

311311
<dependency>

graylog2-server/src/main/java/org/graylog2/grok/GrokPatternService.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323

2424
import java.util.Collection;
2525
import java.util.HashSet;
26+
import java.util.LinkedHashMap;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Optional;
2930
import java.util.Set;
3031
import java.util.regex.Matcher;
32+
import java.util.regex.Pattern;
3133

3234
public interface GrokPatternService {
3335
GrokPattern load(String patternId) throws NotFoundException;
@@ -56,13 +58,24 @@ public interface GrokPatternService {
5658

5759
static Set<String> extractPatternNames(String namedPattern) {
5860
final Set<String> result = new HashSet<>();
59-
final Set<String> namedGroups = GrokUtils.getNameGroups(GrokUtils.GROK_PATTERN.pattern());
60-
final Matcher matcher = GrokUtils.GROK_PATTERN.matcher(namedPattern);
61+
// We have to use java.util.Regex here to get the names because ".find()" on the "com.google.code.regexp.Matcher"
62+
// would run in an endless loop.
63+
final Set<String> namedGroups = GrokUtils.getNameGroups(GrokUtils.GROK_PATTERN.namedPattern());
64+
final Matcher matcher = Pattern.compile(GrokUtils.GROK_PATTERN.namedPattern()).matcher(namedPattern);
6165
while (matcher.find()) {
62-
final Map<String, String> group = GrokUtils.namedGroups(matcher, namedGroups);
66+
final Map<String, String> group = namedGroups(matcher, namedGroups);
6367
final String patternName = group.get("pattern");
6468
result.add(patternName);
6569
}
6670
return result;
6771
}
72+
73+
static Map<String, String> namedGroups(Matcher matcher, Set<String> groupNames) {
74+
Map<String, String> namedGroups = new LinkedHashMap<>();
75+
for (String groupName : groupNames) {
76+
String groupValue = matcher.group(groupName);
77+
namedGroups.put(groupName, groupValue);
78+
}
79+
return namedGroups;
80+
}
6881
}

graylog2-server/src/test/java/org/graylog/plugins/pipelineprocessor/functions/FunctionsSnippetsTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ public static void registerFunctions() {
305305
GrokPattern.create("GREEDY", ".*"),
306306
GrokPattern.create("BASE10NUM", "(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))"),
307307
GrokPattern.create("NUMBER", "(?:%{BASE10NUM:UNWANTED})"),
308+
GrokPattern.create("UNDERSCORE", "(?<test_field>test)"),
308309
GrokPattern.create("NUM", "%{BASE10NUM}")
309310
);
310311
when(grokPatternService.loadAll()).thenReturn(patterns);
@@ -630,11 +631,15 @@ public void grok() {
630631
final Message message = evaluateRule(rule);
631632

632633
assertThat(message).isNotNull();
633-
assertThat(message.getFieldCount()).isEqualTo(5);
634+
assertThat(message.getFieldCount()).isEqualTo(6);
634635
assertThat(message.getTimestamp()).isEqualTo(DateTime.parse("2015-07-31T10:05:36.773Z"));
635636
// named captures only
636637
assertThat(message.hasField("num")).isTrue();
637638
assertThat(message.hasField("BASE10NUM")).isFalse();
639+
640+
// Test for issue 5563 and 5794
641+
// ensure named groups with underscore work
642+
assertThat(message.hasField("test_field")).isTrue();
638643
}
639644

640645
@Test
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* This file is part of Graylog.
3+
*
4+
* Graylog is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog2.grok;
18+
19+
import org.junit.Test;
20+
21+
import java.util.Set;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
public class GrokPatternServiceTest {
26+
@Test
27+
public void extractPatternNames() {
28+
final Set<String> names = GrokPatternService.extractPatternNames("%{EMAILLOCALPART}@%{HOSTNAME}");
29+
30+
assertThat(names).containsOnly("HOSTNAME", "EMAILLOCALPART");
31+
}
32+
}

graylog2-server/src/test/java/org/graylog2/inputs/extractors/GrokExtractorTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,25 @@ public void testIssue4773() throws Exception {
257257
);
258258
}
259259

260+
@Test
261+
public void testIssue5563() {
262+
// See: https://github.com/Graylog2/graylog2-server/issues/5563
263+
// https://github.com/Graylog2/graylog2-server/issues/5704
264+
final Map<String, Object> config = new HashMap<>();
265+
266+
config.put("named_captures_only", true);
267+
268+
patternSet.add(GrokPattern.create("YOLO", "(?<test_field>test)"));
269+
// Make sure that the user can use a capture name with an "_".
270+
final GrokExtractor extractor = makeExtractor("%{YOLO}", config);
271+
272+
assertThat(extractor.run("test"))
273+
.hasSize(1)
274+
.containsOnly(
275+
new Extractor.Result("test", "test_field", -1, -1)
276+
);
277+
}
278+
260279
private GrokExtractor makeExtractor(String pattern) {
261280
return makeExtractor(pattern, new HashMap<>());
262281
}

graylog2-server/src/test/resources/org/graylog/plugins/pipelineprocessor/functions/grok.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ then
77
// only named captures
88
let matches1 = grok("%{NUM:num}", "10", true);
99
set_fields(matches1);
10+
11+
//test for underscore
12+
let matches2 = grok("%{UNDERSCORE}", "test", true);
13+
set_fields(matches2);
1014
end

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<jest.version>2.4.15+jackson</jest.version>
101101
<gelfclient.version>1.4.4</gelfclient.version>
102102
<geoip2.version>2.12.0</geoip2.version>
103-
<grok.version>0.1.9</grok.version>
103+
<grok.version>0.1.9-graylog-1</grok.version>
104104
<guava-retrying.version>2.0.0</guava-retrying.version>
105105
<guava.version>25.1-jre</guava.version>
106106
<guice.version>4.2.0</guice.version>

0 commit comments

Comments
 (0)