-
Notifications
You must be signed in to change notification settings - Fork 145
/
webhook_handler.go
184 lines (160 loc) · 4.69 KB
/
webhook_handler.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
package worker
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/hatchet-dev/hatchet/internal/signature"
"github.com/hatchet-dev/hatchet/pkg/client"
)
type WebhookHandlerOptions struct {
Secret string
}
type HealthCheckResponse struct {
Actions []string `json:"actions"`
}
func (w *Worker) WebhookHttpHandler(opts WebhookHandlerOptions, workflows ...workflowConverter) http.HandlerFunc {
return func(writer http.ResponseWriter, r *http.Request) {
// health check with actions
if r.Method == http.MethodGet {
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write([]byte("OK!"))
return
}
if r.Method != http.MethodPut && r.Method != http.MethodPost {
writer.WriteHeader(http.StatusMethodNotAllowed)
_, _ = writer.Write([]byte("Method not allowed"))
return
}
data, err := io.ReadAll(r.Body)
if err != nil {
w.l.Error().Err(err).Msg("error reading body")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
expected := r.Header.Get("X-Hatchet-Signature")
actual, err := signature.Sign(string(data), opts.Secret)
if err != nil {
w.l.Error().Err(err).Msg("error signing request")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
if expected != actual {
w.l.Error().Err(fmt.Errorf("expected signature %s, got %s", expected, actual)).Msg("error in request signature")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte("wrong signature"))
return
}
if r.Method == http.MethodPut {
for _, wf := range workflows {
err = w.RegisterWorkflow(wf)
if err != nil {
w.l.Error().Err(err).Msg("error registering workflow")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
}
var actions []string
for _, action := range w.actions {
actions = append(actions, action.Name())
}
res := HealthCheckResponse{
Actions: actions,
}
data, err := json.Marshal(res)
if err != nil {
panic(err)
}
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write(data)
return
}
var action ActionPayload
if err := json.Unmarshal(data, &action); err != nil {
w.l.Error().Err(err).Msg("error unmarshaling action")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
actionWithPayload := action.Action
actionWithPayload.ActionPayload = []byte(action.ActionPayload)
timestamp := time.Now().UTC()
_, err = w.client.Dispatcher().SendStepActionEvent(r.Context(),
&client.ActionEvent{
Action: actionWithPayload,
EventTimestamp: ×tamp,
EventType: client.ActionEventTypeStarted,
},
)
if err != nil {
w.l.Error().Err(err).Msg("error dispatching event")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
ctx, err := newHatchetContext(r.Context(), actionWithPayload, w.client, w.l)
if err != nil {
w.l.Error().Err(err).Msg("error creating context")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
resp, err := w.webhookProcess(ctx)
if err != nil {
// TODO handle error gracefully and send a failed event
w.l.Error().Err(err).Msg("error processing request")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
timestamp = time.Now().UTC()
_, err = w.client.Dispatcher().SendStepActionEvent(r.Context(),
&client.ActionEvent{
Action: actionWithPayload,
EventTimestamp: ×tamp,
EventType: client.ActionEventTypeCompleted,
EventPayload: resp,
},
)
if err != nil {
w.l.Error().Err(err).Msg("error dispatching event")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write([]byte("OK"))
}
}
func (w *Worker) webhookProcess(ctx HatchetContext) (interface{}, error) {
var do Action
for _, action := range w.actions {
split := strings.Split(action.Name(), ":") // service:action
if split[1] == ctx.StepName() {
do = action
break
}
}
if do == nil {
return nil, fmt.Errorf("fatal: could not find action for step run %s", ctx.StepName())
}
res := do.Run(ctx)
if len(res) != 2 {
return nil, fmt.Errorf("invalid response from action, expected 2 values")
}
if res[1] != nil {
err, ok := res[1].(error)
if !ok {
return nil, fmt.Errorf("invalid response from action, expected error")
}
if err != nil {
return nil, err
}
}
return res[0], nil
}