diff --git a/config/http_config.go b/config/http_config.go index 55cc5b07..73d31f19 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -717,7 +717,7 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon return nil, fmt.Errorf("unable to use client secret: %w", err) } } - rt = NewOAuth2RoundTripper(oauthCredential, cfg.OAuth2, rt, &opts) + rt = NewOAuth2RoundTripper(oauthCredential, cfg.OAuth2, rt, optFuncs...) } if cfg.HTTPHeaders != nil { @@ -942,16 +942,26 @@ type oauth2RoundTripper struct { client *http.Client } -func NewOAuth2RoundTripper(oauthCredential SecretReader, config *OAuth2, next http.RoundTripper, opts *httpClientOptions) http.RoundTripper { +// NewOAuth2RoundTripper returns a round tripper that performs OAuth2 +// authentication. The opts variadic parameter accepts any HTTPClientOption +// (e.g. WithDialContextFunc, WithKeepAlivesDisabled) so that callers outside +// this package can fully configure the transport without needing access to the +// unexported *httpClientOptions type. +func NewOAuth2RoundTripper(oauthCredential SecretReader, config *OAuth2, next http.RoundTripper, optFuncs ...HTTPClientOption) http.RoundTripper { if oauthCredential == nil { oauthCredential = NewInlineSecret("") } + opts := defaultHTTPClientOptions + for _, opt := range optFuncs { + opt.applyToHTTPClientOptions(&opts) + } + return &oauth2RoundTripper{ config: config, // A correct tokenSource will be added later on. lastRT: &oauth2.Transport{Base: next}, - opts: opts, + opts: &opts, oauthCredential: oauthCredential, } } diff --git a/config/http_config_test.go b/config/http_config_test.go index 9968d37a..060598b7 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -1519,7 +1519,7 @@ endpoint_params: require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig) secret := NewInlineSecret(string(expectedConfig.ClientSecret)) - rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions) + rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport) client := http.Client{ Transport: rt, @@ -1654,7 +1654,7 @@ endpoint_params: require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig) secret := NewFileSecret(expectedConfig.ClientSecretFile) - rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions) + rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport) client := http.Client{ Transport: rt, @@ -1768,7 +1768,7 @@ endpoint_params: require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig) clientCertificateKey := NewFileSecret(expectedConfig.ClientCertificateKeyFile) - rt := NewOAuth2RoundTripper(clientCertificateKey, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions) + rt := NewOAuth2RoundTripper(clientCertificateKey, &expectedConfig, http.DefaultTransport) client := http.Client{ Transport: rt,