diff options
author | 2017-07-01 16:17:53 -0400 | |
---|---|---|
committer | 2017-07-01 13:17:53 -0700 | |
commit | 7fada97ee384aad27f7c7796038eef2684a3642c (patch) | |
tree | 43d9087c8854a9098272ed295b5a4c93b806d50b /middleware/pkg/tls | |
parent | 7e97379bc5bfdcfcb6727ed4c324f7f95be4afd9 (diff) | |
download | coredns-7fada97ee384aad27f7c7796038eef2684a3642c.tar.gz coredns-7fada97ee384aad27f7c7796038eef2684a3642c.tar.zst coredns-7fada97ee384aad27f7c7796038eef2684a3642c.zip |
middleware/etcd: move NewHTTPTransport to pkg/tls (#769)
Diffstat (limited to 'middleware/pkg/tls')
-rw-r--r-- | middleware/pkg/tls/tls.go | 23 | ||||
-rw-r--r-- | middleware/pkg/tls/tls_test.go | 20 |
2 files changed, 43 insertions, 0 deletions
diff --git a/middleware/pkg/tls/tls.go b/middleware/pkg/tls/tls.go index 62889f542..13882c353 100644 --- a/middleware/pkg/tls/tls.go +++ b/middleware/pkg/tls/tls.go @@ -5,6 +5,9 @@ import ( "crypto/x509" "fmt" "io/ioutil" + "net" + "net/http" + "time" ) // NewTLSConfigFromArgs returns a TLS config based upon the passed @@ -102,3 +105,23 @@ func loadRoots(caPath string) (*x509.CertPool, error) { } return roots, nil } + +// NetHTTPSTransport returns an HTTP transport configured using tls.Config +func NewHTTPSTransport(cc *tls.Config) *http.Transport { + // this seems like a bad idea but was here in the previous version + if cc != nil { + cc.InsecureSkipVerify = true + } + + tr := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + Dial: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).Dial, + TLSHandshakeTimeout: 10 * time.Second, + TLSClientConfig: cc, + } + + return tr +} diff --git a/middleware/pkg/tls/tls_test.go b/middleware/pkg/tls/tls_test.go index 6d0cb7372..408469045 100644 --- a/middleware/pkg/tls/tls_test.go +++ b/middleware/pkg/tls/tls_test.go @@ -79,3 +79,23 @@ func TestNewTLSConfigFromArgs(t *testing.T) { t.Error("Certificateis should have a single entry when three args passed") } } + +func TestNewHTTPSTransport(t *testing.T) { + rmFunc, _, _, ca := getPEMFiles(t) + defer rmFunc() + + cc, err := NewTLSClientConfig(ca) + if err != nil { + t.Errorf("Failed to create TLSConfig: %s", err) + } + + tr := NewHTTPSTransport(cc) + if tr == nil { + t.Errorf("Failed to create https transport with cc") + } + + tr = NewHTTPSTransport(nil) + if tr == nil { + t.Errorf("Failed to create https transport without cc") + } +} |