-
Notifications
You must be signed in to change notification settings - Fork 0
/
httproutes.go
485 lines (442 loc) · 13.3 KB
/
httproutes.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package internet
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/labstack/echo"
"github.com/pkg/errors"
"github.com/getblank/blank-router/config"
"github.com/getblank/blank-router/settings"
"github.com/getblank/blank-router/taskq"
"github.com/getblank/uuid"
)
const apiV1baseURI = "/api/v1/"
var (
routesBuildingCompleted bool
errUserIDNotFound = errors.New("not found")
)
type result struct {
Type string `json:"type"`
Data string `json:"data"`
RAWData interface{} `json:"rawData"`
Code int `json:"code"`
Header map[string]string `json:"header"`
FileName string `json:"fileName"`
Store string `json:"store"`
ID string `json:"_id"`
}
func onConfigUpdate(c map[string]config.Store) {
w.Disconnect()
log.Info("New config arrived")
if routesBuildingCompleted {
log.Warn("Routes already built. Need to restart if http hooks or actions modified.")
}
httpEnabledStores := []config.Store{}
for s, store := range c {
if !strings.HasPrefix(store.Store, "_") {
httpEnabledStores = append(httpEnabledStores, store)
}
storeName := s
groupURI := "/hooks/" + storeName + "/"
var lowerGroupURI string
if lowerStoreName := strings.ToLower(storeName); lowerStoreName != storeName {
lowerGroupURI = "/hooks/" + lowerStoreName + "/"
}
group := e.Group(groupURI)
lowerGroup := e.Group(lowerGroupURI)
for i, hook := range store.HTTPHooks {
if hook.URI == "" {
log.Error("Empty URI in hook", strconv.Itoa(i), " for "+groupURI+". Will ignored")
continue
}
var handler func(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc)
var lowerHandler func(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc)
switch hook.Method {
case "GET", "Get", "get":
handler = group.GET
if lowerGroupURI != "" {
lowerHandler = lowerGroup.GET
}
case "POST", "Post", "post":
handler = group.POST
if lowerGroupURI != "" {
lowerHandler = lowerGroup.POST
}
case "PUT", "Put", "put":
handler = group.PUT
if lowerGroupURI != "" {
lowerHandler = lowerGroup.PUT
}
case "PATCH", "Patch", "patch":
handler = group.PATCH
if lowerGroupURI != "" {
lowerHandler = lowerGroup.PATCH
}
case "DELETE", "Delete", "delete":
handler = group.DELETE
if lowerGroupURI != "" {
lowerHandler = lowerGroup.DELETE
}
case "HEAD", "Head", "head":
handler = group.HEAD
if lowerGroupURI != "" {
lowerHandler = lowerGroup.HEAD
}
case "OPTIONS", "Options", "options":
handler = group.OPTIONS
if lowerGroupURI != "" {
lowerHandler = lowerGroup.OPTIONS
}
default:
log.Warn("UNKNOWN HTTP METHOD. Will use GET method ", hook)
handler = group.GET
if lowerGroupURI != "" {
lowerHandler = lowerGroup.GET
}
}
hookIndex := i
var hookHandler = func(c echo.Context) error {
t := taskq.Task{
Store: storeName,
Type: taskq.HTTPHook,
UserID: "root",
Arguments: map[string]interface{}{
"request": extractRequest(c),
"hookIndex": hookIndex,
},
}
_res, err := taskq.PushAndGetResult(&t, 0)
if err != nil {
return c.JSON(http.StatusSeeOther, err.Error())
}
res, err := parseResult(_res)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return defaultResponse(res, c)
}
handler(hook.URI, hookHandler)
log.Infof("Created '%s' httpHook for store '%s' with path %s", hook.Method, storeName, groupURI+hook.URI)
if lowerHandler != nil {
lowerHandler(hook.URI, hookHandler)
log.Infof("Created '%s' httpHook on lower case for store '%s' with path %s", hook.Method, storeName, lowerGroupURI+hook.URI)
}
}
if len(store.Actions) > 0 {
createHTTPActions(storeName, store.Actions)
}
if len(store.StoreActions) > 0 {
createHTTPActions(storeName, store.StoreActions)
}
if store.Type == "file" || store.Type == "files" {
createFileHandlers(storeName)
}
}
routesBuildingCompleted = true
log.Info("Routes building complete")
createRESTAPI(httpEnabledStores)
log.Info("REST API building complete")
getPublicRSAKey()
log.Info("RSA keys received")
}
func createFileHandlers(storeName string) {
groupURI := "/files/" + storeName
group := e.Group(groupURI)
group.POST("/", postFileHandler(storeName), jwtAuthMiddleware(false))
group.GET("/:id", getFileHandler(storeName), jwtAuthMiddleware(true))
group.POST("/:id", postFileHandler(storeName), jwtAuthMiddleware(false))
group.DELETE("/:id", deleteFileHandler(storeName), jwtAuthMiddleware(false))
log.Infof("Created handlers for fileStore '%s' with path %s:id", storeName, groupURI)
}
func writeFileFromFileStore(c echo.Context, storeName, fileID string) error {
res, err := http.Get(settings.GetFileStoreAddress() + "/" + storeName + "/" + fileID)
if err != nil {
return c.JSON(res.StatusCode, res.Status)
}
defer res.Body.Close()
fileName := res.Header.Get("file-name")
c.Response().Header().Add("Content-Type", getContentType(fileName))
c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename="+fileName)
body, _ := ioutil.ReadAll(res.Body)
_, err = c.Response().Write(body)
return err
}
func createHTTPActions(storeName string, actions []config.Action) {
groupURI := "/actions/" + storeName + "/"
group := e.Group(groupURI)
for _, v := range actions {
if v.Type != "http" {
continue
}
actionID := v.ID
group.GET(actionID, func(c echo.Context) error {
_cred := c.Get("cred")
if _cred == nil {
log.Warn("HTTP ACTION: no cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
cred, ok := _cred.(credentials)
if !ok {
log.Warn("HTTP ACTION: invalid cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
t := taskq.Task{
Store: storeName,
Type: taskq.DbAction,
UserID: cred.userID,
Arguments: map[string]interface{}{
"request": extractRequest(c),
"actionId": actionID,
},
}
if cred.claims != nil {
t.Arguments["tokenInfo"] = cred.claims.toMap()
}
if itemID := c.QueryParam("item-id"); itemID != "" {
t.Arguments["itemId"] = itemID
}
_res, err := taskq.PushAndGetResult(&t, 0)
if err != nil {
return c.JSON(http.StatusSeeOther, err.Error())
}
res, err := parseResult(_res)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return defaultResponse(res, c)
}, jwtAuthMiddleware(false))
log.Infof("Registered httpAction for store '%s' with path %s", storeName, groupURI+v.ID)
}
}
func extractRequest(c echo.Context) map[string]interface{} {
params := map[string]string{}
for _, p := range c.ParamNames() {
params[p] = c.Param(p)
}
header := map[string]string{}
for k, v := range c.Request().Header {
if len(v) > 0 {
header[k] = v[0]
}
}
formParams, _ := c.FormParams()
var data interface{}
if _data := formParams["data"]; len(_data) > 0 {
data = _data[0]
}
var body interface{}
bodyBuf := bytes.NewBuffer(nil)
_, err := io.Copy(bodyBuf, c.Request().Body)
if err != nil && err != io.EOF {
log.Errorf("Can't read request http body. Error: %v", err)
} else {
if rtype := header["Content-Type"]; strings.HasPrefix(rtype, "application/json") || strings.HasPrefix(rtype, "text/plain") {
body = bodyBuf.String()
} else {
body = base64.StdEncoding.EncodeToString(bodyBuf.Bytes())
}
}
return map[string]interface{}{
"params": params,
"query": c.QueryParams(),
"form": formParams,
"ip": extractIP(c),
"referer": c.Request().Referer(),
"header": header,
"body": body,
"data": data,
}
}
// Thanks to gin framework for algorithm
// https://github.com/gin-gonic/gin/blob/master/context.go
func extractIP(c echo.Context) string {
if clientIPHeader, ok := c.Request().Header["X-Forwarded-For"]; ok {
if xRealIP, ok := c.Request().Header["X-Real-Ip"]; ok {
clientIP := strings.TrimSpace(xRealIP[0])
if len(clientIP) > 0 {
return clientIP
}
}
clientIP := clientIPHeader[0]
if index := strings.IndexByte(clientIP, ','); index >= 0 {
clientIP = clientIP[0:index]
}
clientIP = strings.TrimSpace(clientIP)
if len(clientIP) > 0 {
return clientIP
}
}
if ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request().RemoteAddr)); err == nil {
return ip
}
return ""
}
func defaultResponse(res *result, c echo.Context) error {
code := res.Code
if code == 0 {
code = http.StatusOK
}
for k, v := range res.Header {
c.Response().Header().Set(k, v)
}
switch res.Type {
case "REDIRECT", "redirect":
if code == 200 {
code = http.StatusFound
}
return c.Redirect(code, res.Data)
case "JSON", "json":
if res.RAWData == nil {
return c.JSONBlob(code, []byte(res.Data))
}
return c.JSON(code, res.RAWData)
case "HTML", "html":
return c.HTML(code, res.Data)
case "XML", "xml":
return c.XMLBlob(code, []byte(res.Data))
case "file":
if res.Store != "" && res.ID != "" {
return writeFileFromFileStore(c, res.Store, res.ID)
}
buffer, err := base64.StdEncoding.DecodeString(res.Data)
if err != nil {
return c.JSON(http.StatusInternalServerError, "can't decode file")
}
c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename="+res.FileName)
return c.Blob(200, getContentType(res.FileName), buffer)
// return c.ServeContent(bytes.NewReader(buffer), res.FileName, time.Now())
default:
return c.JSON(http.StatusSeeOther, "unknown encoding type")
}
}
func parseResult(_res interface{}) (*result, error) {
encoded, err := json.Marshal(_res)
if err != nil {
return nil, errors.Wrap(err, "when parse result")
}
res := new(result)
err = json.Unmarshal(encoded, res)
if err != nil {
err = errors.Wrap(err, "when unmarshal result")
}
return res, err
}
func getFileHandler(storeName string) echo.HandlerFunc {
return func(c echo.Context) error {
_cred := c.Get("cred")
if _cred == nil {
log.Warn("FILES GET: no cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
cred, ok := _cred.(credentials)
if !ok {
log.Warn("FILES GET: invalid cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
fileID := c.Param("id")
t := taskq.Task{
Type: taskq.DbGet,
UserID: cred.userID,
Store: storeName,
Arguments: map[string]interface{}{"_id": fileID},
}
if cred.claims != nil {
t.Arguments["tokenInfo"] = cred.claims.toMap()
}
_, err := taskq.PushAndGetResult(&t, 0)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
writeFileFromFileStore(c, storeName, fileID)
return nil
}
}
func postFileHandler(storeName string) echo.HandlerFunc {
return func(c echo.Context) error {
_cred := c.Get("cred")
if _cred == nil {
log.Warn("FILE POST: no cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
cred, ok := _cred.(credentials)
if !ok {
log.Warn("FILE POST: invalid cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
fileID := c.Param("id")
if fileID == "" {
fileID = uuid.NewV4()
}
fileHeader, err := c.FormFile("file")
if err != nil {
return c.JSON(http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
}
fileName := fileHeader.Filename
t := taskq.Task{
Type: taskq.DbSet,
UserID: cred.userID,
Store: storeName,
Arguments: map[string]interface{}{"item": map[string]string{"_id": fileID, "name": fileName}},
}
if cred.claims != nil {
t.Arguments["tokenInfo"] = cred.claims.toMap()
}
_, err = taskq.PushAndGetResult(&t, 0)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
file, err := fileHeader.Open()
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
defer file.Close()
req, err := http.NewRequest(http.MethodPost, settings.GetFileStoreAddress()+"/"+storeName+"/"+fileID, file)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
req.Header.Set("file-name", fileName)
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, fileID)
}
}
func deleteFileHandler(storeName string) echo.HandlerFunc {
return func(c echo.Context) error {
_cred := c.Get("cred")
if _cred == nil {
log.Warn("FILES DELETE: no cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
cred, ok := _cred.(credentials)
if !ok {
log.Warn("FILES DELETE: invalid cred in echo context")
return c.JSON(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
fileID := c.Param("id")
t := taskq.Task{
Type: taskq.DbDelete,
UserID: cred.userID,
Store: storeName,
Arguments: map[string]interface{}{"item": map[string]string{"_id": fileID}},
}
if cred.claims != nil {
t.Arguments["tokenInfo"] = cred.claims.toMap()
}
_, err := taskq.PushAndGetResult(&t, 0)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
// we don't delete files from file store for now
return c.JSON(http.StatusOK, nil)
}
}