Skip to content

Commit 517c52c

Browse files
author
Julien Pivotto
authored
Merge pull request #274 from roidelapluie/remote-private-field
Remove private field in http config
2 parents 7a93127 + 45105da commit 517c52c

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

config/http_config.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ type HTTPClientConfig struct {
111111
ProxyURL URL `yaml:"proxy_url,omitempty"`
112112
// TLSConfig to use to connect to the targets.
113113
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
114-
// Used to make sure that the configuration is valid and that BearerToken to
115-
// Authorization.Credentials change has been handled.
116-
valid bool
117114
}
118115

119116
// SetDirectory joins any relative file paths with dir.
@@ -169,8 +166,6 @@ func (c *HTTPClientConfig) Validate() error {
169166
c.BearerTokenFile = ""
170167
}
171168
}
172-
173-
c.valid = true
174169
return nil
175170
}
176171

@@ -207,12 +202,6 @@ func NewClientFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, e
207202
// NewRoundTripperFromConfig returns a new HTTP RoundTripper configured for the
208203
// given config.HTTPClientConfig. The name is used as go-conntrack metric label.
209204
func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, enableHTTP2 bool) (http.RoundTripper, error) {
210-
// Make sure that the configuration is valid.
211-
if !cfg.valid {
212-
if err := cfg.Validate(); err != nil {
213-
return nil, err
214-
}
215-
}
216205
newRT := func(tlsConfig *tls.Config) (http.RoundTripper, error) {
217206
// The only timeout we care about is the configured scrape timeout.
218207
// It is applied on request. So we leave out any timings here.
@@ -254,6 +243,13 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, disableKeepAli
254243
} else if cfg.Authorization != nil && len(cfg.Authorization.CredentialsFile) > 0 {
255244
rt = NewAuthorizationCredentialsFileRoundTripper(cfg.Authorization.Type, cfg.Authorization.CredentialsFile, rt)
256245
}
246+
// Backwards compatibility, be nice with importers who would not have
247+
// called Validate().
248+
if len(cfg.BearerToken) > 0 {
249+
rt = NewAuthorizationCredentialsRoundTripper("Bearer", cfg.BearerToken, rt)
250+
} else if len(cfg.BearerTokenFile) > 0 {
251+
rt = NewAuthorizationCredentialsFileRoundTripper("Bearer", cfg.BearerTokenFile, rt)
252+
}
257253

258254
if cfg.BasicAuth != nil {
259255
rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, cfg.BasicAuth.Password, cfg.BasicAuth.PasswordFile, rt)

config/http_config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ func TestNewClientFromConfig(t *testing.T) {
306306
}
307307
defer testServer.Close()
308308

309+
err = validConfig.clientConfig.Validate()
310+
if err != nil {
311+
t.Fatal(err.Error())
312+
}
309313
client, err := NewClientFromConfig(validConfig.clientConfig, "test", false, true)
310314
if err != nil {
311315
t.Errorf("Can't create a client from this config: %+v", validConfig.clientConfig)

0 commit comments

Comments
 (0)