From 400ca7c9f06b2d99bf3f34fd8b13680ef547db6e Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 5 Mar 2021 18:04:28 +0100 Subject: [PATCH] [Ingest-Manager] Fix capabilities resolution in inspect command (#24346) [Ingest-Manager] Fix capabilities resolution in inspect command (#24346) --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + .../elastic-agent/pkg/capabilities/input.go | 20 ++++++++-- .../elastic-agent/pkg/capabilities/output.go | 39 +++++++++++++++++-- .../elastic-agent/pkg/capabilities/upgrade.go | 2 +- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 25c2d5dad11..cf1270c3cf9 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -37,6 +37,7 @@ - Fix reloading of log level for services {pull}[24055]24055 - Fix: Successfully installed and enrolled agent running standalone{pull}[24128]24128 - Make installer atomic on windows {pull}[24253]24253 +- Fix capabilities resolution in inspect command {pull}[24346]24346 - Fix windows installer during enroll {pull}[24343]24343 ==== New features diff --git a/x-pack/elastic-agent/pkg/capabilities/input.go b/x-pack/elastic-agent/pkg/capabilities/input.go index 6515bd5b715..11cb818883a 100644 --- a/x-pack/elastic-agent/pkg/capabilities/input.go +++ b/x-pack/elastic-agent/pkg/capabilities/input.go @@ -92,11 +92,23 @@ func inputsMap(cfgInputs interface{}, l *logger.Logger) []map[string]interface{} inputsMap := make([]map[string]interface{}, 0, len(inputsSet)) for _, s := range inputsSet { - mm, ok := s.(map[string]interface{}) - if !ok { + switch mm := s.(type) { + case map[string]interface{}: + inputsMap = append(inputsMap, mm) + case map[interface{}]interface{}: + newMap := make(map[string]interface{}) + for k, v := range mm { + key, ok := k.(string) + if !ok { + continue + } + + newMap[key] = v + } + inputsMap = append(inputsMap, newMap) + default: continue } - inputsMap = append(inputsMap, mm) } return inputsMap @@ -188,7 +200,7 @@ func (c *multiInputsCapability) Apply(in interface{}) (interface{}, error) { inputsMap, err = c.cleanupInput(inputsMap) if err != nil { - c.log.Errorf("cleaning up config object failed for capability 'multi-outputs': %v", err) + c.log.Errorf("cleaning up config object failed for capability 'multi-inputs': %v", err) return in, nil } diff --git a/x-pack/elastic-agent/pkg/capabilities/output.go b/x-pack/elastic-agent/pkg/capabilities/output.go index bf47123f337..593b1bb3130 100644 --- a/x-pack/elastic-agent/pkg/capabilities/output.go +++ b/x-pack/elastic-agent/pkg/capabilities/output.go @@ -182,11 +182,21 @@ func (c *multiOutputsCapability) cleanupOutput(cfgMap map[string]interface{}) (m return cfgMap, nil } - outputsMap, ok := outputsIface.(map[string]interface{}) - if !ok { + switch outputsMap := outputsIface.(type) { + case map[string]interface{}: + handleOutputMapStr(outputsMap) + cfgMap[outputKey] = outputsMap + case map[interface{}]interface{}: + handleOutputMapIface(outputsMap) + cfgMap[outputKey] = outputsMap + default: return nil, fmt.Errorf("outputs must be a map") } + return cfgMap, nil +} + +func handleOutputMapStr(outputsMap map[string]interface{}) { for outputName, outputIface := range outputsMap { acceptValue := true @@ -208,7 +218,28 @@ func (c *multiOutputsCapability) cleanupOutput(cfgMap map[string]interface{}) (m delete(outputMap, conditionKey) } +} - cfgMap[outputKey] = outputsMap - return cfgMap, nil +func handleOutputMapIface(outputsMap map[interface{}]interface{}) { + for outputName, outputIface := range outputsMap { + acceptValue := true + + outputMap, ok := outputIface.(map[interface{}]interface{}) + if ok { + conditionIface, found := outputMap[conditionKey] + if found { + conditionVal, ok := conditionIface.(bool) + if ok { + acceptValue = conditionVal + } + } + } + + if !acceptValue { + delete(outputsMap, outputName) + continue + } + + delete(outputMap, conditionKey) + } } diff --git a/x-pack/elastic-agent/pkg/capabilities/upgrade.go b/x-pack/elastic-agent/pkg/capabilities/upgrade.go index 8712529c841..94969e5dd40 100644 --- a/x-pack/elastic-agent/pkg/capabilities/upgrade.go +++ b/x-pack/elastic-agent/pkg/capabilities/upgrade.go @@ -147,7 +147,7 @@ type multiUpgradeCapability struct { func (c *multiUpgradeCapability) Apply(in interface{}) (interface{}, error) { upgradeMap := upgradeObject(in) if upgradeMap == nil { - c.log.Warnf("expecting map config object but got nil for capability 'multi-outputs'") + c.log.Warnf("expecting map config object but got nil for capability 'multi-upgrade'") // not an upgrade we don't alter origin return in, nil }