Skip to content

Commit adab03e

Browse files
committed
Add option 'insecure' for BasicAuthTransport
1 parent a15bf28 commit adab03e

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

cmd/icinga-kubernetes/main.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"k8s.io/client-go/kubernetes"
3939
kclientcmd "k8s.io/client-go/tools/clientcmd"
4040
"k8s.io/klog/v2"
41-
"net/http"
4241
"os"
4342
"strings"
4443
"sync"
@@ -316,14 +315,15 @@ func main() {
316315
}
317316

318317
if cfg.Prometheus.Url != "" {
319-
var basicAuthTransport http.RoundTripper
318+
basicAuthTransport := &com.BasicAuthTransport{}
320319

321-
if cfg.Prometheus.Username != "" && cfg.Prometheus.Password != "" {
322-
basicAuthTransport = &com.BasicAuthTransport{
323-
RoundTripper: http.DefaultTransport,
324-
Username: cfg.Prometheus.Username,
325-
Password: cfg.Prometheus.Password,
326-
}
320+
if cfg.Prometheus.Insecure == "true" {
321+
basicAuthTransport.Insecure = true
322+
}
323+
324+
if cfg.Prometheus.Username != "" {
325+
basicAuthTransport.Username = cfg.Prometheus.Username
326+
basicAuthTransport.Password = cfg.Prometheus.Password
327327
}
328328

329329
promClient, err := promapi.NewClient(promapi.Config{

pkg/com/basic_auth_transport.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com
22

33
import (
4+
"crypto/tls"
45
"net/http"
56
)
67

@@ -9,11 +10,27 @@ type BasicAuthTransport struct {
910
http.RoundTripper
1011
Username string
1112
Password string
13+
Insecure bool
1214
}
1315

1416
// RoundTrip executes a single HTTP transaction with the basic auth credentials.
1517
func (t *BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
16-
req.SetBasicAuth(t.Username, t.Password)
18+
if t.Username != "" {
19+
req.SetBasicAuth(t.Username, t.Password)
20+
}
1721

18-
return t.RoundTripper.RoundTrip(req)
22+
rt := t.RoundTripper
23+
if rt == nil {
24+
rt = http.DefaultTransport
25+
}
26+
27+
if t.Insecure {
28+
if transport, ok := rt.(*http.Transport); ok {
29+
transportCopy := transport.Clone()
30+
transportCopy.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
31+
rt = transportCopy
32+
}
33+
}
34+
35+
return rt.RoundTrip(req)
1936
}

0 commit comments

Comments
 (0)