-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
321 lines (278 loc) · 8.72 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/julienschmidt/httprouter"
uuid "github.com/satori/go.uuid"
)
// admin http endpoint used for healthchecking
func admin(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// don't return good until we've received auth info from iris and are ready serve requests
if authChecker.CheckStatus() {
fmt.Fprint(w, "GOOD\r\n")
return
}
fmt.Fprint(w, "BAD\r\n")
return
}
// postNotification handle out of bound notification creation requests
func postNotification(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
allowed, err := authChecker.CheckAuth(req)
if err != nil {
http.Error(w, "Error checking HMAC authentication", 500)
return
} else if !allowed {
http.Error(w, "HMAC authentication failed", 401)
return
}
decoder := json.NewDecoder(req.Body)
var notification Notification
err = decoder.Decode(¬ification)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to parse request body with error: %s", err), 400)
return
}
// if queue is already full reject out of band notification requests
if messageQueueManager.CheckQueueFull() {
http.Error(w, "Sender queue for this node is full", 429)
return
}
// render messages from notification and then queue them for sending
messages, err := irisClient.GetBuildMessage(notification)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to build notification messages with error: %s", err), 400)
return
}
for _, message := range messages {
muuid := uuid.NewV4()
message.MessageUUID = muuid
// construct queue item from message and add it to the message queue
msgQueueItem := MessageQueueItem{
isOOB: true,
active: true,
queued: false,
retries: 0,
message: message,
}
messageQueueManager.AddMessage(msgQueueItem)
}
w.WriteHeader(http.StatusOK)
}
// postMessageStatus update message status
func postMessageStatus(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
allowed, err := authChecker.CheckAuth(req)
if err != nil {
http.Error(w, "Error checking HMAC authentication", 500)
return
} else if !allowed {
http.Error(w, "HMAC authentication failed", 401)
return
}
type statusBody struct {
MsgID string `json:"id"`
Status string `json:"status"`
VendorIdentifier string `json:"vendor_identifier"`
}
decoder := json.NewDecoder(req.Body)
var body statusBody
err = decoder.Decode(&body)
if err != nil {
http.Error(w, "Failed to parse request body", 400)
return
}
if len(body.MsgID) == 0 && len(body.VendorIdentifier) == 0 {
http.Error(w, "invalid request, both id and vendor_identifier fields are empty", 400)
return
}
err = dbClient.writeStatus(body.MsgID, body.Status, body.VendorIdentifier)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusOK)
}
// postMessageChangelogs update message changelog
func postMessageChangelog(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
allowed, err := authChecker.CheckAuth(req)
if err != nil {
http.Error(w, "Error checking HMAC authentication", 500)
return
} else if !allowed {
http.Error(w, "HMAC authentication failed", 401)
return
}
type changelogBody struct {
MsgID string `json:"id"`
ChangeType string `json:"change_type"`
Old string `json:"old"`
New string `json:"new"`
Description string `json:"description"`
}
decoder := json.NewDecoder(req.Body)
var body changelogBody
err = decoder.Decode(&body)
if err != nil {
http.Error(w, "Failed to parse request body", 400)
return
}
err = dbClient.writeChangelog(body.MsgID, body.ChangeType, body.Old, body.New, body.Description)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusOK)
}
// retrieve message details given query params and filters
func getMessages(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
rawParams := make(map[string]interface{})
parsedParams := make(map[string]interface{})
values := req.URL.Query()
for k, v := range values {
rawParams[k] = v
}
// Query() returns map[string][]string so convert into the appropriate types for each legal param
for k, v := range rawParams {
stringParams := []string{"id", "batch", "application", "target", "destination", "mode", "plan", "body", "priority", "subject"}
uint64Params := []string{"incident_id"}
intParams := []string{"limit"}
timeParams := []string{"sent", "created"}
// query filters are delineated with __ split to get parameter name without filter
paramSplit := strings.Split(k, "__")
// attempt to convert parameter values to their correct types
if contains(uint64Params, paramSplit[0]) {
uintSlice := make([]uint64, 0)
for _, stringVal := range v.([]string) {
number, err := strconv.ParseUint(string(stringVal), 10, 64)
if err != nil {
errStr := fmt.Sprintf("Error: parameter %s could not be parsed as uint64", paramSplit[0])
http.Error(w, errStr, 400)
return
}
uintSlice = append(uintSlice, number)
}
// if slice is only a single element store it as a simple variable instead
if len(uintSlice) == 1 {
parsedParams[k] = uintSlice[0]
} else if len(uintSlice) > 1 {
parsedParams[k] = uintSlice
}
} else if contains(timeParams, paramSplit[0]) {
// time params cannot be lists so if it is not a single element slice it is malformed
if len(v.([]string)) == 1 {
i, err := strconv.ParseInt(v.([]string)[0], 10, 64)
if err != nil {
errStr := fmt.Sprintf("Error: parameter %s could not be parsed as unix timestamp", paramSplit[0])
http.Error(w, errStr, 400)
return
}
// replace created key with sent since those are not two distinct fields in IMP
if paramSplit[0] == "created" {
sentStr := strings.Replace(k, "created", "sent", 1)
if _, ok := rawParams[sentStr]; ok {
continue
}
k = sentStr
}
tm := time.Unix(i, 0)
parsedParams[k] = tm
} else {
errStr := fmt.Sprintf("Error: parameter %s cannot be a list", paramSplit[0])
http.Error(w, errStr, 400)
return
}
} else if contains(stringParams, paramSplit[0]) {
// values already correct type just determine if it is a single value or a slice
if len(v.([]string)) == 1 {
parsedParams[k] = v.([]string)[0]
} else {
parsedParams[k] = v
}
} else if contains(intParams, paramSplit[0]) {
// only int param is limit and it cannot be a list so if it is not a single element slice it is malformed
if len(v.([]string)) == 1 {
i, err := strconv.Atoi(v.([]string)[0])
if err != nil {
errStr := fmt.Sprintf("Error: parameter %s could not be parsed as int", paramSplit[0])
http.Error(w, errStr, 400)
return
}
parsedParams[k] = i
} else {
errStr := fmt.Sprintf("Error: parameter %s cannot be a list", paramSplit[0])
http.Error(w, errStr, 400)
return
}
}
}
if len(parsedParams) == 0 {
http.Error(w, "Query too broad, no valid params were found...", 400)
return
}
messages, err := dbClient.getMessages(parsedParams)
if err != nil {
http.Error(w, "error: could not fetch messages", 500)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(messages)
if err != nil {
http.Error(w, "error: could not encode message response", 500)
return
}
}
// retrieve message status for a message id
func getMessageStatus(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// parse query params
values := req.URL.Query()
msgID := values.Get("id")
if len(msgID) == 0 {
http.Error(w, "No message ID found...", 400)
return
}
status, err := dbClient.getStatus(msgID)
if err != nil {
http.Error(w, "error: could not fetch status", 500)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(status)
if err != nil {
http.Error(w, "error: could not encode status response", 500)
return
}
}
// retrieve message status for a message id
func getMessageChangelogs(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// parse query params
values := req.URL.Query()
msgID := values.Get("id")
if len(msgID) == 0 {
http.Error(w, "No message ID found...", 400)
return
}
status, err := dbClient.getChangelogs(msgID)
if err != nil {
http.Error(w, "error: could not fetch changelogs", 500)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(status)
if err != nil {
http.Error(w, "error: could not encode message response", 500)
return
}
}
// helper function contains checks if a string is present in a slice
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}