Skip to content

Commit

Permalink
Prevent using DefaultTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed May 22, 2021
1 parent 4ce0b50 commit e704f09
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 131 deletions.
105 changes: 0 additions & 105 deletions draft/usage.md

This file was deleted.

30 changes: 4 additions & 26 deletions pkg/api/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ func WithHostname(hostname string) Option {
// WithDisableSSLVerify disables verification of insecure certificates.
func WithDisableSSLVerify() Option {
return func(c *Client) {
var transport *http.Transport
if c.client.Transport == nil {
transport = http.DefaultTransport.(*http.Transport).Clone()
} else {
transport = c.client.Transport.(*http.Transport).Clone()
}
var transport *http.Transport = GetOrCreateTransport(c)

tlsConfig := transport.TLSClientConfig
tlsConfig.InsecureSkipVerify = true
Expand Down Expand Up @@ -88,13 +83,7 @@ func WithNTLM(creds string) (Option, error) {
return func(c *Client) {
withAuth(c)

var transport http.RoundTripper
if c.client.Transport == nil {
transport = http.DefaultTransport
} else {
transport = c.client.Transport.(*http.Transport).Clone()
}

var transport *http.Transport = GetOrCreateTransport(c)
c.client.Transport = ntlmssp.Negotiator{
RoundTripper: transport,
}
Expand Down Expand Up @@ -134,13 +123,8 @@ func WithProxy(proxyURL string) (Option, error) {
}

return func(c *Client) {
transport := http.DefaultTransport.(*http.Transport).Clone()
if c.client.Transport != nil {
transport = c.client.Transport.(*http.Transport).Clone()
}

var transport *http.Transport = GetOrCreateTransport(c)
transport.Proxy = http.ProxyURL(u)

c.client.Transport = transport
}, nil
}
Expand All @@ -166,13 +150,7 @@ func WithSSLCertFile(filepath string) (Option, error) {
// WithSSLCertPool overrides the default CA cert pool to trust specified cert pool.
func WithSSLCertPool(caCertPool *x509.CertPool) (Option, error) {
return func(c *Client) {
var transport *http.Transport
if c.client.Transport == nil {
transport = http.DefaultTransport.(*http.Transport).Clone()
} else {
transport = c.client.Transport.(*http.Transport).Clone()
}

var transport *http.Transport = GetOrCreateTransport(c)
tlsConfig := transport.TLSClientConfig
tlsConfig.RootCAs = caCertPool
transport.TLSClientConfig = tlsConfig
Expand Down
8 changes: 8 additions & 0 deletions pkg/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func NewTransport() *http.Transport {
ForceAttemptHTTP2: true,
}
}

// GetOrCreateTransport gets the client's Transport if already exists, or initializes a new one
func GetOrCreateTransport(c *Client) *http.Transport {
if c.client.Transport != nil {
return c.client.Transport.(*http.Transport).Clone()
}
return NewTransport()
}

0 comments on commit e704f09

Please sign in to comment.