-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsupport.go
276 lines (237 loc) · 6.48 KB
/
support.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
package lambda
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambdacontext"
"strings"
)
type EvtType int
const (
EtUnknown EvtType = iota
EtFlogoOnDemand
EtAwsApiGw
EtAwsAutoScaling
EtAwsCloudFront
EtAwsCloudWatch
EtAwsCloudWatchLogs
EtAwsCodeCommit
EtAwsCodePipeline
EtAwsCognito
EtAwsConfig
EtAwsDynamodb
EtAwsKinesis
EtAwsKinesisAnalytics
EtAwsKinesisFirehose
EtAwsS3
EtAwsSes
EtAwsSns
EtAwsSqs
)
var toEventType = map[string]EvtType{
"flogo:ondemand": EtFlogoOnDemand,
"aws:apigw": EtAwsApiGw,
"aws:autoscaling": EtAwsAutoScaling,
"aws:cloudfront": EtAwsCloudFront,
"aws:cloudwatch": EtAwsCloudWatch,
"aws:cloudwatch-logs": EtAwsCloudWatchLogs,
"aws:codecommit": EtAwsCodeCommit,
"aws:codepipeline": EtAwsCodePipeline,
"aws:cognito": EtAwsCognito,
"aws:config": EtAwsConfig,
"aws:dynamodb": EtAwsDynamodb,
"aws:kinesis": EtAwsKinesis,
"aws:kinesis-analytics": EtAwsKinesisAnalytics,
"aws:kinesis-firehose": EtAwsKinesisFirehose,
"aws:s3": EtAwsS3,
"aws:ses": EtAwsSes,
"aws:sns": EtAwsSns,
"aws:sqs": EtAwsSqs,
}
var fromEventType = map[EvtType]string{
EtFlogoOnDemand: "flogo:ondemand",
EtAwsApiGw: "aws:apigw",
EtAwsAutoScaling: "aws:autoscaling",
EtAwsCloudFront: "aws:cloudfront",
EtAwsCloudWatch: "aws:cloudwatch",
EtAwsCloudWatchLogs: "aws:cloudwatch-logs",
EtAwsCodeCommit: "aws:codecommit",
EtAwsCodePipeline: "aws:codepipeline",
EtAwsCognito: "aws:cognito",
EtAwsConfig: "aws:config",
EtAwsDynamodb: "aws:dynamodb",
EtAwsKinesis: "aws:kinesis",
EtAwsKinesisAnalytics: "aws:kinesis-analytics",
EtAwsKinesisFirehose: "aws:kinesis-firehose",
EtAwsS3: "aws:s3",
EtAwsSes: "aws:ses",
EtAwsSns: "aws:sns",
EtAwsSqs: "aws:sqs",
}
func ToEventType(str string) EvtType {
if et, exists := toEventType[str]; exists {
return et
}
return EtUnknown
}
func FromEventType(et EvtType) string {
if et, exists := fromEventType[et]; exists {
return et
}
return "unknown"
}
func GetEventType(payload map[string]interface{}) EvtType {
if fg, exists := payload["flogo"]; exists {
if fgPkg, ok := fg.(map[string]interface{}); ok {
if _, exists := fgPkg["flow"]; exists {
return EtFlogoOnDemand
}
}
return EtUnknown
}
// look throw all AWS types that have 'Records'
// aws:codecommit, aws:dynamodb, aws:kinesis, aws:s3, aws:ses, aws:sns, aws:sqs
if records, exists := payload["Records"]; exists {
if rArray, ok := records.([]interface{}); ok {
if zeroRecord, ok := rArray[0].(map[string]interface{}); ok {
if es, exists := zeroRecord["eventSource"]; exists {
if eventSource, ok := es.(string); ok {
return ToEventType(eventSource)
} else {
return EtUnknown
}
}
if es, exists := zeroRecord["EventSource"]; exists {
if eventSource, ok := es.(string); ok {
return ToEventType(eventSource)
} else {
return EtUnknown
}
}
if _, exists := zeroRecord["cf"]; exists {
return EtAwsCloudFront
}
}
}
return EtUnknown
}
// look throw all AWS types that have 'records'
// aws:kinesis-analytics, aws:kinesis-firehose"
if records, exists := payload["records"]; exists {
if arn, exists := payload["applicationArn"]; exists {
if arnStr, ok := arn.(string); ok {
if strings.HasPrefix(arnStr, "arn:aws:kinesisanalytics") {
return EtAwsKinesisAnalytics
}
} else {
return EtUnknown
}
}
if rArray, ok := records.([]interface{}); ok {
if zeroRecord, ok := rArray[0].(map[string]interface{}); ok {
if _, exists := zeroRecord["kinesisRecordMetadata"]; exists {
return EtAwsKinesisFirehose
}
}
}
return EtUnknown
}
//API Gateway
if _, exists := payload["requestContext"]; exists {
return EtAwsApiGw
//custom auth request type-request
// "type": "REQUEST",
// "methodArn": "arn:aws:execute-api:...
}
if src, exists := payload["source"]; exists {
if source, ok := src.(string); ok {
if source == "aws.autoscaling" {
return EtAwsAutoScaling
}
if source == "aws.events" {
return EtAwsCloudWatch
}
}
}
if _, exists := payload["awslogs"]; exists {
return EtAwsCloudWatchLogs
}
if _, exists := payload["CodePipeline.job"]; exists {
return EtAwsCodePipeline
}
if et, exists := payload["eventType"]; exists {
if eventType, ok := et.(string); ok {
if eventType == "SyncTrigger" {
return EtAwsCognito
}
}
}
if _, exists := payload["configRuleId"]; exists {
return EtAwsConfig
}
return EtUnknown
}
func ExtractRequestDetails(ctx context.Context, payload []byte) (*RequestDetails, error) {
var payloadObj map[string]interface{}
err := json.Unmarshal(payload, &payloadObj)
if err != nil {
return nil, err
}
et := GetEventType(payloadObj)
lambdaCtx, _ := lambdacontext.FromContext(ctx)
ctxInfo := map[string]interface{}{
"logStreamName": lambdacontext.LogStreamName,
"logGroupName": lambdacontext.LogGroupName,
"functionName": lambdacontext.FunctionName,
"functionVersion": lambdacontext.FunctionVersion,
"awsRequestId": lambdaCtx.AwsRequestID,
"memoryLimitInMB": lambdacontext.MemoryLimitInMB,
}
return &RequestDetails{CtxInfo: ctxInfo, Event: payloadObj, Payload: payload, EventType: et}, nil
}
type RequestDetails struct {
CtxInfo map[string]interface{}
Payload []byte
Event map[string]interface{}
EventType EvtType
}
func GenerateResponse(details *RequestDetails, result map[string]interface{}) ([]byte, error) {
responseData := result["data"]
statusCode := result["status"].(int)
var responseRaw []byte
if val, ok := responseData.(string); ok {
responseRaw = []byte(val)
} else {
var err error
responseRaw, err = json.Marshal(responseData)
if err != nil {
return nil, err
}
}
var resultBytes []byte
// Check if API GW request. If so, build the correct response
switch details.EventType {
case EtAwsApiGw:
resp := events.APIGatewayProxyResponse{
StatusCode: func() int {
if statusCode == 0 {
return 200
} else {
return statusCode
}
}(),
Body: func() string {
return string(responseRaw)
}(),
IsBase64Encoded: false,
}
var err error
resultBytes, err = json.Marshal(resp)
if err != nil {
return nil, err
}
default:
resultBytes = responseRaw
}
return resultBytes, nil
}