From b4b8c06719cbde5164da668d731fe011ce0f33ff Mon Sep 17 00:00:00 2001 From: Vijay Samuel Date: Mon, 13 Aug 2018 14:12:41 -0700 Subject: [PATCH] Make docker input check if container strings are empty --- CHANGELOG.asciidoc | 1 + filebeat/input/docker/input.go | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 02f21fae3e64..c78a642dcbc5 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -85,6 +85,7 @@ https://github.com/elastic/beats/compare/v6.4.0...master[Check the HEAD diff] - Make inputsource generic taking bufio.SplitFunc as input {pull}7746[7746] - Add custom unpack to log hints config to avoid env resolution {pull}7710[7710] - Keep raw user agent information after parsing as user_agent_raw in Filebeat modules. {pull}7823[7832] +- Make docker input check if container strings are empty {pull}7960[7960] *Heartbeat* diff --git a/filebeat/input/docker/input.go b/filebeat/input/docker/input.go index ce34276e5479..91724c6ff070 100644 --- a/filebeat/input/docker/input.go +++ b/filebeat/input/docker/input.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/filebeat/input/log" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/common/cfgwarn" + "github.com/elastic/beats/libbeat/logp" "github.com/pkg/errors" ) @@ -44,18 +45,32 @@ func NewInput( context input.Context, ) (input.Input, error) { cfgwarn.Experimental("Docker input is enabled.") + logger := logp.NewLogger("docker") + // Wrap log input with custom docker settings config := defaultConfig if err := cfg.Unpack(&config); err != nil { return nil, errors.Wrap(err, "reading docker input config") } - // Wrap log input with custom docker settings - if len(config.Containers.IDs) == 0 { + // Docker input should make sure that no callers should ever pass empty strings as container IDs + // Hence we explicitly make sure that we catch such things and print stack traces in the event of + // an invocation so that it can be fixed. + var ids []string + for _, containerID := range config.Containers.IDs { + if containerID != "" { + ids = append(ids, containerID) + } else { + logger.Error("Docker container ID can't be empty for Docker input config") + logger.Debugw("Empty docker container ID was received", logp.Stack("stacktrace")) + } + } + + if len(ids) == 0 { return nil, errors.New("Docker input requires at least one entry under 'containers.ids'") } - for idx, containerID := range config.Containers.IDs { + for idx, containerID := range ids { cfg.SetString("paths", idx, path.Join(config.Containers.Path, containerID, "*.log")) }