-
Notifications
You must be signed in to change notification settings - Fork 17
/
ident.go
423 lines (399 loc) · 9.46 KB
/
ident.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package sigma
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
)
type identType int
func (i identType) String() string {
switch i {
case identKeyword:
return "KEYWORD"
case identSelection:
return "SELECTION"
default:
return "UNK"
}
}
const (
identErr identType = iota
identSelection
identKeyword
)
func checkIdentType(name string, data interface{}) identType {
t := reflectIdentKind(data)
if strings.HasPrefix(name, "keyword") {
if data == nil {
return identKeyword
}
if t != identKeyword {
return identErr
}
}
return t
}
func reflectIdentKind(data interface{}) identType {
switch v := data.(type) {
case map[string]interface{}, map[interface{}]interface{}:
return identSelection
case []interface{}:
k, ok := isSameKind(v)
if !ok {
return identErr
}
switch k {
case reflect.Map:
return identSelection
default:
return identKeyword
}
default:
return identKeyword
}
}
func newRuleFromIdent(rule interface{}, kind identType, noCollapseWS bool) (Branch, error) {
switch kind {
case identKeyword:
return NewKeyword(rule, noCollapseWS)
case identSelection:
return NewSelectionBranch(rule, noCollapseWS)
}
return nil, fmt.Errorf("unknown rule kind, should be keyword or selection")
}
// Keyword is a container for patterns joined by logical disjunction
type Keyword struct {
S StringMatcher
stats
}
// Match implements Matcher
func (k Keyword) Match(msg Event) (bool, bool) {
msgs, ok := msg.Keywords()
if !ok {
return false, false
}
for _, m := range msgs {
if k.S.StringMatch(m) {
return true, true
}
}
return false, true
}
func NewKeyword(expr interface{}, noCollapseWS bool) (*Keyword, error) {
switch val := expr.(type) {
case []string:
return newStringKeyword(TextPatternKeyword, false, noCollapseWS, val...)
case []interface{}:
k, ok := isSameKind(val)
if !ok {
return nil, ErrInvalidKind{
Kind: reflect.Array,
T: identKeyword,
Critical: false,
Msg: "mixed type slice",
}
}
switch v := k; {
case v == reflect.String:
return newStringKeyword(TextPatternKeyword, false, noCollapseWS, castIfaceToString(val)...)
default:
return nil, ErrInvalidKind{
Kind: v,
T: identKeyword,
Critical: false,
Msg: "unsupported data type",
}
}
default:
// TODO
return nil, ErrInvalidKeywordConstruct{Expr: expr}
}
}
func newStringKeyword(mod TextPatternModifier, lower, noCollapseWS bool, patterns ...string) (*Keyword, error) {
matcher, err := NewStringMatcher(mod, lower, false, noCollapseWS, patterns...)
if err != nil {
return nil, err
}
return &Keyword{S: matcher}, nil
}
type SelectionNumItem struct {
Key string
Pattern NumMatcher
}
type SelectionStringItem struct {
Key string
Pattern StringMatcher
}
type Selection struct {
N []SelectionNumItem
S []SelectionStringItem
stats
}
// Match implements Matcher
// TODO - numeric and boolean pattern match
func (s Selection) Match(msg Event) (bool, bool) {
for _, v := range s.N {
val, ok := msg.Select(v.Key)
if !ok {
return false, false
}
switch vt := val.(type) {
case string:
n, err := strconv.Atoi(vt)
if err != nil {
// TODO - better debugging
return false, true
}
if !v.Pattern.NumMatch(n) {
return false, true
}
case json.Number:
n, err := vt.Int64()
if err != nil {
// TODO - better debugging
return false, true
}
if !v.Pattern.NumMatch(int(n)) {
return false, true
}
case float64:
// JSON numbers are all by spec float64 values
if !v.Pattern.NumMatch(int(vt)) {
return false, true
}
case int:
// JSON numbers are all by spec float64 values
if !v.Pattern.NumMatch(vt) {
return false, true
}
case int64:
// JSON numbers are all by spec float64 values
if !v.Pattern.NumMatch(int(vt)) {
return false, true
}
case int32:
// JSON numbers are all by spec float64 values
if !v.Pattern.NumMatch(int(vt)) {
return false, true
}
case uint:
// JSON numbers are all by spec float64 values
if !v.Pattern.NumMatch(int(vt)) {
return false, true
}
case uint32:
// JSON numbers are all by spec float64 values
if !v.Pattern.NumMatch(int(vt)) {
return false, true
}
case uint64:
// JSON numbers are all by spec float64 values
if !v.Pattern.NumMatch(int(vt)) {
return false, true
}
}
}
for _, v := range s.S {
val, ok := msg.Select(v.Key)
if !ok {
return false, false
}
switch vt := val.(type) {
case string:
if !v.Pattern.StringMatch(vt) {
return false, true
}
case json.Number:
if !v.Pattern.StringMatch(vt.String()) {
return false, true
}
case float64:
// TODO - tmp hack that also loses floating point accuracy
if !v.Pattern.StringMatch(strconv.Itoa(int(vt))) {
return false, true
}
default:
s.incrementMismatchCount()
return false, true
}
}
return true, true
}
func (s *Selection) incrementMismatchCount() *Selection {
s.stats.TypeMismatchCount++
return s
}
func newSelectionFromMap(expr map[string]interface{}, noCollapseWS bool) (*Selection, error) {
sel := &Selection{S: make([]SelectionStringItem, 0)}
for key, pattern := range expr {
var mod TextPatternModifier
var all bool
if strings.Contains(key, "|") {
bits := strings.Split(key, "|")
// allow support for longer chaining later on; simplifies specifier validation as well (I think)
for _, curBit := range bits[1:] {
// excepting 'all', the supported modifiers are mutually exclusive; last one wins
switch curBit {
case "startswith":
mod = TextPatternPrefix
case "endswith":
mod = TextPatternSuffix
case "re":
mod = TextPatternRegex // this is really a type, not a transformation per spec
case "contains":
mod = TextPatternContains
case "all":
all = true
default:
return nil, fmt.Errorf("selection key %s specifier %s invalid",
key, curBit)
}
}
// strip off the specifier from the key so we can look it up correctly
key = bits[0]
}
switch pat := pattern.(type) {
case string:
m, err := NewStringMatcher(mod, false, all, noCollapseWS, pat)
if err != nil {
return nil, err
}
sel.S = append(sel.S, SelectionStringItem{Key: key, Pattern: m})
case int:
m, err := NewNumMatcher(pat)
if err != nil {
return nil, err
}
sel.N = func() []SelectionNumItem {
item := SelectionNumItem{
Key: key, Pattern: m,
}
if sel.N == nil {
sel.N = []SelectionNumItem{item}
}
return append(sel.N, item)
}()
case []interface{}:
// TODO - move this part to separate function and reuse in NewKeyword
k, ok := isSameKind(pat)
if !ok {
return nil, ErrInvalidKind{
Kind: reflect.Array,
T: identKeyword,
Critical: false,
Msg: "mixed type slice",
}
}
switch k {
case reflect.String:
m, err := NewStringMatcher(mod, false, all, noCollapseWS, castIfaceToString(pat)...)
if err != nil {
return nil, err
}
sel.S = append(sel.S, SelectionStringItem{Key: key, Pattern: m})
case reflect.Int:
m, err := NewNumMatcher(castIfaceToInt(pat)...)
if err != nil {
return nil, err
}
sel.N = func() []SelectionNumItem {
item := SelectionNumItem{
Key: key, Pattern: m,
}
if sel.N == nil {
sel.N = []SelectionNumItem{item}
}
return append(sel.N, item)
}()
default:
return nil, ErrInvalidKind{
Kind: k,
T: identKeyword,
Critical: false,
Msg: "unsupported data type",
}
}
default:
if t := reflect.TypeOf(pattern); t != nil {
return nil, ErrInvalidKind{
Kind: t.Kind(),
T: identSelection,
Critical: true,
Msg: "unsupported selection value",
}
}
return nil, ErrUnableToReflect
}
}
return sel, nil
}
func NewSelectionBranch(expr interface{}, noCollapseWS bool) (Branch, error) {
switch v := expr.(type) {
case []interface{}:
selections := make([]Branch, 0)
for _, item := range v {
b, err := NewSelectionBranch(item, noCollapseWS)
if err != nil {
return nil, err
}
selections = append(selections, b)
}
return NodeSimpleOr(selections).Reduce(), nil
case map[interface{}]interface{}:
return newSelectionFromMap(cleanUpInterfaceMap(v), noCollapseWS)
default:
return nil, ErrInvalidKind{
Kind: reflect.TypeOf(expr).Kind(),
T: identSelection,
Critical: true,
Msg: "unsupported selection root container",
}
}
}
func isSameKind(data []interface{}) (reflect.Kind, bool) {
var current, last reflect.Kind
for i, d := range data {
cType := reflect.TypeOf(d)
if cType == nil {
return reflect.Invalid, false
}
current = cType.Kind()
if i > 0 {
if current != last {
return current, false
}
}
last = current
}
return current, true
}
func castIfaceToString(items []interface{}) []string {
tx := make([]string, 0)
for _, val := range items {
tx = append(tx, fmt.Sprintf("%v", val))
}
return tx
}
func castIfaceToInt(items []interface{}) []int {
tx := make([]int, 0)
for _, val := range items {
if n, ok := val.(int); ok {
tx = append(tx, n)
}
}
return tx
}
// Yaml can have non-string keys, so go-yaml unmarshals to map[interface{}]interface{}
// really annoying
func cleanUpInterfaceMap(rx map[interface{}]interface{}) map[string]interface{} {
tx := make(map[string]interface{})
for k, v := range rx {
tx[fmt.Sprintf("%v", k)] = v
}
return tx
}
// stats holds various rule statistics
type stats struct {
TypeMismatchCount uint64
}