/
sms.go
462 lines (411 loc) · 12.5 KB
/
sms.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
package twilio
import (
"context"
"database/sql"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/target/goalert/alert"
"github.com/target/goalert/config"
"github.com/target/goalert/notification"
"github.com/target/goalert/permission"
"github.com/target/goalert/retry"
"github.com/target/goalert/util/log"
"github.com/pkg/errors"
)
var (
lastReplyRx = regexp.MustCompile(`^'?\s*(c|close|a|ack[a-z]*)\s*'?$`)
shortReplyRx = regexp.MustCompile(`^'?\s*([0-9]+)\s*(c|a)\s*'?$`)
alertReplyRx = regexp.MustCompile(`^'?\s*(c|close|a|ack[a-z]*)\s*#?\s*([0-9]+)\s*'?$`)
svcReplyRx = regexp.MustCompile(`^'?\s*([0-9]+)\s*(cc|aa)\s*'?$`)
)
// SMS implements a notification.Sender for Twilio SMS.
type SMS struct {
b *dbSMS
c *Config
respCh chan *notification.MessageResponse
statCh chan *notification.MessageStatus
ban *dbBan
}
// NewSMS performs operations like validating essential parameters, registering the Twilio client and db
// and adding routes for successful and unsuccessful message delivery to Twilio
func NewSMS(ctx context.Context, db *sql.DB, c *Config) (*SMS, error) {
b, err := newDB(ctx, db)
if err != nil {
return nil, err
}
s := &SMS{
b: b,
c: c,
respCh: make(chan *notification.MessageResponse),
statCh: make(chan *notification.MessageStatus, 10),
}
s.ban, err = newBanDB(ctx, db, c, "twilio_sms_errors")
if err != nil {
return nil, errors.Wrap(err, "init Twilio SMS DB")
}
return s, nil
}
// Status provides the current status of a message.
func (s *SMS) Status(ctx context.Context, id, providerID string) (*notification.MessageStatus, error) {
msg, err := s.c.GetSMS(ctx, providerID)
if err != nil {
return nil, err
}
return msg.messageStatus(id), nil
}
// ListenStatus will return a channel that is fed async status updates.
func (s *SMS) ListenStatus() <-chan *notification.MessageStatus { return s.statCh }
// ListenResponse will return a channel that is fed async message responses.
func (s *SMS) ListenResponse() <-chan *notification.MessageResponse { return s.respCh }
// Send implements the notification.Sender interface.
func (s *SMS) Send(ctx context.Context, msg notification.Message) (*notification.MessageStatus, error) {
cfg := config.FromContext(ctx)
if !cfg.Twilio.Enable {
return nil, errors.New("Twilio provider is disabled")
}
if msg.Destination().Type != notification.DestTypeSMS {
return nil, errors.Errorf("unsupported destination type %s; expected SMS", msg.Destination().Type)
}
destNumber := msg.Destination().Value
if destNumber == cfg.Twilio.FromNumber {
return nil, errors.New("refusing to send outgoing SMS to FromNumber")
}
ctx = log.WithFields(ctx, log.Fields{
"Phone": destNumber,
"Type": "TwilioSMS",
})
b, err := s.ban.IsBanned(ctx, destNumber, true)
if err != nil {
return nil, errors.Wrap(err, "check ban status")
}
if b {
return nil, errors.New("number had too many outgoing errors recently")
}
makeSMSCode := func(alertID int, serviceID string) int {
var code int
if hasTwoWaySMSSupport(ctx, destNumber) {
code, err = s.b.insertDB(ctx, destNumber, msg.ID(), alertID, serviceID)
if err != nil {
log.Log(ctx, errors.Wrap(err, "insert alert id for SMS callback -- sending 1-way SMS as fallback"))
}
}
return code
}
var message string
switch t := msg.(type) {
case notification.AlertStatus:
message, err = alertSMS{
ID: t.AlertID,
Body: t.LogEntry,
}.Render()
case notification.AlertStatusBundle:
message, err = alertSMS{
ID: t.AlertID,
Body: t.LogEntry,
Count: t.Count - 1,
}.Render()
case notification.AlertBundle:
var link string
if !cfg.General.DisableSMSLinks {
link = cfg.CallbackURL(fmt.Sprintf("/services/%s/alerts", t.ServiceID))
}
message, err = alertSMS{
Count: t.Count,
Body: t.ServiceName,
Link: link,
Code: makeSMSCode(0, t.ServiceID),
}.Render()
case notification.Alert:
var link string
if !cfg.General.DisableSMSLinks {
link = cfg.CallbackURL(fmt.Sprintf("/alerts/%d", t.AlertID))
}
message, err = alertSMS{
ID: t.AlertID,
Body: t.Summary,
Link: link,
Code: makeSMSCode(t.AlertID, ""),
}.Render()
case notification.Test:
message = fmt.Sprintf("This is a test message from GoAlert.")
case notification.Verification:
message = fmt.Sprintf("GoAlert verification code: %d", t.Code)
default:
return nil, errors.Errorf("unhandled message type %T", t)
}
if err != nil {
return nil, errors.Wrap(err, "render message")
}
opts := &SMSOptions{
ValidityPeriod: time.Second * 10,
CallbackParams: make(url.Values),
}
opts.CallbackParams.Set(msgParamID, msg.ID())
// Actually send notification to end user & receive Message Status
resp, err := s.c.SendSMS(ctx, destNumber, message, opts)
if err != nil {
return nil, errors.Wrap(err, "send message")
}
return resp.messageStatus(msg.ID()), nil
}
func (s *SMS) ServeStatusCallback(w http.ResponseWriter, req *http.Request) {
if disabled(w, req) {
return
}
ctx := req.Context()
status := MessageStatus(req.FormValue("MessageStatus"))
sid := validSID(req.FormValue("MessageSid"))
number := validPhone(req.FormValue("To"))
if status == "" || sid == "" || number == "" {
http.Error(w, "", http.StatusBadRequest)
return
}
ctx = log.WithFields(ctx, log.Fields{
"Status": status,
"SID": sid,
"Phone": number,
"Type": "TwilioSMS",
})
msg := Message{SID: sid, Status: status}
log.Debugf(ctx, "Got Twilio SMS status callback.")
s.statCh <- msg.messageStatus(req.URL.Query().Get(msgParamID))
if status != MessageStatusFailed {
// ignore other types
return
}
err := s.ban.RecordError(context.Background(), number, true, "send failed")
if err != nil {
log.Log(ctx, errors.Wrap(err, "record error"))
}
}
// isStopMessage checks the body of the message against single-word matches
// i.e. "stop" will unsubscribe, however "please stop" will not.
func isStopMessage(body string) bool {
switch strings.ToLower(body) {
case "stop", "stopall", "unsubscribe", "cancel", "end", "quit":
return true
}
return false
}
// isStartMessage checks the body of the message against single-word matches
// i.e. "start" will resubscribe, however "please start" will not.
func isStartMessage(body string) bool {
switch strings.ToLower(body) {
case "start", "yes", "unstop":
return true
}
return false
}
func (s *SMS) ServeMessage(w http.ResponseWriter, req *http.Request) {
if disabled(w, req) {
return
}
ctx := req.Context()
cfg := config.FromContext(ctx)
from := validPhone(req.FormValue("From"))
if from == "" || from == cfg.Twilio.FromNumber {
http.Error(w, "", http.StatusBadRequest)
return
}
ctx = log.WithFields(ctx, log.Fields{
"Number": from,
"Type": "TwilioSMS",
})
respond := func(errMsg string, msg string) {
if errMsg != "" {
err := s.ban.RecordError(context.Background(), from, false, errMsg)
if err != nil {
log.Log(ctx, errors.Wrap(err, "record error"))
}
}
_, err := s.c.SendSMS(ctx, from, msg, &SMSOptions{FromNumber: req.FormValue("to")})
if err != nil {
log.Log(ctx, errors.Wrap(err, "send response"))
}
// TODO: we should track & queue these
// (maybe the engine should generate responses instead)
}
var banned bool
var err error
err = retry.DoTemporaryError(func(int) error {
banned, err = s.ban.IsBanned(ctx, from, false)
return errors.Wrap(err, "look up ban status")
},
retry.Log(ctx),
retry.Limit(10),
retry.FibBackoff(time.Second),
)
if err != nil {
log.Log(ctx, err)
respond("", "System error. Visit the dashboard to manage alerts.")
return
}
if banned {
http.Error(w, "", http.StatusTooManyRequests)
return
}
// handle start and stop codes from user
body := req.FormValue("Body")
if isStartMessage(body) || isStopMessage(body) {
r := notification.ResultStart
msg := "process START message"
if isStopMessage(body) {
r = notification.ResultStop
msg = "process STOP message"
}
err := retry.DoTemporaryError(func(int) error {
errCh := make(chan error, 1)
s.respCh <- ¬ification.MessageResponse{
Ctx: ctx,
From: notification.Dest{Type: notification.DestTypeSMS, Value: from},
Result: r,
Err: errCh,
}
return errors.Wrap(<-errCh, msg)
},
retry.Log(ctx),
retry.Limit(10),
retry.FibBackoff(time.Second),
)
if err != nil {
log.Log(ctx, err)
}
return
}
if cfg.Twilio.DisableTwoWaySMS {
respond("response codes disabled", "Response codes are currently disabled. Visit the dashboard to manage alerts.")
return
}
body = strings.TrimSpace(body)
body = strings.ToLower(body)
var lookupFn func() (*codeInfo, error)
var result notification.Result
var isSvc bool
if m := lastReplyRx.FindStringSubmatch(body); len(m) == 2 {
if strings.HasPrefix(m[1], "a") {
result = notification.ResultAcknowledge
} else {
result = notification.ResultResolve
}
lookupFn = func() (*codeInfo, error) { return s.b.LookupByCode(ctx, from, 0) }
} else if m := shortReplyRx.FindStringSubmatch(body); len(m) == 3 {
if strings.HasPrefix(m[2], "a") {
result = notification.ResultAcknowledge
} else {
result = notification.ResultResolve
}
code, err := strconv.Atoi(m[1])
if err != nil {
log.Debug(ctx, errors.Wrap(err, "parse code"))
} else {
ctx = log.WithField(ctx, "Code", code)
lookupFn = func() (*codeInfo, error) { return s.b.LookupByCode(ctx, from, code) }
}
} else if m := alertReplyRx.FindStringSubmatch(body); len(m) == 3 {
if strings.HasPrefix(m[1], "a") {
result = notification.ResultAcknowledge
} else {
result = notification.ResultResolve
}
alertID, err := strconv.Atoi(m[2])
if err != nil {
log.Debug(ctx, errors.Wrap(err, "parse alertID"))
} else {
ctx = log.WithField(ctx, "AlertID", alertID)
lookupFn = func() (*codeInfo, error) { return s.b.LookupByAlertID(ctx, from, alertID) }
}
} else if m := svcReplyRx.FindStringSubmatch(body); len(m) == 3 {
isSvc = true
if strings.HasPrefix(m[2], "a") {
result = notification.ResultAcknowledge
} else {
result = notification.ResultResolve
}
code, err := strconv.Atoi(m[1])
if err != nil {
log.Debug(ctx, errors.Wrap(err, "parse code"))
} else {
ctx = log.WithField(ctx, "Code", code)
lookupFn = func() (*codeInfo, error) { return s.b.LookupSvcByCode(ctx, from, code) }
}
}
if lookupFn == nil {
respond("unknown action", "Sorry, but that isn't a request GoAlert understood. Visit the Web UI for more information. To unsubscribe, reply with STOP.")
ctx = log.WithField(ctx, "SMSBody", body)
log.Debug(ctx, errors.Wrap(err, "parse alert action"))
return
}
var prefix string
if result == notification.ResultAcknowledge {
prefix = "Acknowledged"
} else {
prefix = "Closed"
}
var nonSystemErr bool
var info *codeInfo
err = retry.DoTemporaryError(func(int) error {
info, err = lookupFn()
if err != nil {
return errors.Wrap(err, "lookup code")
}
errCh := make(chan error, 1)
s.respCh <- ¬ification.MessageResponse{
Ctx: ctx,
ID: info.CallbackID,
From: notification.Dest{Type: notification.DestTypeSMS, Value: from},
Result: result,
Err: errCh,
}
return errors.Wrap(<-errCh, "process notification response")
},
retry.Log(ctx),
retry.Limit(10),
retry.FibBackoff(time.Second),
)
if errors.Is(err, sql.ErrNoRows) || (isSvc && info.ServiceName == "") || (!isSvc && info.AlertID == 0) {
respond("unknown callbackID", "Unknown reply code for this action. Visit the dashboard to manage alerts.")
return
}
msg := "System error. Visit the dashboard to manage alerts."
if alert.IsAlreadyClosed(err) {
nonSystemErr = true
msg = fmt.Sprintf("Alert #%d already closed", alert.AlertID(err))
} else if alert.IsAlreadyAcknowledged(err) {
nonSystemErr = true
msg = fmt.Sprintf("Alert #%d already acknowledged", alert.AlertID(err))
}
if nonSystemErr {
var e alert.LogEntryFetcher
// alert store returns the special error struct, twilio checks if it's special, and if so, pulls the log entry
if errors.As(err, &e) {
err = nil
// we pass a 'sudo' context to give permission
permission.SudoContext(ctx, func(sCtx context.Context) {
entry, err := e.LogEntry(sCtx)
if err != nil {
log.Log(sCtx, errors.Wrap(err, "fetch log entry"))
} else {
msg += "\n\n" + entry.String()
}
})
}
log.Log(ctx, errors.Wrap(err, "process notification response"))
respond("", msg)
return
}
if err != nil {
log.Log(ctx, err)
respond("", msg)
return
}
if info.ServiceName != "" {
respond("", fmt.Sprintf("%s all alerts for service '%s'", prefix, info.ServiceName))
} else {
respond("", fmt.Sprintf("%s alert #%d", prefix, info.AlertID))
}
}