tech(alerting): enforce POST for webhooks

This commit is contained in:
bergquist 2016-06-16 08:15:48 +02:00
parent efea3bc9cb
commit 2e809cae05
4 changed files with 33 additions and 29 deletions

View File

@ -14,10 +14,9 @@ type SendEmailCommand struct {
type SendWebhook struct {
Url string
AuthUser string
AuthPassword string
User string
Password string
Body string
Method string
}
type SendResetPasswordEmailCommand struct {

View File

@ -67,9 +67,8 @@ func (this *EmailNotifier) Dispatch(alertResult *AlertResult) {
type WebhookNotifier struct {
Url string
Method string
AuthUser string
AuthPassword string
User string
Password string
log log.Logger
}
@ -77,7 +76,9 @@ func (this *WebhookNotifier) Dispatch(alertResult *AlertResult) {
this.log.Info("Sending webhook")
cmd := &m.SendWebhook{
Url: this.Url,
Method: this.Method,
User: this.User,
Password: this.Password,
Body: alertResult.Description,
}
bus.Dispatch(cmd)
@ -131,9 +132,8 @@ 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(),
User: settings.Get("user").MustString(),
Password: settings.Get("password").MustString(),
log: log.New("alerting.notification.webhook"),
}
}

View File

@ -59,9 +59,8 @@ func Init() error {
func sendWebhook(cmd *m.SendWebhook) error {
addToWebhookQueue(&Webhook{
Url: cmd.Url,
AuthUser: cmd.AuthUser,
AuthPassword: cmd.AuthPassword,
Method: cmd.Method,
User: cmd.User,
Password: cmd.Password,
Body: cmd.Body,
})

View File

@ -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
User string
Password string
Body string
Method 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