2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2019-08-02 20:34:54 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-08-05 18:30:35 +00:00
|
|
|
"reflect"
|
2019-08-02 20:34:54 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-30 15:04:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2019-08-02 20:34:54 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiscoveryChainRead(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-08-02 20:34:54 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
2022-08-23 13:13:43 +00:00
|
|
|
newTarget := func(opts structs.DiscoveryTargetOpts) *structs.DiscoveryTarget {
|
|
|
|
if opts.Namespace == "" {
|
|
|
|
opts.Namespace = "default"
|
|
|
|
}
|
|
|
|
if opts.Partition == "" {
|
|
|
|
opts.Partition = "default"
|
|
|
|
}
|
|
|
|
if opts.Datacenter == "" {
|
|
|
|
opts.Datacenter = "dc1"
|
|
|
|
}
|
|
|
|
t := structs.NewDiscoveryTarget(opts)
|
2019-08-19 18:03:03 +00:00
|
|
|
t.SNI = connect.TargetSNI(t, connect.TestClusterID+".consul")
|
|
|
|
t.Name = t.SNI
|
2022-04-07 21:58:21 +00:00
|
|
|
t.ConnectTimeout = 5 * time.Second // default
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
targetWithConnectTimeout := func(t *structs.DiscoveryTarget, connectTimeout time.Duration) *structs.DiscoveryTarget {
|
|
|
|
t.ConnectTimeout = connectTimeout
|
2019-08-19 18:03:03 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
for _, method := range []string{"GET", "POST"} {
|
|
|
|
require.True(t, t.Run(method+": error on no service name", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
req *http.Request
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if method == "GET" {
|
|
|
|
req, err = http.NewRequest("GET", "/v1/discovery-chain/", nil)
|
|
|
|
} else {
|
|
|
|
apiReq := &discoveryChainReadRequest{}
|
|
|
|
req, err = http.NewRequest("POST", "/v1/discovery-chain/", jsonReader(apiReq))
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = a.srv.DiscoveryChainRead(resp, req)
|
|
|
|
require.Error(t, err)
|
2022-04-29 17:42:49 +00:00
|
|
|
require.True(t, isHTTPBadRequest(err))
|
2019-08-02 20:34:54 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
require.True(t, t.Run(method+": read default chain", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
req *http.Request
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if method == "GET" {
|
|
|
|
req, err = http.NewRequest("GET", "/v1/discovery-chain/web", nil)
|
|
|
|
} else {
|
|
|
|
apiReq := &discoveryChainReadRequest{}
|
|
|
|
req, err = http.NewRequest("POST", "/v1/discovery-chain/web", jsonReader(apiReq))
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.DiscoveryChainRead(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.(discoveryChainReadResponse)
|
|
|
|
|
|
|
|
expect := &structs.CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
2021-09-07 20:29:32 +00:00
|
|
|
Partition: "default",
|
2019-08-02 20:34:54 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Protocol: "tcp",
|
2022-03-30 15:04:18 +00:00
|
|
|
Default: true,
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*structs.DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc1": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: structs.DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &structs.DiscoveryResolver{
|
|
|
|
Default: true,
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*structs.DiscoveryTarget{
|
2022-08-23 13:13:43 +00:00
|
|
|
"web.default.default.dc1": newTarget(structs.DiscoveryTargetOpts{Service: "web"}),
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, value.Chain)
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.True(t, t.Run(method+": read default chain; evaluate in dc2", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
req *http.Request
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if method == "GET" {
|
|
|
|
req, err = http.NewRequest("GET", "/v1/discovery-chain/web?compile-dc=dc2", nil)
|
|
|
|
} else {
|
|
|
|
apiReq := &discoveryChainReadRequest{}
|
|
|
|
req, err = http.NewRequest("POST", "/v1/discovery-chain/web?compile-dc=dc2", jsonReader(apiReq))
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.DiscoveryChainRead(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.(discoveryChainReadResponse)
|
|
|
|
|
|
|
|
expect := &structs.CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
2021-09-07 20:29:32 +00:00
|
|
|
Partition: "default",
|
2019-08-02 20:34:54 +00:00
|
|
|
Datacenter: "dc2",
|
2022-03-30 15:04:18 +00:00
|
|
|
Default: true,
|
2019-08-02 20:34:54 +00:00
|
|
|
Protocol: "tcp",
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*structs.DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc2": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: structs.DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &structs.DiscoveryResolver{
|
|
|
|
Default: true,
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*structs.DiscoveryTarget{
|
2022-08-23 13:13:43 +00:00
|
|
|
"web.default.default.dc2": newTarget(structs.DiscoveryTargetOpts{Service: "web", Datacenter: "dc2"}),
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, value.Chain)
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.True(t, t.Run(method+": read default chain with cache", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
req *http.Request
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if method == "GET" {
|
|
|
|
req, err = http.NewRequest("GET", "/v1/discovery-chain/web?cached", nil)
|
|
|
|
} else {
|
|
|
|
apiReq := &discoveryChainReadRequest{}
|
|
|
|
req, err = http.NewRequest("POST", "/v1/discovery-chain/web?cached", jsonReader(apiReq))
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.DiscoveryChainRead(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// The GET request primes the cache so the POST is a hit.
|
|
|
|
if method == "GET" {
|
|
|
|
// Should be a cache miss
|
|
|
|
require.Equal(t, "MISS", resp.Header().Get("X-Cache"))
|
|
|
|
} else {
|
|
|
|
// Should be a cache HIT now!
|
|
|
|
require.Equal(t, "HIT", resp.Header().Get("X-Cache"))
|
|
|
|
}
|
|
|
|
|
|
|
|
value := obj.(discoveryChainReadResponse)
|
|
|
|
|
|
|
|
expect := &structs.CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
2021-09-07 20:29:32 +00:00
|
|
|
Partition: "default",
|
2019-08-02 20:34:54 +00:00
|
|
|
Datacenter: "dc1",
|
2022-03-30 15:04:18 +00:00
|
|
|
Default: true,
|
2019-08-02 20:34:54 +00:00
|
|
|
Protocol: "tcp",
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*structs.DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc1": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: structs.DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &structs.DiscoveryResolver{
|
|
|
|
Default: true,
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*structs.DiscoveryTarget{
|
2022-08-23 13:13:43 +00:00
|
|
|
"web.default.default.dc1": newTarget(structs.DiscoveryTargetOpts{Service: "web"}),
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, value.Chain)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Now create one config entry.
|
|
|
|
out := false
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Apply", &structs.ConfigEntryRequest{
|
2019-08-02 20:34:54 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: &structs.ServiceResolverConfigEntry{
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
2019-08-05 18:30:35 +00:00
|
|
|
Failover: map[string]structs.ServiceResolverFailover{
|
|
|
|
"*": {
|
|
|
|
Datacenters: []string{"dc2"},
|
|
|
|
},
|
|
|
|
},
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
}, &out))
|
|
|
|
require.True(t, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure background refresh works
|
|
|
|
require.True(t, t.Run("GET: read modified chain", func(t *testing.T) {
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/discovery-chain/web?cached", nil)
|
|
|
|
r.Check(err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.DiscoveryChainRead(resp, req)
|
|
|
|
r.Check(err)
|
|
|
|
|
|
|
|
// Should be a cache hit! The data should've updated in the cache
|
|
|
|
// in the background so this should've been fetched directly from
|
|
|
|
// the cache.
|
|
|
|
if resp.Header().Get("X-Cache") != "HIT" {
|
|
|
|
r.Fatalf("should be a cache hit")
|
|
|
|
}
|
2019-08-05 18:30:35 +00:00
|
|
|
|
|
|
|
value := obj.(discoveryChainReadResponse)
|
|
|
|
|
|
|
|
expect := &structs.CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
2021-09-07 20:29:32 +00:00
|
|
|
Partition: "default",
|
2019-08-05 18:30:35 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Protocol: "tcp",
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc1",
|
2019-08-05 18:30:35 +00:00
|
|
|
Nodes: map[string]*structs.DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc1": {
|
2019-08-05 18:30:35 +00:00
|
|
|
Type: structs.DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc1",
|
2019-08-05 18:30:35 +00:00
|
|
|
Resolver: &structs.DiscoveryResolver{
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc1",
|
2019-08-05 18:30:35 +00:00
|
|
|
Failover: &structs.DiscoveryFailover{
|
2021-09-07 20:29:32 +00:00
|
|
|
Targets: []string{"web.default.default.dc2"},
|
2019-08-05 18:30:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*structs.DiscoveryTarget{
|
2022-04-07 21:58:21 +00:00
|
|
|
"web.default.default.dc1": targetWithConnectTimeout(
|
2022-08-23 13:13:43 +00:00
|
|
|
newTarget(structs.DiscoveryTargetOpts{Service: "web"}),
|
2022-04-07 21:58:21 +00:00
|
|
|
33*time.Second,
|
|
|
|
),
|
|
|
|
"web.default.default.dc2": targetWithConnectTimeout(
|
2022-08-23 13:13:43 +00:00
|
|
|
newTarget(structs.DiscoveryTargetOpts{Service: "web", Datacenter: "dc2"}),
|
2022-04-07 21:58:21 +00:00
|
|
|
33*time.Second,
|
|
|
|
),
|
2019-08-05 18:30:35 +00:00
|
|
|
},
|
2023-05-12 02:28:16 +00:00
|
|
|
AutoVirtualIPs: []string{"240.0.0.1"},
|
|
|
|
ManualVirtualIPs: []string{},
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expect, value.Chain) {
|
|
|
|
r.Fatalf("should be equal: expected=%+v, got=%+v", expect, value.Chain)
|
|
|
|
}
|
2019-08-02 20:34:54 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
2022-04-07 21:58:21 +00:00
|
|
|
expectTarget_DC1 := targetWithConnectTimeout(
|
2022-08-23 13:13:43 +00:00
|
|
|
newTarget(structs.DiscoveryTargetOpts{Service: "web"}),
|
2022-04-11 15:56:57 +00:00
|
|
|
22*time.Second,
|
2022-04-07 21:58:21 +00:00
|
|
|
)
|
2021-10-22 21:13:48 +00:00
|
|
|
expectTarget_DC1.MeshGateway = structs.MeshGatewayConfig{
|
|
|
|
Mode: structs.MeshGatewayModeLocal,
|
|
|
|
}
|
|
|
|
|
2022-04-07 21:58:21 +00:00
|
|
|
expectTarget_DC2 := targetWithConnectTimeout(
|
2022-08-23 13:13:43 +00:00
|
|
|
newTarget(structs.DiscoveryTargetOpts{Service: "web", Datacenter: "dc2"}),
|
2022-04-11 15:56:57 +00:00
|
|
|
22*time.Second,
|
2022-04-07 21:58:21 +00:00
|
|
|
)
|
2019-08-05 18:30:35 +00:00
|
|
|
expectTarget_DC2.MeshGateway = structs.MeshGatewayConfig{
|
2019-08-02 20:34:54 +00:00
|
|
|
Mode: structs.MeshGatewayModeLocal,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectModifiedWithOverrides := &structs.CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
2021-09-07 20:29:32 +00:00
|
|
|
Partition: "default",
|
2019-08-02 20:34:54 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Protocol: "grpc",
|
|
|
|
CustomizationHash: "98809527",
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*structs.DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc1": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: structs.DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &structs.DiscoveryResolver{
|
|
|
|
ConnectTimeout: 22 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc1",
|
2019-08-05 18:30:35 +00:00
|
|
|
Failover: &structs.DiscoveryFailover{
|
2021-09-07 20:29:32 +00:00
|
|
|
Targets: []string{"web.default.default.dc2"},
|
2019-08-05 18:30:35 +00:00
|
|
|
},
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*structs.DiscoveryTarget{
|
2021-10-22 21:13:48 +00:00
|
|
|
expectTarget_DC1.ID: expectTarget_DC1,
|
|
|
|
expectTarget_DC2.ID: expectTarget_DC2,
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
2023-05-12 02:28:16 +00:00
|
|
|
AutoVirtualIPs: []string{"240.0.0.1"},
|
|
|
|
ManualVirtualIPs: []string{},
|
2019-08-02 20:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, t.Run("POST: read modified chain with overrides (camel case)", func(t *testing.T) {
|
|
|
|
body := ` {
|
|
|
|
"OverrideMeshGateway": {
|
|
|
|
"Mode": "local"
|
|
|
|
},
|
|
|
|
"OverrideProtocol": "grpc",
|
|
|
|
"OverrideConnectTimeout": "22s"
|
|
|
|
} `
|
|
|
|
req, err := http.NewRequest("POST", "/v1/discovery-chain/web", strings.NewReader(body))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.DiscoveryChainRead(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.(discoveryChainReadResponse)
|
|
|
|
|
|
|
|
require.Equal(t, expectModifiedWithOverrides, value.Chain)
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.True(t, t.Run("POST: read modified chain with overrides (snake case)", func(t *testing.T) {
|
|
|
|
body := ` {
|
|
|
|
"override_mesh_gateway": {
|
|
|
|
"mode": "local"
|
|
|
|
},
|
|
|
|
"override_protocol": "grpc",
|
|
|
|
"override_connect_timeout": "22s"
|
|
|
|
} `
|
|
|
|
req, err := http.NewRequest("POST", "/v1/discovery-chain/web", strings.NewReader(body))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.DiscoveryChainRead(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.(discoveryChainReadResponse)
|
|
|
|
|
|
|
|
require.Equal(t, expectModifiedWithOverrides, value.Chain)
|
|
|
|
}))
|
|
|
|
}
|