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

[Elastic Agent] Fix issue where inputs without processors defined would panic #21628

Merged
merged 3 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -214,17 +214,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",
},
},
}),
},
},
blakerouse marked this conversation as resolved.
Show resolved Hide resolved
}

for name, test := range testcases {
Expand Down