-
Notifications
You must be signed in to change notification settings - Fork 43
/
lambda.go
281 lines (241 loc) · 7.67 KB
/
lambda.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
// Copyright 2021 Nitric Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lambda_service
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/url"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
ep "github.com/nitrictech/nitric/pkg/plugins/events"
"github.com/nitrictech/nitric/pkg/plugins/gateway"
"github.com/nitrictech/nitric/pkg/providers/aws/core"
"github.com/nitrictech/nitric/pkg/triggers"
"github.com/nitrictech/nitric/pkg/worker"
)
type eventType int
const (
unknown eventType = iota
sns
httpEvent
xforwardHeader string = "x-forwarded-for"
)
type LambdaRuntimeHandler func(handler interface{})
func getEventType(request map[string]interface{}) eventType {
// If our event is a HTTP request
if _, ok := request["rawPath"]; ok {
return httpEvent
} else if records, ok := request["Records"]; ok {
recordsList, _ := records.([]interface{})
record, _ := recordsList[0].(map[string]interface{})
// We have some kind of event here...
// we'll assume its an SNS
var eventSource string
if es, ok := record["EventSource"]; ok {
eventSource = es.(string)
} else if es, ok := record["eventSource"]; ok {
eventSource = es.(string)
}
switch eventSource {
case "aws:sns":
return sns
}
}
return unknown
}
func (s *LambdaGateway) getTopicNameForArn(topicArn string) (string, error) {
topics, err := s.provider.GetResources(core.AwsResource_Topic)
if err != nil {
return "", fmt.Errorf("error retrieving topics: %v", err)
}
for name, arn := range topics {
if arn == topicArn {
return name, nil
}
}
return "", fmt.Errorf("could not find topic for arn %s", topicArn)
}
func (s *LambdaGateway) triggersFromRequest(data map[string]interface{}) ([]triggers.Trigger, error) {
bytes, _ := json.Marshal(data)
trigs := make([]triggers.Trigger, 0)
switch getEventType(data) {
case sns:
snsEvent := &events.SNSEvent{}
if err := json.Unmarshal(bytes, snsEvent); err == nil {
for _, snsRecord := range snsEvent.Records {
messageString := snsRecord.SNS.Message
// FIXME: What about non-nitric SNS events???
messageJson := &ep.NitricEvent{}
var payloadBytes []byte
var id string
// Populate the JSON
if err := json.Unmarshal([]byte(messageString), messageJson); err == nil {
payloadMap := messageJson.Payload
id = messageJson.ID
payloadBytes, _ = json.Marshal(&payloadMap)
} else {
// just try to capture the raw message
payloadBytes = []byte(messageString)
id = snsRecord.SNS.MessageID
}
tName, err := s.getTopicNameForArn(snsRecord.SNS.TopicArn)
if err == nil {
trigs = append(trigs, &triggers.Event{
ID: id,
Topic: tName,
Payload: payloadBytes,
})
} else {
log.Default().Printf("unable to find nitric topic: %v", err)
}
}
}
case httpEvent:
evt := &events.APIGatewayV2HTTPRequest{}
err := json.Unmarshal(bytes, evt)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal httpEvent: %v", err)
}
// Copy the headers and re-write for the proxy
headerCopy := make(map[string][]string)
for key, val := range evt.Headers {
if strings.ToLower(key) == "host" {
headerCopy[xforwardHeader] = append(headerCopy[xforwardHeader], string(val))
} else {
headerCopy[key] = append(headerCopy[key], string(val))
}
}
// Copy the cookies over
headerCopy["Cookie"] = evt.Cookies
// Parse the raw query string
qVals, err := url.ParseQuery(evt.RawQueryString)
if err != nil {
return nil, fmt.Errorf("error parsing query for httpEvent: %v", err)
}
trigs = append(trigs, &triggers.HttpRequest{
// FIXME: Translate to http.Header
Header: headerCopy,
Body: []byte(evt.Body),
Method: evt.RequestContext.HTTP.Method,
Path: evt.RawPath,
Query: qVals,
})
default:
return nil, fmt.Errorf("unhandled event type %v", data)
}
return trigs, nil
}
type LambdaGateway struct {
pool worker.WorkerPool
provider core.AwsProvider
runtime LambdaRuntimeHandler
gateway.UnimplementedGatewayPlugin
finished chan int
}
func (s *LambdaGateway) handle(ctx context.Context, data map[string]interface{}) (interface{}, error) {
trigs, err := s.triggersFromRequest(data)
if err != nil {
return nil, err
}
for _, request := range trigs {
switch request.GetTriggerType() {
case triggers.TriggerType_Request:
if httpEvent, ok := request.(*triggers.HttpRequest); ok {
wrkr, err := s.pool.GetWorker(&worker.GetWorkerOptions{
Http: httpEvent,
})
if err != nil {
return nil, fmt.Errorf("unable to get worker to handle http trigger")
}
response, err := wrkr.HandleHttpRequest(httpEvent)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 500,
Body: "Error processing lambda request",
// TODO: Need to determine best case when to use this...
IsBase64Encoded: true,
}, nil
}
lambdaHTTPHeaders := make(map[string]string)
if response.Header != nil {
response.Header.VisitAll(func(key []byte, val []byte) {
lambdaHTTPHeaders[string(key)] = string(val)
})
}
responseString := base64.StdEncoding.EncodeToString(response.Body)
// We want to sniff the content type of the body that we have here as lambda cannot gzip it...
return events.APIGatewayProxyResponse{
StatusCode: response.StatusCode,
Headers: lambdaHTTPHeaders,
Body: responseString,
// TODO: Need to determine best case when to use this...
IsBase64Encoded: true,
}, nil
} else {
return nil, fmt.Errorf("found non HttpRequest in event with trigger type: %s", triggers.TriggerType_Request.String())
}
case triggers.TriggerType_Subscription:
if event, ok := request.(*triggers.Event); ok {
wrkr, err := s.pool.GetWorker(&worker.GetWorkerOptions{
Event: event,
})
if err != nil {
return nil, fmt.Errorf("unable to get worker to event trigger")
}
if err := wrkr.HandleEvent(event); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("found non Event in event with trigger type: %s", triggers.TriggerType_Subscription.String())
}
}
}
return nil, nil
}
// Start the lambda gateway handler
func (s *LambdaGateway) Start(pool worker.WorkerPool) error {
//s.finished = make(chan int)
s.pool = pool
// Here we want to begin polling lambda for incoming requests...
s.runtime(s.handle)
// Unblock the 'Stop' function if it's waiting.
go func() { s.finished <- 1 }()
return nil
}
func (s *LambdaGateway) Stop() error {
// XXX: This is a NO_OP Process, as this is a pull based system
// We don't need to stop listening to anything
log.Default().Println("gateway 'Stop' called, waiting for lambda runtime to finish")
// Lambda can't be stopped, need to wait for it to finish
<-s.finished
return nil
}
func New(provider core.AwsProvider) (gateway.GatewayService, error) {
return &LambdaGateway{
provider: provider,
runtime: lambda.Start,
finished: make(chan int),
}, nil
}
func NewWithRuntime(provider core.AwsProvider, runtime LambdaRuntimeHandler) (gateway.GatewayService, error) {
return &LambdaGateway{
provider: provider,
runtime: runtime,
finished: make(chan int),
}, nil
}