forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
condition.go
494 lines (409 loc) · 8.93 KB
/
condition.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package processors
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/match"
"github.com/elastic/beats/libbeat/logp"
)
type RangeValue struct {
gte *float64
gt *float64
lte *float64
lt *float64
}
type EqualsValue struct {
Int uint64
Str string
}
type Condition struct {
equals map[string]EqualsValue
matches struct {
name string
filters map[string]match.Matcher
}
rangexp map[string]RangeValue
or []Condition
and []Condition
not *Condition
}
type WhenProcessor struct {
condition *Condition
p Processor
}
func NewConditional(
ruleFactory Constructor,
) Constructor {
return func(cfg common.Config) (Processor, error) {
rule, err := ruleFactory(cfg)
if err != nil {
return nil, err
}
return addCondition(cfg, rule)
}
}
func NewCondition(config *ConditionConfig) (*Condition, error) {
c := Condition{}
if config == nil {
// empty condition
return nil, nil
}
var err error
switch {
case config.Equals != nil:
err = c.setEquals(config.Equals)
case config.Contains != nil:
c.matches.name = "contains"
c.matches.filters, err = compileMatches(config.Contains.fields, match.CompileString)
case config.Regexp != nil:
c.matches.name = "regexp"
c.matches.filters, err = compileMatches(config.Regexp.fields, match.Compile)
case config.Range != nil:
err = c.setRange(config.Range)
case len(config.OR) > 0:
c.or, err = NewConditionList(config.OR)
case len(config.AND) > 0:
c.and, err = NewConditionList(config.AND)
case config.NOT != nil:
c.not, err = NewCondition(config.NOT)
default:
err = errors.New("missing condition")
}
if err != nil {
return nil, err
}
logp.Debug("processors", "New condition %s", c)
return &c, nil
}
func NewConditionList(config []ConditionConfig) ([]Condition, error) {
out := make([]Condition, len(config))
for i, condConfig := range config {
cond, err := NewCondition(&condConfig)
if err != nil {
return nil, err
}
out[i] = *cond
}
return out, nil
}
func (c *Condition) setEquals(cfg *ConditionFields) error {
c.equals = map[string]EqualsValue{}
for field, value := range cfg.fields {
uintValue, err := extractInt(value)
if err == nil {
c.equals[field] = EqualsValue{Int: uintValue}
} else {
sValue, err := extractString(value)
if err != nil {
return err
}
c.equals[field] = EqualsValue{Str: sValue}
}
}
return nil
}
func compileMatches(
fields map[string]interface{},
compile func(string) (match.Matcher, error),
) (map[string]match.Matcher, error) {
if len(fields) == 0 {
return nil, nil
}
out := map[string]match.Matcher{}
for field, value := range fields {
var err error
switch v := value.(type) {
case string:
out[field], err = compile(v)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unexpected type %T of %v", value, value)
}
}
return out, nil
}
func (c *Condition) setRange(cfg *ConditionFields) error {
c.rangexp = map[string]RangeValue{}
updateRangeValue := func(key string, op string, value float64) error {
field := strings.TrimSuffix(key, "."+op)
_, exists := c.rangexp[field]
if !exists {
c.rangexp[field] = RangeValue{}
}
rv := c.rangexp[field]
switch op {
case "gte":
rv.gte = &value
case "gt":
rv.gt = &value
case "lt":
rv.lt = &value
case "lte":
rv.lte = &value
default:
return fmt.Errorf("unexpected field %s", op)
}
c.rangexp[field] = rv
return nil
}
for key, value := range cfg.fields {
floatValue, err := extractFloat(value)
if err != nil {
return err
}
list := strings.Split(key, ".")
err = updateRangeValue(key, list[len(list)-1], floatValue)
if err != nil {
return err
}
}
return nil
}
func (c *Condition) Check(event common.MapStr) bool {
if len(c.or) > 0 {
return c.checkOR(event)
}
if len(c.and) > 0 {
return c.checkAND(event)
}
if c.not != nil {
return c.checkNOT(event)
}
return c.checkEquals(event) &&
c.checkMatches(event) &&
c.checkRange(event)
}
func (c *Condition) checkEquals(event common.MapStr) bool {
for field, equalValue := range c.equals {
value, err := event.GetValue(field)
if err != nil {
return false
}
intValue, err := extractInt(value)
if err == nil {
if intValue != equalValue.Int {
return false
}
} else {
sValue, err := extractString(value)
if err != nil {
logp.Warn("unexpected type %T in equals condition as it accepts only integers and strings. ", value)
return false
}
if sValue != equalValue.Str {
return false
}
}
}
return true
}
func (c *Condition) checkMatches(event common.MapStr) bool {
matchers := c.matches.filters
if matchers == nil {
return true
}
for field, matcher := range matchers {
value, err := event.GetValue(field)
if err != nil {
return false
}
switch v := value.(type) {
case string:
if !matcher.MatchString(v) {
return false
}
case []string:
if !matcher.MatchAnyString(v) {
return false
}
default:
str, err := extractString(value)
if err != nil {
logp.Warn("unexpected type %T in %v condition as it accepts only strings.", value, c.matches.name)
return false
}
if !matcher.MatchString(str) {
return false
}
}
}
return true
}
func (c *Condition) checkRange(event common.MapStr) bool {
checkValue := func(value float64, rangeValue RangeValue) bool {
if rangeValue.gte != nil {
if value < *rangeValue.gte {
return false
}
}
if rangeValue.gt != nil {
if value <= *rangeValue.gt {
return false
}
}
if rangeValue.lte != nil {
if value > *rangeValue.lte {
return false
}
}
if rangeValue.lt != nil {
if value >= *rangeValue.lt {
return false
}
}
return true
}
for field, rangeValue := range c.rangexp {
value, err := event.GetValue(field)
if err != nil {
return false
}
switch value.(type) {
case int, int8, int16, int32, int64:
intValue := reflect.ValueOf(value).Int()
if !checkValue(float64(intValue), rangeValue) {
return false
}
case uint, uint8, uint16, uint32, uint64:
uintValue := reflect.ValueOf(value).Uint()
if !checkValue(float64(uintValue), rangeValue) {
return false
}
case float64, float32, common.Float:
floatValue := reflect.ValueOf(value).Float()
if !checkValue(floatValue, rangeValue) {
return false
}
default:
logp.Warn("unexpected type %T in range condition. ", value)
return false
}
}
return true
}
func (c *Condition) checkOR(event common.MapStr) bool {
for _, cond := range c.or {
if cond.Check(event) {
return true
}
}
return false
}
func (c *Condition) checkAND(event common.MapStr) bool {
for _, cond := range c.and {
if !cond.Check(event) {
return false
}
}
return true
}
func (c *Condition) checkNOT(event common.MapStr) bool {
if c.not.Check(event) {
return false
}
return true
}
func (c Condition) String() string {
s := ""
if len(c.equals) > 0 {
s = s + fmt.Sprintf("equals: %v", c.equals)
}
if len(c.matches.filters) > 0 {
s = s + fmt.Sprintf("%v: %v", c.matches.name, c.matches.filters)
}
if len(c.rangexp) > 0 {
s = s + fmt.Sprintf("range: %v", c.rangexp)
}
if len(c.or) > 0 {
for _, cond := range c.or {
s = s + cond.String() + " or "
}
s = s[:len(s)-len(" or ")] //delete the last or
}
if len(c.and) > 0 {
for _, cond := range c.and {
s = s + cond.String() + " and "
}
s = s[:len(s)-len(" and ")] //delete the last and
}
if c.not != nil {
s = s + "not " + c.not.String()
}
return s
}
func (r RangeValue) String() string {
s := ""
if r.gte != nil {
s = s + fmt.Sprintf(">= %v", *r.gte)
}
if r.gt != nil {
if len(s) > 0 {
s = s + " and "
}
s = s + fmt.Sprintf("> %v", *r.gt)
}
if r.lte != nil {
if len(s) > 0 {
s = s + " and "
}
s = s + fmt.Sprintf("<= %v", *r.lte)
}
if r.lt != nil {
if len(s) > 0 {
s = s + " and "
}
s = s + fmt.Sprintf("< %v", *r.lt)
}
return s
}
func (e EqualsValue) String() string {
if len(e.Str) > 0 {
return e.Str
}
return strconv.Itoa(int(e.Int))
}
func NewConditionRule(
config ConditionConfig,
p Processor,
) (Processor, error) {
cond, err := NewCondition(&config)
if err != nil {
logp.Err("Failed to initialize lookup condition: %v", err)
return nil, err
}
if cond == nil {
return p, nil
}
return &WhenProcessor{cond, p}, nil
}
func (r *WhenProcessor) Run(event common.MapStr) (common.MapStr, error) {
if !r.condition.Check(event) {
return event, nil
}
return r.p.Run(event)
}
func (r *WhenProcessor) String() string {
return fmt.Sprintf("%v, condition=%v", r.p.String(), r.condition.String())
}
func addCondition(
cfg common.Config,
p Processor,
) (Processor, error) {
if !cfg.HasField("when") {
return p, nil
}
sub, err := cfg.Child("when", -1)
if err != nil {
return nil, err
}
condConfig := ConditionConfig{}
if err := sub.Unpack(&condConfig); err != nil {
return nil, err
}
return NewConditionRule(condConfig, p)
}