Alerting: Support tls config for webhook receiver (#93513)

Adds the ability to configure tls settings on the webhook receiver (e.g. to skip server certificate validation)
This commit is contained in:
Tito Lins
2024-10-22 12:44:32 +02:00
committed by GitHub
parent d722a25084
commit 71d04a326b
19 changed files with 178 additions and 48 deletions
+4 -18
View File
@@ -7,10 +7,10 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
alertingReceivers "github.com/grafana/alerting/receivers"
"github.com/grafana/grafana/pkg/util"
)
@@ -23,6 +23,7 @@ type Webhook struct {
HttpMethod string
HttpHeader map[string]string
ContentType string
TLSConfig *tls.Config
// Validation is a function that will validate the response body and statusCode of the webhook. Any returned error will cause the webhook request to be considered failed.
// This can be useful when a webhook service communicates failures in creative ways, such as using the response body instead of the status code.
@@ -34,21 +35,6 @@ type WebhookClient interface {
Do(req *http.Request) (*http.Response, error)
}
var netTransport = &http.Transport{
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateFreelyAsClient,
},
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
var netClient WebhookClient = &http.Client{
Timeout: time.Second * 30,
Transport: netTransport,
}
func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *Webhook) error {
if webhook.HttpMethod == "" {
webhook.HttpMethod = http.MethodPost
@@ -85,7 +71,7 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
request.Header.Set(k, v)
}
resp, err := netClient.Do(request)
resp, err := alertingReceivers.NewTLSClient(webhook.TLSConfig).Do(request)
if err != nil {
return redactURL(err)
}