Skip to content

Commit

Permalink
config: basic_auth doesn't need to require a username (#135)
Browse files Browse the repository at this point in the history
Issue: prometheus/prometheus#4173

Signed-off-by: Adam Shannon <adamkshannon@gmail.com>
  • Loading branch information
adamdecaf authored and brian-brazil committed May 18, 2018
1 parent d811d2e commit 7600349
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 0 additions & 3 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
26 changes: 26 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions config/testdata/http.conf.basic-auth.no-username.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
basic_auth:
password: secret

0 comments on commit 7600349

Please sign in to comment.