2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-03-15 14:07:40 +00:00
|
|
|
//go:build !consulent
|
|
|
|
// +build !consulent
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
package extensionruntime
|
2022-03-15 14:07:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2022-05-05 20:39:39 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2022-03-15 14:07:40 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2023-02-06 17:14:35 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/extensioncommon"
|
2022-03-15 14:07:40 +00:00
|
|
|
)
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func TestGetRuntimeConfigurations_TerminatingGateway(t *testing.T) {
|
2022-04-13 15:45:25 +00:00
|
|
|
snap := proxycfg.TestConfigSnapshotTerminatingGatewayWithLambdaServiceAndServiceResolvers(t)
|
2022-03-15 14:07:40 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
expected := map[api.CompoundServiceName][]extensioncommon.RuntimeConfig{
|
2022-12-21 06:26:20 +00:00
|
|
|
apiService: {},
|
|
|
|
cacheService: {},
|
|
|
|
dbService: {},
|
|
|
|
webService: {
|
|
|
|
{
|
|
|
|
EnvoyExtension: api.EnvoyExtension{
|
2023-01-30 21:35:26 +00:00
|
|
|
Name: api.BuiltinAWSLambdaExtension,
|
2022-12-21 06:26:20 +00:00
|
|
|
Arguments: map[string]interface{}{
|
2023-01-26 23:44:52 +00:00
|
|
|
"ARN": "arn:aws:lambda:us-east-1:111111111111:function:lambda-1234",
|
2022-12-21 06:26:20 +00:00
|
|
|
"PayloadPassthrough": true,
|
|
|
|
},
|
|
|
|
},
|
2023-05-23 11:55:06 +00:00
|
|
|
ServiceName: webService,
|
|
|
|
IsSourcedFromUpstream: true,
|
2023-02-06 17:14:35 +00:00
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
2022-12-21 06:26:20 +00:00
|
|
|
apiService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "api.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
2022-12-21 06:26:20 +00:00
|
|
|
"api.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
|
|
|
},
|
|
|
|
EnvoyID: "api",
|
|
|
|
OutgoingProxyKind: "terminating-gateway",
|
|
|
|
},
|
|
|
|
cacheService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "cache.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
2022-12-21 06:26:20 +00:00
|
|
|
"cache.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
|
|
|
},
|
|
|
|
EnvoyID: "cache",
|
|
|
|
OutgoingProxyKind: "terminating-gateway",
|
|
|
|
},
|
|
|
|
dbService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
2022-12-21 06:26:20 +00:00
|
|
|
"db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
2022-12-19 20:19:37 +00:00
|
|
|
},
|
2022-12-21 06:26:20 +00:00
|
|
|
EnvoyID: "db",
|
|
|
|
OutgoingProxyKind: "terminating-gateway",
|
|
|
|
},
|
|
|
|
webService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "web.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
2022-12-21 06:26:20 +00:00
|
|
|
"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",
|
2022-12-19 20:19:37 +00:00
|
|
|
},
|
2022-03-31 20:24:46 +00:00
|
|
|
},
|
2022-03-15 14:07:40 +00:00
|
|
|
Kind: api.ServiceKindTerminatingGateway,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
require.Equal(t, expected, GetRuntimeConfigurations(snap))
|
2022-03-15 14:07:40 +00:00
|
|
|
}
|
2022-05-05 20:39:39 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func TestGetRuntimeConfigurations_ConnectProxy(t *testing.T) {
|
2022-05-05 20:39:39 +00:00
|
|
|
dbService := api.CompoundServiceName{
|
|
|
|
Name: "db",
|
|
|
|
Partition: "default",
|
|
|
|
Namespace: "default",
|
|
|
|
}
|
2022-12-22 18:03:33 +00:00
|
|
|
webService := api.CompoundServiceName{
|
|
|
|
Name: "web",
|
|
|
|
Partition: "",
|
|
|
|
Namespace: "default",
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:55:06 +00:00
|
|
|
// Setup multiple extensions to ensure only the expected one (AWS) is in the ExtensionConfiguration map
|
|
|
|
// sourced from upstreams, and all local extensions are included.
|
2022-12-19 20:19:37 +00:00
|
|
|
envoyExtensions := []structs.EnvoyExtension{
|
|
|
|
{
|
2023-01-30 21:35:26 +00:00
|
|
|
Name: api.BuiltinAWSLambdaExtension,
|
2022-12-19 20:19:37 +00:00
|
|
|
Arguments: map[string]interface{}{
|
2023-01-26 23:44:52 +00:00
|
|
|
"ARN": "arn:aws:lambda:us-east-1:111111111111:function:lambda-1234",
|
2022-12-19 20:19:37 +00:00
|
|
|
"PayloadPassthrough": true,
|
|
|
|
},
|
|
|
|
},
|
2022-12-22 18:03:33 +00:00
|
|
|
{
|
|
|
|
Name: "ext2",
|
|
|
|
Arguments: map[string]interface{}{
|
|
|
|
"arg1": 1,
|
|
|
|
"arg2": "val2",
|
|
|
|
},
|
|
|
|
},
|
2022-05-05 20:39:39 +00:00
|
|
|
}
|
2022-12-19 20:19:37 +00:00
|
|
|
|
2022-05-05 20:39:39 +00:00
|
|
|
serviceDefaults := &structs.ServiceConfigEntry{
|
2022-12-19 20:19:37 +00:00
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "db",
|
|
|
|
Protocol: "http",
|
|
|
|
EnvoyExtensions: envoyExtensions,
|
2022-05-05 20:39:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:53:42 +00:00
|
|
|
serviceDefaultsV2 := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "db-v2",
|
|
|
|
Protocol: "http",
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceSplitter := &structs.ServiceSplitterConfigEntry{
|
|
|
|
Kind: structs.ServiceSplitter,
|
|
|
|
Name: "db",
|
|
|
|
Splits: []structs.ServiceSplit{
|
|
|
|
{Weight: 50},
|
|
|
|
{Weight: 50, Service: "db-v2"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-22 18:03:33 +00:00
|
|
|
// Setup a snapshot where the db upstream is on a connect proxy.
|
2023-05-30 18:53:42 +00:00
|
|
|
snapConnect := proxycfg.TestConfigSnapshotDiscoveryChain(t, "default", false, nil, nil, serviceDefaults, serviceDefaultsV2, serviceSplitter)
|
2022-12-22 18:03:33 +00:00
|
|
|
// Setup a snapshot where the db upstream is on a terminating gateway.
|
2023-05-30 18:53:42 +00:00
|
|
|
snapTermGw := proxycfg.TestConfigSnapshotDiscoveryChain(t, "register-to-terminating-gateway", false, nil, nil, serviceDefaults, serviceDefaultsV2, serviceSplitter)
|
2022-12-22 18:03:33 +00:00
|
|
|
// Setup a snapshot with the local service web has extensions.
|
2023-03-22 18:56:18 +00:00
|
|
|
snapWebConnect := proxycfg.TestConfigSnapshotDiscoveryChain(t, "default", false, func(ns *structs.NodeService) {
|
2022-12-22 18:03:33 +00:00
|
|
|
ns.Proxy.EnvoyExtensions = envoyExtensions
|
|
|
|
}, nil)
|
2022-12-21 06:26:20 +00:00
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
snapshot *proxycfg.ConfigSnapshot
|
2023-01-30 21:35:26 +00:00
|
|
|
expected map[api.CompoundServiceName][]extensioncommon.RuntimeConfig
|
2022-12-21 06:26:20 +00:00
|
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"connect proxy upstream": {
|
|
|
|
snapshot: snapConnect,
|
2023-01-30 21:35:26 +00:00
|
|
|
expected: map[api.CompoundServiceName][]extensioncommon.RuntimeConfig{
|
2022-12-21 06:26:20 +00:00
|
|
|
dbService: {
|
|
|
|
{
|
|
|
|
EnvoyExtension: api.EnvoyExtension{
|
2023-01-30 21:35:26 +00:00
|
|
|
Name: api.BuiltinAWSLambdaExtension,
|
2022-12-21 06:26:20 +00:00
|
|
|
Arguments: map[string]interface{}{
|
2023-01-26 23:44:52 +00:00
|
|
|
"ARN": "arn:aws:lambda:us-east-1:111111111111:function:lambda-1234",
|
2022-12-21 06:26:20 +00:00
|
|
|
"PayloadPassthrough": true,
|
|
|
|
},
|
|
|
|
},
|
2023-05-23 11:55:06 +00:00
|
|
|
ServiceName: dbService,
|
|
|
|
IsSourcedFromUpstream: true,
|
2023-02-06 17:14:35 +00:00
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
2022-12-22 18:03:33 +00:00
|
|
|
dbService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
|
|
|
"db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
|
|
|
"db-v2.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
2022-12-22 18:03:33 +00:00
|
|
|
},
|
|
|
|
EnvoyID: "db",
|
|
|
|
OutgoingProxyKind: "connect-proxy",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Kind: api.ServiceKindConnectProxy,
|
|
|
|
},
|
2022-12-21 06:26:20 +00:00
|
|
|
},
|
2022-12-22 18:03:33 +00:00
|
|
|
webService: {},
|
2022-05-05 20:39:39 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-21 06:26:20 +00:00
|
|
|
"terminating gateway upstream": {
|
|
|
|
snapshot: snapTermGw,
|
2023-01-30 21:35:26 +00:00
|
|
|
expected: map[api.CompoundServiceName][]extensioncommon.RuntimeConfig{
|
2022-12-21 06:26:20 +00:00
|
|
|
dbService: {
|
|
|
|
{
|
|
|
|
EnvoyExtension: api.EnvoyExtension{
|
2023-01-30 21:35:26 +00:00
|
|
|
Name: api.BuiltinAWSLambdaExtension,
|
2022-12-21 06:26:20 +00:00
|
|
|
Arguments: map[string]interface{}{
|
2023-01-26 23:44:52 +00:00
|
|
|
"ARN": "arn:aws:lambda:us-east-1:111111111111:function:lambda-1234",
|
2022-12-21 06:26:20 +00:00
|
|
|
"PayloadPassthrough": true,
|
|
|
|
},
|
|
|
|
},
|
2023-05-23 11:55:06 +00:00
|
|
|
ServiceName: dbService,
|
|
|
|
IsSourcedFromUpstream: true,
|
2023-02-06 17:14:35 +00:00
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
2022-12-22 18:03:33 +00:00
|
|
|
dbService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
|
|
|
"db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
|
|
|
"db-v2.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
2022-12-22 18:03:33 +00:00
|
|
|
},
|
|
|
|
EnvoyID: "db",
|
|
|
|
OutgoingProxyKind: "terminating-gateway",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Kind: api.ServiceKindConnectProxy,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
webService: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"local service extensions": {
|
|
|
|
snapshot: snapWebConnect,
|
2023-01-30 21:35:26 +00:00
|
|
|
expected: map[api.CompoundServiceName][]extensioncommon.RuntimeConfig{
|
2022-12-22 18:03:33 +00:00
|
|
|
dbService: {},
|
|
|
|
webService: {
|
|
|
|
{
|
|
|
|
EnvoyExtension: api.EnvoyExtension{
|
2023-01-30 21:35:26 +00:00
|
|
|
Name: api.BuiltinAWSLambdaExtension,
|
2022-12-22 18:03:33 +00:00
|
|
|
Arguments: map[string]interface{}{
|
2023-01-26 23:44:52 +00:00
|
|
|
"ARN": "arn:aws:lambda:us-east-1:111111111111:function:lambda-1234",
|
2022-12-22 18:03:33 +00:00
|
|
|
"PayloadPassthrough": true,
|
|
|
|
},
|
|
|
|
},
|
2023-05-23 11:55:06 +00:00
|
|
|
ServiceName: webService,
|
|
|
|
Kind: api.ServiceKindConnectProxy,
|
|
|
|
IsSourcedFromUpstream: false,
|
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
2023-04-06 21:12:07 +00:00
|
|
|
dbService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
2023-04-06 21:12:07 +00:00
|
|
|
"db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
|
|
|
},
|
|
|
|
EnvoyID: "db",
|
|
|
|
OutgoingProxyKind: "connect-proxy",
|
|
|
|
},
|
|
|
|
},
|
2022-12-22 18:03:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
EnvoyExtension: api.EnvoyExtension{
|
|
|
|
Name: "ext2",
|
|
|
|
Arguments: map[string]interface{}{
|
|
|
|
"arg1": 1,
|
|
|
|
"arg2": "val2",
|
|
|
|
},
|
|
|
|
},
|
2023-05-23 11:55:06 +00:00
|
|
|
ServiceName: webService,
|
|
|
|
Kind: api.ServiceKindConnectProxy,
|
|
|
|
IsSourcedFromUpstream: false,
|
|
|
|
Upstreams: map[api.CompoundServiceName]*extensioncommon.UpstreamData{
|
2023-04-06 21:12:07 +00:00
|
|
|
dbService: {
|
2023-05-30 18:53:42 +00:00
|
|
|
PrimarySNI: "db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul",
|
|
|
|
SNIs: map[string]struct{}{
|
2023-04-06 21:12:07 +00:00
|
|
|
"db.default.dc1.internal.11111111-2222-3333-4444-555555555555.consul": {},
|
|
|
|
},
|
|
|
|
EnvoyID: "db",
|
|
|
|
OutgoingProxyKind: "connect-proxy",
|
|
|
|
},
|
|
|
|
},
|
2022-12-22 18:03:33 +00:00
|
|
|
},
|
2022-12-21 06:26:20 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-05 20:39:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-21 06:26:20 +00:00
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-01-30 21:35:26 +00:00
|
|
|
require.Equal(t, tc.expected, GetRuntimeConfigurations(tc.snapshot))
|
2022-12-21 06:26:20 +00:00
|
|
|
})
|
|
|
|
}
|
2022-05-05 20:39:39 +00:00
|
|
|
}
|