2019-02-19 23:56:41 +00:00
|
|
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-03-01 00:23:06 +00:00
|
|
|
// +build !oss
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-07-28 19:22:16 +00:00
|
|
|
"path/filepath"
|
2019-02-19 23:56:41 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
|
|
|
|
"github.com/99designs/httpsignatures-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// required http headers
|
|
|
|
var headers = []string{
|
|
|
|
"date",
|
|
|
|
"digest",
|
|
|
|
}
|
|
|
|
|
|
|
|
var signer = httpsignatures.NewSigner(
|
|
|
|
httpsignatures.AlgorithmHmacSha256,
|
|
|
|
headers...,
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new Webhook sender.
|
2019-04-16 15:21:05 +00:00
|
|
|
func New(config Config) core.WebhookSender {
|
2019-02-19 23:56:41 +00:00
|
|
|
return &sender{
|
2019-07-28 19:22:16 +00:00
|
|
|
Events: config.Events,
|
2019-04-16 15:21:05 +00:00
|
|
|
Endpoints: config.Endpoint,
|
|
|
|
Secret: config.Secret,
|
|
|
|
System: config.System,
|
2019-02-19 23:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:21:05 +00:00
|
|
|
type payload struct {
|
|
|
|
*core.WebhookData
|
|
|
|
System *core.System `json:"system,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
type sender struct {
|
|
|
|
Client *http.Client
|
2019-07-28 19:22:16 +00:00
|
|
|
Events []string
|
2019-02-19 23:56:41 +00:00
|
|
|
Endpoints []string
|
|
|
|
Secret string
|
2019-04-16 15:21:05 +00:00
|
|
|
System *core.System
|
2019-02-19 23:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send sends the JSON encoded webhook to the global
|
|
|
|
// HTTP endpoints.
|
2019-04-16 15:21:05 +00:00
|
|
|
func (s *sender) Send(ctx context.Context, in *core.WebhookData) error {
|
2019-02-19 23:56:41 +00:00
|
|
|
if len(s.Endpoints) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-28 19:22:16 +00:00
|
|
|
if s.match(in.Event, in.Action) == false {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-16 15:21:05 +00:00
|
|
|
wrapper := payload{
|
|
|
|
WebhookData: in,
|
|
|
|
System: s.System,
|
|
|
|
}
|
|
|
|
data, _ := json.Marshal(wrapper)
|
2019-02-19 23:56:41 +00:00
|
|
|
for _, endpoint := range s.Endpoints {
|
2019-04-16 15:21:05 +00:00
|
|
|
err := s.send(endpoint, s.Secret, in.Event, data)
|
2019-02-19 23:56:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) send(endpoint, secret, event string, data []byte) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
req, err := http.NewRequest("POST", endpoint, buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
req.Header.Add("X-Drone-Event", event)
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.Header.Add("Digest", "SHA-256="+digest(data))
|
|
|
|
req.Header.Add("Date", time.Now().UTC().Format(http.TimeFormat))
|
|
|
|
err = signer.SignRequest("hmac-key", s.Secret, req)
|
2019-09-17 11:43:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-19 23:56:41 +00:00
|
|
|
res, err := s.client().Do(req)
|
|
|
|
if res != nil {
|
|
|
|
res.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-28 19:22:16 +00:00
|
|
|
func (s *sender) match(event, action string) bool {
|
|
|
|
if len(s.Events) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var name string
|
|
|
|
switch {
|
|
|
|
case action == "":
|
|
|
|
name = event
|
|
|
|
case action != "":
|
|
|
|
name = event + ":" + action
|
|
|
|
}
|
|
|
|
for _, pattern := range s.Events {
|
|
|
|
if ok, _ := filepath.Match(pattern, name); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
func (s *sender) client() *http.Client {
|
|
|
|
if s.Client == nil {
|
|
|
|
return http.DefaultClient
|
|
|
|
}
|
|
|
|
return s.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func digest(data []byte) string {
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write(data)
|
|
|
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
|
|
}
|