Skip to content

Commit

Permalink
fix(auth): handle non-Transport DefaultTransport
Browse files Browse the repository at this point in the history
If a client does not specify opts.BaseRoundTripper in httptransport.NewClient, avoid a panic if the global http.DefaultTransport has been overwritten to something that is not an instance of *http.Transport.
  • Loading branch information
adapap committed Aug 22, 2024
1 parent 9afb797 commit be8c35f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
14 changes: 14 additions & 0 deletions auth/httptransport/httptransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,20 @@ func TestNewClient_BaseRoundTripper(t *testing.T) {
}
}

func TestNewClient_HandlesNonTransportAsDefaultTransport(t *testing.T) {
// Override the global http.DefaultTransport.
dt := http.DefaultTransport
http.DefaultTransport = &rt{}
defer func() { http.DefaultTransport = dt }()

_, err := NewClient(&Options{
APIKey: "key",
})
if err != nil {
t.Fatalf("NewClient() = %v", err)
}
}

type staticTP string

func (tp staticTP) Token(context.Context) (*auth.Token, error) {
Expand Down
6 changes: 5 additions & 1 deletion auth/httptransport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func newTransport(base http.RoundTripper, opts *Options) (http.RoundTripper, err
// http.DefaultTransport.
// If TLSCertificate is available, set TLSClientConfig as well.
func defaultBaseTransport(clientCertSource cert.Provider, dialTLSContext func(context.Context, string, string) (net.Conn, error)) http.RoundTripper {
trans := http.DefaultTransport.(*http.Transport).Clone()
defaultTransport, ok := http.DefaultTransport.(*http.Transport)
if !ok {
defaultTransport = transport.BaseTransport()
}
trans := defaultTransport.Clone()
trans.MaxIdleConnsPerHost = 100

if clientCertSource != nil {
Expand Down
4 changes: 2 additions & 2 deletions auth/internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func ValidateUniverseDomain(clientUniverseDomain, credentialsUniverseDomain stri

// DefaultHTTPClientWithTLS constructs an HTTPClient using the provided tlsConfig, to support mTLS.
func DefaultHTTPClientWithTLS(tlsConfig *tls.Config) *http.Client {
trans := baseTransport()
trans := BaseTransport()
trans.TLSClientConfig = tlsConfig
return &http.Client{Transport: trans}
}

func baseTransport() *http.Transport {
func BaseTransport() *http.Transport {

Check failure on line 89 in auth/internal/transport/transport.go

View workflow job for this annotation

GitHub Actions / vet

exported function BaseTransport should have comment or be unexported
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Expand Down

0 comments on commit be8c35f

Please sign in to comment.