Files
karma/internal/uri/urls.go
Łukasz Mierzwa fd0eb46adf Sanitize Alertmanager URI before putting it in /alerts.json reponse
Alertmanager URI might contain basic auth username & password, we should replace password with 'xxx' in logs and and error messages.
Go will still print it in HTTP request errors, but that will be fixed in the next Go release - https://go-review.googlesource.com/c/go/+/102855
Fixes #259
2018-04-27 09:36:12 -07:00

33 lines
715 B
Go

package uri
import (
"net/url"
"path"
)
// JoinURL can be used to join a base url (http(s)://domain.com) and a path (/my/path)
// it will return a joined string or an error (if you supply invalid url)
func JoinURL(base string, sub string) (string, error) {
u, err := url.Parse(base)
if err != nil {
return "", err
}
u.Path = path.Join(u.Path, sub)
return u.String(), nil
}
// SanitizeURI returns a copy of an URI string with password replaced by "xxx"
func SanitizeURI(s string) string {
u, err := url.Parse(s)
if err != nil {
return s
}
if u.User != nil {
if _, pwdSet := u.User.Password(); pwdSet {
u.User = url.UserPassword(u.User.Username(), "xxx")
}
return u.String()
}
return s
}