Fixed regression in -alertmanager.url flag. Basic auth was ignored.
- Included basic auth parsing while parsing to AlertmanagerConfig - Added test case Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
This commit is contained in:
parent
990e40c959
commit
d7febe97fa
|
@ -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
|
||||
|
@ -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 }}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,7 +18,6 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
_ "net/http/pprof" // Comment this line to disable pprof endpoint.
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
@ -26,7 +25,6 @@ import (
|
|||
|
||||
"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"
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue