-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
382 lines (329 loc) · 9.78 KB
/
service.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
package ironhook
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"time"
"github.com/gofrs/uuid"
"github.com/spf13/viper"
"go.uber.org/zap"
"gorm.io/gorm"
)
type WebhookEndpointService interface {
Create(WebhookEndpoint) (WebhookEndpoint, error)
UpdateURL(WebhookEndpoint) (WebhookEndpoint, error)
Verify(WebhookEndpoint) (WebhookEndpoint, error)
Get(WebhookEndpoint) (WebhookEndpoint, error)
Delete(WebhookEndpoint) error
Notify(WebhookEndpoint, WebhookNotification) error
LastNotificationSent(WebhookEndpoint) (WebhookNotification, error)
ListEndpoints() (*[]WebhookEndpoint, error)
}
type WebhookEndpointServiceImpl struct {
WebhookEndpointService
db *gorm.DB
log *zap.Logger
http *http.Client
}
// Creates a new Webhook service, connects to a database and applies migrations
func NewWebhookService(custom_http_client *http.Client) (WebhookEndpointService, error) {
// Viper Config
// ------------
initConfig()
// Logging
// -------
logger, err := newLoggerAtLevel(
viper.GetString("log_level"),
)
if err != nil {
return nil, err
}
// DB instance
// -----------
logger.Info("Getting a SQL store")
db, err := databaseConnection(
viper.GetString("db_engine"),
viper.GetString("db_dsn"),
)
if err != nil {
return nil, err
}
// DB migrations
// -------------
logger.Info("Applying database migrations")
err = db.AutoMigrate(
&WebhookEndpointDB{},
&WebhookNotificationDB{},
)
if err != nil {
return nil, err
}
// Universal HTTP client
// ---------------------
var http_client *http.Client
if custom_http_client == nil {
http_client = &http.Client{
Timeout: time.Second * 3,
}
} else {
http_client = custom_http_client
}
// Pulling it all together
// -----------------------
logger.Info("Pulling together a new Webhooks service")
return &WebhookEndpointServiceImpl{
db: db,
log: logger,
http: http_client,
}, nil
}
// Creates a new unverified Webhook Endpoint. The next step would be to run
// the verification process on this endpoint.
//
// verified_endpoint, err := service.Verify(endpoint)
//
// Otherwise you will not be able to send Notifiations to the endpoint.
func (s *WebhookEndpointServiceImpl) Create(endpoint WebhookEndpoint) (WebhookEndpoint, error) {
endpoint.Status = Unverified
if endpoint.UUID == uuid.Nil {
endpoint.UUID = uuid.Must(uuid.NewV4())
}
if endpoint.URL == "" {
// recover by retrying with a non-empty endpoint URL
return endpoint, ErrEmptyEndpointURL
}
db_endpoint := WebhookEndpointDB{
UUID: endpoint.UUID,
URL: endpoint.URL,
Status: endpoint.Status,
}
s.log.Info(
"Creating a new endpoint",
zap.String("URL", db_endpoint.URL),
zap.String("UUID", db_endpoint.UUID.String()),
)
defer func() {
s.log.Info("A new endpoint created",
zap.String("URL", db_endpoint.URL),
zap.String("UUID", db_endpoint.UUID.String()))
}()
tx := s.db.Create(&db_endpoint)
return endpoint, tx.Error
}
// Updates the endpoint with a new URL. The endpoint is then switched to Unverified
// so you will have to verify it again.
//
// verified_endpoint, err := service.Verify(endpoint)
//
// Otherwise you will not be able to send Notifiations to the endpoint.
//
// endpoint.UUID is used to find the webhook in the database.
func (s *WebhookEndpointServiceImpl) UpdateURL(endpoint WebhookEndpoint) (WebhookEndpoint, error) {
model_endpoint, err := s.fetchWebhookEndpointFromDB(endpoint)
if err != nil {
return endpoint, err
}
if model_endpoint == nil {
// that shouldn't happen if there are no error
return endpoint, ErrInternalProcessingError
}
if endpoint.URL == model_endpoint.URL {
s.log.Info("URL is same as before, nothing to do", zap.String("UUID", endpoint.UUID.String()))
return endpoint, nil
}
s.log.Info("updating the URL and Status", zap.String("UUID", endpoint.UUID.String()))
model_endpoint.URL = endpoint.URL
model_endpoint.Status = Unverified
tx := s.db.Save(&model_endpoint)
return endpoint, tx.Error
}
// Verifies user's control over the provided endpoint.
// Runs a simple check to see if the endpoint responds
// with an expected answer.
//
// given a request like below
//
// <endpoint.URL>/verification?id=abcd
//
// The verification process expects to see "abcd" in the response body.
func (s *WebhookEndpointServiceImpl) Verify(endpoint WebhookEndpoint) (WebhookEndpoint, error) {
model_endpoint, err := s.fetchWebhookEndpointFromDB(endpoint)
if err != nil {
return endpoint, err
}
if model_endpoint == nil {
// this shouldnt happen if there are no errors
return endpoint, ErrInternalProcessingError
}
if model_endpoint.Status > Suspended {
// Verified = 2, Healthy = 3
s.log.Info("endpoint already verified")
return endpoint, nil
}
url, err := prepareEndpointForVerification(model_endpoint.URL, model_endpoint.UUID.String())
if err != nil {
return endpoint, err
}
var resp *http.Response
client := &http.Client{
Timeout: time.Second * 3,
}
resp, err = client.Get(url)
if err != nil {
return endpoint, err
}
defer resp.Body.Close()
verificationResult := verifyResponseBodyForVerification(
resp.Body,
endpoint.UUID.String(),
)
if verificationResult != nil {
s.log.Warn(
"Failed endpoint verification",
zap.String("UUID", endpoint.UUID.String()),
zap.Error(verificationResult))
return endpoint, ErrFailedEndpointVerification
}
s.log.Info("successfully verified an endpoint",
zap.String("UUID", endpoint.UUID.String()))
// mark the URL unverified
model_endpoint.Status = Verified
endpoint.Status = Verified
tx := s.db.Save(&model_endpoint)
return endpoint, tx.Error
}
// Deletes the indicated Endpoint
//
// endpoint.UUID is used to find the webhook in the database.
func (s *WebhookEndpointServiceImpl) Delete(endpoint WebhookEndpoint) error {
model_endpoint, err := s.fetchWebhookEndpointFromDB(endpoint)
if err != nil {
return err
}
if model_endpoint == nil {
// shouldnt happen if there are no errors
return ErrInternalProcessingError
}
tx := s.db.Delete(&model_endpoint)
return tx.Error
}
// Fetches the indicated Endpoint from the database
//
// endpoint.UUID is used to find the webhook in the database.
func (s *WebhookEndpointServiceImpl) Get(endpoint WebhookEndpoint) (WebhookEndpoint, error) {
model_endpoint, err := s.fetchWebhookEndpointFromDB(endpoint)
if err != nil {
return endpoint, err
}
if model_endpoint == nil {
// shouldnt happen if there are no errors
return endpoint, ErrInternalProcessingError
}
simplified_webhook_endpoint := WebhookEndpoint{
UUID: model_endpoint.UUID,
URL: model_endpoint.URL,
Status: model_endpoint.Status,
}
return simplified_webhook_endpoint, nil
}
// Notify sends a Notification to a verified Endpoint.
//
// Notification's Topic and Body can be empty
func (s *WebhookEndpointServiceImpl) Notify(endpoint WebhookEndpoint, notification WebhookNotification) error {
ref_endpoint, err := s.Get(endpoint)
if err != nil {
return err
}
if ref_endpoint.Status < Verified {
// recover by running service.Verify(endpoint) first
return ErrEndpointNotYetActivated
}
jsonval, err := json.Marshal(notification)
if err != nil {
return err
}
final_url, err := prepareEndpointForNotification(ref_endpoint.URL)
if err != nil {
return err
}
request, err := http.NewRequest("GET", final_url, bytes.NewBuffer(jsonval))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
response, err := s.http.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
// TODO: introduce toggle for notifications persistence
// save the notification
db_notifiaction := WebhookNotificationDB{
EventUUID: notification.EventUUID,
Topic: notification.Topic,
Body: notification.Body,
EndpointUUID: ref_endpoint.UUID,
}
tx := s.db.Create(&db_notifiaction)
if tx.Error != nil {
return tx.Error
}
// TODO: perhaps worth narrowing down
if response.StatusCode >= 400 {
body, err := io.ReadAll(response.Body)
if err != nil {
return ErrFailedNotifyingTheEndpoint
}
s.log.Warn(
"notifiaction returned HTTP error code:",
zap.Int("StatusCode", response.StatusCode),
zap.String("Body", string(body)),
)
return ErrFailedNotifyingTheEndpoint
}
return nil
}
func (s *WebhookEndpointServiceImpl) LastNotificationSent(endpoint WebhookEndpoint) (WebhookNotification, error) {
if endpoint.UUID == uuid.Nil {
return WebhookNotification{}, ErrEmptyEndpointUUID
}
var db_notifiaction WebhookNotificationDB
tx := s.db.Last(&db_notifiaction, "endpoint_uuid = ?", endpoint.UUID)
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
s.log.Error("couldnt find any notifications in the database for provided endpoint", zap.Error(tx.Error))
return WebhookNotification{}, ErrRecordNotFound
} else {
s.log.Error("couldnt fetch a notification from the database", zap.Error(tx.Error))
return WebhookNotification{}, tx.Error
}
}
return WebhookNotification{
EventUUID: db_notifiaction.EventUUID,
Topic: db_notifiaction.Topic,
Body: db_notifiaction.Body,
}, nil
}
func (s *WebhookEndpointServiceImpl) ListEndpoints() (*[]WebhookEndpoint, error) {
var db_endpoints []WebhookEndpointDB
tx := s.db.Find(&db_endpoints)
if tx.Error != nil {
return nil, tx.Error
}
return endpointsDbToWeb(&db_endpoints), nil
}
var ErrEndpointNotYetActivated error = errors.New(
`the endpoint youre trying to notify hasnt yet been activated.
recover by running service.Verify(endpoint) first`,
)
var ErrFailedNotifyingTheEndpoint error = errors.New(
`the endpoint youre trying to notify returned an error.
This is perhaps an external issue, recovering might require
checking if the request body is parsed correctly and on both ends`,
)
var ErrInternalProcessingError error = errors.New(
`an internal error occured that shouldn't have happened.
Please submit an issue with as much detail as possible`,
)