Skip to content

Commit

Permalink
Fixed regression in -alertmanager.url flag. Basic auth was ignored.
Browse files Browse the repository at this point in the history
- Included basic auth parsing while parsing to AlertmanagerConfig
- Added test case

Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed Jan 16, 2017
1 parent 990e40c commit d7febe9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 19 deletions.
40 changes: 40 additions & 0 deletions cmd/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"github.com/prometheus/prometheus/storage/local/index"
"github.com/prometheus/prometheus/storage/remote"
"github.com/prometheus/prometheus/web"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/common/model"
)

// cfg contains immutable configuration parameters for a running Prometheus
Expand Down Expand Up @@ -365,6 +367,44 @@ func validateAlertmanagerURL(u string) error {
return nil
}

func parseAlertmanagerURLToConfig(us string) (*config.AlertmanagerConfig, error) {
u, err := url.Parse(us)
if err != nil {
return nil, err
}
acfg := &config.AlertmanagerConfig{
Scheme: u.Scheme,
PathPrefix: u.Path,
Timeout: cfg.notifierTimeout,
ServiceDiscoveryConfig: config.ServiceDiscoveryConfig{
StaticConfigs: []*config.TargetGroup{
{
Targets: []model.LabelSet{
{
model.AddressLabel: model.LabelValue(u.Host),
},
},
},
},
},
}

if u.User != nil {
acfg.HTTPClientConfig = config.HTTPClientConfig{
BasicAuth: &config.BasicAuth{
Username: u.User.Username(),
},
}

if password, isSet := u.User.Password(); isSet {
acfg.HTTPClientConfig.BasicAuth.Password = password
}
}

return acfg, nil
}


var helpTmpl = `
usage: prometheus [<args>]
{{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }}
Expand Down
43 changes: 43 additions & 0 deletions cmd/prometheus/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,46 @@ func TestParse(t *testing.T) {
}
}
}

func TestParseAlertmanagerURLToConfig(t *testing.T) {
tests := []struct {
url string
username string
password string
}{
{
url: "http://alertmanager.company.com",
username: "",
password: "",
},
{
url: "https://user:password@alertmanager.company.com",
username: "user",
password: "password",
},
}

for i, test := range tests {
acfg, err := parseAlertmanagerURLToConfig(test.url)
if err != nil {
t.Errorf("%d. expected alertmanager URL to be valid, got %s", i, err)
}

if acfg.HTTPClientConfig.BasicAuth != nil {
if test.username != acfg.HTTPClientConfig.BasicAuth.Username {
t.Errorf("%d. expected alertmanagerConfig username to be %q, got %q",
i, test.username, acfg.HTTPClientConfig.BasicAuth.Username)
}

if test.password != acfg.HTTPClientConfig.BasicAuth.Password {
t.Errorf("%d. expected alertmanagerConfig password to be %q, got %q", i,
test.password, acfg.HTTPClientConfig.BasicAuth.Username)
}
continue
}

if test.username != "" || test.password != "" {
t.Errorf("%d. expected alertmanagerConfig to have basicAuth filled, but was not", i)
}
}
}
20 changes: 1 addition & 19 deletions cmd/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ import (
"flag"
"fmt"
_ "net/http/pprof" // Comment this line to disable pprof endpoint.
"net/url"
"os"
"os/signal"
"syscall"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/model"
"github.com/prometheus/common/version"
"golang.org/x/net/context"

Expand Down Expand Up @@ -264,26 +262,10 @@ func reloadConfig(filename string, rls ...Reloadable) (err error) {

// Add AlertmanagerConfigs for legacy Alertmanager URL flags.
for us := range cfg.alertmanagerURLs {
u, err := url.Parse(us)
acfg, err := parseAlertmanagerURLToConfig(us)
if err != nil {
return err
}
acfg := &config.AlertmanagerConfig{
Scheme: u.Scheme,
PathPrefix: u.Path,
Timeout: cfg.notifierTimeout,
ServiceDiscoveryConfig: config.ServiceDiscoveryConfig{
StaticConfigs: []*config.TargetGroup{
{
Targets: []model.LabelSet{
{
model.AddressLabel: model.LabelValue(u.Host),
},
},
},
},
},
}
conf.AlertingConfig.AlertmanagerConfigs = append(conf.AlertingConfig.AlertmanagerConfigs, acfg)
}

Expand Down

0 comments on commit d7febe9

Please sign in to comment.