notify: redact Slack webhook URL from logs

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-04-03 17:44:32 +02:00
parent a36bc1bb8d
commit 0edf8bef44
2 changed files with 47 additions and 3 deletions

View File

@ -585,9 +585,10 @@ func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
return false, err
}
resp, err := post(ctx, c, n.conf.APIURL.String(), contentTypeJSON, &buf)
u := n.conf.APIURL.String()
resp, err := post(ctx, c, u, contentTypeJSON, &buf)
if err != nil {
return true, err
return true, redactURL(err)
}
resp.Body.Close()
@ -1276,6 +1277,16 @@ func hashKey(s string) string {
return fmt.Sprintf("%x", h.Sum(nil))
}
// redactURL removes the URL part from an error of *url.Error type.
func redactURL(err error) error {
e, ok := err.(*url.Error)
if !ok {
return err
}
e.URL = "<redacted>"
return e
}
func post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {

View File

@ -21,17 +21,19 @@ import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/go-kit/kit/log"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
)
func TestWebhookRetry(t *testing.T) {
@ -122,6 +124,37 @@ func TestSlackRetry(t *testing.T) {
}
}
func TestSlackRedactedURL(t *testing.T) {
done := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cancel()
<-done
}))
defer func() {
close(done)
srv.Close()
}()
u, err := url.Parse(srv.URL)
require.NoError(t, err)
notifier := NewSlack(
&config.SlackConfig{
APIURL: &config.SecretURL{URL: u},
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
createTmpl(t),
log.NewNopLogger(),
)
ok, err := notifier.Notify(ctx, []*types.Alert{}...)
require.True(t, ok)
require.Error(t, err)
require.NotContains(t, err.Error(), srv.URL)
}
func TestHipchatRetry(t *testing.T) {
notifier := new(Hipchat)
retryCodes := append(defaultRetryCodes(), http.StatusTooManyRequests)