cmd/prometheus: don't allow quotes at beginning or end of url

This prevents accidental copy/paste error where a the web.external-url
or alertmanager.url params could have an extra set of quotes.
See also: https://github.com/prometheus/prometheus/issues/1229
This commit is contained in:
Paul Gier 2017-10-04 10:01:04 -05:00
parent f79b55d057
commit 08af129b4d
2 changed files with 21 additions and 0 deletions

View file

@ -319,6 +319,11 @@ func parse(args []string) error {
return nil
}
func startsOrEndsWithQuote(s string) bool {
return strings.HasPrefix(s, "\"") || strings.HasPrefix(s, "'") ||
strings.HasSuffix(s, "\"") || strings.HasSuffix(s, "'")
}
func parsePrometheusURL() error {
if cfg.prometheusURL == "" {
hostname, err := os.Hostname()
@ -330,6 +335,8 @@ func parsePrometheusURL() error {
return err
}
cfg.prometheusURL = fmt.Sprintf("http://%s:%s/", hostname, port)
} else if startsOrEndsWithQuote(cfg.prometheusURL) {
return fmt.Errorf("web.external-url must not begin or end with quotes")
}
promURL, err := url.Parse(cfg.prometheusURL)
@ -349,6 +356,8 @@ func parsePrometheusURL() error {
func validateAlertmanagerURL(u string) error {
if u == "" {
return nil
} else if startsOrEndsWithQuote(u) {
return fmt.Errorf("alertmanager.url must not begin or end with quotes: %s", u)
}
url, err := url.Parse(u)
if err != nil {

View file

@ -40,6 +40,10 @@ func TestParse(t *testing.T) {
input: []string{"-web.external-url", "'https://url/prometheus'"},
valid: false,
},
{
input: []string{"-web.external-url", "'relative/path/with/quotes'"},
valid: false,
},
{
input: []string{"-alertmanager.url", ""},
valid: true,
@ -56,6 +60,14 @@ func TestParse(t *testing.T) {
input: []string{"-alertmanager.url", "https://double--dash.de"},
valid: true,
},
{
input: []string{"-alertmanager.url", "'http://starts/with/quote"},
valid: false,
},
{
input: []string{"-alertmanager.url", "ends/with/quote\""},
valid: false,
},
}
for i, test := range tests {