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
|
|
|
|
2018-03-07 01:32:41 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2023-03-24 20:40:19 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-03-07 01:32:41 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2023-07-21 21:48:25 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2018-03-07 01:32:41 +00:00
|
|
|
)
|
|
|
|
|
2018-03-09 16:34:55 +00:00
|
|
|
// TestRegisterRequest returns a RegisterRequest for registering a typical service.
|
|
|
|
func TestRegisterRequest(t testing.T) *RegisterRequest {
|
|
|
|
return &RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &NodeService{
|
|
|
|
Service: "web",
|
|
|
|
Address: "",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:32:41 +00:00
|
|
|
// TestRegisterRequestProxy returns a RegisterRequest for registering a
|
|
|
|
// Connect proxy.
|
|
|
|
func TestRegisterRequestProxy(t testing.T) *RegisterRequest {
|
|
|
|
return &RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
2018-03-09 06:13:35 +00:00
|
|
|
Service: TestNodeServiceProxy(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
// TestRegisterIngressGateway returns a RegisterRequest for registering an
|
|
|
|
// ingress gateway
|
|
|
|
func TestRegisterIngressGateway(t testing.T) *RegisterRequest {
|
|
|
|
return &RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: TestNodeServiceIngressGateway(t, ""),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 00:03:22 +00:00
|
|
|
// TestNodeService returns a *NodeService representing a valid regular service: "web".
|
2023-07-21 21:48:25 +00:00
|
|
|
func TestNodeService() *NodeService {
|
|
|
|
return TestNodeServiceWithName("web")
|
2021-08-19 00:03:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 21:48:25 +00:00
|
|
|
func TestNodeServiceWithName(name string) *NodeService {
|
2018-05-04 21:10:03 +00:00
|
|
|
return &NodeService{
|
|
|
|
Kind: ServiceKindTypical,
|
2021-08-19 00:03:22 +00:00
|
|
|
Service: name,
|
2019-07-12 19:16:21 +00:00
|
|
|
Port: 8080,
|
2018-05-04 21:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 15:46:34 +00:00
|
|
|
const peerTrustDomain = "1c053652-8512-4373-90cf-5a7f6263a994.consul"
|
|
|
|
|
2023-03-24 20:40:19 +00:00
|
|
|
func TestCheckNodeServiceWithNameInPeer(t testing.T, name, dc, peer, ip string, useHostname bool, remoteEntMeta acl.EnterpriseMeta) CheckServiceNode {
|
|
|
|
|
|
|
|
// Non-default partitions have a different spiffe format.
|
|
|
|
spiffe := fmt.Sprintf("spiffe://%s/ns/default/dc/%s/svc/%s", peerTrustDomain, dc, name)
|
|
|
|
if !remoteEntMeta.InDefaultPartition() {
|
|
|
|
spiffe = fmt.Sprintf("spiffe://%s/ap/%s/ns/%s/dc/%s/svc/%s",
|
|
|
|
peerTrustDomain, remoteEntMeta.PartitionOrDefault(), remoteEntMeta.NamespaceOrDefault(), dc, name)
|
|
|
|
}
|
2022-10-06 13:54:14 +00:00
|
|
|
service := &NodeService{
|
2023-02-16 15:22:41 +00:00
|
|
|
Kind: ServiceKindTypical,
|
|
|
|
Service: name,
|
|
|
|
// We should not see this port number appear in most xds golden tests,
|
|
|
|
// because the WAN addr should typically be used.
|
|
|
|
Port: 9090,
|
2022-10-06 13:54:14 +00:00
|
|
|
PeerName: peer,
|
2022-08-30 15:46:34 +00:00
|
|
|
Connect: ServiceConnect{
|
|
|
|
PeerMeta: &PeeringServiceMeta{
|
|
|
|
SNI: []string{
|
2023-03-24 20:40:19 +00:00
|
|
|
fmt.Sprintf("%s.%s.%s.%s.external.%s",
|
|
|
|
name, remoteEntMeta.NamespaceOrDefault(), remoteEntMeta.PartitionOrDefault(), peer, peerTrustDomain),
|
2022-08-30 15:46:34 +00:00
|
|
|
},
|
2023-03-24 20:40:19 +00:00
|
|
|
SpiffeID: []string{spiffe},
|
2022-08-30 15:46:34 +00:00
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
},
|
2023-02-16 15:22:41 +00:00
|
|
|
// This value should typically be seen in golden file output, since this is a peered service.
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
TaggedAddressWAN: {
|
|
|
|
Address: ip,
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
},
|
2022-08-30 15:46:34 +00:00
|
|
|
}
|
2022-10-06 13:54:14 +00:00
|
|
|
|
|
|
|
if useHostname {
|
|
|
|
service.TaggedAddresses = map[string]ServiceAddress{
|
|
|
|
TaggedAddressLAN: {
|
|
|
|
Address: ip,
|
|
|
|
Port: 443,
|
|
|
|
},
|
|
|
|
TaggedAddressWAN: {
|
|
|
|
Address: name + ".us-east-1.elb.notaws.com",
|
|
|
|
Port: 8443,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CheckServiceNode{
|
|
|
|
Node: &Node{
|
2023-02-16 15:22:41 +00:00
|
|
|
ID: "test1",
|
|
|
|
Node: "test1",
|
|
|
|
// We should not see this address appear in most xds golden tests,
|
|
|
|
// because the WAN addr should typically be used.
|
|
|
|
Address: "1.23.45.67",
|
|
|
|
Datacenter: dc,
|
2022-10-06 13:54:14 +00:00
|
|
|
},
|
|
|
|
Service: service,
|
|
|
|
}
|
2022-08-30 15:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 06:13:35 +00:00
|
|
|
// TestNodeServiceProxy returns a *NodeService representing a valid
|
|
|
|
// Connect proxy.
|
|
|
|
func TestNodeServiceProxy(t testing.T) *NodeService {
|
2021-09-07 20:29:32 +00:00
|
|
|
return TestNodeServiceProxyInPartition(t, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeServiceProxyInPartition(t testing.T, partition string) *NodeService {
|
|
|
|
entMeta := DefaultEnterpriseMetaInPartition(partition)
|
2018-03-09 06:13:35 +00:00
|
|
|
return &NodeService{
|
2021-09-07 20:29:32 +00:00
|
|
|
Kind: ServiceKindConnectProxy,
|
|
|
|
Service: "web-proxy",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 2222,
|
|
|
|
Proxy: TestConnectProxyConfig(t),
|
|
|
|
EnterpriseMeta: *entMeta,
|
2018-03-07 01:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-27 13:33:12 +00:00
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
func TestNodeServiceExpose(t testing.T) *NodeService {
|
|
|
|
return &NodeService{
|
|
|
|
Kind: ServiceKindConnectProxy,
|
|
|
|
Service: "test-svc",
|
|
|
|
Address: "localhost",
|
|
|
|
Port: 8080,
|
|
|
|
Proxy: ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
Expose: ExposeConfig{
|
|
|
|
Paths: []ExposePath{
|
|
|
|
{
|
|
|
|
Path: "/foo",
|
|
|
|
LocalPathPort: 8080,
|
|
|
|
ListenerPort: 21500,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/bar",
|
|
|
|
LocalPathPort: 8080,
|
|
|
|
ListenerPort: 21501,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
// TestNodeServiceMeshGateway returns a *NodeService representing a valid Mesh Gateway
|
|
|
|
func TestNodeServiceMeshGateway(t testing.T) *NodeService {
|
|
|
|
return TestNodeServiceMeshGatewayWithAddrs(t,
|
|
|
|
"10.1.2.3",
|
|
|
|
8443,
|
|
|
|
ServiceAddress{Address: "10.1.2.3", Port: 8443},
|
|
|
|
ServiceAddress{Address: "198.18.4.5", Port: 443})
|
|
|
|
}
|
|
|
|
|
2023-06-09 12:22:32 +00:00
|
|
|
func TestNodeServiceAPIGateway(t testing.T) *NodeService {
|
|
|
|
return &NodeService{
|
|
|
|
Kind: ServiceKindAPIGateway,
|
|
|
|
Service: "api-gateway",
|
|
|
|
Address: "1.1.1.1",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 16:20:56 +00:00
|
|
|
func TestNodeServiceTerminatingGateway(t testing.T, address string) *NodeService {
|
|
|
|
return &NodeService{
|
|
|
|
Kind: ServiceKindTerminatingGateway,
|
|
|
|
Port: 8443,
|
|
|
|
Service: "terminating-gateway",
|
|
|
|
Address: address,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
func TestNodeServiceMeshGatewayWithAddrs(t testing.T, address string, port int, lanAddr, wanAddr ServiceAddress) *NodeService {
|
|
|
|
return &NodeService{
|
|
|
|
Kind: ServiceKindMeshGateway,
|
|
|
|
Service: "mesh-gateway",
|
|
|
|
Address: address,
|
|
|
|
Port: port,
|
|
|
|
Proxy: ConnectProxyConfig{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
2020-01-17 14:54:17 +00:00
|
|
|
TaggedAddressLAN: lanAddr,
|
|
|
|
TaggedAddressWAN: wanAddr,
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
2021-11-09 16:45:36 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: 1,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:59:10 +00:00
|
|
|
func TestNodeServiceIngressGateway(t testing.T, address string) *NodeService {
|
|
|
|
return &NodeService{
|
|
|
|
Kind: ServiceKindIngressGateway,
|
|
|
|
Service: "ingress-gateway",
|
|
|
|
Address: address,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
// TestNodeServiceSidecar returns a *NodeService representing a service
|
|
|
|
// registration with a nested Sidecar registration.
|
|
|
|
func TestNodeServiceSidecar(t testing.T) *NodeService {
|
|
|
|
return &NodeService{
|
|
|
|
Service: "web",
|
|
|
|
Port: 2222,
|
|
|
|
Connect: ServiceConnect{
|
|
|
|
SidecarService: &ServiceDefinition{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|