forked from akrylysov/algnhsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalb.go
53 lines (46 loc) · 1.61 KB
/
alb.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
package algnhsa
import (
"context"
"encoding/json"
"errors"
"strings"
"github.com/aws/aws-lambda-go/events"
)
var (
errALBUnexpectedRequest = errors.New("expected ALBTargetGroupRequest")
errALBExpectedMultiValueHeaders = errors.New("expected multi value headers; enable Multi value headers in target group settings")
)
func getALBSourceIP(event events.ALBTargetGroupRequest) string {
if xff, ok := event.MultiValueHeaders["x-forwarded-for"]; ok && len(xff) > 0 {
ips := strings.SplitN(xff[0], ",", 2)
if len(ips) > 0 {
return ips[0]
}
}
return ""
}
func newALBRequest(ctx context.Context, payload []byte, opts *Options) (lambdaRequest, error) {
var event events.ALBTargetGroupRequest
if err := json.Unmarshal(payload, &event); err != nil {
return lambdaRequest{}, err
}
if event.RequestContext.ELB.TargetGroupArn == "" {
return lambdaRequest{}, errALBUnexpectedRequest
}
if len(event.MultiValueHeaders) == 0 {
return lambdaRequest{}, errALBExpectedMultiValueHeaders
}
req := lambdaRequest{
HTTPMethod: event.HTTPMethod,
Path: event.Path,
QueryStringParameters: event.QueryStringParameters,
MultiValueQueryStringParameters: event.MultiValueQueryStringParameters,
Headers: event.Headers,
MultiValueHeaders: event.MultiValueHeaders,
Body: event.Body,
IsBase64Encoded: event.IsBase64Encoded,
SourceIP: getALBSourceIP(event),
Context: newTargetGroupRequestContext(ctx, event),
}
return req, nil
}