Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signature support, fix #41 #49

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
targets:
webhook1:
url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
# secret for signature
secret: this_is_secret
webhook2:
url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

type Target struct {
URL string `yaml:"url"`
URL string `yaml:"url"`
Secret string `yaml:"secret"`
}
43 changes: 36 additions & 7 deletions notifier/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package notifier

import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"net/http"
"net/url"
"strconv"
"time"

"github.com/pkg/errors"

"github.com/timonwong/prometheus-webhook-dingtalk/config"
"github.com/timonwong/prometheus-webhook-dingtalk/models"
"github.com/timonwong/prometheus-webhook-dingtalk/template"
)
Expand All @@ -33,30 +39,53 @@ func BuildDingTalkNotification(promMessage *models.WebhookMessage) (*models.Ding
return notification, nil
}

func SendDingTalkNotification(httpClient *http.Client, webhookURL string, notification *models.DingTalkNotification) (*models.DingTalkNotificationResponse, error) {
func SendDingTalkNotification(httpClient *http.Client, target config.Target, notification *models.DingTalkNotification) (*models.DingTalkNotificationResponse, error) {
body, err := json.Marshal(&notification)
if err != nil {
return nil, errors.Wrap(err, "error encoding DingTalk request")
}

httpReq, err := http.NewRequest("POST", webhookURL, bytes.NewReader(body))
targetURL := target.URL
// Calculate signature when secret is provided
if target.Secret != "" {
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
stringToSign := []byte(timestamp + "\n" + target.Secret)

mac := hmac.New(sha256.New, []byte(target.Secret))
mac.Write(stringToSign) // nolint: errcheck
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))

u, err := url.Parse(targetURL)
if err != nil {
return nil, errors.Wrap(err, "failed to parse target url")
}

qs := u.Query()
qs.Set("timestamp", timestamp)
qs.Set("sign", signature)
u.RawQuery = qs.Encode()

targetURL = u.String()
}

httpReq, err := http.NewRequest("POST", targetURL, bytes.NewReader(body))
if err != nil {
return nil, errors.Wrap(err, "error building DingTalk request")
}
httpReq.Header.Set("Content-Type", "application/json")

req, err := httpClient.Do(httpReq)
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, errors.Wrap(err, "error sending notification to DingTalk")
}
defer req.Body.Close()
defer resp.Body.Close()

if req.StatusCode != 200 {
return nil, errors.Errorf("unacceptable response code %d", req.StatusCode)
if resp.StatusCode != 200 {
return nil, errors.Errorf("unacceptable response code %d", resp.StatusCode)
}

var robotResp models.DingTalkNotificationResponse
enc := json.NewDecoder(req.Body)
enc := json.NewDecoder(resp.Body)
if err := enc.Decode(&robotResp); err != nil {
return nil, errors.Wrap(err, "error decoding response from DingTalk")
}
Expand Down
2 changes: 1 addition & 1 deletion webrouter/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (rs *DingTalkResource) SendNotification(w http.ResponseWriter, r *http.Requ
return
}

robotResp, err := notifier.SendDingTalkNotification(rs.HttpClient, target.URL, notification)
robotResp, err := notifier.SendDingTalkNotification(rs.HttpClient, target, notification)
if err != nil {
level.Error(logger).Log("msg", "Failed to send notification", "err", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
Expand Down