From d12d545b9d3c370232d543fbcf0a081da3692c06 Mon Sep 17 00:00:00 2001 From: satk0 Date: Wed, 30 Oct 2024 23:00:13 +0100 Subject: [PATCH] Add httpInsecure Config #359 --- cmd/exporter/main.go | 1 + fixtures/valid_config.yml | 1 + internal/fetcher/logstash_client/client.go | 11 ++++++++--- internal/fetcher/logstash_client/client_test.go | 16 ++++++++++++++-- internal/fetcher/logstash_client/queries_test.go | 4 ++-- pkg/config/exporter_config.go | 7 +++++++ pkg/config/exporter_config_test.go | 10 ++++++++++ pkg/manager/collector_manager.go | 8 ++++---- pkg/manager/collector_manager_test.go | 2 +- 9 files changed, 48 insertions(+), 12 deletions(-) diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go index 7794777c..39f9471e 100644 --- a/cmd/exporter/main.go +++ b/cmd/exporter/main.go @@ -68,6 +68,7 @@ func main() { collectorManager := manager.NewCollectorManager( exporterConfig.Logstash.Servers, exporterConfig.Logstash.HttpTimeout, + exporterConfig.Logstash.HttpInsecure, ) prometheus.MustRegister(collectorManager) diff --git a/fixtures/valid_config.yml b/fixtures/valid_config.yml index 768b88f6..d8f2e7dc 100644 --- a/fixtures/valid_config.yml +++ b/fixtures/valid_config.yml @@ -2,6 +2,7 @@ logstash: servers: - url: "http://localhost:9601" httpTimeout: 3s + httpInsecure: true server: host: "127.0.0.1" port: 9200 diff --git a/internal/fetcher/logstash_client/client.go b/internal/fetcher/logstash_client/client.go index bf14eb4d..fdb7bf79 100644 --- a/internal/fetcher/logstash_client/client.go +++ b/internal/fetcher/logstash_client/client.go @@ -2,6 +2,7 @@ package logstash_client import ( "context" + "crypto/tls" "encoding/json" "net/http" @@ -29,14 +30,18 @@ func (client *DefaultClient) GetEndpoint() string { const defaultLogstashEndpoint = "http://localhost:9600" // NewClient returns a new instance of the DefaultClient configured with the given endpoint -func NewClient(endpoint string) Client { +func NewClient(endpoint string, httpInsecure bool) Client { if endpoint == "" { endpoint = defaultLogstashEndpoint } return &DefaultClient{ - httpClient: &http.Client{}, - endpoint: endpoint, + httpClient: &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: httpInsecure}, + }, + }, + endpoint: endpoint, } } diff --git a/internal/fetcher/logstash_client/client_test.go b/internal/fetcher/logstash_client/client_test.go index df0a86d8..7cf8ac4d 100644 --- a/internal/fetcher/logstash_client/client_test.go +++ b/internal/fetcher/logstash_client/client_test.go @@ -20,7 +20,7 @@ func TestNewClient(t *testing.T) { t.Run("should return a new client for the default endpoint", func(t *testing.T) { t.Parallel() - client := NewClient("") + client := NewClient("", false) if client.(*DefaultClient).endpoint != defaultLogstashEndpoint { t.Errorf("expected endpoint to be %s, got %s", defaultLogstashEndpoint, client.(*DefaultClient).endpoint) @@ -31,13 +31,25 @@ func TestNewClient(t *testing.T) { t.Parallel() expectedEndpoint := "http://localhost:9601" - client := NewClient(expectedEndpoint) + client := NewClient(expectedEndpoint, false) receivedEndpoint := client.GetEndpoint() if receivedEndpoint != expectedEndpoint { t.Errorf("expected endpoint to be %s, got %s", expectedEndpoint, receivedEndpoint) } }) + + t.Run("should return a new client with http insecure configuration", func(t *testing.T) { + t.Parallel() + + client := NewClient("", true) + + checkHttpInsecure := client.(*DefaultClient).httpClient.Transport. + (*http.Transport).TLSClientConfig.InsecureSkipVerify + if checkHttpInsecure != true { + t.Errorf("expected http insecure to be %t, got %t", true, checkHttpInsecure) + } + }) } func TestGetMetrics(t *testing.T) { diff --git a/internal/fetcher/logstash_client/queries_test.go b/internal/fetcher/logstash_client/queries_test.go index 5d6fab2a..e87eb102 100644 --- a/internal/fetcher/logstash_client/queries_test.go +++ b/internal/fetcher/logstash_client/queries_test.go @@ -22,7 +22,7 @@ func TestGetNodeInfo(t *testing.T) { })) defer ts.Close() - client := NewClient(ts.URL) + client := NewClient(ts.URL, false) response, err := client.GetNodeInfo(context.Background()) if err != nil { @@ -49,7 +49,7 @@ func TestGetNodeStats(t *testing.T) { })) defer ts.Close() - client := NewClient(ts.URL) + client := NewClient(ts.URL, false) response, err := client.GetNodeStats(context.Background()) if err != nil { diff --git a/pkg/config/exporter_config.go b/pkg/config/exporter_config.go index 557e1606..d8434bfd 100644 --- a/pkg/config/exporter_config.go +++ b/pkg/config/exporter_config.go @@ -15,6 +15,7 @@ const ( defaultLogFormat = "text" defaultLogstashURL = "http://localhost:9600" defaultHttpTimeout = time.Second * 2 + defaultHttpInsecure = false ) var ( @@ -30,6 +31,7 @@ type LogstashServer struct { type LogstashConfig struct { Servers []*LogstashServer `yaml:"servers"` HttpTimeout time.Duration `yaml:"httpTimeout"` + HttpInsecure bool `yaml:"httpInsecure"` } // ServerConfig represents the server configuration @@ -107,6 +109,11 @@ func mergeWithDefault(config *Config) *Config { config.Logstash.HttpTimeout = defaultHttpTimeout } + if !config.Logstash.HttpInsecure { + slog.Debug("using default http insecure", "httpInsecure", defaultHttpInsecure) + config.Logstash.HttpInsecure = defaultHttpInsecure + } + return config } diff --git a/pkg/config/exporter_config_test.go b/pkg/config/exporter_config_test.go index 2130e42f..3cbc9495 100644 --- a/pkg/config/exporter_config_test.go +++ b/pkg/config/exporter_config_test.go @@ -78,6 +78,9 @@ func TestMergeWithDefault(t *testing.T) { if mergedConfig.Logstash.HttpTimeout != defaultHttpTimeout { t.Errorf("expected http timeout to be %v, got %v", defaultHttpTimeout, mergedConfig.Logstash.HttpTimeout) } + if mergedConfig.Logstash.HttpInsecure != defaultHttpInsecure { + t.Errorf("expected http insecure to be %v, got %v", defaultHttpInsecure, mergedConfig.Logstash.HttpInsecure) + } }) t.Run("merge with nil config", func(t *testing.T) { @@ -100,6 +103,9 @@ func TestMergeWithDefault(t *testing.T) { if mergedConfig.Logstash.HttpTimeout != defaultHttpTimeout { t.Errorf("expected http timeout to be %v, got %v", defaultHttpTimeout, mergedConfig.Logstash.HttpTimeout) } + if mergedConfig.Logstash.HttpInsecure != defaultHttpInsecure { + t.Errorf("expected http insecure to be %v, got %v", defaultHttpInsecure, mergedConfig.Logstash.HttpInsecure) + } }) t.Run("merge with non-empty config", func(t *testing.T) { @@ -119,6 +125,7 @@ func TestMergeWithDefault(t *testing.T) { {Host: "http://localhost:9602"}, }, HttpTimeout: 3 * time.Second, + HttpInsecure: true, }, } @@ -146,6 +153,9 @@ func TestMergeWithDefault(t *testing.T) { if mergedConfig.Logstash.HttpTimeout != 3*time.Second { t.Errorf("expected http timeout to be %v, got %v", 3*time.Second, mergedConfig.Logstash.HttpTimeout) } + if mergedConfig.Logstash.HttpInsecure != true { + t.Errorf("expected http insecure to be %v, got %v", true, mergedConfig.Logstash.HttpInsecure) + } }) } diff --git a/pkg/manager/collector_manager.go b/pkg/manager/collector_manager.go index 8a9fa455..00154940 100644 --- a/pkg/manager/collector_manager.go +++ b/pkg/manager/collector_manager.go @@ -28,19 +28,19 @@ type CollectorManager struct { httpTimeout time.Duration } -func getClientsForEndpoints(endpoints []*config.LogstashServer) []logstash_client.Client { +func getClientsForEndpoints(endpoints []*config.LogstashServer, httpInsecure bool) []logstash_client.Client { clients := make([]logstash_client.Client, len(endpoints)) for i, endpoint := range endpoints { - clients[i] = logstash_client.NewClient(endpoint.Host) + clients[i] = logstash_client.NewClient(endpoint.Host, httpInsecure) } return clients } // NewCollectorManager creates a new CollectorManager with the provided logstash servers and http timeout -func NewCollectorManager(servers []*config.LogstashServer, httpTimeout time.Duration) *CollectorManager { - clients := getClientsForEndpoints(servers) +func NewCollectorManager(servers []*config.LogstashServer, httpTimeout time.Duration, httpInsecure bool) *CollectorManager { + clients := getClientsForEndpoints(servers, httpInsecure) collectors := getCollectors(clients) diff --git a/pkg/manager/collector_manager_test.go b/pkg/manager/collector_manager_test.go index 483a3c9f..aa2a7cf0 100644 --- a/pkg/manager/collector_manager_test.go +++ b/pkg/manager/collector_manager_test.go @@ -26,7 +26,7 @@ func TestNewCollectorManager(t *testing.T) { } mockEndpoints := []*config.LogstashServer{endpoint1, endpoint2} - cm := NewCollectorManager(mockEndpoints, httpTimeout) + cm := NewCollectorManager(mockEndpoints, httpTimeout, false) if cm == nil { t.Error("expected collector manager to be initialized")