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

Validation for Grok patterns #78

Merged
merged 7 commits into from
Dec 16, 2023
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
29 changes: 29 additions & 0 deletions src/Grok.Net.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using GrokNet;
using PCRE;
using Xunit;
Expand Down Expand Up @@ -395,5 +396,33 @@ public void GrokResult_To_Dictionary()
Assert.True(grokResult.ContainsKey("email"));
Assert.True(grokResult.ContainsKey("comment"));
}

[Fact]
public void InvalidPattern_ThrowsException()
{
// Arrange
const string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
06-22-19 22:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";

var sut = new Grok("%{MONTHDA:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}");

// Act & Assert
FormatException exception = Assert.Throws<FormatException>(() => sut.Parse(logs));
Assert.Contains("Invalid Grok pattern: Pattern 'MONTHDA' not found.", exception.Message);
}

[Fact]
public void InvaidPattern_With_Custom_Patterns()
{
// Arrange
const string zipcode = "122001";
const string email = "Bob.Davis@microsoft.com";

var sut = new Grok("%{ZIPCOD:zipcode}:%{EMAILADDRESS:email}", ReadCustomFile());

// Act & Assert
FormatException exception = Assert.Throws<FormatException>(() => sut.Parse($"{zipcode}:{email}"));
Assert.Contains("Invalid Grok pattern: Pattern 'ZIPCOD' not found.", exception.Message);
}
}
}
18 changes: 17 additions & 1 deletion src/Grok.Net/Grok.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -104,6 +104,7 @@ public GrokResult Parse(string text)
{
if (_compiledRegex == null)
{
ValidateGrokPattern();
ParsePattern();
}

Expand Down Expand Up @@ -286,5 +287,20 @@ private string ReplaceWithoutName(PcreMatch match)

return "()";
}

private void ValidateGrokPattern()
{
var grokPatternRegex = new PcreRegex("%\\{([^:}]+)(?::[^}]+)?(?::[^}]+)?\\}");
IEnumerable<PcreMatch> matches = grokPatternRegex.Matches(_grokPattern);

foreach (PcreMatch match in matches)
{
var patternName = match.Groups[1].Value;
if (!_patterns.ContainsKey(patternName))
{
throw new FormatException($"Invalid Grok pattern: Pattern '{patternName}' not found.");
}
}
}
}
}
Loading