app/vmalert: properly set transport for HTTP clients

Fixes issue #586
This commit is contained in:
kreedom 2020-06-27 10:31:54 +03:00 committed by Aliaksandr Valialkin
parent 2b504f17de
commit 63c36e2e69
2 changed files with 10 additions and 20 deletions

View File

@ -24,7 +24,6 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver" "github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
"github.com/VictoriaMetrics/fasthttp"
"github.com/VictoriaMetrics/metrics" "github.com/VictoriaMetrics/metrics"
) )
@ -283,21 +282,15 @@ func getTLSConfig(certFile, keyFile, CAFile, serverName *string, insecureSkipVer
} }
func getTransport(URL, certFile, keyFile, CAFile, serverName *string, insecureSkipVerify *bool) (*http.Transport, error) { func getTransport(URL, certFile, keyFile, CAFile, serverName *string, insecureSkipVerify *bool) (*http.Transport, error) {
var u fasthttp.URI t := http.DefaultTransport.(*http.Transport).Clone()
u.Update(*URL) if !strings.HasPrefix(*URL, "https") {
return t, nil
var t *http.Transport
if string(u.Scheme()) == "https" {
t = http.DefaultTransport.(*http.Transport).Clone()
tlsCfg, err := getTLSConfig(certFile, keyFile, CAFile, serverName, insecureSkipVerify)
if err != nil {
return nil, err
}
t.TLSClientConfig = tlsCfg
} }
tlsCfg, err := getTLSConfig(certFile, keyFile, CAFile, serverName, insecureSkipVerify)
if err != nil {
return nil, err
}
t.TLSClientConfig = tlsCfg
return t, nil return t, nil
} }

View File

@ -87,15 +87,12 @@ func TestGetTransport(t *testing.T) {
var certFile, keyFile, CAFile, serverName string var certFile, keyFile, CAFile, serverName string
var insecureSkipVerify bool var insecureSkipVerify bool
URL := "http://victoriametrics.com" URL := "http://victoriametrics.com"
tr, err := getTransport(&URL, &certFile, &keyFile, &CAFile, &serverName, &insecureSkipVerify) _, err := getTransport(&URL, &certFile, &keyFile, &CAFile, &serverName, &insecureSkipVerify)
if err != nil { if err != nil {
t.Errorf("unexpected error %s", err) t.Errorf("unexpected error %s", err)
} }
if tr != nil {
t.Errorf("expected Transport to be nil, got %v", tr)
}
URL = "https://victoriametrics.com" URL = "https://victoriametrics.com"
tr, err = getTransport(&URL, &certFile, &keyFile, &CAFile, &serverName, &insecureSkipVerify) tr, err := getTransport(&URL, &certFile, &keyFile, &CAFile, &serverName, &insecureSkipVerify)
if err != nil { if err != nil {
t.Errorf("unexpected error %s", err) t.Errorf("unexpected error %s", err)
} }