Merge pull request #2345 from Bplotka/fixed-alertmanager-flag-auth

Fixed regression in `-alertmanager.url flag`. Basic auth was ignored.
This commit is contained in:
Fabian Reinartz 2017-01-16 18:29:51 +01:00 committed by GitHub
commit 5418a42965
3 changed files with 83 additions and 19 deletions

View file

@ -27,6 +27,8 @@ import (
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
"github.com/prometheus/common/log" "github.com/prometheus/common/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/notifier"
"github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/storage/local" "github.com/prometheus/prometheus/storage/local"
@ -365,6 +367,43 @@ func validateAlertmanagerURL(u string) error {
return nil 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 = ` var helpTmpl = `
usage: prometheus [<args>] usage: prometheus [<args>]
{{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }} {{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }}

View file

@ -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)
}
}
}

View file

@ -18,7 +18,6 @@ import (
"flag" "flag"
"fmt" "fmt"
_ "net/http/pprof" // Comment this line to disable pprof endpoint. _ "net/http/pprof" // Comment this line to disable pprof endpoint.
"net/url"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -26,7 +25,6 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log" "github.com/prometheus/common/log"
"github.com/prometheus/common/model"
"github.com/prometheus/common/version" "github.com/prometheus/common/version"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -264,26 +262,10 @@ func reloadConfig(filename string, rls ...Reloadable) (err error) {
// Add AlertmanagerConfigs for legacy Alertmanager URL flags. // Add AlertmanagerConfigs for legacy Alertmanager URL flags.
for us := range cfg.alertmanagerURLs { for us := range cfg.alertmanagerURLs {
u, err := url.Parse(us) acfg, err := parseAlertmanagerURLToConfig(us)
if err != nil { if err != nil {
return err 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) conf.AlertingConfig.AlertmanagerConfigs = append(conf.AlertingConfig.AlertmanagerConfigs, acfg)
} }