Skip to content

Commit f3d18f9

Browse files
authored
[Elastic Agent] Fix issue where inputs without processors defined would panic (#21628)
* Fix #21581. * Add changelog entry. * Add test for processors as a map.
1 parent d35dfb5 commit f3d18f9

3 files changed

Lines changed: 183 additions & 4 deletions

File tree

x-pack/elastic-agent/CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Thread safe sorted set {pull}21290[21290]
1515
- Copy Action store on upgrade {pull}21298[21298]
1616
- Include inputs in action store actions {pull}21298[21298]
17+
- Fix issue where inputs without processors defined would panic {pull}21628[21628]
1718

1819
==== New features
1920

x-pack/elastic-agent/pkg/agent/application/emitter.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,20 @@ func promoteProcessors(dict *transpiler.Dict) *transpiler.Dict {
219219
if p == nil {
220220
return dict
221221
}
222+
var currentList *transpiler.List
222223
current, ok := dict.Find("processors")
223-
currentList, isList := current.Value().(*transpiler.List)
224-
if !isList {
225-
return dict
224+
if ok {
225+
currentList, ok = current.Value().(*transpiler.List)
226+
if !ok {
227+
return dict
228+
}
226229
}
227230
ast, _ := transpiler.NewAST(map[string]interface{}{
228231
"processors": p,
229232
})
230233
procs, _ := transpiler.Lookup(ast, "processors")
231234
nodes := nodesFromList(procs.Value().(*transpiler.List))
232-
if ok {
235+
if ok && currentList != nil {
233236
nodes = append(nodes, nodesFromList(currentList)...)
234237
}
235238
dictNodes := dict.Value().([]transpiler.Node)

x-pack/elastic-agent/pkg/agent/application/emitter_test.go

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,181 @@ func TestRenderInputs(t *testing.T) {
479479
}),
480480
},
481481
},
482+
"inputs without processors and vars with processors": {
483+
input: transpiler.NewKey("inputs", transpiler.NewList([]transpiler.Node{
484+
transpiler.NewDict([]transpiler.Node{
485+
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
486+
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
487+
transpiler.NewDict([]transpiler.Node{
488+
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
489+
transpiler.NewStrVal("/var/log/${var1.name}.log"),
490+
})),
491+
}),
492+
})),
493+
}),
494+
})),
495+
expected: transpiler.NewList([]transpiler.Node{
496+
transpiler.NewDict([]transpiler.Node{
497+
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
498+
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
499+
transpiler.NewDict([]transpiler.Node{
500+
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
501+
transpiler.NewStrVal("/var/log/value1.log"),
502+
})),
503+
}),
504+
})),
505+
transpiler.NewKey("processors", transpiler.NewList([]transpiler.Node{
506+
transpiler.NewDict([]transpiler.Node{
507+
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
508+
transpiler.NewKey("fields", transpiler.NewDict([]transpiler.Node{
509+
transpiler.NewKey("custom", transpiler.NewStrVal("value1")),
510+
})),
511+
transpiler.NewKey("to", transpiler.NewStrVal("dynamic")),
512+
})),
513+
}),
514+
})),
515+
}),
516+
transpiler.NewDict([]transpiler.Node{
517+
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
518+
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
519+
transpiler.NewDict([]transpiler.Node{
520+
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
521+
transpiler.NewStrVal("/var/log/value2.log"),
522+
})),
523+
}),
524+
})),
525+
transpiler.NewKey("processors", transpiler.NewList([]transpiler.Node{
526+
transpiler.NewDict([]transpiler.Node{
527+
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
528+
transpiler.NewKey("fields", transpiler.NewDict([]transpiler.Node{
529+
transpiler.NewKey("custom", transpiler.NewStrVal("value2")),
530+
})),
531+
transpiler.NewKey("to", transpiler.NewStrVal("dynamic")),
532+
})),
533+
}),
534+
})),
535+
}),
536+
}),
537+
varsArray: []*transpiler.Vars{
538+
mustMakeVarsP(map[string]interface{}{
539+
"var1": map[string]interface{}{
540+
"name": "value1",
541+
},
542+
},
543+
"var1",
544+
[]map[string]interface{}{
545+
{
546+
"add_fields": map[string]interface{}{
547+
"fields": map[string]interface{}{
548+
"custom": "value1",
549+
},
550+
"to": "dynamic",
551+
},
552+
},
553+
}),
554+
mustMakeVarsP(map[string]interface{}{
555+
"var1": map[string]interface{}{
556+
"name": "value2",
557+
},
558+
},
559+
"var1",
560+
[]map[string]interface{}{
561+
{
562+
"add_fields": map[string]interface{}{
563+
"fields": map[string]interface{}{
564+
"custom": "value2",
565+
},
566+
"to": "dynamic",
567+
},
568+
},
569+
}),
570+
},
571+
},
572+
"processors incorrectly a map": {
573+
input: transpiler.NewKey("inputs", transpiler.NewList([]transpiler.Node{
574+
transpiler.NewDict([]transpiler.Node{
575+
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
576+
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
577+
transpiler.NewDict([]transpiler.Node{
578+
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
579+
transpiler.NewStrVal("/var/log/${var1.name}.log"),
580+
})),
581+
}),
582+
})),
583+
transpiler.NewKey("processors", transpiler.NewDict([]transpiler.Node{
584+
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
585+
transpiler.NewKey("invalid", transpiler.NewStrVal("value")),
586+
})),
587+
})),
588+
}),
589+
})),
590+
expected: transpiler.NewList([]transpiler.Node{
591+
transpiler.NewDict([]transpiler.Node{
592+
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
593+
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
594+
transpiler.NewDict([]transpiler.Node{
595+
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
596+
transpiler.NewStrVal("/var/log/value1.log"),
597+
})),
598+
}),
599+
})),
600+
transpiler.NewKey("processors", transpiler.NewDict([]transpiler.Node{
601+
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
602+
transpiler.NewKey("invalid", transpiler.NewStrVal("value")),
603+
})),
604+
})),
605+
}),
606+
transpiler.NewDict([]transpiler.Node{
607+
transpiler.NewKey("type", transpiler.NewStrVal("logfile")),
608+
transpiler.NewKey("streams", transpiler.NewList([]transpiler.Node{
609+
transpiler.NewDict([]transpiler.Node{
610+
transpiler.NewKey("paths", transpiler.NewList([]transpiler.Node{
611+
transpiler.NewStrVal("/var/log/value2.log"),
612+
})),
613+
}),
614+
})),
615+
transpiler.NewKey("processors", transpiler.NewDict([]transpiler.Node{
616+
transpiler.NewKey("add_fields", transpiler.NewDict([]transpiler.Node{
617+
transpiler.NewKey("invalid", transpiler.NewStrVal("value")),
618+
})),
619+
})),
620+
}),
621+
}),
622+
varsArray: []*transpiler.Vars{
623+
mustMakeVarsP(map[string]interface{}{
624+
"var1": map[string]interface{}{
625+
"name": "value1",
626+
},
627+
},
628+
"var1",
629+
[]map[string]interface{}{
630+
{
631+
"add_fields": map[string]interface{}{
632+
"fields": map[string]interface{}{
633+
"custom": "value1",
634+
},
635+
"to": "dynamic",
636+
},
637+
},
638+
}),
639+
mustMakeVarsP(map[string]interface{}{
640+
"var1": map[string]interface{}{
641+
"name": "value2",
642+
},
643+
},
644+
"var1",
645+
[]map[string]interface{}{
646+
{
647+
"add_fields": map[string]interface{}{
648+
"fields": map[string]interface{}{
649+
"custom": "value2",
650+
},
651+
"to": "dynamic",
652+
},
653+
},
654+
}),
655+
},
656+
},
482657
}
483658

484659
for name, test := range testcases {

0 commit comments

Comments
 (0)