2016-06-15 07:45:05 -05:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
2016-06-16 01:15:48 -05:00
|
|
|
"bytes"
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2019-01-11 01:56:50 -06:00
|
|
|
"crypto/tls"
|
2016-07-27 05:09:55 -05:00
|
|
|
"fmt"
|
2018-12-27 06:16:58 -06:00
|
|
|
"io"
|
2016-07-27 05:09:55 -05:00
|
|
|
"io/ioutil"
|
2017-02-24 10:22:12 -06:00
|
|
|
"net"
|
2016-06-15 07:45:05 -05:00
|
|
|
"net/http"
|
2017-02-24 10:22:12 -06:00
|
|
|
"time"
|
2016-06-15 07:45:05 -05:00
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
"golang.org/x/net/context/ctxhttp"
|
|
|
|
|
2016-06-16 01:15:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2016-06-15 07:45:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Webhook struct {
|
2017-03-23 15:53:54 -05:00
|
|
|
Url string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Body string
|
|
|
|
HttpMethod string
|
|
|
|
HttpHeader map[string]string
|
|
|
|
ContentType string
|
2016-06-15 07:45:05 -05:00
|
|
|
}
|
|
|
|
|
2017-02-24 10:22:12 -06:00
|
|
|
var netTransport = &http.Transport{
|
2019-01-11 01:56:50 -06:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
Renegotiation: tls.RenegotiateFreelyAsClient,
|
|
|
|
},
|
2017-03-02 06:57:28 -06:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2017-02-24 10:22:12 -06:00
|
|
|
Dial: (&net.Dialer{
|
2019-05-08 03:37:48 -05:00
|
|
|
Timeout: 30 * time.Second,
|
2017-02-24 10:22:12 -06:00
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
var netClient = &http.Client{
|
|
|
|
Timeout: time.Second * 30,
|
|
|
|
Transport: netTransport,
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:01:32 -05:00
|
|
|
func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *Webhook) error {
|
|
|
|
ns.log.Debug("Sending webhook", "url", webhook.Url, "http method", webhook.HttpMethod)
|
2016-07-27 05:09:55 -05:00
|
|
|
|
2016-10-18 09:18:16 -05:00
|
|
|
if webhook.HttpMethod == "" {
|
|
|
|
webhook.HttpMethod = http.MethodPost
|
|
|
|
}
|
|
|
|
|
2020-11-24 03:42:54 -06:00
|
|
|
if webhook.HttpMethod != http.MethodPost && webhook.HttpMethod != http.MethodPut {
|
|
|
|
return fmt.Errorf("webhook only supports HTTP methods PUT or POST")
|
|
|
|
}
|
|
|
|
|
2016-10-18 09:18:16 -05:00
|
|
|
request, err := http.NewRequest(webhook.HttpMethod, webhook.Url, bytes.NewReader([]byte(webhook.Body)))
|
2016-06-15 07:45:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-23 15:53:54 -05:00
|
|
|
if webhook.ContentType == "" {
|
|
|
|
webhook.ContentType = "application/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", webhook.ContentType)
|
2016-12-05 03:44:31 -06:00
|
|
|
request.Header.Add("User-Agent", "Grafana")
|
2018-05-07 08:40:43 -05:00
|
|
|
|
2016-12-05 03:44:31 -06:00
|
|
|
if webhook.User != "" && webhook.Password != "" {
|
|
|
|
request.Header.Add("Authorization", util.GetBasicAuthHeader(webhook.User, webhook.Password))
|
|
|
|
}
|
|
|
|
|
2017-01-19 02:29:40 -06:00
|
|
|
for k, v := range webhook.HttpHeader {
|
|
|
|
request.Header.Set(k, v)
|
2017-01-19 01:25:21 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 10:22:12 -06:00
|
|
|
resp, err := ctxhttp.Do(ctx, netClient, request)
|
2016-06-15 07:45:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-27 06:16:58 -06:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
if resp.StatusCode/100 == 2 {
|
2020-06-26 13:47:06 -05:00
|
|
|
ns.log.Debug("Webhook succeeded", "url", webhook.Url, "statuscode", resp.Status)
|
2018-12-27 06:16:58 -06:00
|
|
|
// flushing the body enables the transport to reuse the same connection
|
2019-10-22 07:08:18 -05:00
|
|
|
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
|
|
|
|
ns.log.Error("Failed to copy resp.Body to ioutil.Discard", "err", err)
|
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2020-06-26 13:47:06 -05:00
|
|
|
ns.log.Debug("Webhook failed", "url", webhook.Url, "statuscode", resp.Status, "body", string(body))
|
2016-10-03 02:38:03 -05:00
|
|
|
return fmt.Errorf("Webhook response status %v", resp.Status)
|
2016-06-15 07:45:05 -05:00
|
|
|
}
|