2022-03-07 17:47:14 +00:00
|
|
|
package proxycfg
|
|
|
|
|
|
|
|
import (
|
2023-03-10 20:52:54 +00:00
|
|
|
"fmt"
|
2022-06-06 14:15:33 +00:00
|
|
|
"time"
|
|
|
|
|
2022-03-07 17:47:14 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-03-22 18:56:18 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-03-07 17:47:14 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/discoverychain"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-03-10 20:52:54 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2022-06-06 14:15:33 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2022-03-07 17:47:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestConfigSnapshot returns a fully populated snapshot
|
2022-05-20 14:47:40 +00:00
|
|
|
func TestConfigSnapshot(t testing.T, nsFn func(ns *structs.NodeService), extraUpdates []UpdateEvent) *ConfigSnapshot {
|
2022-03-07 17:47:14 +00:00
|
|
|
roots, leaf := TestCerts(t)
|
|
|
|
|
|
|
|
// no entries implies we'll get a default chain
|
|
|
|
dbChain := discoverychain.TestCompileConfigEntries(t, "db", "default", "default", "dc1", connect.TestClusterID+".consul", nil)
|
2022-03-30 15:04:18 +00:00
|
|
|
assert.True(t, dbChain.Default)
|
2022-03-07 17:47:14 +00:00
|
|
|
|
|
|
|
var (
|
2023-03-22 18:56:18 +00:00
|
|
|
upstreams = structs.TestUpstreams(t, false)
|
2022-03-07 17:47:14 +00:00
|
|
|
dbUpstream = upstreams[0]
|
|
|
|
geoUpstream = upstreams[1]
|
|
|
|
|
|
|
|
dbUID = NewUpstreamID(&dbUpstream)
|
|
|
|
geoUID = NewUpstreamID(&geoUpstream)
|
|
|
|
|
|
|
|
webSN = structs.ServiceIDString("web", nil)
|
|
|
|
)
|
|
|
|
|
2022-05-20 14:47:40 +00:00
|
|
|
baseEvents := []UpdateEvent{
|
2022-03-07 17:47:14 +00:00
|
|
|
{
|
|
|
|
CorrelationID: rootsWatchID,
|
|
|
|
Result: roots,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: leafWatchID,
|
|
|
|
Result: leaf,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: intentionsWatchID,
|
2022-07-01 15:15:49 +00:00
|
|
|
Result: structs.Intentions{}, // no intentions defined
|
2022-03-07 17:47:14 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: svcChecksWatchIDPrefix + webSN,
|
|
|
|
Result: []structs.CheckType{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: "upstream:" + geoUID.String(),
|
|
|
|
Result: &structs.PreparedQueryExecuteResponse{
|
|
|
|
Nodes: TestPreparedQueryNodes(t, "geo-cache"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: "discovery-chain:" + dbUID.String(),
|
|
|
|
Result: &structs.DiscoveryChainResponse{
|
|
|
|
Chain: dbChain,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: "upstream-target:" + dbChain.ID() + ":" + dbUID.String(),
|
|
|
|
Result: &structs.IndexedCheckServiceNodes{
|
|
|
|
Nodes: TestUpstreamNodes(t, "db"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return testConfigSnapshotFixture(t, &structs.NodeService{
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 9999,
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8080,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
Upstreams: upstreams,
|
|
|
|
},
|
|
|
|
Meta: nil,
|
|
|
|
TaggedAddresses: nil,
|
|
|
|
}, nsFn, nil, testSpliceEvents(baseEvents, extraUpdates))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestConfigSnapshotDiscoveryChain returns a fully populated snapshot using a discovery chain
|
|
|
|
func TestConfigSnapshotDiscoveryChain(
|
|
|
|
t testing.T,
|
|
|
|
variation string,
|
2023-03-22 18:56:18 +00:00
|
|
|
enterprise bool,
|
2022-03-07 17:47:14 +00:00
|
|
|
nsFn func(ns *structs.NodeService),
|
2022-05-20 14:47:40 +00:00
|
|
|
extraUpdates []UpdateEvent,
|
2022-03-07 17:47:14 +00:00
|
|
|
additionalEntries ...structs.ConfigEntry,
|
|
|
|
) *ConfigSnapshot {
|
|
|
|
roots, leaf := TestCerts(t)
|
|
|
|
|
2023-03-22 18:56:18 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
|
|
|
if enterprise {
|
|
|
|
entMeta = acl.NewEnterpriseMetaWithPartition("ap1", "ns1")
|
|
|
|
}
|
|
|
|
|
2022-03-07 17:47:14 +00:00
|
|
|
var (
|
2023-03-22 18:56:18 +00:00
|
|
|
upstreams = structs.TestUpstreams(t, enterprise)
|
2022-03-07 17:47:14 +00:00
|
|
|
geoUpstream = upstreams[1]
|
|
|
|
|
|
|
|
geoUID = NewUpstreamID(&geoUpstream)
|
|
|
|
|
2023-03-22 18:56:18 +00:00
|
|
|
webSN = structs.ServiceIDString("web", &entMeta)
|
2022-03-07 17:47:14 +00:00
|
|
|
)
|
|
|
|
|
2022-05-20 14:47:40 +00:00
|
|
|
baseEvents := testSpliceEvents([]UpdateEvent{
|
2022-03-07 17:47:14 +00:00
|
|
|
{
|
|
|
|
CorrelationID: rootsWatchID,
|
|
|
|
Result: roots,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: leafWatchID,
|
|
|
|
Result: leaf,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: intentionsWatchID,
|
2022-07-01 15:15:49 +00:00
|
|
|
Result: structs.Intentions{}, // no intentions defined
|
2022-03-07 17:47:14 +00:00
|
|
|
},
|
2022-03-30 18:43:59 +00:00
|
|
|
{
|
|
|
|
CorrelationID: meshConfigEntryID,
|
|
|
|
Result: &structs.ConfigEntryResponse{
|
|
|
|
Entry: nil,
|
|
|
|
},
|
|
|
|
},
|
2022-03-07 17:47:14 +00:00
|
|
|
{
|
|
|
|
CorrelationID: svcChecksWatchIDPrefix + webSN,
|
|
|
|
Result: []structs.CheckType{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: "upstream:" + geoUID.String(),
|
|
|
|
Result: &structs.PreparedQueryExecuteResponse{
|
|
|
|
Nodes: TestPreparedQueryNodes(t, "geo-cache"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, setupTestVariationConfigEntriesAndSnapshot(
|
2023-03-24 20:40:19 +00:00
|
|
|
t, variation, enterprise, upstreams, additionalEntries...,
|
2022-03-07 17:47:14 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
return testConfigSnapshotFixture(t, &structs.NodeService{
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 9999,
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8080,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
Upstreams: upstreams,
|
|
|
|
},
|
|
|
|
Meta: nil,
|
|
|
|
TaggedAddresses: nil,
|
2023-03-22 18:56:18 +00:00
|
|
|
EnterpriseMeta: entMeta,
|
2022-03-07 17:47:14 +00:00
|
|
|
}, nsFn, nil, testSpliceEvents(baseEvents, extraUpdates))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigSnapshotExposeConfig(t testing.T, nsFn func(ns *structs.NodeService)) *ConfigSnapshot {
|
|
|
|
roots, leaf := TestCerts(t)
|
|
|
|
|
|
|
|
var (
|
|
|
|
webSN = structs.ServiceIDString("web", nil)
|
|
|
|
)
|
|
|
|
|
2022-05-20 14:47:40 +00:00
|
|
|
baseEvents := []UpdateEvent{
|
2022-03-07 17:47:14 +00:00
|
|
|
{
|
|
|
|
CorrelationID: rootsWatchID,
|
|
|
|
Result: roots,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: leafWatchID, Result: leaf,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: intentionsWatchID,
|
2022-07-01 15:15:49 +00:00
|
|
|
Result: structs.Intentions{}, // no intentions defined
|
2022-03-07 17:47:14 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: svcChecksWatchIDPrefix + webSN,
|
|
|
|
Result: []structs.CheckType{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return testConfigSnapshotFixture(t, &structs.NodeService{
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Address: "1.2.3.4",
|
|
|
|
Port: 8080,
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
LocalServicePort: 8080,
|
|
|
|
Expose: structs.ExposeConfig{
|
|
|
|
Checks: false,
|
|
|
|
Paths: []structs.ExposePath{
|
|
|
|
{
|
|
|
|
LocalPathPort: 8080,
|
|
|
|
Path: "/health1",
|
|
|
|
ListenerPort: 21500,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LocalPathPort: 8080,
|
|
|
|
Path: "/health2",
|
|
|
|
ListenerPort: 21501,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Meta: nil,
|
|
|
|
TaggedAddresses: nil,
|
|
|
|
}, nsFn, nil, baseEvents)
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:15:33 +00:00
|
|
|
func TestConfigSnapshotExposeChecks(t testing.T) *ConfigSnapshot {
|
|
|
|
return TestConfigSnapshot(t,
|
|
|
|
func(ns *structs.NodeService) {
|
|
|
|
ns.Address = "1.2.3.4"
|
|
|
|
ns.Port = 8080
|
|
|
|
ns.Proxy.Upstreams = nil
|
|
|
|
ns.Proxy.Expose = structs.ExposeConfig{
|
|
|
|
Checks: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[]UpdateEvent{
|
|
|
|
{
|
|
|
|
CorrelationID: svcChecksWatchIDPrefix + structs.ServiceIDString("web", nil),
|
|
|
|
Result: []structs.CheckType{{
|
|
|
|
CheckID: types.CheckID("http"),
|
|
|
|
Name: "http",
|
|
|
|
HTTP: "http://127.0.0.1:8181/debug",
|
|
|
|
ProxyHTTP: "http://:21500/debug",
|
|
|
|
Method: "GET",
|
|
|
|
Interval: 10 * time.Second,
|
|
|
|
Timeout: 1 * time.Second,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-07 17:47:14 +00:00
|
|
|
func TestConfigSnapshotGRPCExposeHTTP1(t testing.T) *ConfigSnapshot {
|
|
|
|
roots, leaf := TestCerts(t)
|
|
|
|
|
|
|
|
return testConfigSnapshotFixture(t, &structs.NodeService{
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
Service: "grpc-proxy",
|
|
|
|
Address: "1.2.3.4",
|
|
|
|
Port: 8080,
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "grpc",
|
|
|
|
DestinationServiceID: "grpc",
|
|
|
|
LocalServicePort: 8080,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "grpc",
|
|
|
|
},
|
|
|
|
Expose: structs.ExposeConfig{
|
|
|
|
Checks: false,
|
|
|
|
Paths: []structs.ExposePath{
|
|
|
|
{
|
|
|
|
LocalPathPort: 8090,
|
|
|
|
Path: "/healthz",
|
|
|
|
ListenerPort: 21500,
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Meta: nil,
|
|
|
|
TaggedAddresses: nil,
|
2022-05-20 14:47:40 +00:00
|
|
|
}, nil, nil, []UpdateEvent{
|
2022-03-07 17:47:14 +00:00
|
|
|
{
|
|
|
|
CorrelationID: rootsWatchID,
|
|
|
|
Result: roots,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: leafWatchID,
|
|
|
|
Result: leaf,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: intentionsWatchID,
|
2022-07-01 15:15:49 +00:00
|
|
|
Result: structs.Intentions{}, // no intentions defined
|
2022-03-07 17:47:14 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: svcChecksWatchIDPrefix + structs.ServiceIDString("grpc", nil),
|
|
|
|
Result: []structs.CheckType{},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2023-03-10 20:52:54 +00:00
|
|
|
|
|
|
|
// TestConfigSnapshotDiscoveryChain returns a fully populated snapshot using a discovery chain
|
|
|
|
func TestConfigSnapshotHCPMetrics(t testing.T) *ConfigSnapshot {
|
|
|
|
// DiscoveryChain without an UpstreamConfig should yield a
|
|
|
|
// filter chain when in transparent proxy mode
|
|
|
|
var (
|
|
|
|
collector = structs.NewServiceName(api.HCPMetricsCollectorName, nil)
|
|
|
|
collectorUID = NewUpstreamIDFromServiceName(collector)
|
|
|
|
collectorChain = discoverychain.TestCompileConfigEntries(t, api.HCPMetricsCollectorName, "default", "default", "dc1", connect.TestClusterID+".consul", nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
return TestConfigSnapshot(t, func(ns *structs.NodeService) {
|
|
|
|
ns.Proxy.Config = map[string]interface{}{
|
|
|
|
"envoy_hcp_metrics_bind_socket_dir": "/tmp/consul/hcp-metrics",
|
|
|
|
}
|
|
|
|
}, []UpdateEvent{
|
|
|
|
{
|
|
|
|
CorrelationID: meshConfigEntryID,
|
|
|
|
Result: &structs.ConfigEntryResponse{
|
|
|
|
Entry: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: "discovery-chain:" + collectorUID.String(),
|
|
|
|
Result: &structs.DiscoveryChainResponse{
|
|
|
|
Chain: collectorChain,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CorrelationID: fmt.Sprintf("upstream-target:%s.default.default.dc1:", api.HCPMetricsCollectorName) + collectorUID.String(),
|
|
|
|
Result: &structs.IndexedCheckServiceNodes{
|
|
|
|
Nodes: []structs.CheckServiceNode{
|
|
|
|
{
|
|
|
|
Node: &structs.Node{
|
|
|
|
Address: "8.8.8.8",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: api.HCPMetricsCollectorName,
|
|
|
|
Address: "9.9.9.9",
|
|
|
|
Port: 9090,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|