From c1b0384777bcf2c2d7a406dcbb5a0e85e3df6b41 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Fri, 18 May 2018 10:43:14 -0500 Subject: [PATCH] config: basic_auth doesn't need to require a username Issue: https://github.com/prometheus/prometheus/issues/4173 Signed-off-by: Adam Shannon --- config/http_config.go | 3 --- config/http_config_test.go | 26 +++++++++++++++++++ .../http.conf.basic-auth.no-username.yaml | 2 ++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 config/testdata/http.conf.basic-auth.no-username.yaml diff --git a/config/http_config.go b/config/http_config.go index 59a12e95..da5d5901 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -85,9 +85,6 @@ func (c *HTTPClientConfig) Validate() error { if c.BasicAuth != nil && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) { return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured") } - if c.BasicAuth != nil && c.BasicAuth.Username == "" { - return fmt.Errorf("basic_auth requires a username") - } if c.BasicAuth != nil && (string(c.BasicAuth.Password) != "" && c.BasicAuth.PasswordFile != "") { return fmt.Errorf("at most one of basic_auth password & password_file must be configured") } diff --git a/config/http_config_test.go b/config/http_config_test.go index bd377185..4639ae47 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -476,6 +476,32 @@ func TestBasicAuthNoPassword(t *testing.T) { } } +func TestBasicAuthNoUsername(t *testing.T) { + cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.basic-auth.no-username.yaml") + if err != nil { + t.Errorf("Error loading HTTP client config: %v", err) + } + client, err := NewClientFromConfig(*cfg, "test") + if err != nil { + t.Errorf("Error creating HTTP Client: %v", err) + } + + rt, ok := client.Transport.(*basicAuthRoundTripper) + if !ok { + t.Fatalf("Error casting to basic auth transport, %v", client.Transport) + } + + if rt.username != "" { + t.Errorf("Got unexpected username: %s", rt.username) + } + if string(rt.password) != "secret" { + t.Errorf("Unexpected HTTP client password: %s", string(rt.password)) + } + if string(rt.passwordFile) != "" { + t.Errorf("Expected empty HTTP client passwordFile: %s", rt.passwordFile) + } +} + func TestBasicAuthPasswordFile(t *testing.T) { cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.basic-auth.good.yaml") if err != nil { diff --git a/config/testdata/http.conf.basic-auth.no-username.yaml b/config/testdata/http.conf.basic-auth.no-username.yaml new file mode 100644 index 00000000..86e29037 --- /dev/null +++ b/config/testdata/http.conf.basic-auth.no-username.yaml @@ -0,0 +1,2 @@ +basic_auth: + password: secret