Notifications: Redact URL from errors (#85687)

* Remove url logs and redact

* Reinclude redacted URL
This commit is contained in:
Alexander Weaver 2024-06-18 16:02:33 -05:00 committed by GitHub
parent 3bbc821131
commit 7c69f3657b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,10 +4,12 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
"net/http" "net/http"
"net/url"
"time" "time"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
@ -62,6 +64,11 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
if err != nil { if err != nil {
return err return err
} }
url, err := url.Parse(webhook.Url)
if err != nil {
// Should not be possible - NewRequestWithContext should also err if the URL is bad.
return err
}
if webhook.ContentType == "" { if webhook.ContentType == "" {
webhook.ContentType = "application/json" webhook.ContentType = "application/json"
@ -80,7 +87,7 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
resp, err := netClient.Do(request) resp, err := netClient.Do(request)
if err != nil { if err != nil {
return err return redactURL(err)
} }
defer func() { defer func() {
if err := resp.Body.Close(); err != nil { if err := resp.Body.Close(); err != nil {
@ -96,16 +103,25 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
if webhook.Validation != nil { if webhook.Validation != nil {
err := webhook.Validation(body, resp.StatusCode) err := webhook.Validation(body, resp.StatusCode)
if err != nil { if err != nil {
ns.log.Debug("Webhook failed validation", "url", webhook.Url, "statuscode", resp.Status, "body", string(body)) ns.log.Debug("Webhook failed validation", "url", url.Redacted(), "statuscode", resp.Status, "body", string(body), "error", err)
return fmt.Errorf("webhook failed validation: %w", err) return fmt.Errorf("webhook failed validation: %w", err)
} }
} }
if resp.StatusCode/100 == 2 { if resp.StatusCode/100 == 2 {
ns.log.Debug("Webhook succeeded", "url", webhook.Url, "statuscode", resp.Status) ns.log.Debug("Webhook succeeded", "url", url.Redacted(), "statuscode", resp.Status)
return nil return nil
} }
ns.log.Debug("Webhook failed", "url", webhook.Url, "statuscode", resp.Status, "body", string(body)) ns.log.Debug("Webhook failed", "url", url.Redacted(), "statuscode", resp.Status, "body", string(body))
return fmt.Errorf("webhook response status %v", resp.Status) return fmt.Errorf("webhook response status %v", resp.Status)
} }
func redactURL(err error) error {
var e *url.Error
if !errors.As(err, &e) {
return err
}
e.URL = "<redacted>"
return e
}