forked from step-security/secure-repo
/
main.go
72 lines (53 loc) · 1.61 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
type Handler struct {
}
func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
httpRequest := &events.APIGatewayV2HTTPRequest{}
err := json.Unmarshal([]byte(req), &httpRequest)
if err == nil && httpRequest.RawPath != "" {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
dynamoDbSvc := dynamodb.New(sess)
var response events.APIGatewayProxyResponse
if httpRequest.RequestContext.HTTP.Method == "OPTIONS" {
response = events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
}
returnValue, _ := json.Marshal(&response)
return returnValue, nil
}
if strings.Contains(httpRequest.RawPath, "/secure-workflow") {
fixResponse, err := SecureWorkflow(httpRequest.QueryStringParameters, httpRequest.Body, dynamoDbSvc)
if err != nil {
response = events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: err.Error(),
}
} else {
output, _ := json.Marshal(fixResponse)
response = events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: string(output),
}
}
}
returnValue, _ := json.Marshal(&response)
return returnValue, nil
}
return nil, fmt.Errorf("request was neither APIGatewayV2HTTPRequest nor SQSEvent")
}
func main() {
lambda.StartHandler(Handler{})
}