Skip to content

Commit

Permalink
[Elastic Agent] Fix issue where inputs without processors defined wou…
Browse files Browse the repository at this point in the history
…ld panic (elastic#21628)

* Fix elastic#21581.

* Add changelog entry.

* Add test for processors as a map.

(cherry picked from commit f3d18f9)
  • Loading branch information
blakerouse committed Oct 12, 2020
1 parent 1192782 commit 6102901
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 4 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Thread safe sorted set {pull}21290[21290]
- Copy Action store on upgrade {pull}21298[21298]
- Include inputs in action store actions {pull}21298[21298]
- Fix issue where inputs without processors defined would panic {pull}21628[21628]

==== New features

Expand Down
11 changes: 7 additions & 4 deletions x-pack/elastic-agent/pkg/agent/application/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,20 @@ func promoteProcessors(dict *transpiler.Dict) *transpiler.Dict {
if p == nil {
return dict
}
var currentList *transpiler.List
current, ok := dict.Find("processors")
currentList, isList := current.Value().(*transpiler.List)
if !isList {
return dict
if ok {
currentList, ok = current.Value().(*transpiler.List)
if !ok {
return dict
}
}
ast, _ := transpiler.NewAST(map[string]interface{}{
"processors": p,
})
procs, _ := transpiler.Lookup(ast, "processors")
nodes := nodesFromList(procs.Value().(*transpiler.List))
if ok {
if ok && currentList != nil {
nodes = append(nodes, nodesFromList(currentList)...)
}
dictNodes := dict.Value().([]transpiler.Node)
Expand Down
175 changes: 175 additions & 0 deletions x-pack/elastic-agent/pkg/agent/application/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,181 @@ func TestRenderInputs(t *testing.T) {
}),
},
},
"inputs without processors and vars with processors": {
input: transpiler.NewKey("inputs", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
transpiler.NewStrVal("/var/log/${var1.name}.log"),
})),
}),
})),
}),
})),
expected: transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
transpiler.NewStrVal("/var/log/value1.log"),
})),
}),
})),
transpiler.NewKey("processors", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("fields", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("custom", transpiler.NewStrVal("value1")),
})),
transpiler.NewKey("to", transpiler.NewStrVal("dynamic")),
})),
}),
})),
}),
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
transpiler.NewStrVal("/var/log/value2.log"),
})),
}),
})),
transpiler.NewKey("processors", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("fields", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("custom", transpiler.NewStrVal("value2")),
})),
transpiler.NewKey("to", transpiler.NewStrVal("dynamic")),
})),
}),
})),
}),
}),
varsArray: []*transpiler.Vars{
mustMakeVarsP(map[string]interface{}{
"var1": map[string]interface{}{
"name": "value1",
},
},
"var1",
[]map[string]interface{}{
{
"add_fields": map[string]interface{}{
"fields": map[string]interface{}{
"custom": "value1",
},
"to": "dynamic",
},
},
}),
mustMakeVarsP(map[string]interface{}{
"var1": map[string]interface{}{
"name": "value2",
},
},
"var1",
[]map[string]interface{}{
{
"add_fields": map[string]interface{}{
"fields": map[string]interface{}{
"custom": "value2",
},
"to": "dynamic",
},
},
}),
},
},
"processors incorrectly a map": {
input: transpiler.NewKey("inputs", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
transpiler.NewStrVal("/var/log/${var1.name}.log"),
})),
}),
})),
transpiler.NewKey("processors", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("invalid", transpiler.NewStrVal("value")),
})),
})),
}),
})),
expected: transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
transpiler.NewStrVal("/var/log/value1.log"),
})),
}),
})),
transpiler.NewKey("processors", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("invalid", transpiler.NewStrVal("value")),
})),
})),
}),
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
transpiler.NewStrVal("/var/log/value2.log"),
})),
}),
})),
transpiler.NewKey("processors", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
transpiler.NewKey("invalid", transpiler.NewStrVal("value")),
})),
})),
}),
}),
varsArray: []*transpiler.Vars{
mustMakeVarsP(map[string]interface{}{
"var1": map[string]interface{}{
"name": "value1",
},
},
"var1",
[]map[string]interface{}{
{
"add_fields": map[string]interface{}{
"fields": map[string]interface{}{
"custom": "value1",
},
"to": "dynamic",
},
},
}),
mustMakeVarsP(map[string]interface{}{
"var1": map[string]interface{}{
"name": "value2",
},
},
"var1",
[]map[string]interface{}{
{
"add_fields": map[string]interface{}{
"fields": map[string]interface{}{
"custom": "value2",
},
"to": "dynamic",
},
},
}),
},
},
}

for name, test := range testcases {
Expand Down

0 comments on commit 6102901

Please sign in to comment.