Skip to content

Commit

Permalink
Validation for Grok patterns (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFromRVA authored Dec 16, 2023
1 parent 81042e9 commit 819f4dd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
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.");
}
}
}
}
}

0 comments on commit 819f4dd

Please sign in to comment.