From 2e809cae057ccb82f569408742203eda7e72e8d6 Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 16 Jun 2016 08:15:48 +0200 Subject: [PATCH] tech(alerting): enforce POST for webhooks --- pkg/models/notifications.go | 9 ++++---- pkg/services/alerting/notifier.go | 24 ++++++++++----------- pkg/services/notifications/notifications.go | 9 ++++---- pkg/services/notifications/webhook.go | 20 +++++++++++------ 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index cd62a4c046d..d357b9cf562 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -13,11 +13,10 @@ type SendEmailCommand struct { } type SendWebhook struct { - Url string - AuthUser string - AuthPassword string - Body string - Method string + Url string + User string + Password string + Body string } type SendResetPasswordEmailCommand struct { diff --git a/pkg/services/alerting/notifier.go b/pkg/services/alerting/notifier.go index 7378ff45fda..ec48a8fbcb7 100644 --- a/pkg/services/alerting/notifier.go +++ b/pkg/services/alerting/notifier.go @@ -66,18 +66,19 @@ func (this *EmailNotifier) Dispatch(alertResult *AlertResult) { } type WebhookNotifier struct { - Url string - Method string - AuthUser string - AuthPassword string - log log.Logger + Url string + User string + Password string + log log.Logger } func (this *WebhookNotifier) Dispatch(alertResult *AlertResult) { this.log.Info("Sending webhook") cmd := &m.SendWebhook{ - Url: this.Url, - Method: this.Method, + Url: this.Url, + User: this.User, + Password: this.Password, + Body: alertResult.Description, } bus.Dispatch(cmd) @@ -130,10 +131,9 @@ var createNotifier = func(notificationType string, settings *simplejson.Json) No } return &WebhookNotifier{ - Url: settings.Get("url").MustString(), - Method: settings.Get("method").MustString(), - AuthUser: settings.Get("user").MustString(), - AuthPassword: settings.Get("password").MustString(), - log: log.New("alerting.notification.webhook"), + Url: settings.Get("url").MustString(), + User: settings.Get("user").MustString(), + Password: settings.Get("password").MustString(), + log: log.New("alerting.notification.webhook"), } } diff --git a/pkg/services/notifications/notifications.go b/pkg/services/notifications/notifications.go index a22acbc2d36..04b11f73b84 100644 --- a/pkg/services/notifications/notifications.go +++ b/pkg/services/notifications/notifications.go @@ -58,11 +58,10 @@ func Init() error { func sendWebhook(cmd *m.SendWebhook) error { addToWebhookQueue(&Webhook{ - Url: cmd.Url, - AuthUser: cmd.AuthUser, - AuthPassword: cmd.AuthPassword, - Method: cmd.Method, - Body: cmd.Body, + Url: cmd.Url, + User: cmd.User, + Password: cmd.Password, + Body: cmd.Body, }) return nil diff --git a/pkg/services/notifications/webhook.go b/pkg/services/notifications/webhook.go index 7d591ddb26b..11a8839fbc8 100644 --- a/pkg/services/notifications/webhook.go +++ b/pkg/services/notifications/webhook.go @@ -1,18 +1,19 @@ package notifications import ( + "bytes" "net/http" "time" "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/util" ) type Webhook struct { - Url string - AuthUser string - AuthPassword string - Body string - Method string + Url string + User string + Password string + Body string } var webhookQueue chan *Webhook @@ -40,9 +41,14 @@ func processWebhookQueue() { func sendWebRequest(webhook *Webhook) error { webhookLog.Error("Sending stuff! ", "url", webhook.Url) - client := http.Client{Timeout: time.Duration(3 * time.Second)} + client := http.Client{ + Timeout: time.Duration(3 * time.Second), + } - request, err := http.NewRequest(webhook.Method, webhook.Url, nil /*io.reader*/) + request, err := http.NewRequest("POST", webhook.Url, bytes.NewReader([]byte(webhook.Body))) + if webhook.User != "" && webhook.Password != "" { + request.Header.Add("Authorization", util.GetBasicAuthHeader(webhook.User, webhook.Password)) + } if err != nil { return err