Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #16348 to 7.x: Fix loading processors from autodiscover hints #16391

Merged
merged 3 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Remove superfluous use of number_of_routing_shards setting from the default template. {pull}16038[16038]
- Fix index names for indexing not always guaranteed to be lower case. {pull}16081[16081]
- Upgrade go-ucfg to latest v0.8.1. {pull}15937{15937}
- 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]
blakerouse marked this conversation as resolved.
Show resolved Hide resolved
- Fix loading processors from annotation hints. {pull}16348[16348]

*Auditbeat*

Expand Down
15 changes: 14 additions & 1 deletion libbeat/autodiscover/builder/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion libbeat/autodiscover/builder/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"))
}
}