mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
tech(alerting): enforce POST for webhooks
This commit is contained in:
@@ -14,10 +14,9 @@ type SendEmailCommand struct {
|
|||||||
|
|
||||||
type SendWebhook struct {
|
type SendWebhook struct {
|
||||||
Url string
|
Url string
|
||||||
AuthUser string
|
User string
|
||||||
AuthPassword string
|
Password string
|
||||||
Body string
|
Body string
|
||||||
Method string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SendResetPasswordEmailCommand struct {
|
type SendResetPasswordEmailCommand struct {
|
||||||
|
|||||||
@@ -67,9 +67,8 @@ func (this *EmailNotifier) Dispatch(alertResult *AlertResult) {
|
|||||||
|
|
||||||
type WebhookNotifier struct {
|
type WebhookNotifier struct {
|
||||||
Url string
|
Url string
|
||||||
Method string
|
User string
|
||||||
AuthUser string
|
Password string
|
||||||
AuthPassword string
|
|
||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +76,9 @@ func (this *WebhookNotifier) Dispatch(alertResult *AlertResult) {
|
|||||||
this.log.Info("Sending webhook")
|
this.log.Info("Sending webhook")
|
||||||
cmd := &m.SendWebhook{
|
cmd := &m.SendWebhook{
|
||||||
Url: this.Url,
|
Url: this.Url,
|
||||||
Method: this.Method,
|
User: this.User,
|
||||||
|
Password: this.Password,
|
||||||
|
Body: alertResult.Description,
|
||||||
}
|
}
|
||||||
|
|
||||||
bus.Dispatch(cmd)
|
bus.Dispatch(cmd)
|
||||||
@@ -131,9 +132,8 @@ var createNotifier = func(notificationType string, settings *simplejson.Json) No
|
|||||||
|
|
||||||
return &WebhookNotifier{
|
return &WebhookNotifier{
|
||||||
Url: settings.Get("url").MustString(),
|
Url: settings.Get("url").MustString(),
|
||||||
Method: settings.Get("method").MustString(),
|
User: settings.Get("user").MustString(),
|
||||||
AuthUser: settings.Get("user").MustString(),
|
Password: settings.Get("password").MustString(),
|
||||||
AuthPassword: settings.Get("password").MustString(),
|
|
||||||
log: log.New("alerting.notification.webhook"),
|
log: log.New("alerting.notification.webhook"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,9 +59,8 @@ func Init() error {
|
|||||||
func sendWebhook(cmd *m.SendWebhook) error {
|
func sendWebhook(cmd *m.SendWebhook) error {
|
||||||
addToWebhookQueue(&Webhook{
|
addToWebhookQueue(&Webhook{
|
||||||
Url: cmd.Url,
|
Url: cmd.Url,
|
||||||
AuthUser: cmd.AuthUser,
|
User: cmd.User,
|
||||||
AuthPassword: cmd.AuthPassword,
|
Password: cmd.Password,
|
||||||
Method: cmd.Method,
|
|
||||||
Body: cmd.Body,
|
Body: cmd.Body,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
package notifications
|
package notifications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
|
"github.com/grafana/grafana/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Webhook struct {
|
type Webhook struct {
|
||||||
Url string
|
Url string
|
||||||
AuthUser string
|
User string
|
||||||
AuthPassword string
|
Password string
|
||||||
Body string
|
Body string
|
||||||
Method string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var webhookQueue chan *Webhook
|
var webhookQueue chan *Webhook
|
||||||
@@ -40,9 +41,14 @@ func processWebhookQueue() {
|
|||||||
func sendWebRequest(webhook *Webhook) error {
|
func sendWebRequest(webhook *Webhook) error {
|
||||||
webhookLog.Error("Sending stuff! ", "url", webhook.Url)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user