From 0ef1f82f50eca47543c79e47f636c79ac3775611 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 26 Oct 2023 11:14:02 +0200 Subject: [PATCH 1/4] Disable exposing metrics server on all devices --- .../agent/application/monitoring/server.go | 5 ++ internal/pkg/core/monitoring/config/config.go | 50 +++++++++++++++++-- .../pkg/core/monitoring/config/config_test.go | 45 +++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/internal/pkg/agent/application/monitoring/server.go b/internal/pkg/agent/application/monitoring/server.go index 47561d29e49..87e637b4dd4 100644 --- a/internal/pkg/agent/application/monitoring/server.go +++ b/internal/pkg/agent/application/monitoring/server.go @@ -20,6 +20,7 @@ import ( "github.com/elastic/elastic-agent-libs/config" "github.com/elastic/elastic-agent-libs/monitoring" "github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator" + monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config" "github.com/elastic/elastic-agent/pkg/core/logger" ) @@ -38,6 +39,10 @@ func NewServer( log.Warnf("failed to create monitoring drop: %v", err) } + if endpointConfig.Host == "" { + endpointConfig.Host = monitoringCfg.DefaultHost + } + cfg, err := config.NewConfigFrom(endpointConfig) if err != nil { return nil, err diff --git a/internal/pkg/core/monitoring/config/config.go b/internal/pkg/core/monitoring/config/config.go index 1f326e29a8d..c7c1cdd2aba 100644 --- a/internal/pkg/core/monitoring/config/config.go +++ b/internal/pkg/core/monitoring/config/config.go @@ -4,10 +4,19 @@ package config -import "time" +import ( + "time" -const defaultPort = 6791 -const defaultNamespace = "default" + c "github.com/elastic/elastic-agent-libs/config" +) + +const ( + defaultPort = 6791 + defaultNamespace = "default" + + // DefaultHost is used when host is not defined or empty + DefaultHost = "localhost" +) // MonitoringConfig describes a configuration of a monitoring type MonitoringConfig struct { @@ -33,6 +42,39 @@ type MonitoringHTTPConfig struct { Buffer *BufferConfig `yaml:"buffer" config:"buffer"` } +// Unpack reads a config object into the settings. +func (c *MonitoringHTTPConfig) Unpack(cfg *c.C) error { + // do not use MonitoringHTTPConfig, it will end up in a loop + tmp := struct { + Enabled bool `yaml:"enabled" config:"enabled"` + Host string `yaml:"host" config:"host"` + Port int `yaml:"port" config:"port" validate:"min=0,max=65535,nonzero"` + Buffer *BufferConfig `yaml:"buffer" config:"buffer"` + }{ + Enabled: c.Enabled, + Host: c.Host, + Port: c.Port, + Buffer: c.Buffer, + } + + if err := cfg.Unpack(&tmp); err != nil { + return err + } + + if tmp.Host == "" { + tmp.Host = DefaultHost + } + + *c = MonitoringHTTPConfig{ + Enabled: tmp.Enabled, + Host: tmp.Host, + Port: tmp.Port, + Buffer: tmp.Buffer, + } + + return nil +} + // PprofConfig is a struct for the pprof enablement flag. // It is a nil struct by default to allow the agent to use the a value that the user has injected into fleet.yml as the source of truth that is passed to beats // TODO get this value from Kibana? @@ -55,7 +97,7 @@ func DefaultConfig() *MonitoringConfig { MonitorTraces: false, HTTP: &MonitoringHTTPConfig{ Enabled: false, - Host: "localhost", + Host: DefaultHost, Port: defaultPort, }, Namespace: defaultNamespace, diff --git a/internal/pkg/core/monitoring/config/config_test.go b/internal/pkg/core/monitoring/config/config_test.go index 2363f46ae50..87948312317 100644 --- a/internal/pkg/core/monitoring/config/config_test.go +++ b/internal/pkg/core/monitoring/config/config_test.go @@ -13,6 +13,51 @@ import ( "github.com/elastic/elastic-agent/internal/pkg/config" ) +func TestHost(t *testing.T) { + testCases := []struct { + name string + config string + expectedHost string + }{ + {"no host", `enabled: true +logs: true +metrics: true +http: + enabled: true`, defaultHost}, + {"empty host", `enabled: true +logs: true +metrics: true +http: + enabled: true + host: ""`, defaultHost}, + {"default", `enabled: true +logs: true +metrics: true +http: + enabled: true + host: localhost`, defaultHost}, + {"custom host", `enabled: true +logs: true +metrics: true +http: + enabled: true + host: custom`, "custom"}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + c, err := config.NewConfigFrom(tc.config) + require.NoError(t, err, "failed to create config") + + cfg := DefaultConfig() + c.Unpack(&cfg) + require.NoError(t, err, "failed to unpack config") + + require.Equal(t, tc.expectedHost, cfg.HTTP.Host) + }) + } +} + func TestAPMConfig(t *testing.T) { tcs := map[string]struct { in map[string]interface{} From 7245392dbf53e0d8b8c7b2f79b8fb1e77b2aae32 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 26 Oct 2023 11:20:46 +0200 Subject: [PATCH 2/4] test --- internal/pkg/core/monitoring/config/config_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/pkg/core/monitoring/config/config_test.go b/internal/pkg/core/monitoring/config/config_test.go index 87948312317..b16b8dc188d 100644 --- a/internal/pkg/core/monitoring/config/config_test.go +++ b/internal/pkg/core/monitoring/config/config_test.go @@ -23,19 +23,19 @@ func TestHost(t *testing.T) { logs: true metrics: true http: - enabled: true`, defaultHost}, + enabled: true`, DefaultHost}, {"empty host", `enabled: true logs: true metrics: true http: enabled: true - host: ""`, defaultHost}, + host: ""`, DefaultHost}, {"default", `enabled: true logs: true metrics: true http: enabled: true - host: localhost`, defaultHost}, + host: localhost`, DefaultHost}, {"custom host", `enabled: true logs: true metrics: true From 2097d9f7f5604fa0ed227b967d02ad113cf40e33 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 26 Oct 2023 11:24:19 +0200 Subject: [PATCH 3/4] test --- internal/pkg/core/monitoring/config/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/core/monitoring/config/config_test.go b/internal/pkg/core/monitoring/config/config_test.go index b16b8dc188d..f77c6dec028 100644 --- a/internal/pkg/core/monitoring/config/config_test.go +++ b/internal/pkg/core/monitoring/config/config_test.go @@ -50,7 +50,7 @@ http: require.NoError(t, err, "failed to create config") cfg := DefaultConfig() - c.Unpack(&cfg) + err = c.Unpack(&cfg) require.NoError(t, err, "failed to unpack config") require.Equal(t, tc.expectedHost, cfg.HTTP.Host) From 1bc64fc98892d82f7670150f6b703cce0e138005 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 26 Oct 2023 12:50:06 +0200 Subject: [PATCH 4/4] whitespace --- internal/pkg/agent/application/monitoring/server.go | 2 +- internal/pkg/core/monitoring/config/config.go | 3 ++- internal/pkg/core/monitoring/config/config_test.go | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/pkg/agent/application/monitoring/server.go b/internal/pkg/agent/application/monitoring/server.go index 87e637b4dd4..94b28807700 100644 --- a/internal/pkg/agent/application/monitoring/server.go +++ b/internal/pkg/agent/application/monitoring/server.go @@ -39,7 +39,7 @@ func NewServer( log.Warnf("failed to create monitoring drop: %v", err) } - if endpointConfig.Host == "" { + if strings.TrimSpace(endpointConfig.Host) == "" { endpointConfig.Host = monitoringCfg.DefaultHost } diff --git a/internal/pkg/core/monitoring/config/config.go b/internal/pkg/core/monitoring/config/config.go index c7c1cdd2aba..cc6f345e123 100644 --- a/internal/pkg/core/monitoring/config/config.go +++ b/internal/pkg/core/monitoring/config/config.go @@ -5,6 +5,7 @@ package config import ( + "strings" "time" c "github.com/elastic/elastic-agent-libs/config" @@ -61,7 +62,7 @@ func (c *MonitoringHTTPConfig) Unpack(cfg *c.C) error { return err } - if tmp.Host == "" { + if strings.TrimSpace(tmp.Host) == "" { tmp.Host = DefaultHost } diff --git a/internal/pkg/core/monitoring/config/config_test.go b/internal/pkg/core/monitoring/config/config_test.go index f77c6dec028..70ae6a77b4c 100644 --- a/internal/pkg/core/monitoring/config/config_test.go +++ b/internal/pkg/core/monitoring/config/config_test.go @@ -30,6 +30,12 @@ metrics: true http: enabled: true host: ""`, DefaultHost}, + {"whitespace host", `enabled: true +logs: true +metrics: true +http: + enabled: true + host: " "`, DefaultHost}, {"default", `enabled: true logs: true metrics: true