Skip to content

Commit

Permalink
fix: check all matches when looking for rule IDs (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
theseion authored Jun 13, 2024
1 parent f0c2ebc commit ab6cd4b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
21 changes: 14 additions & 7 deletions waflog/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ func (ll *FTWLogLines) TriggeredRules() []uint {
ruleIds := []uint{}
for _, line := range lines {
log.Trace().Msgf("ftw/waflog: Looking for any rule in %s", line)
match := regex.FindSubmatch(line)
match := regex.FindAllSubmatch(line, -1)
if match != nil {
log.Trace().Msgf("ftw/waflog: Found %s at %s", regex.String(), line)
submatch := string(match[1])
ruleId, err := strconv.ParseUint(submatch, 10, 0)
if err != nil {
log.Error().Msgf("Failed to parse uint from %s", submatch)
continue
for _, nextMatch := range match {
for _, submatchBytes := range nextMatch {
if len(submatchBytes) == 0 {
continue
}
submatch := string(submatchBytes)
ruleId, err := strconv.ParseUint(submatch, 10, 0)
if err != nil {
log.Error().Caller().Msgf("Failed to parse uint from %s", submatch)
continue
}
ruleIds = append(ruleIds, uint(ruleId))
}
}
ruleIds = append(ruleIds, uint(ruleId))
}
}
ll.triggeredRules = ruleIds
Expand Down
33 changes: 33 additions & 0 deletions waflog/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,36 @@ func (s *readTestSuite) TestFTWLogLines_CheckForLogMarkerIn404() {
foundMarker := ll.CheckLogForMarker(stageID, 100)
s.Equal(strings.ToLower(markerLine), strings.ToLower(string(foundMarker)))
}

func (s *readTestSuite) TestFindAllIdsInLogs() {
cfg, err := config.NewConfigFromEnv()
s.Require().NoError(err)
s.NotNil(cfg)

stageID := "dead-beaf-deadbeef-deadbeef-dead"
markerLine := "X-cRs-TeSt: " + stageID
logLines := fmt.Sprint("\n", markerLine,
`[id "1"] something else [id "2"]`,
`"id": 3, something else {"id":4}`+"\n",
"\n", markerLine)
filename, err := utils.CreateTempFileWithContent(logLines, "test-errorlog-")
s.Require().NoError(err)

cfg.LogFile = filename
log, err := os.Open(filename)
s.Require().NoError(err)

ll := &FTWLogLines{
logFile: log,
LogMarkerHeaderName: bytes.ToLower([]byte(cfg.LogMarkerHeaderName)),
}
ll.WithStartMarker([]byte(markerLine))
ll.WithEndMarker([]byte(markerLine))

foundRuleIds := ll.TriggeredRules()
s.Len(foundRuleIds, 4)
s.Contains(foundRuleIds, uint(1))
s.Contains(foundRuleIds, uint(2))
s.Contains(foundRuleIds, uint(3))
s.Contains(foundRuleIds, uint(4))
}

0 comments on commit ab6cd4b

Please sign in to comment.