-
Notifications
You must be signed in to change notification settings - Fork 9
/
webhook.go
312 lines (295 loc) · 9.84 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
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
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webhook
import (
"context"
"crypto/hmac"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/go-resty/resty/v2"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/modules/workq"
"github.com/go-sigma/sigma/pkg/modules/workq/definition"
"github.com/go-sigma/sigma/pkg/types"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/ptr"
)
func init() {
workq.TopicHandlers[enums.DaemonWebhook] = definition.Consumer{
Handler: webhookRunner,
MaxRetry: 6,
Concurrency: 10,
Timeout: time.Minute * 10,
}
}
func webhookRunner(ctx context.Context, data []byte) error {
ctx = log.Logger.WithContext(ctx)
var payload types.DaemonWebhookPayload
err := json.Unmarshal(data, &payload)
if err != nil {
return fmt.Errorf("unmarshal payload failed: %v", err)
}
w := webhook{
namespaceServiceFactory: dao.NewNamespaceServiceFactory(),
repositoryServiceFactory: dao.NewRepositoryServiceFactory(),
tagServiceFactory: dao.NewTagServiceFactory(),
webhookServiceFactory: dao.NewWebhookServiceFactory(),
}
switch payload.Type {
case enums.WebhookTypeResend:
return w.decorator(w.resend)(ctx, payload)
case enums.WebhookTypePing:
return w.decorator(w.ping)(ctx, payload)
case enums.WebhookTypeSend:
return w.send(ctx, payload)
}
return nil
}
type webhook struct {
namespaceServiceFactory dao.NamespaceServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
tagServiceFactory dao.TagServiceFactory
webhookServiceFactory dao.WebhookServiceFactory
}
type clientOption struct {
SslVerify bool
RetryTimes int
RetryDuration int
}
func (w webhook) resend(ctx context.Context, payload types.DaemonWebhookPayload) (*models.WebhookLog, error) {
webhookService := w.webhookServiceFactory.New()
webhookLogObj, err := webhookService.GetLog(ctx, ptr.To(payload.WebhookLogID))
if err != nil {
return nil, err
}
var result = &models.WebhookLog{
WebhookID: webhookLogObj.WebhookID,
ResourceType: webhookLogObj.ResourceType,
ReqHeader: webhookLogObj.ReqHeader,
ReqBody: webhookLogObj.ReqBody,
}
var headers map[string]string
err = json.Unmarshal(webhookLogObj.ReqHeader, &headers)
if err != nil {
return nil, err
}
headers, err = w.secretHeader(webhookLogObj.Webhook.Secret, webhookLogObj.ReqBody, headers)
if err != nil {
return nil, err
}
client := w.client(clientOption{
SslVerify: webhookLogObj.Webhook.SslVerify,
RetryTimes: webhookLogObj.Webhook.RetryTimes,
RetryDuration: webhookLogObj.Webhook.RetryDuration,
})
resp, err := client.SetContext(ctx).
SetDoNotParseResponse(true).
SetHeaders(headers).
SetBody(webhookLogObj.ReqBody).
Execute(http.MethodPost, webhookLogObj.Webhook.URL)
if err != nil {
return nil, err
}
respBody, err := w.respBody(resp)
if err != nil {
return nil, err
}
result.RespHeader = utils.MustMarshal(resp.Header())
result.RespBody = respBody
return result, nil
}
func (w webhook) send(ctx context.Context, payload types.DaemonWebhookPayload) error {
webhookService := w.webhookServiceFactory.New()
filter := map[string]any{
query.Webhook.Enable.ColumnName().String(): true,
query.Webhook.NamespaceID.ColumnName().String(): payload.NamespaceID,
}
switch payload.ResourceType {
case enums.WebhookResourceTypeNamespace:
filter[query.Webhook.EventNamespace.ColumnName().String()] = true
case enums.WebhookResourceTypeRepository:
filter[query.Webhook.EventRepository.ColumnName().String()] = true
case enums.WebhookResourceTypeTag:
filter[query.Webhook.EventTag.ColumnName().String()] = true
case enums.WebhookResourceTypeArtifact:
filter[query.Webhook.EventArtifact.ColumnName().String()] = true
case enums.WebhookResourceTypeMember:
filter[query.Webhook.EventMember.ColumnName().String()] = true
case enums.WebhookResourceTypeDaemonTaskGcArtifactRule, enums.WebhookResourceTypeDaemonTaskGcArtifactRunner,
enums.WebhookResourceTypeDaemonTaskGcBlobRule, enums.WebhookResourceTypeDaemonTaskGcBlobRunner,
enums.WebhookResourceTypeDaemonTaskGcRepositoryRule, enums.WebhookResourceTypeDaemonTaskGcRepositoryRunner,
enums.WebhookResourceTypeDaemonTaskGcTagRule, enums.WebhookResourceTypeDaemonTaskGcTagRunner:
filter[query.Webhook.EventDaemonTaskGc.ColumnName().String()] = true
}
webhookObjs, err := webhookService.GetByFilter(ctx, filter)
if err != nil {
return err
}
headers := w.defaultHeaders()
for _, webhookObj := range webhookObjs {
headers, err = w.secretHeader(webhookObj.Secret, payload.Payload, headers)
if err != nil {
log.Error().Err(err).Msg("Calculate secret header failed")
continue
}
webhookLogObj := &models.WebhookLog{
WebhookID: webhookObj.ID,
ResourceType: payload.ResourceType,
Action: payload.Action,
ReqHeader: utils.MustMarshal(headers),
ReqBody: payload.Payload,
}
client := w.client(clientOption{
SslVerify: webhookObj.SslVerify,
RetryTimes: webhookObj.RetryTimes,
RetryDuration: webhookObj.RetryDuration,
})
resp, err := client.SetContext(ctx).
SetDoNotParseResponse(true).
SetHeaders(headers).
SetBody(webhookLogObj.ReqBody).
Execute(http.MethodPost, webhookObj.URL)
if err != nil {
log.Error().Err(err).Msg("Send webhook failed")
continue
}
respBody, err := w.respBody(resp)
if err != nil {
log.Error().Err(err).Msg("Parse response body failed")
continue
}
webhookLogObj.RespHeader = utils.MustMarshal(resp.Header())
webhookLogObj.RespBody = respBody
err = webhookService.CreateLog(ctx, webhookLogObj)
if err != nil {
log.Error().Err(err).Msg("Create webhook log failed") // must be database something wrong, webhook has been sent, so we can ignore this error
continue
}
}
return nil
}
func (w webhook) ping(ctx context.Context, payload types.DaemonWebhookPayload) (*models.WebhookLog, error) {
webhookService := w.webhookServiceFactory.New()
webhookObj, err := webhookService.Get(ctx, payload.WebhookID)
if err != nil {
return nil, err
}
headers := w.defaultHeaders()
pingObj := types.DaemonWebhookPayloadPing{
ResourceType: enums.WebhookResourceTypeWebhook,
Action: enums.WebhookActionPing,
}
pingObj.Namespace, err = w.getNamespace(ctx, webhookObj.NamespaceID)
if err != nil {
return nil, err
}
body := utils.MustMarshal(pingObj)
headers, err = w.secretHeader(webhookObj.Secret, body, headers)
if err != nil {
return nil, err
}
var result = &models.WebhookLog{
WebhookID: payload.WebhookID,
ResourceType: payload.ResourceType,
Action: payload.Action,
ReqHeader: utils.MustMarshal(headers),
ReqBody: body,
}
client := w.client(clientOption{
SslVerify: webhookObj.SslVerify,
RetryTimes: webhookObj.RetryTimes,
RetryDuration: webhookObj.RetryDuration,
})
resp, err := client.SetContext(ctx).
SetDoNotParseResponse(true).
SetHeaders(headers).
SetBody(body).
Execute(http.MethodPost, webhookObj.URL)
if err != nil {
return nil, err
}
respBody, err := w.respBody(resp)
if err != nil {
return nil, err
}
result.RespHeader = utils.MustMarshal(resp.Header())
result.RespBody = respBody
return result, nil
}
func (w webhook) secretHeader(secret *string, body []byte, headers map[string]string) (map[string]string, error) {
delete(headers, consts.WebhookSecretHeader)
if secret == nil {
return headers, nil
}
hash := hmac.New(sha256.New, []byte(ptr.To(secret)))
_, err := hash.Write(body)
if err != nil {
return nil, err
}
headers[consts.WebhookSecretHeader] = hex.EncodeToString(hash.Sum(nil))
return headers, nil
}
func (w webhook) client(opt clientOption) *resty.Request {
client := resty.New()
if !opt.SslVerify {
client = resty.NewWithClient(&http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, // nolint: gosec
})
}
client = client.SetRetryCount(opt.RetryTimes)
client = client.SetRetryAfter(func(c *resty.Client, r *resty.Response) (time.Duration, error) {
return time.Duration(opt.RetryDuration) * time.Second, nil
})
client = client.AddRetryCondition(func(r *resty.Response, err error) bool {
return err != nil || r.StatusCode() >= http.StatusInternalServerError || r.StatusCode() == http.StatusTooManyRequests
})
return client.R()
}
func (w webhook) decorator(runner func(context.Context, types.DaemonWebhookPayload) (*models.WebhookLog, error)) func(ctx context.Context, payload types.DaemonWebhookPayload) error {
return func(ctx context.Context, payload types.DaemonWebhookPayload) error {
webhookLogObj, err := runner(ctx, payload)
if err != nil {
return err
}
webhookService := w.webhookServiceFactory.New()
err = webhookService.CreateLog(ctx, webhookLogObj)
if err != nil {
return err
}
return nil
}
}
func (w webhook) respBody(resp *resty.Response) ([]byte, error) {
reader := resp.RawBody()
defer reader.Close() // nolint: errcheck
return io.ReadAll(&io.LimitedReader{R: reader, N: 10 * 1024})
}
func (w webhook) defaultHeaders() map[string]string {
return map[string]string{
"User-Agent": consts.UserAgent,
echo.HeaderContentType: "application/json",
}
}