-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
webhook.go
138 lines (121 loc) · 2.71 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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.
// +build !oss
package webhook
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"net/http"
"path/filepath"
"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.
func New(config Config) core.WebhookSender {
return &sender{
Events: config.Events,
Endpoints: config.Endpoint,
Secret: config.Secret,
System: config.System,
}
}
type payload struct {
*core.WebhookData
System *core.System `json:"system,omitempty"`
}
type sender struct {
Client *http.Client
Events []string
Endpoints []string
Secret string
System *core.System
}
// Send sends the JSON encoded webhook to the global
// HTTP endpoints.
func (s *sender) Send(ctx context.Context, in *core.WebhookData) error {
if len(s.Endpoints) == 0 {
return nil
}
if s.match(in.Event, in.Action) == false {
return nil
}
wrapper := payload{
WebhookData: in,
System: s.System,
}
data, _ := json.Marshal(wrapper)
for _, endpoint := range s.Endpoints {
err := s.send(endpoint, s.Secret, in.Event, data)
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)
if err != nil {
return err
}
res, err := s.client().Do(req)
if res != nil {
res.Body.Close()
}
return err
}
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
}
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))
}