generated from traefik/plugindemo
/
main.go
310 lines (259 loc) · 7.31 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
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
// Package awslambdaplugin exposes a middleware that invokes a lambda function mimicking the ALB.
package awslambdaplugin
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
)
// Config the plugin configuration.
type Config struct {
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
Region string `json:"region,omitempty"`
FunctionArn string `json:"functionArn,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Region: "",
FunctionArn: "",
Endpoint: "",
}
}
// AwsLambdaPlugin plugin main struct.
type AwsLambdaPlugin struct {
next http.Handler
functionArn string
name string
client *lambda.Lambda
}
// LambdaRequest represents a request to send to lambda.
type LambdaRequest struct {
HTTPMethod string `json:"httpMethod"`
Path string `json:"path"`
QueryStringParameters map[string]string `json:"queryStringParameters"`
MultiValueQueryStringParameters map[string][]string `json:"multiValueQueryStringParameters"`
MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
IsBase64Encoded bool `json:"isBase64Encoded"`
}
// LambdaResponse represents a response to a lambda HTTP request from LB.
type LambdaResponse struct {
StatusCode int `json:"statusCode"`
StatusDescription string `json:"statusDescription"`
IsBase64Encoded bool `json:"isBase64Encoded"`
Headers map[string]string `json:"headers"`
MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
Body string `json:"body"`
}
// New created a new AwsLambdaPlugin plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.FunctionArn) == 0 {
return nil, fmt.Errorf("function arn cannot be empty")
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
var region *string
if len(config.Region) > 0 {
region = aws.String(config.Region)
}
var endpoint *string
if len(config.Endpoint) > 0 {
endpoint = aws.String(config.Endpoint)
}
var creds *credentials.Credentials
if len(config.AccessKey) > 0 && len(config.SecretKey) > 0 {
creds = credentials.NewStaticCredentials(config.AccessKey, config.SecretKey, "")
}
client := lambda.New(sess, &aws.Config{
Region: region,
Endpoint: endpoint,
Credentials: creds,
})
return &AwsLambdaPlugin{
functionArn: config.FunctionArn,
client: client,
next: next,
name: name,
}, nil
}
func (a *AwsLambdaPlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
base64Encoded, body := bodyToBase64(req)
resp := a.invokeFunction(LambdaRequest{
HTTPMethod: req.Method,
Path: req.URL.Path,
QueryStringParameters: valuesToMap(req.URL.Query()),
MultiValueQueryStringParameters: valuesToMultiMap(req.URL.Query()),
Headers: headersToMap(req.Header),
MultiValueHeaders: headersToMultiMap(req.Header),
Body: body,
IsBase64Encoded: base64Encoded,
})
body = resp.Body
if resp.IsBase64Encoded {
buf, err := base64.StdEncoding.DecodeString(body)
if err != nil {
panic(err)
}
body = string(buf)
}
for key, value := range resp.Headers {
rw.Header().Set(key, value)
}
for key, values := range resp.MultiValueHeaders {
for _, value := range values {
rw.Header().Add(key, value)
}
}
rw.WriteHeader(resp.StatusCode)
_, err := rw.Write([]byte(body))
if err != nil {
panic(err)
}
}
func bodyToBase64(req *http.Request) (bool, string) {
base64Encoded := false
body := ""
if req.ContentLength != 0 {
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
_, err := io.Copy(encoder, req.Body)
if err != nil {
panic(err)
}
err = encoder.Close()
if err != nil {
panic(err)
}
body = buf.String()
base64Encoded = true
}
return base64Encoded, body
}
func (a *AwsLambdaPlugin) invokeFunction(request LambdaRequest) LambdaResponse {
payload, err := json.Marshal(request)
if err != nil {
panic(err)
}
result, err := a.client.Invoke(&lambda.InvokeInput{
FunctionName: aws.String(a.functionArn),
Payload: payload,
})
if err != nil {
panic(err)
}
if *result.StatusCode != 200 {
panic(fmt.Errorf("call to lambda failed"))
}
var resp LambdaResponse
err = json.Unmarshal(result.Payload, &resp)
if err != nil {
panic(err)
}
return resp
}
func headersToMap(h http.Header) map[string]string {
values := map[string]string{}
for name, headers := range h {
if len(headers) != 1 {
continue
}
values[name] = headers[0]
}
return values
}
func headersToMultiMap(h http.Header) map[string][]string {
values := map[string][]string{}
for name, headers := range h {
if len(headers) < 2 {
continue
}
values[name] = headers
}
return values
}
func valueToString(f interface{}) (string, bool) {
var v string
typeof := reflect.TypeOf(f)
s := reflect.ValueOf(f)
switch typeof.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v = strconv.FormatInt(s.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v = strconv.FormatUint(s.Uint(), 10)
case reflect.Float32:
v = strconv.FormatFloat(s.Float(), 'f', 4, 32)
case reflect.Float64:
v = strconv.FormatFloat(s.Float(), 'f', 4, 64)
case reflect.String:
v = s.String()
case reflect.Slice:
t, valid := valuesToStrings(f)
if !valid || len(t) != 1 {
return "", false
}
v = t[0]
default:
return "", false
}
return v, true
}
func valuesToStrings(f interface{}) ([]string, bool) {
typeof := reflect.TypeOf(f)
if typeof.Kind() != reflect.Slice {
return []string{}, false
}
var v []string
switch typeof.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Float32, reflect.Float64, reflect.String:
s := reflect.ValueOf(f)
for i := 0; i < s.Len(); i++ {
conv, valid := valueToString(s.Index(i).Interface())
if !valid {
continue
}
v = append(v, conv)
}
default:
return []string{}, false
}
return v, true
}
func valuesToMap(i url.Values) map[string]string {
values := map[string]string{}
for name, val := range i {
value, valid := valueToString(val)
if !valid {
continue
}
values[name] = value
}
return values
}
func valuesToMultiMap(i url.Values) map[string][]string {
values := map[string][]string{}
for name, val := range i {
value, valid := valuesToStrings(val)
if !valid || len(value) == 1 {
continue
}
values[name] = value
}
return values
}