-
Notifications
You must be signed in to change notification settings - Fork 156
/
hooks.go
531 lines (454 loc) · 16 KB
/
hooks.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package hooks
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"path"
"strconv"
"strings"
"time"
"github.com/buger/jsonparser"
"github.com/hashicorp/go-retryablehttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
otrace "go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/wundergraph/wundergraph/pkg/authentication"
"github.com/wundergraph/wundergraph/pkg/logging"
"github.com/wundergraph/wundergraph/pkg/pool"
"github.com/wundergraph/wundergraph/pkg/trace"
)
type WunderGraphRequest struct {
Method string `json:"method"`
RequestURI string `json:"requestURI"`
Headers map[string]string `json:"headers"`
Body json.RawMessage `json:"body,omitempty"`
}
type WunderGraphResponse struct {
StatusCode int `json:"statusCode"`
Status string `json:"status"`
Method string `json:"method"`
RequestURI string `json:"requestURI"`
Headers map[string]string `json:"headers"`
Body json.RawMessage `json:"body,omitempty"`
}
func HttpRequestToWunderGraphRequestJSON(r *http.Request, withBody bool) ([]byte, error) {
var body []byte
if withBody {
body, _ = ioutil.ReadAll(r.Body)
r.Body = ioutil.NopCloser(bytes.NewReader(body))
}
return json.Marshal(WunderGraphRequest{
Method: r.Method,
RequestURI: r.URL.String(),
Headers: HeaderSliceToCSV(r.Header),
Body: body,
})
}
func HeaderSliceToCSV(headers map[string][]string) map[string]string {
result := make(map[string]string, len(headers))
for k, v := range headers {
result[k] = strings.Join(v, ",")
}
return result
}
func HeaderCSVToSlice(headers map[string]string) map[string][]string {
result := make(map[string][]string, len(headers))
for k, v := range headers {
result[k] = []string{v}
}
return result
}
type HookResponseError struct {
Code string `json:"code"`
Message string `json:"message"`
StatusCode int `json:"statusCode"`
Stack string `json:"stack"`
}
func (e *HookResponseError) Error() string {
return e.Message
}
type HookResponse interface {
HookName() string
OperationName() string
ResponseError() error
}
type OnWsConnectionInitHookPayload struct {
DataSourceID string `json:"dataSourceId"`
Request WunderGraphRequest `json:"request"`
}
type OnRequestHookPayload struct {
Request WunderGraphRequest `json:"request"`
OperationName string `json:"operationName"`
OperationType string `json:"operationType"`
}
type OnRequestHookResponse struct {
Skip bool `json:"skip"`
Cancel bool `json:"cancel"`
Request *WunderGraphRequest `json:"request"`
}
type OnResponseHookPayload struct {
Response WunderGraphResponse `json:"response"`
OperationName string `json:"operationName"`
OperationType string `json:"operationType"`
}
type OnResponseHookResponse struct {
Skip bool `json:"skip"`
Cancel bool `json:"cancel"`
Response *WunderGraphResponse `json:"response"`
}
type MiddlewareHookResponse struct {
Error *HookResponseError `json:"error,omitempty"`
Op string `json:"op"`
Hook string `json:"hook"`
Response json.RawMessage `json:"response"`
Input json.RawMessage `json:"input"`
SetClientRequestHeaders map[string]string `json:"setClientRequestHeaders"`
// ClientResponseStatusCode is the status code that should be returned to the client
ClientResponseStatusCode int
}
func (r *MiddlewareHookResponse) OperationName() string {
return r.Op
}
func (r *MiddlewareHookResponse) HookName() string {
return r.Hook
}
func (r *MiddlewareHookResponse) ResponseError() error {
// XXX: Don't simplify this unless you know what you're doing
if r.Error != nil {
return r.Error
}
return nil
}
type UploadHookResponse struct {
Hook string `json:"hook"`
Error *HookResponseError `json:"error"`
FileKey string `json:"fileKey"`
}
func (r *UploadHookResponse) OperationName() string {
return "upload"
}
func (r *UploadHookResponse) HookName() string {
return r.Hook
}
func (r *UploadHookResponse) ResponseError() error {
// XXX: Don't simplify this unless you know what you're doing
if r.Error != nil {
return r.Error
}
return nil
}
type MiddlewareHook string
const (
MockResolve MiddlewareHook = "mockResolve"
PreResolve MiddlewareHook = "preResolve"
PostResolve MiddlewareHook = "postResolve"
CustomResolve MiddlewareHook = "customResolve"
MutatingPreResolve MiddlewareHook = "mutatingPreResolve"
MutatingPostResolve MiddlewareHook = "mutatingPostResolve"
PostAuthentication MiddlewareHook = "postAuthentication"
PostLogout MiddlewareHook = "postLogout"
MutatingPostAuthentication MiddlewareHook = "mutatingPostAuthentication"
RevalidateAuthentication MiddlewareHook = "revalidateAuthentication"
// HttpTransportOnRequest to the origin
HttpTransportOnRequest MiddlewareHook = "onOriginRequest"
// HttpTransportOnResponse from the origin
HttpTransportOnResponse MiddlewareHook = "onOriginResponse"
HttpTransportOnTransport MiddlewareHook = "onOriginTransport"
WsTransportOnConnectionInit MiddlewareHook = "onConnectionInit"
)
type UploadHook string
const (
PreUpload UploadHook = "preUpload"
PostUpload UploadHook = "postUpload"
)
type Client struct {
serverUrl string
httpClient *retryablehttp.Client
subscriptionsClient *retryablehttp.Client
log *zap.Logger
}
type ClientOptions struct {
ServerURL string
EnableTracing bool
Logger *zap.Logger
}
func NewClient(opts *ClientOptions) *Client {
rt := http.DefaultTransport
if opts.EnableTracing {
rt = trace.NewTransport(rt,
otelhttp.WithSpanOptions(otrace.WithAttributes(trace.HooksClientAttribute)),
)
}
return &Client{
serverUrl: opts.ServerURL,
httpClient: buildClient(60*time.Second, rt),
subscriptionsClient: buildClient(0, rt),
log: opts.Logger,
}
}
func buildClient(requestTimeout time.Duration, rt http.RoundTripper) *retryablehttp.Client {
httpClient := retryablehttp.NewClient()
httpClient.HTTPClient.Transport = rt
// we will try 40 times with a constant delay of 50ms after max 2s we will give up
httpClient.RetryMax = 40
// keep it low and linear to increase the chance
// that we can continue as soon as the server is back from a cold start
httpClient.Backoff = retryablehttp.LinearJitterBackoff
httpClient.RetryWaitMax = 50 * time.Millisecond
httpClient.RetryWaitMin = 50 * time.Millisecond
httpClient.HTTPClient.Timeout = requestTimeout
httpClient.Logger = log.New(io.Discard, "", log.LstdFlags)
httpClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
if resp != nil && resp.StatusCode == http.StatusInternalServerError {
return false, nil
}
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}
return httpClient
}
func (c *Client) DoGlobalRequest(ctx context.Context, hook MiddlewareHook, hookID string, jsonData []byte, buf *bytes.Buffer) (*MiddlewareHookResponse, error) {
return c.doMiddlewareRequest(ctx, "global/httpTransport", hook, hookID, jsonData, buf)
}
func (c *Client) DoWsTransportRequest(ctx context.Context, hook MiddlewareHook, jsonData []byte, buf *bytes.Buffer) (*MiddlewareHookResponse, error) {
return c.doMiddlewareRequest(ctx, "global/wsTransport", hook, "", jsonData, buf)
}
func (c *Client) DoOperationRequest(ctx context.Context, operationName string, hook MiddlewareHook, jsonData []byte, buf *bytes.Buffer) (*MiddlewareHookResponse, error) {
return c.doMiddlewareRequest(ctx, "operation/"+operationName, hook, "", jsonData, buf)
}
func (c *Client) DoFunctionRequest(ctx context.Context, operationName string, jsonData []byte, buf *bytes.Buffer) (*MiddlewareHookResponse, error) {
jsonData = c.setInternalHookData(ctx, jsonData, buf)
r, err := http.NewRequestWithContext(ctx, "POST", c.serverUrl+"/functions/"+operationName, bytes.NewReader(jsonData))
if err != nil {
return nil, err
}
r.Header.Set("Content-Type", "application/json")
r.Header.Set(logging.RequestIDHeader, logging.RequestIDFromContext(ctx))
req, err := retryablehttp.FromRequest(r)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error calling function %s: %w", operationName, err)
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var hookRes MiddlewareHookResponse
err = dec.Decode(&hookRes)
if err != nil {
return nil, fmt.Errorf("error decoding function response: %w", err)
}
if err := hookRes.ResponseError(); err != nil {
if err.Error() == "" {
// Hook failed but didn't report a message
return nil, fmt.Errorf("hook %s failed for operation %s", hookRes.HookName(), hookRes.OperationName())
}
return nil, err
}
hookRes.ClientResponseStatusCode = resp.StatusCode
return &hookRes, nil
}
func (c *Client) DoFunctionSubscriptionRequest(ctx context.Context, operationName string, jsonData []byte, subscribeOnce bool, out io.Writer, buf *bytes.Buffer) error {
jsonData = c.setInternalHookData(ctx, jsonData, buf)
r, err := http.NewRequestWithContext(ctx, "POST", c.serverUrl+"/functions/"+operationName, bytes.NewReader(jsonData))
if err != nil {
return err
}
r.Header.Set("Content-Type", "application/json")
r.Header.Set(logging.RequestIDHeader, logging.RequestIDFromContext(ctx))
if subscribeOnce {
r.Header.Set("X-WG-Subscribe-Once", "true")
}
req, err := retryablehttp.FromRequest(r)
if err != nil {
return err
}
resp, err := c.subscriptionsClient.Do(req)
if err != nil {
return fmt.Errorf("error calling function %s: %w", operationName, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("server function call did not return an ok response: %s", resp.Status)
}
flusher, ok := out.(http.Flusher)
if !ok {
return fmt.Errorf("client connection is not flushable")
}
reader := bufio.NewReader(resp.Body)
var (
line []byte
)
for {
line, err = reader.ReadBytes('\n')
if err == nil {
// Read the next line separator
var b byte
b, err = reader.ReadByte()
if err == io.EOF {
err = nil
}
if b != '\n' {
return fmt.Errorf("invalid chunk separator %s", string(b))
}
}
if err != nil {
if err == io.EOF {
return nil
}
return err
}
// Skip the \n terminator
_, err = out.Write(line[:len(line)-1])
if err != nil {
if ctx.Err() != nil {
return nil
}
return fmt.Errorf("error writing to client: %w", err)
}
flusher.Flush()
}
}
func (c *Client) DoAuthenticationRequest(ctx context.Context, hook MiddlewareHook, jsonData []byte, buf *bytes.Buffer) (*MiddlewareHookResponse, error) {
return c.doMiddlewareRequest(ctx, "authentication", hook, "", jsonData, buf)
}
func (c *Client) DoUploadRequest(ctx context.Context, providerName string, profileName string, hook UploadHook, jsonData []byte, buf *bytes.Buffer) (*UploadHookResponse, error) {
var hookResponse UploadHookResponse
if err := c.doRequest(ctx, &hookResponse, path.Join("upload", providerName, profileName), MiddlewareHook(hook), "", jsonData, buf); err != nil {
return nil, err
}
return &hookResponse, nil
}
func (c *Client) setInternalHookData(ctx context.Context, jsonData []byte, buf *bytes.Buffer) []byte {
if len(jsonData) == 0 {
jsonData = []byte(`{}`)
}
// Make sure we account for both pool.ClientRequestKey being nil and being non present
if clientRequest, ok := ctx.Value(pool.ClientRequestKey).(*http.Request); ok && clientRequest != nil {
_, wgClientRequestType, _, _ := jsonparser.Get(jsonData, "__wg", "clientRequest")
if clientRequestData, err := HttpRequestToWunderGraphRequestJSON(clientRequest, false); err == nil && wgClientRequestType == jsonparser.NotExist {
buf.Reset()
_, _ = buf.Write(jsonData)
// because we modify the original json data, we need to make sure that the original data is not modified
// so we copy it first with enough space to append the clientRequestData
jsonData, _ = jsonparser.Set(buf.Bytes(), clientRequestData, "__wg", "clientRequest")
}
}
return jsonData
}
func (c *Client) doRequest(ctx context.Context, hookResponse HookResponse, action string, hook MiddlewareHook, hookID string, jsonData []byte, buf *bytes.Buffer) error {
jsonData = c.setInternalHookData(ctx, jsonData, buf)
hookURL := c.serverUrl + "/" + action + "/" + string(hook)
if hookID != "" {
hookURL += "/" + hookID
}
r, err := http.NewRequestWithContext(ctx, "POST", hookURL, bytes.NewReader(jsonData))
if err != nil {
return err
}
r.Header.Set("Content-Type", "application/json")
r.Header.Set(logging.RequestIDHeader, logging.RequestIDFromContext(ctx))
req, err := retryablehttp.FromRequest(r)
if err != nil {
return err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("hook %s failed with error: %w", string(hook), err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("hook %s failed with reading body with error: %w", string(hook), err)
}
// Decode the hook response first, since we might want to propagate the error message
if err := json.Unmarshal(data, hookResponse); err != nil {
return fmt.Errorf("hook %s response could not be decoded: %w", string(hook), err)
}
if err := hookResponse.ResponseError(); err != nil {
if err.Error() == "" {
// Hook failed but didn't report a message
return fmt.Errorf("hook %s failed for operation %s", hookResponse.HookName(), hookResponse.OperationName())
}
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("hook %s failed with invalid status code: %d (%s)", string(hook), resp.StatusCode, string(data))
}
return nil
}
func (c *Client) doMiddlewareRequest(ctx context.Context, action string, hook MiddlewareHook, hookID string, jsonData []byte, buf *bytes.Buffer) (*MiddlewareHookResponse, error) {
var hookResponse MiddlewareHookResponse
if err := c.doRequest(ctx, &hookResponse, action, hook, hookID, jsonData, buf); err != nil {
return nil, err
}
return &hookResponse, nil
}
func (c *Client) DoHealthCheckRequest(ctx context.Context) (status bool) {
req, err := retryablehttp.NewRequestWithContext(ctx, "GET", c.serverUrl+"/health", nil)
if err != nil {
return false
}
resp, err := c.httpClient.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == 200
}
func encodeData(r *http.Request, w *bytes.Buffer, variables []byte, response []byte) ([]byte, error) {
const (
wgKey = "__wg"
root = `{"` + wgKey + `":{}}`
)
var err error
buf := w.Bytes()[0:]
buf = append(buf, []byte(root)...)
if user := authentication.UserFromContext(r.Context()); user != nil {
userJson, err := json.Marshal(user)
if err != nil {
return nil, err
}
if buf, err = jsonparser.Set(buf, userJson, wgKey, "user"); err != nil {
return nil, err
}
}
if len(variables) > 2 {
if buf, err = jsonparser.Set(buf, variables, "input"); err != nil {
return nil, err
}
}
if len(response) != 0 {
if buf, err = jsonparser.Set(buf, response, "response"); err != nil {
return nil, err
}
}
if r != nil {
counterHeader := r.Header.Get("Wg-Cycle-Counter")
counter, _ := strconv.ParseInt(counterHeader, 10, 64)
counterValue := []byte(strconv.FormatInt(counter+1, 10))
if buf, err = jsonparser.Set(buf, counterValue, "cycleCounter"); err != nil {
return nil, err
}
}
// If buf required more bytes than what w provided, copy buf into w so next time w's backing slice has enough
// room to fit the whole payload
if cap(buf) > w.Cap() {
_, _ = w.Write(buf)
}
return buf, nil
}
// EncodeData encodes the given input data for a hook as a JSON payload to be sent to the hooks server
func EncodeData(r *http.Request, buf *bytes.Buffer, variables []byte, response []byte) ([]byte, error) {
data, err := encodeData(r, buf, variables, response)
if err != nil {
return nil, fmt.Errorf("encoding hook data: %w", err)
}
return data, nil
}