notify: redact Slack webhook URL from logs
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
parent
a36bc1bb8d
commit
0edf8bef44
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue