-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
311 lines (281 loc) · 8.53 KB
/
config.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
311
package awslambda
import (
"encoding/json"
"net/http"
"strings"
"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"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
// Config specifies configuration for a single awslambda block
type Config struct {
// Path this config block maps to
Path string
// AWS Access Key. If omitted, AWS_ACCESS_KEY_ID env var is used.
AwsAccess string
// AWS Secret Key. If omitted, AWS_SECRET_ACCESS_KEY env var is used.
AwsSecret string
// AWS Region. If omitted, AWS_REGION env var is used.
AwsRegion string
// Optional qualifier to use on Invoke requests.
// This can be used to pin a configuration to a particular alias (e.g. 'prod' or 'dev')
Qualifier string
// Function name include rules. Prefix and suffix '*' globs are supported.
// Functions matching *any* of these rules will be proxied.
// If Include is empty, all function names will be allowed (unless explicitly excluded).
Include []string
// Function name exclude rules. Prefix and suffix '*" globs are supported.
// Functions matching *any* of these rules will be excluded, and not proxied.
// If Exclude is empty, no exclude rules will be applied.
Exclude []string
// Optional strings to prepend or append to the parsed function name from the URL
// before invoking the lambda. These are applied after the Include/Exclude rules are run
NamePrepend string
NameAppend string
// If set, all requests to this path will invoke this function.
// The function name will not be parsed from the URL.
// This is useful for cases where you are multiplexing requests inside
// the lambda function itself.
//
// Note: If set, Include and Exclude will be ignored.
//
Single string
// If true, the Path field and function name will be removed from the
// RequestMeta.Path sent to the lambda function. If Single is set,
// only the Path will be removed.
//
// For example, given: awslambda /api/ and a request to: /api/hello/foo
// the RequestMeta.Path would be /foo
StripPathPrefix bool
// headers to set in the upstream "headers" array - caddy placeholders work here
UpstreamHeaders map[string][]string
invoker Invoker
}
// AcceptsFunction tests whether the given function name is supported for
// this configuration by applying the Include and Exclude rules.
//
// Some additional lightweight sanity tests are also performed. For example,
// empty strings and names containing periods (prohibited by AWS Lambda) will
// return false, but there is no attempt to ensure that all AWS Lambda naming
// rules are validated. That is, some invalid names could be passed through.
//
func (c *Config) AcceptsFunction(name string) bool {
if name == "" || strings.Index(name, ".") >= 0 {
return false
}
if len(c.Include) > 0 {
found := false
for _, k := range c.Include {
if matchGlob(name, k) {
found = true
break
}
}
if !found {
return false
}
}
for _, k := range c.Exclude {
if matchGlob(name, k) {
return false
}
}
return true
}
// ToAwsConfig returns a new *aws.Config instance using the AWS related values on Config.
// If AwsRegion is empty, the AWS_REGION env var is used.
// If AwsAccess is empty, the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env vars are used.
func (c *Config) ToAwsConfig() *aws.Config {
awsConf := aws.NewConfig()
if c.AwsRegion != "" {
awsConf.WithRegion(c.AwsRegion)
}
if c.AwsAccess != "" {
awsConf.WithCredentials(credentials.NewStaticCredentials(
c.AwsAccess, c.AwsSecret, "",
))
}
return awsConf
}
// ParseFunction returns the fragment of path immediately after the
// config path, excluding string and named anchors.
//
// For example, given a path of '/lambda/my-func/pathparam?a=/foo',
// ParseFunction returns 'my-func'
func (c *Config) ParseFunction(path string) string {
path = strings.TrimPrefix(path, c.Path)
pos := strings.Index(path, "?")
if pos > -1 {
path = path[:pos]
}
pos = strings.Index(path, "#")
if pos > -1 {
path = path[:pos]
}
return strings.Split(path, "/")[0]
}
// MaybeToInvokeInput returns a new InvokeInput instanced based on the HTTP request.
// If the function name parsed from the r.URL.Path doesn't comply with the Config's
// include/exclude rules, then nil, nil is returned.
// Otherwise an InvokeInput is returned with all fields populated based on the
// http.Request, and the NameAppend and NamePrepend rules applied (if any).
func (c *Config) MaybeToInvokeInput(r *http.Request) (*lambda.InvokeInput, error) {
// Verify that parsed function name is allowed based on Config rules
funcName := c.Single
if funcName == "" {
funcName = c.ParseFunction(r.URL.Path)
if !c.AcceptsFunction(funcName) {
return nil, nil
}
}
req, err := NewRequest(r)
if err != nil {
return nil, err
}
if c.StripPathPrefix && req.Meta != nil {
req.Meta.Path = c.stripPathPrefix(req.Meta.Path, funcName)
}
if len(c.UpstreamHeaders) > 0 {
// inject upstream headers defined with the header_upstream directive into req.Meta.Headers
// uses caddy's integrated replacer for placeholder replacement (https://caddyserver.com/docs/placeholders)
replInt := r.Context().Value(httpserver.ReplacerCtxKey)
replacer := replInt.(httpserver.Replacer)
for k, v := range c.UpstreamHeaders {
newValue := make([]string, len(v))
for i, v := range v {
newValue[i] = replacer.Replace(v)
}
req.Meta.Headers[strings.ToLower(k)] = newValue
}
}
payload, err := json.Marshal(req)
if err != nil {
return nil, err
}
if c.NamePrepend != "" {
funcName = c.NamePrepend + funcName
}
if c.NameAppend != "" {
funcName = funcName + c.NameAppend
}
input := &lambda.InvokeInput{
FunctionName: &funcName,
Payload: payload,
}
if c.Qualifier != "" {
input.Qualifier = &c.Qualifier
}
return input, nil
}
func (c *Config) initLambdaClient() error {
sess, err := session.NewSession(c.ToAwsConfig())
if err != nil {
return err
}
c.invoker = lambda.New(sess)
return nil
}
func (c *Config) stripPathPrefix(reqPath, funcName string) string {
prefix := c.Path
if c.Single == "" {
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
prefix += funcName
}
if strings.HasPrefix(reqPath, prefix) {
reqPath = reqPath[len(prefix):]
if !strings.HasPrefix(reqPath, "/") {
reqPath = "/" + reqPath
}
}
return reqPath
}
// ParseConfigs parses a Caddy awslambda config block into a Config struct.
func ParseConfigs(c *caddy.Controller) ([]*Config, error) {
var configs []*Config
var conf *Config
last := ""
for c.Next() {
val := c.Val()
lastTmp := last
last = ""
switch lastTmp {
case "awslambda":
conf = &Config{
Path: val,
Include: []string{},
Exclude: []string{},
}
configs = append(configs, conf)
case "aws_access":
conf.AwsAccess = val
case "aws_secret":
conf.AwsSecret = val
case "aws_region":
conf.AwsRegion = val
case "qualifier":
conf.Qualifier = val
case "name_prepend":
conf.NamePrepend = val
case "name_append":
conf.NameAppend = val
case "single":
conf.Single = val
case "strip_path_prefix":
conf.StripPathPrefix = toBool(val)
case "include":
conf.Include = append(conf.Include, val)
conf.Include = append(conf.Include, c.RemainingArgs()...)
case "exclude":
conf.Exclude = append(conf.Exclude, val)
conf.Exclude = append(conf.Exclude, c.RemainingArgs()...)
case "header_upstream":
if conf.UpstreamHeaders == nil {
conf.UpstreamHeaders = make(map[string][]string)
}
value := strings.Join(c.RemainingArgs(), " ")
conf.UpstreamHeaders[val] = []string{value}
default:
last = val
}
}
for _, conf := range configs {
err := conf.initLambdaClient()
if err != nil {
return nil, err
}
}
return configs, nil
}
// toBool treats any of the following as true: 1, yes, y, on, true
// otherwise returns false
func toBool(s string) bool {
s = strings.ToLower(s)
if s == "1" || s == "y" || s == "yes" || s == "true" || s == "on" {
return true
}
return false
}
// matchGlob returns true if string s matches the rule.
// Simple prefix and suffix wildcards are supported with '*'.
// For example, string 'hello' matches rules: 'hello', 'hel*', '*llo', '*ell*'
func matchGlob(s, rule string) bool {
if s == rule {
return true
}
if strings.HasPrefix(rule, "*") {
if strings.HasSuffix(rule, "*") {
rule = rule[1 : len(rule)-1]
return strings.Index(s, rule) >= 0
}
return strings.HasSuffix(s, rule[1:])
} else if strings.HasSuffix(rule, "*") {
return strings.HasPrefix(s, rule[0:len(rule)-1])
} else {
return false
}
}