-
Notifications
You must be signed in to change notification settings - Fork 361
/
webhook.go
241 lines (207 loc) · 6.18 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package actions
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httputil"
"time"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/logging"
)
type Webhook struct {
HookBase
URL string
Timeout time.Duration
QueryParams map[string][]SecureString
Headers map[string]SecureString
}
const (
webhookClientDefaultTimeout = 1 * time.Minute
webhookTimeoutPropertyKey = "timeout"
webhookURLPropertyKey = "url"
queryParamsPropertyKey = "query_params"
HeadersPropertyKey = "headers"
)
var (
errWebhookRequestFailed = errors.New("webhook request failed")
errWebhookWrongFormat = errors.New("webhook wrong format")
)
func NewWebhook(h ActionHook, action *Action, e *http.Server) (Hook, error) {
url, ok := h.Properties[webhookURLPropertyKey]
if !ok {
return nil, fmt.Errorf("missing url: %w", errWebhookWrongFormat)
}
webhookURL, ok := url.(string)
if !ok {
return nil, fmt.Errorf("webhook url must be string: %w", errWebhookWrongFormat)
}
queryParams, err := extractQueryParams(h.Properties)
if err != nil {
return nil, fmt.Errorf("extracting query params: %w", err)
}
headers, err := extractHeaders(h.Properties)
if err != nil {
return nil, fmt.Errorf("extracting headers: %w", err)
}
requestTimeout := webhookClientDefaultTimeout
if timeoutDuration, ok := h.Properties[webhookTimeoutPropertyKey]; ok {
if timeout, ok := timeoutDuration.(string); ok && len(timeout) > 0 {
d, err := time.ParseDuration(timeout)
if err != nil {
return nil, fmt.Errorf("webhook request duration: %w", err)
}
requestTimeout = d
}
}
return &Webhook{
HookBase: HookBase{
ID: h.ID,
ActionName: action.Name,
Endpoint: e,
},
Timeout: requestTimeout,
URL: webhookURL,
QueryParams: queryParams,
Headers: headers,
}, nil
}
func (w *Webhook) Run(ctx context.Context, record graveler.HookRecord, buf *bytes.Buffer) (err error) {
// post event information as json to webhook endpoint
logging.FromContext(ctx).
WithField("hook_type", "webhook").
WithField("event_type", record.EventType).
Debug("hook action executing")
eventData, err := marshalEventInformation(w.ActionName, w.ID, record)
if err != nil {
return err
}
_, _ = fmt.Fprintf(buf, "Request:\n%s %s\n", http.MethodPost, w.URL)
reqReader := bytes.NewReader(eventData)
req, err := http.NewRequest(http.MethodPost, w.URL, reqReader)
if err != nil {
return err
}
w.Headers["Content-Type"] = SecureString{val: "application/json"}
buf.WriteString("Query Params:\n")
q := req.URL.Query()
for k, vals := range w.QueryParams {
for _, v := range vals {
q.Add(k, v.val)
_, _ = fmt.Fprintf(buf, "%s: %s\n", k, v.String())
}
}
buf.WriteString("Headers:\n")
for k, v := range w.Headers {
req.Header.Add(k, v.val)
_, _ = fmt.Fprintf(buf, "%s: %s\n", k, v.String())
}
req.URL.RawQuery = q.Encode()
_, _ = fmt.Fprintf(buf, "Request Body:\n%s\n\n", eventData)
statusCode, err := doHTTPRequestWithLog(ctx, req, buf, w.Timeout)
if err != nil {
return err
}
// check status code
if statusCode < 200 || statusCode >= 300 {
return fmt.Errorf("%w (status code: %d)", errWebhookRequestFailed, statusCode)
}
return nil
}
// doHTTPRequestWithLog helper that uses 'doHTTPRequestResponseWithLog' without response parse
func doHTTPRequestWithLog(ctx context.Context, req *http.Request, buf *bytes.Buffer, timeout time.Duration) (n int, err error) {
return doHTTPRequestResponseWithLog(ctx, req, nil, buf, timeout)
}
// doHTTPRequestResponseWithLog execute a http request with specified timeout. Output variable 'respJSON', if set, used to json decode the response.
// returns the response status code or -1 on error
func doHTTPRequestResponseWithLog(ctx context.Context, req *http.Request, respJSON interface{}, buf *bytes.Buffer, timeout time.Duration) (int, error) {
req = req.WithContext(ctx)
client := &http.Client{
Timeout: timeout,
}
start := time.Now()
resp, err := client.Do(req)
elapsed := time.Since(start)
_, _ = fmt.Fprintf(buf, "\nRequest duration: %s\n", elapsed)
if err != nil {
return -1, err
}
defer func() {
_ = resp.Body.Close()
}()
buf.WriteString("\nResponse:\n")
if dumpResp, err := httputil.DumpResponse(resp, true); err == nil {
buf.Write(dumpResp)
} else {
_, _ = fmt.Fprintf(buf, "Failed dumping response: %s", err)
}
if respJSON != nil {
err = json.NewDecoder(resp.Body).Decode(&respJSON)
if err != nil {
return -1, err
}
}
return resp.StatusCode, nil
}
func extractQueryParams(props map[string]interface{}) (map[string][]SecureString, error) {
params, ok := props[queryParamsPropertyKey]
if !ok {
return nil, nil
}
paramsMap, ok := params.(Properties)
if !ok {
return nil, fmt.Errorf("unsupported query params: %w", errWebhookWrongFormat)
}
res := map[string][]SecureString{}
for k, v := range paramsMap {
if ar, ok := v.([]interface{}); ok {
for _, v := range ar {
av, ok := v.(string)
if !ok {
return nil, fmt.Errorf("query params array should contains only strings: %w", errWebhookWrongFormat)
}
avs, err := NewSecureString(av)
if err != nil {
return nil, fmt.Errorf("reading query param: %w", err)
}
res[k] = append(res[k], avs)
}
continue
}
av, ok := v.(string)
if !ok {
return nil, fmt.Errorf("query params single value should be of type string: %w", errWebhookWrongFormat)
}
avs, err := NewSecureString(av)
if err != nil {
return nil, fmt.Errorf("reading query param: %w", err)
}
res[k] = []SecureString{avs}
}
return res, nil
}
func extractHeaders(props map[string]interface{}) (map[string]SecureString, error) {
params, ok := props[HeadersPropertyKey]
if !ok {
return map[string]SecureString{}, nil
}
paramsMap, ok := params.(Properties)
if !ok {
return nil, fmt.Errorf("unsupported headers: %w", errWebhookWrongFormat)
}
res := map[string]SecureString{}
for k, v := range paramsMap {
vs, ok := v.(string)
if !ok {
return nil, fmt.Errorf("headers array should contains only strings: %w", errWebhookWrongFormat)
}
vss, err := NewSecureString(vs)
if err != nil {
return nil, fmt.Errorf("reading header: %w", err)
}
res[k] = vss
}
return res, nil
}