diff --git a/config/http_config.go b/config/http_config.go index 6060a093..07b389eb 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -111,9 +111,6 @@ type HTTPClientConfig struct { ProxyURL URL `yaml:"proxy_url,omitempty"` // TLSConfig to use to connect to the targets. TLSConfig TLSConfig `yaml:"tls_config,omitempty"` - // Used to make sure that the configuration is valid and that BearerToken to - // Authorization.Credentials change has been handled. - valid bool } // SetDirectory joins any relative file paths with dir. @@ -169,8 +166,6 @@ func (c *HTTPClientConfig) Validate() error { c.BearerTokenFile = "" } } - - c.valid = true return nil } @@ -207,12 +202,6 @@ func NewClientFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, e // NewRoundTripperFromConfig returns a new HTTP RoundTripper configured for the // given config.HTTPClientConfig. The name is used as go-conntrack metric label. func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, enableHTTP2 bool) (http.RoundTripper, error) { - // Make sure that the configuration is valid. - if !cfg.valid { - if err := cfg.Validate(); err != nil { - return nil, err - } - } newRT := func(tlsConfig *tls.Config) (http.RoundTripper, error) { // The only timeout we care about is the configured scrape timeout. // It is applied on request. So we leave out any timings here. @@ -254,6 +243,13 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, disableKeepAli } else if cfg.Authorization != nil && len(cfg.Authorization.CredentialsFile) > 0 { rt = NewAuthorizationCredentialsFileRoundTripper(cfg.Authorization.Type, cfg.Authorization.CredentialsFile, rt) } + // Backwards compatibility, be nice with importers who would not have + // called Validate(). + if len(cfg.BearerToken) > 0 { + rt = NewAuthorizationCredentialsRoundTripper("Bearer", cfg.BearerToken, rt) + } else if len(cfg.BearerTokenFile) > 0 { + rt = NewAuthorizationCredentialsFileRoundTripper("Bearer", cfg.BearerTokenFile, rt) + } if cfg.BasicAuth != nil { rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, cfg.BasicAuth.Password, cfg.BasicAuth.PasswordFile, rt) diff --git a/config/http_config_test.go b/config/http_config_test.go index d0cdee8c..a35ae895 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -306,6 +306,10 @@ func TestNewClientFromConfig(t *testing.T) { } defer testServer.Close() + err = validConfig.clientConfig.Validate() + if err != nil { + t.Fatal(err.Error()) + } client, err := NewClientFromConfig(validConfig.clientConfig, "test", false, true) if err != nil { t.Errorf("Can't create a client from this config: %+v", validConfig.clientConfig)