Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add httpInsecure Config #359 #382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func main() {
collectorManager := manager.NewCollectorManager(
exporterConfig.Logstash.Servers,
exporterConfig.Logstash.HttpTimeout,
exporterConfig.Logstash.HttpInsecure,
)
prometheus.MustRegister(collectorManager)

Expand Down
1 change: 1 addition & 0 deletions fixtures/valid_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ logstash:
servers:
- url: "http://localhost:9601"
httpTimeout: 3s
httpInsecure: true
server:
host: "127.0.0.1"
port: 9200
Expand Down
11 changes: 8 additions & 3 deletions internal/fetcher/logstash_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logstash_client

import (
"context"
"crypto/tls"
"encoding/json"
"net/http"

Expand Down Expand Up @@ -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,
}
}

Expand Down
16 changes: 14 additions & 2 deletions internal/fetcher/logstash_client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions internal/fetcher/logstash_client/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/exporter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
defaultLogFormat = "text"
defaultLogstashURL = "http://localhost:9600"
defaultHttpTimeout = time.Second * 2
defaultHttpInsecure = false
)

var (
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/config/exporter_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -119,6 +125,7 @@ func TestMergeWithDefault(t *testing.T) {
{Host: "http://localhost:9602"},
},
HttpTimeout: 3 * time.Second,
HttpInsecure: true,
},
}

Expand Down Expand Up @@ -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)
}
})
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/manager/collector_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/collector_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down