diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 653b82e8aeb..84764a7ccec 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -186,6 +186,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di *Packetbeat* - Configure good defaults for `add_kubernetes_metadata`. {pull}5707[5707] +- Add support for condition on bool type {issue}5659[5659] {pull}5954[5954] *Winlogbeat* diff --git a/libbeat/processors/condition.go b/libbeat/processors/condition.go index 4e8d28b5355..fc3e0122eeb 100644 --- a/libbeat/processors/condition.go +++ b/libbeat/processors/condition.go @@ -21,8 +21,9 @@ type RangeValue struct { } type EqualsValue struct { - Int uint64 - Str string + Int uint64 + Str string + Bool bool } type Condition struct { @@ -118,13 +119,22 @@ func (c *Condition) setEquals(cfg *ConditionFields) error { uintValue, err := extractInt(value) if err == nil { c.equals[field] = EqualsValue{Int: uintValue} - } else { - sValue, err := extractString(value) - if err != nil { - return err - } + continue + } + + sValue, err := extractString(value) + if err == nil { c.equals[field] = EqualsValue{Str: sValue} + continue + } + + bValue, err := extractBool(value) + if err == nil { + c.equals[field] = EqualsValue{Bool: bValue} + continue } + + return fmt.Errorf("unexpected type %T in equals condition", value) } return nil @@ -257,16 +267,30 @@ func (c *Condition) checkEquals(event ValuesMap) bool { 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) + + continue + } + + sValue, err := extractString(value) + if err == nil { + if sValue != equalValue.Str { return false } - if sValue != equalValue.Str { + + continue + } + + bValue, err := extractBool(value) + if err == nil { + if bValue != equalValue.Bool { return false } + + continue } + + logp.Err("unexpected type %T in equals condition as it accepts only integers, strings or bools. ", value) + return false } return true diff --git a/libbeat/processors/condition_test.go b/libbeat/processors/condition_test.go index 22d19dbdfef..9570228571e 100644 --- a/libbeat/processors/condition_test.go +++ b/libbeat/processors/condition_test.go @@ -129,6 +129,22 @@ func TestCondition(t *testing.T) { }, result: true, }, + { + config: ConditionConfig{ + Equals: &ConditionFields{fields: map[string]interface{}{ + "final": true, + }}, + }, + result: false, + }, + { + config: ConditionConfig{ + Equals: &ConditionFields{fields: map[string]interface{}{ + "final": false, + }}, + }, + result: true, + }, } event := &beat.Event{ @@ -150,7 +166,8 @@ func TestCondition(t *testing.T) { "username": "monica", "keywords": []string{"foo", "bar"}, }, - "type": "process", + "type": "process", + "final": false, }, } diff --git a/libbeat/processors/config.go b/libbeat/processors/config.go index 4d59b188d83..a11038acb33 100644 --- a/libbeat/processors/config.go +++ b/libbeat/processors/config.go @@ -130,3 +130,12 @@ func extractString(unk interface{}) (string, error) { return "", fmt.Errorf("unknown type %T passed to extractString", unk) } } + +func extractBool(unk interface{}) (bool, error) { + switch b := unk.(type) { + case bool: + return b, nil + default: + return false, fmt.Errorf("unknown type %T passed to extractBool", unk) + } +} diff --git a/libbeat/processors/config_test.go b/libbeat/processors/config_test.go index 853152184f2..f8eb23c5f52 100644 --- a/libbeat/processors/config_test.go +++ b/libbeat/processors/config_test.go @@ -15,3 +15,13 @@ func TestExtractString(t *testing.T) { } assert.Equal(t, input, v) } + +func TestExtractBool(t *testing.T) { + input := true + + v, err := extractBool(input) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, input, v) +}