//go:build !consulent // +build !consulent package xdscommon import ( "testing" "github.com/stretchr/testify/require" "github.com/hashicorp/consul/agent/proxycfg" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/api" ) func TestGetExtensionConfigurations_TerminatingGateway(t *testing.T) { snap := proxycfg.TestConfigSnapshotTerminatingGatewayWithLambdaServiceAndServiceResolvers(t) webService := api.CompoundServiceName{ Name: "web", Namespace: "default", Partition: "default", } dbService := api.CompoundServiceName{ Name: "db", Namespace: "default", Partition: "default", } cacheService := api.CompoundServiceName{ Name: "cache", Namespace: "default", Partition: "default", } apiService := api.CompoundServiceName{ Name: "api", Namespace: "default", Partition: "default", } expected := map[api.CompoundServiceName][]ExtensionConfiguration{ apiService: {}, cacheService: {}, dbService: {}, webService: { { EnvoyExtension: api.EnvoyExtension{ Name: structs.BuiltinAWSLambdaExtension, Arguments: map[string]interface{}{ "ARN": "lambda-arn", "PayloadPassthrough": true, "Region": "us-east-1", }, }, ServiceName: webService, Upstreams: map[api.CompoundServiceName]UpstreamData{ apiService: { SNI: map[string]struct{}{ "api.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "api", OutgoingProxyKind: "terminating-gateway", }, cacheService: { SNI: map[string]struct{}{ "cache.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "cache", OutgoingProxyKind: "terminating-gateway", }, dbService: { SNI: map[string]struct{}{ "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "db", OutgoingProxyKind: "terminating-gateway", }, webService: { SNI: map[string]struct{}{ "canary1.web.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, "canary2.web.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, "web.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "web", OutgoingProxyKind: "terminating-gateway", }, }, Kind: api.ServiceKindTerminatingGateway, }, }, } require.Equal(t, expected, GetExtensionConfigurations(snap)) } func TestGetExtensionConfigurations_ConnectProxy(t *testing.T) { dbService := api.CompoundServiceName{ Name: "db", Partition: "default", Namespace: "default", } webService := api.CompoundServiceName{ Name: "web", Partition: "", Namespace: "default", } // Setup multiple extensions to ensure all of them are in the ExtensionConfiguration map. envoyExtensions := []structs.EnvoyExtension{ { Name: structs.BuiltinAWSLambdaExtension, Arguments: map[string]interface{}{ "ARN": "lambda-arn", "PayloadPassthrough": true, "Region": "us-east-1", }, }, { Name: "ext2", Arguments: map[string]interface{}{ "arg1": 1, "arg2": "val2", }, }, } serviceDefaults := &structs.ServiceConfigEntry{ Kind: structs.ServiceDefaults, Name: "db", Protocol: "http", EnvoyExtensions: envoyExtensions, } // Setup a snapshot where the db upstream is on a connect proxy. snapConnect := proxycfg.TestConfigSnapshotDiscoveryChain(t, "default", nil, nil, serviceDefaults) // Setup a snapshot where the db upstream is on a terminating gateway. snapTermGw := proxycfg.TestConfigSnapshotDiscoveryChain(t, "register-to-terminating-gateway", nil, nil, serviceDefaults) // Setup a snapshot with the local service web has extensions. snapWebConnect := proxycfg.TestConfigSnapshotDiscoveryChain(t, "default", func(ns *structs.NodeService) { ns.Proxy.EnvoyExtensions = envoyExtensions }, nil) type testCase struct { snapshot *proxycfg.ConfigSnapshot expected map[api.CompoundServiceName][]ExtensionConfiguration } cases := map[string]testCase{ "connect proxy upstream": { snapshot: snapConnect, expected: map[api.CompoundServiceName][]ExtensionConfiguration{ dbService: { { EnvoyExtension: api.EnvoyExtension{ Name: structs.BuiltinAWSLambdaExtension, Arguments: map[string]interface{}{ "ARN": "lambda-arn", "PayloadPassthrough": true, "Region": "us-east-1", }, }, ServiceName: dbService, Upstreams: map[api.CompoundServiceName]UpstreamData{ dbService: { SNI: map[string]struct{}{ "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "db", OutgoingProxyKind: "connect-proxy", }, }, Kind: api.ServiceKindConnectProxy, }, { EnvoyExtension: api.EnvoyExtension{ Name: "ext2", Arguments: map[string]interface{}{ "arg1": 1, "arg2": "val2", }, }, ServiceName: dbService, Upstreams: map[api.CompoundServiceName]UpstreamData{ dbService: { SNI: map[string]struct{}{ "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "db", OutgoingProxyKind: "connect-proxy", }, }, Kind: api.ServiceKindConnectProxy, }, }, webService: {}, }, }, "terminating gateway upstream": { snapshot: snapTermGw, expected: map[api.CompoundServiceName][]ExtensionConfiguration{ dbService: { { EnvoyExtension: api.EnvoyExtension{ Name: structs.BuiltinAWSLambdaExtension, Arguments: map[string]interface{}{ "ARN": "lambda-arn", "PayloadPassthrough": true, "Region": "us-east-1", }, }, ServiceName: dbService, Upstreams: map[api.CompoundServiceName]UpstreamData{ dbService: { SNI: map[string]struct{}{ "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "db", OutgoingProxyKind: "terminating-gateway", }, }, Kind: api.ServiceKindConnectProxy, }, { EnvoyExtension: api.EnvoyExtension{ Name: "ext2", Arguments: map[string]interface{}{ "arg1": 1, "arg2": "val2", }, }, ServiceName: dbService, Upstreams: map[api.CompoundServiceName]UpstreamData{ dbService: { SNI: map[string]struct{}{ "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {}, }, EnvoyID: "db", OutgoingProxyKind: "terminating-gateway", }, }, Kind: api.ServiceKindConnectProxy, }, }, webService: {}, }, }, "local service extensions": { snapshot: snapWebConnect, expected: map[api.CompoundServiceName][]ExtensionConfiguration{ dbService: {}, webService: { { EnvoyExtension: api.EnvoyExtension{ Name: structs.BuiltinAWSLambdaExtension, Arguments: map[string]interface{}{ "ARN": "lambda-arn", "PayloadPassthrough": true, "Region": "us-east-1", }, }, ServiceName: webService, Upstreams: nil, Kind: api.ServiceKindConnectProxy, }, { EnvoyExtension: api.EnvoyExtension{ Name: "ext2", Arguments: map[string]interface{}{ "arg1": 1, "arg2": "val2", }, }, ServiceName: webService, Upstreams: nil, Kind: api.ServiceKindConnectProxy, }, }, }, }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { require.Equal(t, tc.expected, GetExtensionConfigurations(tc.snapshot)) }) } }