consul/agent/envoyextensions/builtin/aws-lambda/aws_lambda_test.go

439 lines
12 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package awslambda
import (
"fmt"
"testing"
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_endpoint_v3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
envoy_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_lambda_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/aws_lambda/v3"
envoy_http_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
envoy_tls_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
pstruct "google.golang.org/protobuf/types/known/structpb"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/envoyextensions/extensioncommon"
Protobuf Refactoring for Multi-Module Cleanliness (#16302) Protobuf Refactoring for Multi-Module Cleanliness This commit includes the following: Moves all packages that were within proto/ to proto/private Rewrites imports to account for the packages being moved Adds in buf.work.yaml to enable buf workspaces Names the proto-public buf module so that we can override the Go package imports within proto/buf.yaml Bumps the buf version dependency to 1.14.0 (I was trying out the version to see if it would get around an issue - it didn't but it also doesn't break things and it seemed best to keep up with the toolchain changes) Why: In the future we will need to consume other protobuf dependencies such as the Google HTTP annotations for openapi generation or grpc-gateway usage. There were some recent changes to have our own ratelimiting annotations. The two combined were not working when I was trying to use them together (attempting to rebase another branch) Buf workspaces should be the solution to the problem Buf workspaces means that each module will have generated Go code that embeds proto file names relative to the proto dir and not the top level repo root. This resulted in proto file name conflicts in the Go global protobuf type registry. The solution to that was to add in a private/ directory into the path within the proto/ directory. That then required rewriting all the imports. Is this safe? AFAICT yes The gRPC wire protocol doesn't seem to care about the proto file names (although the Go grpc code does tack on the proto file name as Metadata in the ServiceDesc) Other than imports, there were no changes to any generated code as a result of this.
2023-02-17 21:14:46 +00:00
"github.com/hashicorp/consul/proto/private/prototest"
)
func TestConstructor(t *testing.T) {
kind := api.ServiceKindTerminatingGateway
cases := map[string]struct {
extensionName string
arn string
payloadPassthrough bool
region string
expected awsLambda
ok bool
}{
"no arguments": {
ok: false,
},
"a bad name": {
arn: "arn",
region: "blah",
extensionName: "bad",
ok: false,
},
"missing arn": {
region: "blah",
ok: false,
},
"including payload passthrough": {
arn: "arn",
region: "blah",
payloadPassthrough: true,
expected: awsLambda{
ARN: "arn",
PayloadPassthrough: true,
},
ok: true,
},
}
for n, tc := range cases {
t.Run(n, func(t *testing.T) {
extensionName := api.BuiltinAWSLambdaExtension
if tc.extensionName != "" {
extensionName = tc.extensionName
}
svc := api.CompoundServiceName{Name: "svc"}
ext := extensioncommon.RuntimeConfig{
ServiceName: svc,
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
svc: {OutgoingProxyKind: kind},
},
EnvoyExtension: api.EnvoyExtension{
Name: extensionName,
Arguments: map[string]interface{}{
"ARN": tc.arn,
"PayloadPassthrough": tc.payloadPassthrough,
},
},
}
e, err := Constructor(ext.EnvoyExtension)
if tc.ok {
require.NoError(t, err)
require.Equal(t, &extensioncommon.BasicEnvoyExtender{Extension: &tc.expected}, e)
} else {
require.Error(t, err)
}
})
}
}
func TestCanApply(t *testing.T) {
a := awsLambda{}
require.False(t, a.CanApply(&extensioncommon.RuntimeConfig{
Kind: api.ServiceKindConnectProxy,
ServiceName: api.CompoundServiceName{Name: "s1"},
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
{Name: "s1"}: {
OutgoingProxyKind: api.ServiceKindTerminatingGateway,
},
},
}))
require.True(t, a.CanApply(&extensioncommon.RuntimeConfig{
Kind: api.ServiceKindConnectProxy,
ServiceName: api.CompoundServiceName{Name: "s1"},
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
{Name: "s1"}: {
OutgoingProxyKind: api.ServiceKindConnectProxy,
},
},
}))
}
func TestPatchCluster(t *testing.T) {
cases := []struct {
name string
lambda awsLambda
input *envoy_cluster_v3.Cluster
expectedRegion string
isErrExpected bool
}{
{
name: "nominal",
input: &envoy_cluster_v3.Cluster{
Name: "test-cluster",
},
lambda: awsLambda{
ARN: "arn:aws:lambda:us-east-1:111111111111:function:lambda-1234",
PayloadPassthrough: true,
InvocationMode: "Asynchronous",
},
expectedRegion: "us-east-1",
},
{
name: "error invalid arn",
input: &envoy_cluster_v3.Cluster{
Name: "test-cluster",
},
lambda: awsLambda{
ARN: "?!@%^SA",
PayloadPassthrough: true,
InvocationMode: "Asynchronous",
},
isErrExpected: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
transportSocket, err := makeUpstreamTLSTransportSocket(&envoy_tls_v3.UpstreamTlsContext{
Sni: "*.amazonaws.com",
})
require.NoError(t, err)
expectedCluster := &envoy_cluster_v3.Cluster{
Name: tc.input.Name,
ConnectTimeout: tc.input.ConnectTimeout,
ClusterDiscoveryType: &envoy_cluster_v3.Cluster_Type{Type: envoy_cluster_v3.Cluster_LOGICAL_DNS},
DnsLookupFamily: envoy_cluster_v3.Cluster_V4_ONLY,
LbPolicy: envoy_cluster_v3.Cluster_ROUND_ROBIN,
Metadata: &envoy_core_v3.Metadata{
FilterMetadata: map[string]*pstruct.Struct{
"com.amazonaws.lambda": {
Fields: map[string]*pstruct.Value{
"egress_gateway": {Kind: &pstruct.Value_BoolValue{BoolValue: true}},
},
},
},
},
LoadAssignment: &envoy_endpoint_v3.ClusterLoadAssignment{
ClusterName: tc.input.Name,
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{
{
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{
{
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
Endpoint: &envoy_endpoint_v3.Endpoint{
Address: &envoy_core_v3.Address{
Address: &envoy_core_v3.Address_SocketAddress{
SocketAddress: &envoy_core_v3.SocketAddress{
Address: fmt.Sprintf("lambda.%s.amazonaws.com", tc.expectedRegion),
PortSpecifier: &envoy_core_v3.SocketAddress_PortValue{
PortValue: 443,
},
},
},
},
},
},
},
},
},
},
},
TransportSocket: transportSocket,
}
// Test patching the cluster
rc := extensioncommon.RuntimeConfig{}
patchedCluster, patchSuccess, err := tc.lambda.PatchCluster(&rc, tc.input)
if tc.isErrExpected {
assert.Error(t, err)
assert.False(t, patchSuccess)
} else {
assert.NoError(t, err)
assert.True(t, patchSuccess)
assert.Equal(t, expectedCluster, patchedCluster)
}
})
}
}
func TestPatchRoute(t *testing.T) {
tests := map[string]struct {
conf *extensioncommon.RuntimeConfig
route *envoy_route_v3.RouteConfiguration
expectRoute *envoy_route_v3.RouteConfiguration
expectBool bool
}{
"non terminating gateway unmodified": {
conf: &extensioncommon.RuntimeConfig{
Kind: api.ServiceKindConnectProxy,
},
route: &envoy_route_v3.RouteConfiguration{
VirtualHosts: []*envoy_route_v3.VirtualHost{
{
Routes: []*envoy_route_v3.Route{
{
Action: &envoy_route_v3.Route_Route{
Route: &envoy_route_v3.RouteAction{
HostRewriteSpecifier: &envoy_route_v3.RouteAction_HostRewriteLiteral{},
},
},
},
},
},
},
},
expectRoute: &envoy_route_v3.RouteConfiguration{
VirtualHosts: []*envoy_route_v3.VirtualHost{
{
Routes: []*envoy_route_v3.Route{
{
Action: &envoy_route_v3.Route_Route{
Route: &envoy_route_v3.RouteAction{
HostRewriteSpecifier: &envoy_route_v3.RouteAction_HostRewriteLiteral{},
},
},
},
},
},
},
},
expectBool: false,
},
"terminating gateway modified": {
conf: &extensioncommon.RuntimeConfig{
Kind: api.ServiceKindTerminatingGateway,
},
route: &envoy_route_v3.RouteConfiguration{
VirtualHosts: []*envoy_route_v3.VirtualHost{
{
Routes: []*envoy_route_v3.Route{
// This should be modified.
{
Action: &envoy_route_v3.Route_Route{
Route: &envoy_route_v3.RouteAction{
HostRewriteSpecifier: &envoy_route_v3.RouteAction_HostRewriteLiteral{},
},
},
},
// This should be not be modified.
{
Action: &envoy_route_v3.Route_DirectResponse{},
},
},
},
},
},
expectRoute: &envoy_route_v3.RouteConfiguration{
VirtualHosts: []*envoy_route_v3.VirtualHost{
{
Routes: []*envoy_route_v3.Route{
{
Action: &envoy_route_v3.Route_Route{
Route: &envoy_route_v3.RouteAction{
HostRewriteSpecifier: nil,
},
},
},
{
Action: &envoy_route_v3.Route_DirectResponse{},
},
},
},
},
},
expectBool: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
l := awsLambda{}
r, ok, err := l.PatchRoute(tc.conf, tc.route)
require.NoError(t, err)
require.Equal(t, tc.expectRoute, r)
require.Equal(t, tc.expectBool, ok)
})
}
}
func TestPatchFilter(t *testing.T) {
makeAny := func(m proto.Message) *anypb.Any {
v, err := anypb.New(m)
require.NoError(t, err)
return v
}
tests := map[string]struct {
filter *envoy_listener_v3.Filter
expectFilter *envoy_listener_v3.Filter
expectBool bool
expectErr string
}{
"invalid filter name is ignored": {
filter: &envoy_listener_v3.Filter{Name: "something"},
expectFilter: &envoy_listener_v3.Filter{Name: "something"},
expectBool: false,
},
"error getting typed config": {
filter: &envoy_listener_v3.Filter{Name: "envoy.filters.network.http_connection_manager"},
expectFilter: &envoy_listener_v3.Filter{Name: "envoy.filters.network.http_connection_manager"},
expectBool: false,
expectErr: "error getting typed config for http filter",
},
"error getting http connection manager": {
filter: &envoy_listener_v3.Filter{
Name: "envoy.filters.network.http_connection_manager",
ConfigType: &envoy_listener_v3.Filter_TypedConfig{
TypedConfig: &anypb.Any{},
},
},
expectFilter: &envoy_listener_v3.Filter{
Name: "envoy.filters.network.http_connection_manager",
ConfigType: &envoy_listener_v3.Filter_TypedConfig{
TypedConfig: &anypb.Any{},
},
},
expectBool: false,
expectErr: "error unmarshalling filter",
},
"StripAnyHostPort is set": {
filter: &envoy_listener_v3.Filter{
Name: "envoy.filters.network.http_connection_manager",
ConfigType: &envoy_listener_v3.Filter_TypedConfig{
TypedConfig: makeAny(&envoy_http_v3.HttpConnectionManager{}),
},
},
expectFilter: &envoy_listener_v3.Filter{
Name: "envoy.filters.network.http_connection_manager",
ConfigType: &envoy_listener_v3.Filter_TypedConfig{
TypedConfig: makeAny(&envoy_http_v3.HttpConnectionManager{
StripPortMode: &envoy_http_v3.HttpConnectionManager_StripAnyHostPort{
StripAnyHostPort: true,
},
}),
},
},
expectBool: true,
},
"lambda filter injected correctly": {
filter: &envoy_listener_v3.Filter{
Name: "envoy.filters.network.http_connection_manager",
ConfigType: &envoy_listener_v3.Filter_TypedConfig{
TypedConfig: makeAny(&envoy_http_v3.HttpConnectionManager{
HttpFilters: []*envoy_http_v3.HttpFilter{
{Name: "one"},
{Name: "two"},
{Name: "envoy.filters.http.router"},
{Name: "three"},
},
}),
},
},
expectFilter: &envoy_listener_v3.Filter{
Name: "envoy.filters.network.http_connection_manager",
ConfigType: &envoy_listener_v3.Filter_TypedConfig{
TypedConfig: makeAny(&envoy_http_v3.HttpConnectionManager{
StripPortMode: &envoy_http_v3.HttpConnectionManager_StripAnyHostPort{
StripAnyHostPort: true,
},
HttpFilters: []*envoy_http_v3.HttpFilter{
{Name: "one"},
{Name: "two"},
{
Name: "envoy.filters.http.aws_lambda",
ConfigType: &envoy_http_v3.HttpFilter_TypedConfig{TypedConfig: makeAny(
&envoy_lambda_v3.Config{
Arn: "some-arn",
PayloadPassthrough: true,
InvocationMode: envoy_lambda_v3.Config_ASYNCHRONOUS,
},
)},
},
{Name: "envoy.filters.http.router"},
{Name: "three"},
},
}),
},
},
expectBool: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
l := awsLambda{
ARN: "some-arn",
PayloadPassthrough: true,
InvocationMode: "asynchronous",
}
f, ok, err := l.PatchFilter(nil, tc.filter)
require.Equal(t, tc.expectBool, ok)
if tc.expectErr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, tc.expectErr)
}
prototest.AssertDeepEqual(t, tc.expectFilter, f)
})
}
}