Skip to content

Commit

Permalink
add HTTP_INSECURE config (#337)
Browse files Browse the repository at this point in the history
Co-authored-by: Wei Wan <wei.wan@woo.network>
  • Loading branch information
wei840222 and Wei Wan authored Jul 12, 2024
1 parent 96fad72 commit 048b78b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 11 deletions.
5 changes: 4 additions & 1 deletion cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ func main() {
versionInfo := config.GetVersionInfo()
slog.Info(versionInfo.String())

httpInsecure := config.GetHttpInsecure()
slog.Debug("http insecure", "insecure", httpInsecure)

httpTimeout, err := config.GetHttpTimeout()
if err != nil {
slog.Error("failed to get http timeout", "err", err)
os.Exit(1)
}
slog.Debug("http timeout", "timeout", httpTimeout)

collectorManager := collectors.NewCollectorManager(logstashUrl, httpTimeout)
collectorManager := collectors.NewCollectorManager(logstashUrl, httpInsecure, httpTimeout)
appServer := server.NewAppServer(host, port, httpTimeout)
prometheus.MustRegister(collectorManager)

Expand Down
4 changes: 2 additions & 2 deletions collectors/collector_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type CollectorManager struct {
timeout time.Duration
}

func NewCollectorManager(endpoint string, timeout time.Duration) *CollectorManager {
client := logstashclient.NewClient(endpoint)
func NewCollectorManager(endpoint string, insecure bool, timeout time.Duration) *CollectorManager {
client := logstashclient.NewClient(endpoint, insecure)

collectors := getCollectors(client)

Expand Down
2 changes: 1 addition & 1 deletion collectors/collector_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const httpTimeout = 2 * time.Second

func TestNewCollectorManager(t *testing.T) {
mockEndpoint := "http://localhost:9600"
cm := NewCollectorManager(mockEndpoint, httpTimeout)
cm := NewCollectorManager(mockEndpoint, false, httpTimeout)

if cm == nil {
t.Error("Expected collector manager to be initialized")
Expand Down
7 changes: 7 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package config

import (
"os"
"strconv"
"time"
)

const (
defaultHttpTimeout = time.Second * 2
httpTimeoutEnvVar = "HTTP_TIMEOUT"
httpInsecureEnvVar = "HTTP_INSECURE"
)

func GetHttpTimeout() (time.Duration, error) {
Expand All @@ -23,3 +25,8 @@ func GetHttpTimeout() (time.Duration, error) {

return timeout, nil
}

func GetHttpInsecure() bool {
b, _ := strconv.ParseBool(os.Getenv(httpInsecureEnvVar))
return b
}
33 changes: 33 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,36 @@ func TestGetHttpTimeout(t *testing.T) {
}
})
}

func TestGetHttpInsecure(t *testing.T) {
t.Run("DefaultInsecure", func(t *testing.T) {
t.Parallel()
os.Unsetenv(httpInsecureEnvVar)
insecure := GetHttpInsecure()
if insecure != false {
t.Errorf("Expected default insecure of %v, got %v", false, insecure)
}
})

t.Run("CustomInsecure", func(t *testing.T) {
t.Parallel()
expectedInsecure := true
os.Setenv(httpInsecureEnvVar, "true")
defer os.Unsetenv(httpInsecureEnvVar)
insecure := GetHttpInsecure()
if insecure != expectedInsecure {
t.Errorf("Expected insecure of %v, got %v", expectedInsecure, insecure)
}
})

t.Run("InvalidInsecure", func(t *testing.T) {
t.Parallel()
expectedInsecure := false
os.Setenv(httpInsecureEnvVar, "invalid")
defer os.Unsetenv(httpInsecureEnvVar)
insecure := GetHttpInsecure()
if insecure != expectedInsecure {
t.Errorf("Expected insecure of %v, got %v", expectedInsecure, insecure)
}
})
}
11 changes: 8 additions & 3 deletions 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 All @@ -23,14 +24,18 @@ type DefaultClient struct {
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, insecure bool) Client {
if endpoint == "" {
endpoint = defaultLogstashEndpoint
}

return &DefaultClient{
httpClient: &http.Client{},
endpoint: endpoint,
httpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
},
endpoint: endpoint,
}
}

Expand Down
4 changes: 2 additions & 2 deletions fetcher/logstash_client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type TestResponse struct {

func TestNewClient(t *testing.T) {
t.Run("should return a new client for the default endpoint", func(t *testing.T) {
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 @@ -25,7 +25,7 @@ func TestNewClient(t *testing.T) {

t.Run("should return a new client for the given endpoint", func(t *testing.T) {
endpoint := "http://localhost:9601"
client := NewClient(endpoint)
client := NewClient(endpoint, false)

if client.(*DefaultClient).endpoint != endpoint {
t.Errorf("expected endpoint to be %s, got %s", endpoint, client.(*DefaultClient).endpoint)
Expand Down
4 changes: 2 additions & 2 deletions 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

0 comments on commit 048b78b

Please sign in to comment.