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 FortiGate regex parsing fields #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
* @see <a href="http://help.fortinet.com/fos50hlp/54/Content/FortiOS/fortigate-logging-reporting-54/logs.htm#Log_messages">FortiGate logging and reporting overview</a>
*/
public class FortiGateSyslogEvent implements SyslogServerEventIF {
private static final Pattern PRI_PATTERN = Pattern.compile("^<(\\d{1,3})>(.*)$");
private static final Pattern KV_PATTERN = Pattern.compile("(\\w+)=([^\\s\"]*)");
private static final Pattern QUOTED_KV_PATTERN = Pattern.compile("(\\w+)=\"([^\"]*)\"");
private static final Pattern PRI_PATTERN = Pattern.compile("^<(\\d{1,3})>(.*)");
private static final Pattern KV_PATTERN = Pattern.compile("(\\w+)=(?:\"([^\"]*)\"|([^\\s\"]*))");

private String rawEvent;
private ZoneId defaultZoneId;
Expand Down Expand Up @@ -60,7 +59,7 @@ private void initialize(final String rawEvent, DateTimeZone sysLogServerTimeZone
private void parse(String event) {
final Matcher matcher = PRI_PATTERN.matcher(event);
if (!matcher.find()) {
throw new IllegalArgumentException("Invalid Fortigate syslog message");
throw new IllegalArgumentException("Invalid Fortigate syslog message: " + event);
} else {
final String priority = matcher.group(1);
final String message = matcher.group(2);
Expand All @@ -87,11 +86,7 @@ private void parseFields(String event) {
final Map<String, String> fields = new HashMap<>();
final Matcher matcher = KV_PATTERN.matcher(event);
while (matcher.find()) {
fields.put(matcher.group(1), matcher.group(2));
}
final Matcher quotedMatcher = QUOTED_KV_PATTERN.matcher(event);
while (quotedMatcher.find()) {
fields.put(quotedMatcher.group(1), quotedMatcher.group(2));
fields.put(matcher.group(1), matcher.group(2) != null ? matcher.group(2) : matcher.group(3));
}
setFields(fields);
}
Expand Down