2021-04-15 05:31:41 -05:00
|
|
|
package channels
|
|
|
|
|
|
|
|
import (
|
2021-05-19 08:27:41 -05:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-05-20 03:12:08 -05:00
|
|
|
"path"
|
2021-05-19 08:27:41 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-04-15 05:31:41 -05:00
|
|
|
"github.com/prometheus/common/model"
|
2021-05-18 03:04:47 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2021-04-15 05:31:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FooterIconURL = "https://grafana.com/assets/img/fav32.png"
|
|
|
|
ColorAlertFiring = "#D63232"
|
|
|
|
ColorAlertResolved = "#36a64f"
|
|
|
|
)
|
|
|
|
|
2021-07-19 03:58:35 -05:00
|
|
|
type receiverInitError struct {
|
|
|
|
Reason string
|
|
|
|
Err error
|
|
|
|
Cfg NotificationChannelConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e receiverInitError) Error() string {
|
|
|
|
name := ""
|
|
|
|
if e.Cfg.Name != "" {
|
|
|
|
name = fmt.Sprintf("%q ", e.Cfg.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf("failed to validate receiver %sof type %q: %s", name, e.Cfg.Type, e.Reason)
|
|
|
|
if e.Err != nil {
|
|
|
|
return fmt.Sprintf("%s: %s", s, e.Err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e receiverInitError) Unwrap() error { return e.Err }
|
|
|
|
|
2021-04-15 05:31:41 -05:00
|
|
|
func getAlertStatusColor(status model.AlertStatus) string {
|
|
|
|
if status == model.AlertFiring {
|
|
|
|
return ColorAlertFiring
|
|
|
|
}
|
|
|
|
return ColorAlertResolved
|
|
|
|
}
|
2021-05-18 03:04:47 -05:00
|
|
|
|
|
|
|
type NotificationChannelConfig struct {
|
2021-10-07 09:33:50 -05:00
|
|
|
UID string `json:"uid"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
DisableResolveMessage bool `json:"disableResolveMessage"`
|
|
|
|
Settings *simplejson.Json `json:"settings"`
|
|
|
|
SecureSettings map[string][]byte `json:"secureSettings"`
|
2021-05-18 03:04:47 -05:00
|
|
|
}
|
2021-05-19 08:27:41 -05:00
|
|
|
|
|
|
|
type httpCfg struct {
|
|
|
|
body []byte
|
|
|
|
user string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendHTTPRequest sends an HTTP request.
|
|
|
|
// Stubbable by tests.
|
|
|
|
var sendHTTPRequest = func(ctx context.Context, url *url.URL, cfg httpCfg, logger log.Logger) ([]byte, error) {
|
|
|
|
var reader io.Reader
|
|
|
|
if len(cfg.body) > 0 {
|
|
|
|
reader = bytes.NewReader(cfg.body)
|
|
|
|
}
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodPost, url.String(), reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
|
|
|
|
}
|
|
|
|
if cfg.user != "" && cfg.password != "" {
|
|
|
|
request.Header.Set("Authorization", util.GetBasicAuthHeader(cfg.user, cfg.password))
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
request.Header.Set("User-Agent", "Grafana")
|
|
|
|
netTransport := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
Renegotiation: tls.RenegotiateFreelyAsClient,
|
|
|
|
},
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
}).DialContext,
|
|
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
netClient := &http.Client{
|
|
|
|
Timeout: time.Second * 30,
|
|
|
|
Transport: netTransport,
|
|
|
|
}
|
|
|
|
resp, err := netClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
logger.Warn("Failed to close response body", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read response body: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
logger.Warn("HTTP request failed", "url", request.URL.String(), "statusCode", resp.Status, "body",
|
|
|
|
string(respBody))
|
|
|
|
return nil, fmt.Errorf("failed to send HTTP request - status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("Sending HTTP request succeeded", "url", request.URL.String(), "statusCode", resp.Status)
|
|
|
|
return respBody, nil
|
|
|
|
}
|
2021-05-20 03:12:08 -05:00
|
|
|
|
2021-06-03 09:09:32 -05:00
|
|
|
func joinUrlPath(base, additionalPath string, logger log.Logger) string {
|
2021-05-20 03:12:08 -05:00
|
|
|
u, err := url.Parse(base)
|
|
|
|
if err != nil {
|
2021-06-03 09:09:32 -05:00
|
|
|
logger.Debug("failed to parse URL while joining URL", "url", base, "err", err.Error())
|
|
|
|
return base
|
2021-05-20 03:12:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
u.Path = path.Join(u.Path, additionalPath)
|
|
|
|
|
2021-06-03 09:09:32 -05:00
|
|
|
return u.String()
|
2021-05-20 03:12:08 -05:00
|
|
|
}
|
2021-05-26 06:03:55 -05:00
|
|
|
|
|
|
|
// GetBoundary is used for overriding the behaviour for tests
|
|
|
|
// and set a boundary for multipart body. DO NOT set this outside tests.
|
|
|
|
var GetBoundary = func() string {
|
|
|
|
return ""
|
|
|
|
}
|