2019-07-12 19:16:21 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
2020-07-09 22:04:51 +00:00
|
|
|
"path/filepath"
|
2019-07-12 19:16:21 +00:00
|
|
|
"sort"
|
|
|
|
"testing"
|
2020-04-16 23:24:11 +00:00
|
|
|
"time"
|
2019-07-12 19:16:21 +00:00
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
envoy_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2020-09-02 21:13:50 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2020-12-23 17:50:28 +00:00
|
|
|
testinf "github.com/mitchellh/go-testing-interface"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-04-16 23:24:11 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/discoverychain"
|
2019-07-12 19:16:21 +00:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2020-04-16 23:24:11 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-07-31 20:52:49 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/proxysupport"
|
2021-04-29 20:22:03 +00:00
|
|
|
"github.com/hashicorp/consul/lib/stringslice"
|
2020-09-03 22:19:58 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-07-12 19:16:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRoutesFromSnapshot(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
create func(t testinf.T) *proxycfg.ConfigSnapshot
|
|
|
|
// Setup is called before the test starts. It is passed the snapshot from
|
|
|
|
// create func and is allowed to modify it in any way to setup the
|
|
|
|
// test input.
|
|
|
|
setup func(snap *proxycfg.ConfigSnapshot)
|
|
|
|
overrideGoldenName string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "defaults-no-chain",
|
|
|
|
create: proxycfg.TestConfigSnapshot,
|
|
|
|
setup: nil, // Default snapshot
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "connect-proxy-with-chain",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChain,
|
|
|
|
setup: nil,
|
|
|
|
},
|
2019-08-19 17:19:44 +00:00
|
|
|
{
|
|
|
|
name: "connect-proxy-with-chain-external-sni",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChainExternalSNI,
|
|
|
|
setup: nil,
|
|
|
|
},
|
2019-08-02 03:03:34 +00:00
|
|
|
{
|
|
|
|
name: "connect-proxy-with-chain-and-overrides",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChainWithOverrides,
|
|
|
|
setup: nil,
|
|
|
|
},
|
2019-07-12 19:16:21 +00:00
|
|
|
{
|
|
|
|
name: "splitter-with-resolver-redirect",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChain_SplitterWithResolverRedirectMultiDC,
|
|
|
|
setup: nil,
|
|
|
|
},
|
|
|
|
{
|
2020-04-14 21:30:00 +00:00
|
|
|
name: "connect-proxy-with-chain-and-splitter",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChainWithSplitter,
|
|
|
|
setup: nil,
|
2019-07-12 19:16:21 +00:00
|
|
|
},
|
2019-07-17 19:07:08 +00:00
|
|
|
{
|
2020-04-14 21:30:00 +00:00
|
|
|
name: "connect-proxy-with-grpc-router",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChainWithGRPCRouter,
|
|
|
|
setup: nil,
|
2019-07-17 19:07:08 +00:00
|
|
|
},
|
2019-07-12 19:16:21 +00:00
|
|
|
{
|
2020-04-14 21:30:00 +00:00
|
|
|
name: "connect-proxy-with-chain-and-router",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChainWithRouter,
|
|
|
|
setup: nil,
|
2019-07-12 19:16:21 +00:00
|
|
|
},
|
2020-08-28 20:27:40 +00:00
|
|
|
{
|
|
|
|
name: "connect-proxy-lb-in-resolver",
|
|
|
|
create: proxycfg.TestConfigSnapshotDiscoveryChainWithLB,
|
|
|
|
setup: nil,
|
|
|
|
},
|
2019-07-12 19:16:21 +00:00
|
|
|
// TODO(rb): test match stanza skipped for grpc
|
2020-04-14 21:30:00 +00:00
|
|
|
// Start ingress gateway test cases
|
|
|
|
{
|
|
|
|
name: "ingress-defaults-no-chain",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressGateway,
|
|
|
|
setup: nil, // Default snapshot
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-chain",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngress,
|
|
|
|
setup: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-chain-external-sni",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressExternalSNI,
|
|
|
|
setup: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-chain-and-overrides",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithOverrides,
|
|
|
|
setup: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-splitter-with-resolver-redirect",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngress_SplitterWithResolverRedirectMultiDC,
|
|
|
|
setup: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-chain-and-splitter",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithSplitter,
|
|
|
|
setup: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-grpc-router",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithGRPCRouter,
|
|
|
|
setup: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-chain-and-router",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithRouter,
|
|
|
|
setup: nil,
|
|
|
|
},
|
2020-08-28 20:27:40 +00:00
|
|
|
{
|
|
|
|
name: "ingress-lb-in-resolver",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithLB,
|
|
|
|
setup: nil,
|
|
|
|
},
|
2020-04-16 23:24:11 +00:00
|
|
|
{
|
|
|
|
name: "ingress-http-multiple-services",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngress_HTTPMultipleServices,
|
|
|
|
setup: func(snap *proxycfg.ConfigSnapshot) {
|
|
|
|
snap.IngressGateway.Upstreams = map[proxycfg.IngressListenerKey]structs.Upstreams{
|
2020-06-16 17:19:31 +00:00
|
|
|
{Protocol: "http", Port: 8080}: {
|
2020-04-16 23:24:11 +00:00
|
|
|
{
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 8080,
|
2020-07-07 15:43:04 +00:00
|
|
|
IngressHosts: []string{
|
|
|
|
"test1.example.com",
|
|
|
|
"test2.example.com",
|
|
|
|
"test2.example.com:8080",
|
|
|
|
},
|
2020-04-16 23:24:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationName: "bar",
|
|
|
|
LocalBindPort: 8080,
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{Protocol: "http", Port: 443}: {
|
2020-04-16 23:24:11 +00:00
|
|
|
{
|
|
|
|
DestinationName: "baz",
|
|
|
|
LocalBindPort: 443,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationName: "qux",
|
|
|
|
LocalBindPort: 443,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-08-24 14:25:22 +00:00
|
|
|
snap.IngressGateway.Listeners = map[proxycfg.IngressListenerKey]structs.IngressListener{
|
|
|
|
{Protocol: "http", Port: 8080}: {
|
|
|
|
Port: 8080,
|
|
|
|
Services: []structs.IngressService{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{Protocol: "http", Port: 443}: {
|
|
|
|
Port: 443,
|
|
|
|
Services: []structs.IngressService{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "qux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-04-16 23:24:11 +00:00
|
|
|
|
|
|
|
// We do not add baz/qux here so that we test the chain.IsDefault() case
|
|
|
|
entries := []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "foo",
|
|
|
|
ConnectTimeout: 22 * time.Second,
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "bar",
|
|
|
|
ConnectTimeout: 22 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
2021-09-07 20:29:32 +00:00
|
|
|
fooChain := discoverychain.TestCompileConfigEntries(t, "foo", "default", "default", "dc1", connect.TestClusterID+".consul", "dc1", nil, entries...)
|
|
|
|
barChain := discoverychain.TestCompileConfigEntries(t, "bar", "default", "default", "dc1", connect.TestClusterID+".consul", "dc1", nil, entries...)
|
|
|
|
bazChain := discoverychain.TestCompileConfigEntries(t, "baz", "default", "default", "dc1", connect.TestClusterID+".consul", "dc1", nil, entries...)
|
|
|
|
quxChain := discoverychain.TestCompileConfigEntries(t, "qux", "default", "default", "dc1", connect.TestClusterID+".consul", "dc1", nil, entries...)
|
2020-04-16 23:24:11 +00:00
|
|
|
|
|
|
|
snap.IngressGateway.DiscoveryChain = map[string]*structs.CompiledDiscoveryChain{
|
|
|
|
"foo": fooChain,
|
|
|
|
"bar": barChain,
|
|
|
|
"baz": bazChain,
|
|
|
|
"qux": quxChain,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2021-07-13 12:53:59 +00:00
|
|
|
{
|
|
|
|
name: "ingress-with-chain-and-router-header-manip",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithRouter,
|
|
|
|
setup: func(snap *proxycfg.ConfigSnapshot) {
|
|
|
|
k := proxycfg.IngressListenerKey{Port: 9191, Protocol: "http"}
|
|
|
|
l := snap.IngressGateway.Listeners[k]
|
|
|
|
l.Services[0].RequestHeaders = &structs.HTTPHeaderModifiers{
|
|
|
|
Add: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
Set: map[string]string{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
Remove: []string{"qux"},
|
|
|
|
}
|
|
|
|
l.Services[0].ResponseHeaders = &structs.HTTPHeaderModifiers{
|
|
|
|
Add: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
Set: map[string]string{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
Remove: []string{"qux"},
|
|
|
|
}
|
|
|
|
snap.IngressGateway.Listeners[k] = l
|
|
|
|
},
|
|
|
|
},
|
2021-08-24 14:25:22 +00:00
|
|
|
{
|
|
|
|
name: "ingress-with-sds-listener-level",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithRouter,
|
|
|
|
setup: setupIngressWithTwoHTTPServices(t, ingressSDSOpts{
|
|
|
|
// Listener-level SDS means all services share the default route.
|
|
|
|
listenerSDS: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-sds-listener-level-wildcard",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithRouter,
|
|
|
|
setup: setupIngressWithTwoHTTPServices(t, ingressSDSOpts{
|
|
|
|
// Listener-level SDS means all services share the default route.
|
|
|
|
listenerSDS: true,
|
|
|
|
wildcard: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-sds-service-level",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithRouter,
|
|
|
|
setup: setupIngressWithTwoHTTPServices(t, ingressSDSOpts{
|
|
|
|
listenerSDS: false,
|
|
|
|
// Services should get separate routes and no default since they all
|
|
|
|
// have custom certs.
|
|
|
|
webSDS: true,
|
|
|
|
fooSDS: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ingress-with-sds-service-level-mixed-tls",
|
|
|
|
create: proxycfg.TestConfigSnapshotIngressWithRouter,
|
|
|
|
setup: setupIngressWithTwoHTTPServices(t, ingressSDSOpts{
|
|
|
|
listenerSDS: false,
|
|
|
|
// Web needs a separate route as it has custom filter chain but foo
|
|
|
|
// should use default route for listener.
|
|
|
|
webSDS: true,
|
|
|
|
fooSDS: false,
|
|
|
|
}),
|
|
|
|
},
|
2020-08-28 20:27:40 +00:00
|
|
|
{
|
|
|
|
name: "terminating-gateway-lb-config",
|
|
|
|
create: proxycfg.TestConfigSnapshotTerminatingGateway,
|
|
|
|
setup: func(snap *proxycfg.ConfigSnapshot) {
|
|
|
|
snap.TerminatingGateway.ServiceResolvers = map[structs.ServiceName]*structs.ServiceResolverConfigEntry{
|
|
|
|
structs.NewServiceName("web", nil): {
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
DefaultSubset: "v2",
|
|
|
|
Subsets: map[string]structs.ServiceResolverSubset{
|
|
|
|
"v1": {
|
|
|
|
Filter: "Service.Meta.Version == 1",
|
|
|
|
},
|
|
|
|
"v2": {
|
|
|
|
Filter: "Service.Meta.Version == 2",
|
|
|
|
OnlyPassing: true,
|
|
|
|
},
|
|
|
|
},
|
2020-09-02 15:10:50 +00:00
|
|
|
LoadBalancer: &structs.LoadBalancer{
|
2020-09-11 15:21:43 +00:00
|
|
|
Policy: "ring_hash",
|
|
|
|
RingHashConfig: &structs.RingHashConfig{
|
|
|
|
MinimumRingSize: 20,
|
|
|
|
MaximumRingSize: 50,
|
|
|
|
},
|
|
|
|
HashPolicies: []structs.HashPolicy{
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyCookie,
|
|
|
|
FieldValue: "chocolate-chip",
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyHeader,
|
|
|
|
FieldValue: "x-user-id",
|
2020-08-28 20:27:40 +00:00
|
|
|
},
|
2020-09-11 15:21:43 +00:00
|
|
|
{
|
|
|
|
SourceIP: true,
|
|
|
|
Terminal: true,
|
2020-08-28 20:27:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-09-03 14:57:48 +00:00
|
|
|
snap.TerminatingGateway.ServiceConfigs[structs.NewServiceName("web", nil)] = &structs.ServiceConfigResponse{
|
|
|
|
ProxyConfig: map[string]interface{}{"protocol": "http"},
|
|
|
|
}
|
2020-08-28 20:27:40 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-12 19:16:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 20:04:31 +00:00
|
|
|
latestEnvoyVersion := proxysupport.EnvoyVersions[0]
|
2021-04-29 20:22:03 +00:00
|
|
|
latestEnvoyVersion_v2 := proxysupport.EnvoyVersionsV2[0]
|
2020-07-31 20:52:49 +00:00
|
|
|
for _, envoyVersion := range proxysupport.EnvoyVersions {
|
|
|
|
sf, err := determineSupportedProxyFeaturesFromString(envoyVersion)
|
|
|
|
require.NoError(t, err)
|
2020-07-09 22:04:51 +00:00
|
|
|
t.Run("envoy-"+envoyVersion, func(t *testing.T) {
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
// Sanity check default with no overrides first
|
|
|
|
snap := tt.create(t)
|
2019-07-12 19:16:21 +00:00
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
// We need to replace the TLS certs with deterministic ones to make golden
|
|
|
|
// files workable. Note we don't update these otherwise they'd change
|
|
|
|
// golden files for every test case and so not be any use!
|
|
|
|
setupTLSRootsAndLeaf(t, snap)
|
2019-07-12 19:16:21 +00:00
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
if tt.setup != nil {
|
|
|
|
tt.setup(snap)
|
|
|
|
}
|
2019-07-12 19:16:21 +00:00
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
g := newResourceGenerator(testutil.Logger(t), nil, nil, false)
|
|
|
|
g.ProxyFeatures = sf
|
|
|
|
|
|
|
|
routes, err := g.routesFromSnapshot(snap)
|
2021-02-26 22:23:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
sort.Slice(routes, func(i, j int) bool {
|
2021-02-26 22:23:15 +00:00
|
|
|
return routes[i].(*envoy_route_v3.RouteConfiguration).Name < routes[j].(*envoy_route_v3.RouteConfiguration).Name
|
2020-07-09 22:04:51 +00:00
|
|
|
})
|
|
|
|
r, err := createResponse(RouteType, "00000001", "00000001", routes)
|
2021-02-26 22:23:15 +00:00
|
|
|
require.NoError(t, err)
|
2019-07-12 19:16:21 +00:00
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
t.Run("current", func(t *testing.T) {
|
|
|
|
gotJSON := protoToJSON(t, r)
|
2019-07-12 19:16:21 +00:00
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
gName := tt.name
|
|
|
|
if tt.overrideGoldenName != "" {
|
|
|
|
gName = tt.overrideGoldenName
|
|
|
|
}
|
|
|
|
|
|
|
|
require.JSONEq(t, goldenEnvoy(t, filepath.Join("routes", gName), envoyVersion, latestEnvoyVersion, gotJSON), gotJSON)
|
|
|
|
})
|
2019-07-12 19:16:21 +00:00
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
t.Run("v2-compat", func(t *testing.T) {
|
2021-04-29 20:22:03 +00:00
|
|
|
if !stringslice.Contains(proxysupport.EnvoyVersionsV2, envoyVersion) {
|
|
|
|
t.Skip()
|
|
|
|
}
|
2021-02-26 22:23:15 +00:00
|
|
|
respV2, err := convertDiscoveryResponseToV2(r)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gotJSON := protoToJSON(t, respV2)
|
|
|
|
|
|
|
|
gName := tt.name
|
|
|
|
if tt.overrideGoldenName != "" {
|
|
|
|
gName = tt.overrideGoldenName
|
|
|
|
}
|
|
|
|
|
|
|
|
gName += ".v2compat"
|
|
|
|
|
2021-08-25 12:40:47 +00:00
|
|
|
// It's easy to miss a new type that encodes a version from just
|
|
|
|
// looking at the golden files so lets make it an error here. If
|
|
|
|
// there are ever false positives we can maybe include an allow list
|
|
|
|
// here as it seems safer to assume something was missed than to
|
|
|
|
// assume we'll notice the golden file being wrong. Note the first
|
|
|
|
// one matches both resourceApiVersion and transportApiVersion. I
|
|
|
|
// left it as a suffix in case there are other field names that
|
|
|
|
// follow that convention now or in the future.
|
|
|
|
require.NotContains(t, gotJSON, `ApiVersion": "V3"`)
|
|
|
|
require.NotContains(t, gotJSON, `type.googleapis.com/envoy.api.v3`)
|
|
|
|
|
2021-04-29 20:22:03 +00:00
|
|
|
require.JSONEq(t, goldenEnvoy(t, filepath.Join("routes", gName), envoyVersion, latestEnvoyVersion_v2, gotJSON), gotJSON)
|
2021-02-26 22:23:15 +00:00
|
|
|
})
|
2020-07-09 22:04:51 +00:00
|
|
|
})
|
|
|
|
}
|
2019-07-12 19:16:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 21:13:50 +00:00
|
|
|
|
|
|
|
func TestEnvoyLBConfig_InjectToRouteAction(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
2020-09-11 15:21:43 +00:00
|
|
|
lb *structs.LoadBalancer
|
2021-02-26 22:23:15 +00:00
|
|
|
expected *envoy_route_v3.RouteAction
|
2020-09-02 21:13:50 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: "",
|
|
|
|
},
|
|
|
|
// we only modify route actions for hash-based LB policies
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "least request",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyLeastRequest,
|
|
|
|
LeastRequestConfig: &structs.LeastRequestConfig{
|
|
|
|
ChoiceCount: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// we only modify route actions for hash-based LB policies
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "headers",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: "ring_hash",
|
|
|
|
RingHashConfig: &structs.RingHashConfig{
|
|
|
|
MinimumRingSize: 3,
|
|
|
|
MaximumRingSize: 7,
|
|
|
|
},
|
|
|
|
HashPolicies: []structs.HashPolicy{
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyHeader,
|
|
|
|
FieldValue: "x-route-key",
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{
|
|
|
|
HashPolicy: []*envoy_route_v3.RouteAction_HashPolicy{
|
2020-09-02 21:13:50 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Header_{
|
|
|
|
Header: &envoy_route_v3.RouteAction_HashPolicy_Header{
|
2020-09-02 21:13:50 +00:00
|
|
|
HeaderName: "x-route-key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cookies",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyMaglev,
|
|
|
|
HashPolicies: []structs.HashPolicy{
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyCookie,
|
|
|
|
FieldValue: "red-velvet",
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyCookie,
|
|
|
|
FieldValue: "oatmeal",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{
|
|
|
|
HashPolicy: []*envoy_route_v3.RouteAction_HashPolicy{
|
2020-09-02 21:13:50 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Cookie_{
|
|
|
|
Cookie: &envoy_route_v3.RouteAction_HashPolicy_Cookie{
|
2020-09-02 21:13:50 +00:00
|
|
|
Name: "red-velvet",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Cookie_{
|
|
|
|
Cookie: &envoy_route_v3.RouteAction_HashPolicy_Cookie{
|
2020-09-02 21:13:50 +00:00
|
|
|
Name: "oatmeal",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-09-12 00:34:03 +00:00
|
|
|
{
|
|
|
|
name: "non-zero session ttl gets zeroed out",
|
|
|
|
lb: &structs.LoadBalancer{
|
|
|
|
Policy: structs.LBPolicyMaglev,
|
|
|
|
HashPolicies: []structs.HashPolicy{
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyCookie,
|
|
|
|
FieldValue: "oatmeal",
|
|
|
|
CookieConfig: &structs.CookieConfig{
|
|
|
|
TTL: 10 * time.Second,
|
|
|
|
Session: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{
|
|
|
|
HashPolicy: []*envoy_route_v3.RouteAction_HashPolicy{
|
2020-09-12 00:34:03 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Cookie_{
|
|
|
|
Cookie: &envoy_route_v3.RouteAction_HashPolicy_Cookie{
|
2020-09-12 00:34:03 +00:00
|
|
|
Name: "oatmeal",
|
|
|
|
Ttl: ptypes.DurationProto(0 * time.Second),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zero value ttl omitted if not session cookie",
|
|
|
|
lb: &structs.LoadBalancer{
|
|
|
|
Policy: structs.LBPolicyMaglev,
|
|
|
|
HashPolicies: []structs.HashPolicy{
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyCookie,
|
|
|
|
FieldValue: "oatmeal",
|
|
|
|
CookieConfig: &structs.CookieConfig{
|
|
|
|
Path: "/oven",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{
|
|
|
|
HashPolicy: []*envoy_route_v3.RouteAction_HashPolicy{
|
2020-09-12 00:34:03 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Cookie_{
|
|
|
|
Cookie: &envoy_route_v3.RouteAction_HashPolicy_Cookie{
|
2020-09-12 00:34:03 +00:00
|
|
|
Name: "oatmeal",
|
|
|
|
Path: "/oven",
|
|
|
|
Ttl: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-09-02 21:13:50 +00:00
|
|
|
{
|
|
|
|
name: "source addr",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyMaglev,
|
|
|
|
HashPolicies: []structs.HashPolicy{
|
|
|
|
{
|
|
|
|
SourceIP: true,
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{
|
|
|
|
HashPolicy: []*envoy_route_v3.RouteAction_HashPolicy{
|
2020-09-02 21:13:50 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_ConnectionProperties_{
|
|
|
|
ConnectionProperties: &envoy_route_v3.RouteAction_HashPolicy_ConnectionProperties{
|
2020-09-02 21:13:50 +00:00
|
|
|
SourceIp: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "kitchen sink",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyMaglev,
|
|
|
|
HashPolicies: []structs.HashPolicy{
|
|
|
|
{
|
|
|
|
SourceIP: true,
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: structs.HashPolicyCookie,
|
|
|
|
FieldValue: "oatmeal",
|
|
|
|
CookieConfig: &structs.CookieConfig{
|
|
|
|
TTL: 10 * time.Second,
|
|
|
|
Path: "/oven",
|
|
|
|
},
|
|
|
|
},
|
2020-09-12 00:34:03 +00:00
|
|
|
{
|
|
|
|
Field: structs.HashPolicyCookie,
|
|
|
|
FieldValue: "chocolate-chip",
|
|
|
|
CookieConfig: &structs.CookieConfig{
|
|
|
|
Session: true,
|
|
|
|
Path: "/oven",
|
|
|
|
},
|
|
|
|
},
|
2020-09-02 21:13:50 +00:00
|
|
|
{
|
|
|
|
Field: structs.HashPolicyHeader,
|
|
|
|
FieldValue: "special-header",
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_route_v3.RouteAction{
|
|
|
|
HashPolicy: []*envoy_route_v3.RouteAction_HashPolicy{
|
2020-09-02 21:13:50 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_ConnectionProperties_{
|
|
|
|
ConnectionProperties: &envoy_route_v3.RouteAction_HashPolicy_ConnectionProperties{
|
2020-09-02 21:13:50 +00:00
|
|
|
SourceIp: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Cookie_{
|
|
|
|
Cookie: &envoy_route_v3.RouteAction_HashPolicy_Cookie{
|
2020-09-02 21:13:50 +00:00
|
|
|
Name: "oatmeal",
|
|
|
|
Ttl: ptypes.DurationProto(10 * time.Second),
|
|
|
|
Path: "/oven",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-09-12 00:34:03 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Cookie_{
|
|
|
|
Cookie: &envoy_route_v3.RouteAction_HashPolicy_Cookie{
|
2020-09-12 00:34:03 +00:00
|
|
|
Name: "chocolate-chip",
|
|
|
|
Ttl: ptypes.DurationProto(0 * time.Second),
|
|
|
|
Path: "/oven",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-09-02 21:13:50 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
PolicySpecifier: &envoy_route_v3.RouteAction_HashPolicy_Header_{
|
|
|
|
Header: &envoy_route_v3.RouteAction_HashPolicy_Header{
|
2020-09-02 21:13:50 +00:00
|
|
|
HeaderName: "special-header",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Terminal: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-02-26 22:23:15 +00:00
|
|
|
var ra envoy_route_v3.RouteAction
|
2020-09-02 21:13:50 +00:00
|
|
|
err := injectLBToRouteAction(tc.lb, &ra)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-22 21:00:15 +00:00
|
|
|
require.Equal(t, tc.expected, &ra)
|
2020-09-02 21:13:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 14:25:22 +00:00
|
|
|
|
|
|
|
type ingressSDSOpts struct {
|
|
|
|
listenerSDS, webSDS, fooSDS, wildcard bool
|
2021-08-25 12:40:47 +00:00
|
|
|
entMetas map[string]*structs.EnterpriseMeta
|
2021-08-24 14:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setupIngressWithTwoHTTPServices can be used with
|
|
|
|
// proxycfg.TestConfigSnapshotIngressWithRouter to generate a setup func for an
|
|
|
|
// ingress listener with multiple HTTP services and varying SDS configurations
|
|
|
|
// since those affect how we generate routes.
|
|
|
|
func setupIngressWithTwoHTTPServices(t *testing.T, o ingressSDSOpts) func(snap *proxycfg.ConfigSnapshot) {
|
|
|
|
return func(snap *proxycfg.ConfigSnapshot) {
|
|
|
|
|
|
|
|
snap.IngressGateway.TLSConfig.SDS = nil
|
|
|
|
|
2021-08-25 12:40:47 +00:00
|
|
|
webUpstream := structs.Upstream{
|
|
|
|
DestinationName: "web",
|
|
|
|
// We use empty not default here because of the way upstream identifiers
|
|
|
|
// vary between OSS and Enterprise currently causing test conflicts. In
|
|
|
|
// real life `proxycfg` always sets ingress upstream namespaces to
|
|
|
|
// `NamespaceOrDefault` which shouldn't matter because we should be
|
|
|
|
// consistent within a single binary it's just inconvenient if OSS and
|
|
|
|
// enterprise tests generate different output.
|
|
|
|
DestinationNamespace: o.entMetas["web"].NamespaceOrEmpty(),
|
|
|
|
DestinationPartition: o.entMetas["web"].PartitionOrEmpty(),
|
|
|
|
LocalBindPort: 9191,
|
|
|
|
IngressHosts: []string{
|
|
|
|
"www.example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fooUpstream := structs.Upstream{
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationNamespace: o.entMetas["foo"].NamespaceOrEmpty(),
|
|
|
|
DestinationPartition: o.entMetas["foo"].PartitionOrEmpty(),
|
|
|
|
LocalBindPort: 9191,
|
|
|
|
IngressHosts: []string{
|
|
|
|
"foo.example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:25:22 +00:00
|
|
|
// Setup additional HTTP service on same listener with default router
|
|
|
|
snap.IngressGateway.Upstreams = map[proxycfg.IngressListenerKey]structs.Upstreams{
|
2021-08-25 12:40:47 +00:00
|
|
|
{Protocol: "http", Port: 9191}: {webUpstream, fooUpstream},
|
2021-08-24 14:25:22 +00:00
|
|
|
}
|
|
|
|
il := structs.IngressListener{
|
|
|
|
Port: 9191,
|
|
|
|
Services: []structs.IngressService{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Hosts: []string{"www.example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Hosts: []string{"foo.example.com"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-08-25 12:40:47 +00:00
|
|
|
for i, svc := range il.Services {
|
|
|
|
if em, ok := o.entMetas[svc.Name]; ok && em != nil {
|
|
|
|
il.Services[i].EnterpriseMeta = *em
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 14:25:22 +00:00
|
|
|
|
|
|
|
// Now set the appropriate SDS configs
|
|
|
|
if o.listenerSDS {
|
|
|
|
il.TLS = &structs.GatewayTLSConfig{
|
|
|
|
SDS: &structs.GatewayTLSSDSConfig{
|
|
|
|
ClusterName: "listener-cluster",
|
|
|
|
CertResource: "listener-cert",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if o.webSDS {
|
|
|
|
il.Services[0].TLS = &structs.GatewayServiceTLSConfig{
|
|
|
|
SDS: &structs.GatewayTLSSDSConfig{
|
|
|
|
ClusterName: "web-cluster",
|
|
|
|
CertResource: "www-cert",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if o.fooSDS {
|
|
|
|
il.Services[1].TLS = &structs.GatewayServiceTLSConfig{
|
|
|
|
SDS: &structs.GatewayTLSSDSConfig{
|
|
|
|
ClusterName: "foo-cluster",
|
|
|
|
CertResource: "foo-cert",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.wildcard {
|
|
|
|
// undo all that and set just a single wildcard config with no TLS to test
|
|
|
|
// the lookup path where we have to compare an actual resolved upstream to
|
|
|
|
// a wildcard config.
|
|
|
|
il.Services = []structs.IngressService{
|
|
|
|
{
|
|
|
|
Name: "*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// We also don't support user-specified hosts with wildcard so remove
|
|
|
|
// those from the upstreams.
|
|
|
|
ups := snap.IngressGateway.Upstreams[proxycfg.IngressListenerKey{Protocol: "http", Port: 9191}]
|
|
|
|
for i := range ups {
|
|
|
|
ups[i].IngressHosts = nil
|
|
|
|
}
|
|
|
|
snap.IngressGateway.Upstreams[proxycfg.IngressListenerKey{Protocol: "http", Port: 9191}] = ups
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.IngressGateway.Listeners[proxycfg.IngressListenerKey{Protocol: "http", Port: 9191}] = il
|
|
|
|
|
|
|
|
entries := []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
ConnectTimeout: 22 * time.Second,
|
|
|
|
},
|
|
|
|
&structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "foo",
|
|
|
|
ConnectTimeout: 22 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
2021-08-25 12:40:47 +00:00
|
|
|
for i, e := range entries {
|
|
|
|
switch v := e.(type) {
|
|
|
|
// Add other Service types here if we ever need them above
|
|
|
|
case *structs.ServiceResolverConfigEntry:
|
|
|
|
if em, ok := o.entMetas[v.Name]; ok && em != nil {
|
|
|
|
v.EnterpriseMeta = *em
|
|
|
|
entries[i] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webChain := discoverychain.TestCompileConfigEntries(t, "web",
|
2021-09-10 21:09:24 +00:00
|
|
|
o.entMetas["web"].NamespaceOrDefault(),
|
|
|
|
o.entMetas["web"].PartitionOrDefault(), "dc1",
|
2021-08-25 12:40:47 +00:00
|
|
|
connect.TestClusterID+".consul", "dc1", nil, entries...)
|
|
|
|
fooChain := discoverychain.TestCompileConfigEntries(t, "foo",
|
2021-09-10 21:09:24 +00:00
|
|
|
o.entMetas["foo"].NamespaceOrDefault(),
|
|
|
|
o.entMetas["web"].PartitionOrDefault(), "dc1",
|
2021-08-25 12:40:47 +00:00
|
|
|
connect.TestClusterID+".consul", "dc1", nil, entries...)
|
|
|
|
|
|
|
|
snap.IngressGateway.DiscoveryChain[webUpstream.Identifier()] = webChain
|
|
|
|
snap.IngressGateway.DiscoveryChain[fooUpstream.Identifier()] = fooChain
|
2021-08-24 14:25:22 +00:00
|
|
|
}
|
|
|
|
}
|