forked from aquasecurity/lmdrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lreq.go
412 lines (375 loc) · 11 KB
/
lreq.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
package lreq
import (
"cloud.google.com/go/civil"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/seantcanavan/lambda_jwt_router/lres"
"go.mongodb.org/mongo-driver/bson/primitive"
"log"
"net/http"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
var boolRegex = regexp.MustCompile(`^1|true|on|enabled|t$`)
// UnmarshalReq "fills" out a target Go struct with data from the req.
// If body is true, then the req body is assumed to be JSON and simply
// unmarshalled into the target (taking into account that the req body may
// be base-64 encoded). After that, or if body is false, the function will
// traverse the exported fields of the target struct, and fill those that
// include the "lambda" struct tag with values taken from the request's query
// string parameters, path parameters and headers, according to the field's
// struct tag definition. This means a struct value can be filled with data from
// the body, the path, the query string and the headers at the same time.
//
// Field types are currently limited to string, all integer types, all unsigned
// integer types, all float types, booleans, slices of the aforementioned types
// and pointers of these types.
//
// Note that custom types that alias any of the aforementioned types are also
// accepted and the appropriate constant values will be generated. Boolean
// fields accept (in a case-insensitive way) the values "1", "true", "on" and
// "enabled". Any other value is considered false.
//
// Example struct (no body):
//
// type ListPostsInput struct {
// ID uint64 `lambda:"path.id"`
// Page uint64 `lambda:"query.page"`
// PageSize uint64 `lambda:"query.page_size"`
// Search string `lambda:"query.search"`
// ShowDrafts bool `lambda:"query.show_hidden"`
// Languages []string `lambda:"header.Accept-Language"`
// }
//
// Example struct (JSON body):
//
// type UpdatePostInput struct {
// ID uint64 `lambda:"path.id"`
// Author string `lambda:"header.Author"`
// Title string `json:"title"`
// Content string `json:"content"`
// }
func UnmarshalReq(req events.APIGatewayProxyRequest, body bool, target interface{}) error {
if body {
err := unmarshalBody(req, target)
if err != nil {
return err
}
}
return unmarshalEvent(req, target)
}
func unmarshalBody(req events.APIGatewayProxyRequest, target interface{}) (err error) {
if req.IsBase64Encoded {
var body []byte
body, err = base64.StdEncoding.DecodeString(req.Body)
if err != nil {
return fmt.Errorf("failed decoding body: %w", err)
}
err = json.Unmarshal(body, target)
} else {
err = json.Unmarshal([]byte(req.Body), target)
}
if err != nil {
return lres.HTTPError{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("invalid req body: %s", err),
}
}
return nil
}
func unmarshalEvent(req events.APIGatewayProxyRequest, target interface{}) error {
rv := reflect.ValueOf(target)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("invalid unmarshal target, must be pointer to struct")
}
v := rv.Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
typeField := t.Field(i)
valueField := v.Field(i)
lambdaTag := typeField.Tag.Get("lambda")
if lambdaTag == "" {
continue
}
components := strings.Split(lambdaTag, ".")
if len(components) != 2 {
return fmt.Errorf("invalid lambda tag for field %s", typeField.Name)
}
var sourceMap map[string]string
var multiMap map[string][]string
switch components[0] {
case "query":
sourceMap = req.QueryStringParameters
multiMap = req.MultiValueQueryStringParameters
case "path":
sourceMap = req.PathParameters
case "header":
sourceMap = req.Headers
multiMap = req.MultiValueHeaders
default:
return fmt.Errorf(
"invalid param location %q for field %s",
components[0], typeField.Name,
)
}
err := unmarshalField(
typeField.Type,
valueField,
sourceMap,
multiMap,
components[1],
)
if err != nil {
return err
}
}
return nil
}
func unmarshalField(
typeField reflect.Type,
valueField reflect.Value,
params map[string]string,
multiParam map[string][]string,
param string,
) error {
strVal, ok := params[param]
strVals, okMulti := multiParam[param]
switch typeField.Kind() {
case reflect.Array:
if strVal == "" {
return nil
}
objectID, err := primitive.ObjectIDFromHex(strVal)
if err != nil {
return fmt.Errorf("invalid ObjectID: %s", err)
}
valueField.Set(reflect.ValueOf(objectID))
case reflect.String:
valueField.SetString(strVal)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value, err := parseInt64Param(param, strVal, ok)
if err != nil {
return err
}
valueField.SetInt(value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value, err := parseUint64Param(param, strVal, ok)
if err != nil {
return err
}
valueField.SetUint(value)
case reflect.Float32, reflect.Float64:
value, err := parseFloat64Param(param, strVal, ok)
if err != nil {
return err
}
valueField.SetFloat(value)
case reflect.Bool:
valueField.SetBool(boolRegex.MatchString(strings.ToLower(strVal)))
case reflect.Ptr:
if ok {
switch typeField.Elem().Kind() {
case reflect.String:
valueField.Set(reflect.ValueOf(&strVal).Convert(typeField))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value, err := parseInt64Param(param, strVal, ok)
if err != nil {
return err
}
// Create a new pointer to the integer type
intPtr := reflect.New(typeField.Elem())
// Set the value to the newly created pointer
intPtr.Elem().SetInt(value)
// Set the field to the new pointer
valueField.Set(intPtr)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value, err := parseUint64Param(param, strVal, ok)
if err != nil {
return err
}
// Create a new pointer to the integer type
intPtr := reflect.New(typeField.Elem())
// Set the value to the newly created pointer
intPtr.Elem().SetUint(value)
// Set the field to the new pointer
valueField.Set(intPtr)
case reflect.Float32, reflect.Float64:
value, err := parseFloat64Param(param, strVal, ok)
if err != nil {
return err
}
// Create a new pointer to the integer type
intPtr := reflect.New(typeField.Elem())
// Set the value to the newly created pointer
intPtr.Elem().SetFloat(value)
// Set the field to the new pointer
valueField.Set(intPtr)
case reflect.Struct:
if typeField.Elem() == reflect.TypeOf(civil.Date{}) {
if strVal == "" {
return nil
}
parsedCivil, err := civil.ParseDate(strVal)
if err != nil {
return err
}
valueField.Set(reflect.ValueOf(&parsedCivil))
} else if typeField.Elem() == reflect.TypeOf(time.Time{}) {
if strVal == "" {
return nil
}
parsedTime, err := time.Parse(time.RFC3339, strVal)
if err != nil {
return err
}
valueField.Set(reflect.ValueOf(&parsedTime))
}
case reflect.Bool:
b := boolRegex.MatchString(strings.ToLower(strVal))
valueField.Set(reflect.ValueOf(&b))
// Handling mongo DB ID types
default:
switch typeField.Elem() {
case reflect.TypeOf(primitive.ObjectID{}):
if strVal == "" {
return nil
}
objectID, err := primitive.ObjectIDFromHex(strVal)
if err != nil {
return fmt.Errorf("invalid ObjectID: %s", err)
}
valueField.Set(reflect.ValueOf(&objectID))
}
}
}
case reflect.Slice:
if typeField.Elem().Kind() == reflect.Ptr && typeField.Elem().Elem().Kind() == reflect.String {
// Handling the slice of pointers to custom string type (like Number)
stringValues := strVals
if !okMulti {
stringValues = strings.Split(strVal, ",")
}
slice := reflect.MakeSlice(typeField, len(stringValues), len(stringValues))
for i, strVal := range stringValues {
// Create a new instance of the element type (which is a pointer)
newElemPtr := reflect.New(typeField.Elem().Elem())
// Set the value of the new instance
newElemPtr.Elem().SetString(strVal)
// Set the slice element to the new instance
slice.Index(i).Set(newElemPtr)
}
valueField.Set(slice)
} else {
// we'll be extracting values from multiParam, generating a slice and
// putting it in valueField
if okMulti {
slice := reflect.MakeSlice(typeField, len(strVals), len(strVals))
for i, str := range strVals {
err := unmarshalField(
typeField.Elem(),
slice.Index(i),
map[string]string{"param": str},
nil,
"param",
)
if err != nil {
return err
}
}
valueField.Set(slice)
} else {
if ok {
stringParts := strings.Split(strVal, ",")
if len(stringParts) < 1 {
return nil
}
slice := reflect.MakeSlice(typeField, len(stringParts), len(stringParts))
for i, p := range stringParts {
inVal := reflect.ValueOf(p)
asVal := inVal.Convert(typeField.Elem())
slice.Index(i).Set(asVal)
}
valueField.Set(slice)
}
}
}
case reflect.Struct:
switch valueField.Type() {
case reflect.TypeOf(time.Time{}):
if strVal == "" {
return nil
}
parsedTime, err := time.Parse(time.RFC3339, strVal)
if err != nil {
return err
}
valueField.Set(reflect.ValueOf(parsedTime))
case reflect.TypeOf(civil.Date{}):
if strVal == "" {
return nil
}
parsedCivil, err := civil.ParseDate(strVal)
if err != nil {
return err
}
valueField.Set(reflect.ValueOf(parsedCivil))
}
}
return nil
}
func parseInt64Param(param, str string, ok bool) (value int64, err error) {
if !ok {
return value, nil
}
value, err = strconv.ParseInt(str, 10, 64)
if err != nil {
return value, lres.HTTPError{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("%s must be a valid integer", param),
}
}
return value, nil
}
func parseUint64Param(param, str string, ok bool) (value uint64, err error) {
if !ok {
return value, nil
}
value, err = strconv.ParseUint(str, 10, 64)
if err != nil {
return value, lres.HTTPError{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("%s must be a valid, positive integer", param),
}
}
return value, nil
}
func parseFloat64Param(param, str string, ok bool) (value float64, err error) {
if !ok {
return value, nil
}
value, err = strconv.ParseFloat(str, 64)
if err != nil {
return value, lres.HTTPError{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("%s must be a valid floating point number", param),
}
}
return value, nil
}
// MarshalReq will take an interface input, marshal it to JSON, and add the
// JSON as a string to the events.APIGatewayProxyRequest body field before returning.
func MarshalReq(input interface{}) events.APIGatewayProxyRequest {
jsonBytes, err := json.Marshal(input)
if err != nil {
log.Fatalf("%+v", err)
}
return events.APIGatewayProxyRequest{
Body: string(jsonBytes),
}
}