@@ -16,16 +16,19 @@ import (
16
16
"github.com/rs/zerolog/log"
17
17
)
18
18
19
+ const maxRuleIdsEstimate = 15
20
+
21
+ var ruleIdsSet = make (map [uint ]bool , maxRuleIdsEstimate )
22
+
19
23
// TriggeredRules returns the IDs of all the rules found in the log for the current test
20
24
func (ll * FTWLogLines ) TriggeredRules () []uint {
21
- if ll .triggeredRules != nil {
25
+ if ll .triggeredRulesInitialized {
22
26
return ll .triggeredRules
23
27
}
28
+ ll .triggeredRulesInitialized = true
24
29
25
30
lines := ll .getMarkedLines ()
26
-
27
31
regex := regexp .MustCompile (`\[id "(\d+)"\]|"id":\s*"?(\d+)"?` )
28
- ruleIds := []uint {}
29
32
for _ , line := range lines {
30
33
log .Trace ().Msgf ("ftw/waflog: Looking for any rule in %s" , line )
31
34
match := regex .FindAllSubmatch (line , - 1 )
@@ -42,13 +45,21 @@ func (ll *FTWLogLines) TriggeredRules() []uint {
42
45
log .Error ().Caller ().Msgf ("Failed to parse uint from %s" , submatch )
43
46
continue
44
47
}
45
- ruleIds = append ( ruleIds , uint (ruleId ))
48
+ ruleIdsSet [ uint (ruleId )] = true
46
49
}
47
50
}
48
51
}
49
52
}
53
+ ruleIds := make ([]uint , 0 , len (ruleIdsSet ))
54
+ for ruleId := range ruleIdsSet {
55
+ ruleIds = append (ruleIds , ruleId )
56
+ }
50
57
ll .triggeredRules = ruleIds
51
- return ruleIds
58
+ // Reset map for next use
59
+ for key := range ruleIdsSet {
60
+ delete (ruleIdsSet , key )
61
+ }
62
+ return ll .triggeredRules
52
63
}
53
64
54
65
// ContainsAllIds returns true if all of the specified rule IDs appear in the log for the current test.
@@ -106,20 +117,19 @@ func (ll *FTWLogLines) MatchesRegex(pattern string) bool {
106
117
}
107
118
108
119
func (ll * FTWLogLines ) getMarkedLines () [][]byte {
109
- if ll .markedLines != nil {
120
+ if ll .markedLinesInitialized {
110
121
return ll .markedLines
111
122
}
123
+ ll .markedLinesInitialized = true
112
124
113
125
if ll .startMarker == nil || ll .endMarker == nil {
114
126
log .Fatal ().Msg ("Both start and end marker must be set before the log can be inspected" )
115
127
}
116
128
117
- var found [][]byte
118
-
119
129
fi , err := ll .logFile .Stat ()
120
130
if err != nil {
121
131
log .Error ().Caller ().Msgf ("cannot read file's size" )
122
- return found
132
+ return ll . markedLines
123
133
}
124
134
125
135
// Lines in modsec logging can be quite large
@@ -149,11 +159,10 @@ func (ll *FTWLogLines) getMarkedLines() [][]byte {
149
159
150
160
saneCopy := make ([]byte , len (line ))
151
161
copy (saneCopy , line )
152
- found = append (found , saneCopy )
162
+ ll . markedLines = append (ll . markedLines , saneCopy )
153
163
}
154
164
155
- ll .markedLines = found
156
- return found
165
+ return ll .markedLines
157
166
}
158
167
159
168
// CheckLogForMarker reads the log file and searches for a marker line.
0 commit comments