Skip to content

Commit

Permalink
notify/webhook: Fix crash on errors when url_file is used (prometheus…
Browse files Browse the repository at this point in the history
…#3798)

When using url_file the conf.URL will be nil and when an error occurs
we will panic. Given that the URL is considered a secret, let's just
remove the custom details func.

Signed-off-by: Holger Hans Peter Freyther <holger@freyther.de>
  • Loading branch information
zecke committed Apr 6, 2024
1 parent 14cbe63 commit b7b350d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
18 changes: 1 addition & 17 deletions notify/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -55,11 +54,7 @@ func New(conf *config.WebhookConfig, t *template.Template, l log.Logger, httpOpt
client: client,
// Webhooks are assumed to respond with 2xx response codes on a successful
// request and 5xx response codes are assumed to be recoverable.
retrier: &notify.Retrier{
CustomDetailsFunc: func(_ int, body io.Reader) string {
return errDetails(body, conf.URL.String())
},
},
retrier: &notify.Retrier{},
}, nil
}

Expand Down Expand Up @@ -126,14 +121,3 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
}
return shouldRetry, err
}

func errDetails(body io.Reader, url string) string {
if body == nil {
return url
}
bs, err := io.ReadAll(body)
if err != nil {
return url
}
return fmt.Sprintf("%s: %s", url, string(bs))
}
4 changes: 2 additions & 2 deletions notify/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func TestWebhookRetry(t *testing.T) {
`{"status":"invalid event"}`,
)),

exp: fmt.Sprintf(`unexpected status code %d: %s: {"status":"invalid event"}`, http.StatusBadRequest, u.String()),
exp: fmt.Sprintf(`unexpected status code %d: {"status":"invalid event"}`, http.StatusBadRequest),
},
{
status: http.StatusBadRequest,

exp: fmt.Sprintf(`unexpected status code %d: %s`, http.StatusBadRequest, u.String()),
exp: fmt.Sprintf(`unexpected status code %d`, http.StatusBadRequest),
},
} {
t.Run("", func(t *testing.T) {
Expand Down

0 comments on commit b7b350d

Please sign in to comment.