From e33c95507274c3984b26e1afe163e33746ebcd69 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Fri, 14 Feb 2020 14:56:49 -0500 Subject: [PATCH 1/3] Parse processors from hints as JSON. --- libbeat/autodiscover/builder/helper.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libbeat/autodiscover/builder/helper.go b/libbeat/autodiscover/builder/helper.go index e9c00abb340..91fe093acea 100644 --- a/libbeat/autodiscover/builder/helper.go +++ b/libbeat/autodiscover/builder/helper.go @@ -86,7 +86,20 @@ func GetHintAsList(hints common.MapStr, key, config string) []string { // GetProcessors gets processor definitions from the hints and returns a list of configs as a MapStr func GetProcessors(hints common.MapStr, key string) []common.MapStr { - return GetConfigs(hints, key, "processors") + processors := GetConfigs(hints, key, "processors") + for _, proc := range processors { + for key, value := range proc { + if str, ok := value.(string); ok { + cfg := common.MapStr{} + if err := json.Unmarshal([]byte(str), &cfg); err != nil { + logp.Debug("autodiscover.builder", "unable to unmarshal json due to error: %v", err) + continue + } + proc[key] = cfg + } + } + } + return processors } // GetConfigs takes in a key and returns a list of configs as a slice of MapStr From e5023dbc08e22f93876664298839463b00075707 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Fri, 14 Feb 2020 15:25:17 -0500 Subject: [PATCH 2/3] Add test. --- libbeat/autodiscover/builder/helper_test.go | 27 ++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/libbeat/autodiscover/builder/helper_test.go b/libbeat/autodiscover/builder/helper_test.go index e64fe548415..530f1147a8c 100644 --- a/libbeat/autodiscover/builder/helper_test.go +++ b/libbeat/autodiscover/builder/helper_test.go @@ -25,6 +25,30 @@ import ( "github.com/elastic/beats/libbeat/common" ) +func TestGetProcessors(t *testing.T) { + hints := common.MapStr{ + "co": common.MapStr{ + "elastic": common.MapStr{ + "logs": common.MapStr{ + "processors": common.MapStr{ + "add_fields": `{"fields": {"foo": "bar"}}`, + }, + }, + }, + }, + } + procs := GetProcessors(hints, "co.elastic.logs") + assert.Equal(t, []common.MapStr{ + common.MapStr{ + "add_fields": common.MapStr{ + "fields": map[string]interface{}{ + "foo": "bar", + }, + }, + }, + }, procs) +} + func TestGenerateHints(t *testing.T) { tests := []struct { annotations map[string]string @@ -38,6 +62,7 @@ func TestGenerateHints(t *testing.T) { // Scenarios being tested: // logs/multiline.pattern must be a nested common.MapStr under hints.logs + // logs/processors.add_fields must be nested common.MapStr under hints.logs // logs/json.keys_under_root must be a nested common.MapStr under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints @@ -191,6 +216,6 @@ func TestGenerateHints(t *testing.T) { for k, v := range test.annotations { annMap.Put(k, v) } - assert.Equal(t, GenerateHints(annMap, "foobar", "co.elastic"), test.result) + assert.Equal(t, test.result, GenerateHints(annMap, "foobar", "co.elastic")) } } From 4f96ff21f9e26861c9be40635c98b078dd85be10 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Fri, 14 Feb 2020 15:34:48 -0500 Subject: [PATCH 3/3] Add changelog entry. --- CHANGELOG.next.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 782f8090243..26cd8babbdc 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -56,6 +56,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Upgrade go-ucfg to latest v0.8.1. {pull}15937{15937} - Fix index names for indexing not always guaranteed to be lower case. {pull}16081[16081] - Add `ssl.ca_sha256` option to the supported TLS option, this allow to check that a specific certificate is used as part of the verified chain. {issue}15717[15717] +- Fix loading processors from annotation hints. {pull}16348[16348] *Auditbeat*