Alerting: Update grafana/alerting from de176b4a0309 to 83b6de6b0a35 (#105157)
* Update grafana alerting from de176b4a0309 to 83b6de6b0a35 Includes: - https://github.com/grafana/alerting/pull/319 - https://github.com/grafana/alerting/pull/317 * Remove unused SendWebhook method from sender struct grafana/alerting hasn't used the grafana webhook sender for a while now, so this method is no longer used anywhere. - Removed SendWebhook from the sender struct and rename it to emailSender so that its use is clearer. - Also, for similar reasons, the Webhook method on Grafana's webhook sender `sendWebRequestSync` should not call grafana/alerting code for NewTLSClient. The previous grafana/alerting function is vendored into grafana. * Use BuildReceiverIntegrations new func signature
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
alertingHttp "github.com/grafana/alerting/http"
|
||||
alertingNotify "github.com/grafana/alerting/notify"
|
||||
"github.com/grafana/alerting/receivers"
|
||||
alertingTemplates "github.com/grafana/alerting/templates"
|
||||
@@ -366,14 +365,13 @@ func (am *alertmanager) buildReceiverIntegrations(receiver *alertingNotify.APIRe
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := &sender{am.NotificationService}
|
||||
s := &emailSender{am.NotificationService}
|
||||
img := newImageProvider(am.Store, log.New("ngalert.notifier.image-provider"))
|
||||
integrations, err := alertingNotify.BuildReceiverIntegrations(
|
||||
receiverCfg,
|
||||
tmpl,
|
||||
img,
|
||||
LoggerFactory,
|
||||
alertingHttp.DefaultClientConfiguration,
|
||||
func(n receivers.Metadata) (receivers.EmailSender, error) {
|
||||
return s, nil
|
||||
},
|
||||
|
||||
@@ -206,45 +206,13 @@ func createSut(t *testing.T, messageTmpl string, subjectTmpl string, emailTmpl *
|
||||
func getSingleSentMessage(t *testing.T, ns *emailSender) *notifications.Message {
|
||||
t.Helper()
|
||||
|
||||
mailer := ns.ns.GetMailer().(*notifications.FakeMailer)
|
||||
mailer := ns.ns.(*notifications.NotificationService).GetMailer().(*notifications.FakeMailer)
|
||||
require.Len(t, mailer.Sent, 1)
|
||||
sent := mailer.Sent[0]
|
||||
mailer.Sent = []*notifications.Message{}
|
||||
return sent
|
||||
}
|
||||
|
||||
type emailSender struct {
|
||||
ns *notifications.NotificationService
|
||||
}
|
||||
|
||||
func (e emailSender) SendWebhook(ctx context.Context, cmd *receivers.SendWebhookSettings) error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (e emailSender) SendEmail(ctx context.Context, cmd *receivers.SendEmailSettings) error {
|
||||
sendEmailCommand := notifications.SendEmailCommand{
|
||||
To: cmd.To,
|
||||
SingleEmail: cmd.SingleEmail,
|
||||
Template: cmd.Template,
|
||||
Subject: cmd.Subject,
|
||||
Data: cmd.Data,
|
||||
ReplyTo: cmd.ReplyTo,
|
||||
EmbeddedFiles: cmd.EmbeddedFiles,
|
||||
}
|
||||
if len(cmd.EmbeddedContents) > 0 {
|
||||
sendEmailCommand.EmbeddedContents = make([]notifications.EmbeddedContent, len(cmd.EmbeddedContents))
|
||||
for i, ec := range cmd.EmbeddedContents {
|
||||
sendEmailCommand.EmbeddedContents[i] = notifications.EmbeddedContent{
|
||||
Name: ec.Name,
|
||||
Content: ec.Content,
|
||||
}
|
||||
}
|
||||
}
|
||||
return e.ns.SendEmailCommandHandlerSync(ctx, ¬ifications.SendEmailCommandSync{
|
||||
SendEmailCommand: sendEmailCommand,
|
||||
})
|
||||
}
|
||||
|
||||
func createEmailSender(t *testing.T) *emailSender {
|
||||
t.Helper()
|
||||
|
||||
|
||||
@@ -8,25 +8,11 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/notifications"
|
||||
)
|
||||
|
||||
type sender struct {
|
||||
type emailSender struct {
|
||||
ns notifications.Service
|
||||
}
|
||||
|
||||
func (s sender) SendWebhook(ctx context.Context, cmd *receivers.SendWebhookSettings) error {
|
||||
return s.ns.SendWebhookSync(ctx, ¬ifications.SendWebhookSync{
|
||||
Url: cmd.URL,
|
||||
User: cmd.User,
|
||||
Password: cmd.Password,
|
||||
Body: cmd.Body,
|
||||
HttpMethod: cmd.HTTPMethod,
|
||||
HttpHeader: cmd.HTTPHeader,
|
||||
ContentType: cmd.ContentType,
|
||||
Validation: cmd.Validation,
|
||||
TLSConfig: cmd.TLSConfig,
|
||||
})
|
||||
}
|
||||
|
||||
func (s sender) SendEmail(ctx context.Context, cmd *receivers.SendEmailSettings) error {
|
||||
func (s emailSender) SendEmail(ctx context.Context, cmd *receivers.SendEmailSettings) error {
|
||||
sendEmailCommand := notifications.SendEmailCommand{
|
||||
To: cmd.To,
|
||||
SingleEmail: cmd.SingleEmail,
|
||||
|
||||
@@ -7,10 +7,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
alertingHTTP "github.com/grafana/alerting/http"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
@@ -71,7 +71,7 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
|
||||
request.Header.Set(k, v)
|
||||
}
|
||||
|
||||
resp, err := alertingHTTP.NewTLSClient(webhook.TLSConfig).Do(request)
|
||||
resp, err := NewTLSClient(webhook.TLSConfig).Do(request)
|
||||
if err != nil {
|
||||
return redactURL(err)
|
||||
}
|
||||
@@ -111,3 +111,26 @@ func redactURL(err error) error {
|
||||
e.URL = "<redacted>"
|
||||
return e
|
||||
}
|
||||
|
||||
// NewTLSClient creates a new HTTP client with the provided TLS configuration or with default settings.
|
||||
func NewTLSClient(tlsConfig *tls.Config) *http.Client {
|
||||
nc := func(tlsConfig *tls.Config) *http.Client {
|
||||
return &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 5 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if tlsConfig == nil {
|
||||
return nc(&tls.Config{Renegotiation: tls.RenegotiateFreelyAsClient})
|
||||
}
|
||||
|
||||
return nc(tlsConfig)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user