This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.go
174 lines (147 loc) · 5.19 KB
/
plugin.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
package aws
import (
"unicode/utf8"
envoyapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoyauth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
envoyroute "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
envoyhttp "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2"
"github.com/gogo/protobuf/types"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/solo-io/gloo-api/pkg/api/types/v1"
"github.com/solo-io/gloo/pkg/coreplugins/common"
"github.com/solo-io/gloo/pkg/plugin"
)
func init() {
plugin.Register(&Plugin{}, nil)
}
type Plugin struct {
isNeeded bool
}
const (
// define Upstream type name
UpstreamTypeAws = "aws"
// generic plugin info
filterName = "io.solo.lambda"
pluginStage = plugin.OutAuth
// filter-specific metadata
filterMetadataKeyAsync = "async"
// upstream-specific metadata
AwsAccessKey = "access_key"
AwsSecretKey = "secret_key"
awsRegion = "region"
awsHost = "host"
// function-specific metadata
functionNameKey = "name"
functionQualifierKey = "qualifier"
)
func (p *Plugin) GetDependencies(cfg *v1.Config) *plugin.Dependencies {
deps := new(plugin.Dependencies)
for _, upstream := range cfg.Upstreams {
if upstream.Type != UpstreamTypeAws {
continue
}
awsUpstream, err := DecodeUpstreamSpec(upstream.Spec)
if err != nil {
// errors will be handled during validation
// TODO: consider logging error here
continue
}
deps.SecretRefs = append(deps.SecretRefs, awsUpstream.SecretRef)
}
return deps
}
func (p *Plugin) HttpFilters(params *plugin.FilterPluginParams) []plugin.StagedFilter {
defer func() { p.isNeeded = false }()
if p.isNeeded {
return []plugin.StagedFilter{{HttpFilter: &envoyhttp.HttpFilter{Name: filterName}, Stage: pluginStage}}
}
return nil
}
func (p *Plugin) ProcessRoute(_ *plugin.RoutePluginParams, in *v1.Route, out *envoyroute.Route) error {
executionStyle, err := GetExecutionStyle(in.Extensions)
if err != nil {
return err
}
if executionStyle == ExecutionStyleNone {
return nil
}
setRouteAsync(executionStyle == ExecutionStyleAsync, out)
return nil
}
func setRouteAsync(async bool, out *envoyroute.Route) {
if out.Metadata == nil {
out.Metadata = &envoycore.Metadata{}
}
common.InitFilterMetadataField(filterName, filterMetadataKeyAsync, out.Metadata).Kind = &types.Value_BoolValue{
BoolValue: async,
}
}
func (p *Plugin) ProcessUpstream(params *plugin.UpstreamPluginParams, in *v1.Upstream, out *envoyapi.Cluster) error {
if in.Type != UpstreamTypeAws {
return nil
}
p.isNeeded = true
out.Type = envoyapi.Cluster_LOGICAL_DNS
// need to make sure we use ipv4 only dns
out.DnsLookupFamily = envoyapi.Cluster_V4_ONLY
awsUpstream, err := DecodeUpstreamSpec(in.Spec)
if err != nil {
return errors.Wrap(err, "invalid AWS upstream spec")
}
out.Hosts = append(out.Hosts, &envoycore.Address{Address: &envoycore.Address_SocketAddress{SocketAddress: &envoycore.SocketAddress{
Address: awsUpstream.GetLambdaHostname(),
PortSpecifier: &envoycore.SocketAddress_PortValue{PortValue: 443},
}}})
out.TlsContext = &envoyauth.UpstreamTlsContext{
Sni: awsUpstream.GetLambdaHostname(),
}
awsSecrets, ok := params.Secrets[awsUpstream.SecretRef]
if !ok {
return errors.Errorf("aws secrets for ref %v not found", awsUpstream.SecretRef)
}
var secretErrs error
accessKey, ok := awsSecrets[AwsAccessKey]
if !ok {
secretErrs = multierror.Append(secretErrs, errors.Errorf("key %v missing from provided secret", AwsAccessKey))
}
if accessKey == "" || !utf8.Valid([]byte(accessKey)) {
secretErrs = multierror.Append(secretErrs, errors.Errorf("%s not a valid string", AwsAccessKey))
}
secretKey, ok := awsSecrets[AwsSecretKey]
if !ok {
secretErrs = multierror.Append(secretErrs, errors.Errorf("key %v missing from provided secret", AwsSecretKey))
}
if secretKey == "" || !utf8.Valid([]byte(secretKey)) {
secretErrs = multierror.Append(secretErrs, errors.Errorf("%s not a valid string", AwsSecretKey))
}
if out.Metadata == nil {
out.Metadata = &envoycore.Metadata{}
}
common.InitFilterMetadata(filterName, out.Metadata)
out.Metadata.FilterMetadata[filterName] = &types.Struct{
Fields: map[string]*types.Value{
AwsAccessKey: {Kind: &types.Value_StringValue{StringValue: accessKey}},
AwsSecretKey: {Kind: &types.Value_StringValue{StringValue: secretKey}},
awsRegion: {Kind: &types.Value_StringValue{StringValue: awsUpstream.Region}},
awsHost: {Kind: &types.Value_StringValue{StringValue: awsUpstream.GetLambdaHostname()}},
},
}
return secretErrs
}
func (p *Plugin) ParseFunctionSpec(params *plugin.FunctionPluginParams, in v1.FunctionSpec) (*types.Struct, error) {
if params.UpstreamType != UpstreamTypeAws {
return nil, nil
}
functionSpec, err := DecodeFunctionSpec(in)
if err != nil {
return nil, errors.Wrap(err, "invalid lambda function spec")
}
return &types.Struct{
Fields: map[string]*types.Value{
functionNameKey: {Kind: &types.Value_StringValue{StringValue: functionSpec.FunctionName}},
functionQualifierKey: {Kind: &types.Value_StringValue{StringValue: functionSpec.Qualifier}},
},
}, nil
}