2018-10-03 12:36:38 +00:00
|
|
|
package proxycfg
|
|
|
|
|
|
|
|
import (
|
2019-06-18 00:52:01 +00:00
|
|
|
"context"
|
2020-04-16 23:24:11 +00:00
|
|
|
"fmt"
|
2021-02-08 16:19:57 +00:00
|
|
|
"sort"
|
2020-04-16 23:24:11 +00:00
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
2021-04-06 18:19:59 +00:00
|
|
|
|
2021-09-13 12:03:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2021-04-06 18:19:59 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-10-03 12:36:38 +00:00
|
|
|
)
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
// TODO(ingress): Can we think of a better for this bag of data?
|
|
|
|
// A shared data structure that contains information about discovered upstreams
|
|
|
|
type ConfigSnapshotUpstreams struct {
|
|
|
|
Leaf *structs.IssuedCert
|
|
|
|
// DiscoveryChain is a map of upstream.Identifier() ->
|
|
|
|
// CompiledDiscoveryChain's, and is used to determine what services could be
|
|
|
|
// targeted by this upstream. We then instantiate watches for those targets.
|
|
|
|
DiscoveryChain map[string]*structs.CompiledDiscoveryChain
|
|
|
|
|
2021-03-17 19:40:39 +00:00
|
|
|
// WatchedDiscoveryChains is a map of upstream.Identifier() -> CancelFunc's
|
|
|
|
// in order to cancel any watches when the proxy's configuration is
|
|
|
|
// changed. Ingress gateways and transparent proxies need this because
|
|
|
|
// discovery chain watches are added and removed through the lifecycle
|
|
|
|
// of a single proxycfg state instance.
|
|
|
|
WatchedDiscoveryChains map[string]context.CancelFunc
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
// WatchedUpstreams is a map of upstream.Identifier() -> (map of TargetID ->
|
|
|
|
// CancelFunc's) in order to cancel any watches when the configuration is
|
|
|
|
// changed.
|
|
|
|
WatchedUpstreams map[string]map[string]context.CancelFunc
|
|
|
|
|
|
|
|
// WatchedUpstreamEndpoints is a map of upstream.Identifier() -> (map of
|
|
|
|
// TargetID -> CheckServiceNodes) and is used to determine the backing
|
|
|
|
// endpoints of an upstream.
|
2019-08-02 20:34:54 +00:00
|
|
|
WatchedUpstreamEndpoints map[string]map[string]structs.CheckServiceNodes
|
2019-08-05 18:30:35 +00:00
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
// WatchedGateways is a map of upstream.Identifier() -> (map of
|
|
|
|
// TargetID -> CancelFunc) in order to cancel watches for mesh gateways
|
|
|
|
WatchedGateways map[string]map[string]context.CancelFunc
|
|
|
|
|
|
|
|
// WatchedGatewayEndpoints is a map of upstream.Identifier() -> (map of
|
|
|
|
// TargetID -> CheckServiceNodes) and is used to determine the backing
|
|
|
|
// endpoints of a mesh gateway.
|
|
|
|
WatchedGatewayEndpoints map[string]map[string]structs.CheckServiceNodes
|
2021-03-17 19:40:39 +00:00
|
|
|
|
|
|
|
// UpstreamConfig is a map to an upstream's configuration.
|
|
|
|
UpstreamConfig map[string]*structs.Upstream
|
2021-06-09 20:34:17 +00:00
|
|
|
|
|
|
|
// PassthroughEndpoints is a map of: ServiceName -> ServicePassthroughAddrs.
|
|
|
|
PassthroughUpstreams map[string]ServicePassthroughAddrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServicePassthroughAddrs contains the LAN addrs
|
|
|
|
type ServicePassthroughAddrs struct {
|
|
|
|
// SNI is the Service SNI of the upstream.
|
|
|
|
SNI string
|
|
|
|
|
2021-06-30 16:16:33 +00:00
|
|
|
// SpiffeID is the SPIFFE ID to use for upstream SAN validation.
|
|
|
|
SpiffeID connect.SpiffeIDService
|
|
|
|
|
2021-06-09 20:34:17 +00:00
|
|
|
// Addrs is a set of the best LAN addresses for the instances of the upstream.
|
|
|
|
Addrs map[string]struct{}
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type configSnapshotConnectProxy struct {
|
|
|
|
ConfigSnapshotUpstreams
|
|
|
|
|
|
|
|
WatchedServiceChecks map[structs.ServiceID][]structs.CheckType // TODO: missing garbage collection
|
|
|
|
PreparedQueryEndpoints map[string]structs.CheckServiceNodes // DEPRECATED:see:WatchedUpstreamEndpoints
|
2020-08-27 17:20:58 +00:00
|
|
|
|
|
|
|
// NOTE: Intentions stores a list of lists as returned by the Intentions
|
|
|
|
// Match RPC. So far we only use the first list as the list of matching
|
|
|
|
// intentions.
|
|
|
|
Intentions structs.Intentions
|
|
|
|
IntentionsSet bool
|
2021-04-06 18:19:59 +00:00
|
|
|
|
2021-04-28 22:13:29 +00:00
|
|
|
MeshConfig *structs.MeshConfigEntry
|
|
|
|
MeshConfigSet bool
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 21:46:49 +00:00
|
|
|
func (c *configSnapshotConnectProxy) IsEmpty() bool {
|
|
|
|
if c == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return c.Leaf == nil &&
|
2020-08-27 17:20:58 +00:00
|
|
|
!c.IntentionsSet &&
|
2019-10-17 21:46:49 +00:00
|
|
|
len(c.DiscoveryChain) == 0 &&
|
2021-03-17 19:40:39 +00:00
|
|
|
len(c.WatchedDiscoveryChains) == 0 &&
|
2019-10-17 21:46:49 +00:00
|
|
|
len(c.WatchedUpstreams) == 0 &&
|
|
|
|
len(c.WatchedUpstreamEndpoints) == 0 &&
|
|
|
|
len(c.WatchedGateways) == 0 &&
|
|
|
|
len(c.WatchedGatewayEndpoints) == 0 &&
|
|
|
|
len(c.WatchedServiceChecks) == 0 &&
|
2021-03-17 19:40:39 +00:00
|
|
|
len(c.PreparedQueryEndpoints) == 0 &&
|
2021-04-06 18:19:59 +00:00
|
|
|
len(c.UpstreamConfig) == 0 &&
|
2021-06-09 20:34:17 +00:00
|
|
|
len(c.PassthroughUpstreams) == 0 &&
|
2021-04-28 22:13:29 +00:00
|
|
|
!c.MeshConfigSet
|
2019-10-17 21:46:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
type configSnapshotTerminatingGateway struct {
|
2020-06-12 14:57:41 +00:00
|
|
|
// WatchedServices is a map of service name to a cancel function. This cancel
|
2020-04-10 18:06:08 +00:00
|
|
|
// function is tied to the watch of linked service instances for the given
|
|
|
|
// id. If the linked services watch would indicate the removal of
|
|
|
|
// a service altogether we then cancel watching that service for its endpoints.
|
2020-06-12 14:57:41 +00:00
|
|
|
WatchedServices map[structs.ServiceName]context.CancelFunc
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// WatchedIntentions is a map of service name to a cancel function.
|
2020-04-10 18:06:08 +00:00
|
|
|
// This cancel function is tied to the watch of intentions for linked services.
|
|
|
|
// As with WatchedServices, intention watches will be cancelled when services
|
|
|
|
// are no longer linked to the gateway.
|
2020-06-12 14:57:41 +00:00
|
|
|
WatchedIntentions map[structs.ServiceName]context.CancelFunc
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
// NOTE: Intentions stores a map of list of lists as returned by the Intentions
|
|
|
|
// Match RPC. So far we only use the first list as the list of matching
|
|
|
|
// intentions.
|
|
|
|
//
|
|
|
|
// A key being present implies that we have gotten at least one watch reply for the
|
|
|
|
// service. This is logically the same as ConnectProxy.IntentionsSet==true
|
|
|
|
Intentions map[structs.ServiceName]structs.Intentions
|
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// WatchedLeaves is a map of ServiceName to a cancel function.
|
2020-04-10 18:06:08 +00:00
|
|
|
// This cancel function is tied to the watch of leaf certs for linked services.
|
|
|
|
// As with WatchedServices, leaf watches will be cancelled when services
|
|
|
|
// are no longer linked to the gateway.
|
2020-06-12 14:57:41 +00:00
|
|
|
WatchedLeaves map[structs.ServiceName]context.CancelFunc
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// ServiceLeaves is a map of ServiceName to a leaf cert.
|
2020-04-10 18:06:08 +00:00
|
|
|
// Terminating gateways will present different certificates depending
|
|
|
|
// on the service that the caller is trying to reach.
|
2020-06-12 14:57:41 +00:00
|
|
|
ServiceLeaves map[structs.ServiceName]*structs.IssuedCert
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
// WatchedConfigs is a map of ServiceName to a cancel function. This cancel
|
|
|
|
// function is tied to the watch of service configs for linked services. As
|
|
|
|
// with WatchedServices, service config watches will be cancelled when
|
|
|
|
// services are no longer linked to the gateway.
|
|
|
|
WatchedConfigs map[structs.ServiceName]context.CancelFunc
|
|
|
|
|
|
|
|
// ServiceConfigs is a map of service name to the resolved service config
|
|
|
|
// for that service.
|
|
|
|
ServiceConfigs map[structs.ServiceName]*structs.ServiceConfigResponse
|
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// WatchedResolvers is a map of ServiceName to a cancel function.
|
2020-04-14 14:59:23 +00:00
|
|
|
// This cancel function is tied to the watch of resolvers for linked services.
|
|
|
|
// As with WatchedServices, resolver watches will be cancelled when services
|
|
|
|
// are no longer linked to the gateway.
|
2020-06-12 14:57:41 +00:00
|
|
|
WatchedResolvers map[structs.ServiceName]context.CancelFunc
|
2020-04-14 14:59:23 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// ServiceResolvers is a map of service name to an associated
|
2020-04-14 14:59:23 +00:00
|
|
|
// service-resolver config entry for that service.
|
2020-08-27 17:20:58 +00:00
|
|
|
ServiceResolvers map[structs.ServiceName]*structs.ServiceResolverConfigEntry
|
|
|
|
ServiceResolversSet map[structs.ServiceName]bool
|
2020-04-14 14:59:23 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// ServiceGroups is a map of service name to the service instances of that
|
2020-04-10 18:06:08 +00:00
|
|
|
// service in the local datacenter.
|
2020-06-12 14:57:41 +00:00
|
|
|
ServiceGroups map[structs.ServiceName]structs.CheckServiceNodes
|
2020-04-27 22:25:37 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// GatewayServices is a map of service name to the config entry association
|
2020-04-27 22:25:37 +00:00
|
|
|
// between the gateway and a service. TLS configuration stored here is
|
|
|
|
// used for TLS origination from the gateway to the linked service.
|
2020-06-12 14:57:41 +00:00
|
|
|
GatewayServices map[structs.ServiceName]structs.GatewayService
|
2020-06-03 21:28:45 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// HostnameServices is a map of service name to service instances with a hostname as the address.
|
2020-06-03 21:28:45 +00:00
|
|
|
// If hostnames are configured they must be provided to Envoy via CDS not EDS.
|
2020-06-12 14:57:41 +00:00
|
|
|
HostnameServices map[structs.ServiceName]structs.CheckServiceNodes
|
2020-04-10 18:06:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
// ValidServices returns the list of service keys that have enough data to be emitted.
|
|
|
|
func (c *configSnapshotTerminatingGateway) ValidServices() []structs.ServiceName {
|
|
|
|
out := make([]structs.ServiceName, 0, len(c.ServiceGroups))
|
|
|
|
for svc := range c.ServiceGroups {
|
|
|
|
// It only counts if ALL of our watches have come back (with data or not).
|
|
|
|
|
|
|
|
// Skip the service if we don't know if there is a resolver or not.
|
|
|
|
if _, ok := c.ServiceResolversSet[svc]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the service if we don't have a cert to present for mTLS.
|
|
|
|
if cert, ok := c.ServiceLeaves[svc]; !ok || cert == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the service if we haven't gotten our intentions yet.
|
|
|
|
if _, intentionsSet := c.Intentions[svc]; !intentionsSet {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the service if we haven't gotten our service config yet to know
|
|
|
|
// the protocol.
|
|
|
|
if _, ok := c.ServiceConfigs[svc]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, svc)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
func (c *configSnapshotTerminatingGateway) IsEmpty() bool {
|
|
|
|
if c == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return len(c.ServiceLeaves) == 0 &&
|
|
|
|
len(c.WatchedLeaves) == 0 &&
|
|
|
|
len(c.WatchedIntentions) == 0 &&
|
2020-08-27 17:20:58 +00:00
|
|
|
len(c.Intentions) == 0 &&
|
2020-04-10 18:06:08 +00:00
|
|
|
len(c.ServiceGroups) == 0 &&
|
2020-04-14 14:59:23 +00:00
|
|
|
len(c.WatchedServices) == 0 &&
|
2020-04-24 00:16:09 +00:00
|
|
|
len(c.ServiceResolvers) == 0 &&
|
2020-08-27 17:20:58 +00:00
|
|
|
len(c.ServiceResolversSet) == 0 &&
|
2020-04-27 22:25:37 +00:00
|
|
|
len(c.WatchedResolvers) == 0 &&
|
2020-08-27 17:20:58 +00:00
|
|
|
len(c.ServiceConfigs) == 0 &&
|
|
|
|
len(c.WatchedConfigs) == 0 &&
|
2020-06-03 21:28:45 +00:00
|
|
|
len(c.GatewayServices) == 0 &&
|
|
|
|
len(c.HostnameServices) == 0
|
2020-04-10 18:06:08 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
type configSnapshotMeshGateway struct {
|
2020-06-12 14:57:41 +00:00
|
|
|
// WatchedServices is a map of service name to a cancel function. This cancel
|
2020-03-09 20:59:02 +00:00
|
|
|
// function is tied to the watch of connect enabled services for the given
|
|
|
|
// id. If the main datacenter services watch would indicate the removal of
|
2020-04-10 18:06:08 +00:00
|
|
|
// a service altogether we then cancel watching that service for its
|
2020-03-09 20:59:02 +00:00
|
|
|
// connect endpoints.
|
2020-06-12 14:57:41 +00:00
|
|
|
WatchedServices map[structs.ServiceName]context.CancelFunc
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
// WatchedServicesSet indicates that the watch on the datacenters services
|
|
|
|
// has completed. Even when there are no connect services, this being set
|
|
|
|
// (and the Connect roots being available) will be enough for the config
|
|
|
|
// snapshot to be considered valid. In the case of Envoy, this allows it to
|
|
|
|
// start its listeners even when no services would be proxied and allow its
|
|
|
|
// health check to pass.
|
2019-10-17 21:46:49 +00:00
|
|
|
WatchedServicesSet bool
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
// WatchedDatacenters is a map of datacenter name to a cancel function.
|
|
|
|
// This cancel function is tied to the watch of mesh-gateway services in
|
|
|
|
// that datacenter.
|
2019-06-18 00:52:01 +00:00
|
|
|
WatchedDatacenters map[string]context.CancelFunc
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// ServiceGroups is a map of service name to the service instances of that
|
2020-03-09 20:59:02 +00:00
|
|
|
// service in the local datacenter.
|
2020-06-12 14:57:41 +00:00
|
|
|
ServiceGroups map[structs.ServiceName]structs.CheckServiceNodes
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
// ServiceResolvers is a map of service name to an associated
|
2020-03-09 20:59:02 +00:00
|
|
|
// service-resolver config entry for that service.
|
2020-06-12 14:57:41 +00:00
|
|
|
ServiceResolvers map[structs.ServiceName]*structs.ServiceResolverConfigEntry
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
// GatewayGroups is a map of datacenter names to services of kind
|
|
|
|
// mesh-gateway in that datacenter.
|
2020-02-19 16:57:55 +00:00
|
|
|
GatewayGroups map[string]structs.CheckServiceNodes
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
// FedStateGateways is a map of datacenter names to mesh gateways in that
|
|
|
|
// datacenter.
|
|
|
|
FedStateGateways map[string]structs.CheckServiceNodes
|
|
|
|
|
|
|
|
// ConsulServers is the list of consul servers in this datacenter.
|
|
|
|
ConsulServers structs.CheckServiceNodes
|
2020-06-03 21:28:45 +00:00
|
|
|
|
|
|
|
// HostnameDatacenters is a map of datacenters to mesh gateway instances with a hostname as the address.
|
|
|
|
// If hostnames are configured they must be provided to Envoy via CDS not EDS.
|
|
|
|
HostnameDatacenters map[string]structs.CheckServiceNodes
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configSnapshotMeshGateway) Datacenters() []string {
|
|
|
|
sz1, sz2 := len(c.GatewayGroups), len(c.FedStateGateways)
|
|
|
|
|
|
|
|
sz := sz1
|
|
|
|
if sz2 > sz1 {
|
|
|
|
sz = sz2
|
|
|
|
}
|
|
|
|
|
|
|
|
dcs := make([]string, 0, sz)
|
2020-06-16 17:19:31 +00:00
|
|
|
for dc := range c.GatewayGroups {
|
2020-03-09 20:59:02 +00:00
|
|
|
dcs = append(dcs, dc)
|
|
|
|
}
|
2020-06-16 17:19:31 +00:00
|
|
|
for dc := range c.FedStateGateways {
|
2020-03-09 20:59:02 +00:00
|
|
|
if _, ok := c.GatewayGroups[dc]; !ok {
|
|
|
|
dcs = append(dcs, dc)
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 16:19:57 +00:00
|
|
|
|
|
|
|
// Always sort the results to ensure we generate deterministic things over
|
|
|
|
// xDS, such as mesh-gateway listener filter chains.
|
|
|
|
sort.Strings(dcs)
|
2020-03-09 20:59:02 +00:00
|
|
|
return dcs
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 21:46:49 +00:00
|
|
|
func (c *configSnapshotMeshGateway) IsEmpty() bool {
|
|
|
|
if c == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return len(c.WatchedServices) == 0 &&
|
|
|
|
!c.WatchedServicesSet &&
|
|
|
|
len(c.WatchedDatacenters) == 0 &&
|
|
|
|
len(c.ServiceGroups) == 0 &&
|
|
|
|
len(c.ServiceResolvers) == 0 &&
|
2020-03-09 20:59:02 +00:00
|
|
|
len(c.GatewayGroups) == 0 &&
|
|
|
|
len(c.FedStateGateways) == 0 &&
|
2020-06-03 21:28:45 +00:00
|
|
|
len(c.ConsulServers) == 0 &&
|
|
|
|
len(c.HostnameDatacenters) == 0
|
2019-10-17 21:46:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
type configSnapshotIngressGateway struct {
|
|
|
|
ConfigSnapshotUpstreams
|
2020-04-27 23:36:20 +00:00
|
|
|
|
2021-09-13 12:03:16 +00:00
|
|
|
// TLSConfig is the gateway-level TLS configuration. Listener/service level
|
2021-08-24 13:09:01 +00:00
|
|
|
// config is preserved in the Listeners map below.
|
|
|
|
TLSConfig structs.GatewayTLSConfig
|
2021-08-17 11:27:31 +00:00
|
|
|
|
2021-08-24 13:09:01 +00:00
|
|
|
// GatewayConfigLoaded is used to determine if we have received the initial
|
|
|
|
// ingress-gateway config entry yet.
|
|
|
|
GatewayConfigLoaded bool
|
2020-04-27 23:36:20 +00:00
|
|
|
|
|
|
|
// Hosts is the list of extra host entries to add to our leaf cert's DNS SANs.
|
|
|
|
Hosts []string
|
|
|
|
HostsSet bool
|
|
|
|
|
|
|
|
// LeafCertWatchCancel is a CancelFunc to use when refreshing this gateway's
|
|
|
|
// leaf cert watch with different parameters.
|
|
|
|
LeafCertWatchCancel context.CancelFunc
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
// Upstreams is a list of upstreams this ingress gateway should serve traffic
|
|
|
|
// to. This is constructed from the ingress-gateway config entry, and uses
|
|
|
|
// the GatewayServices RPC to retrieve them.
|
2020-04-16 23:24:11 +00:00
|
|
|
Upstreams map[IngressListenerKey]structs.Upstreams
|
2021-07-13 12:53:59 +00:00
|
|
|
|
|
|
|
// Listeners is the original listener config from the ingress-gateway config
|
|
|
|
// entry to save us trying to pass fields through Upstreams
|
|
|
|
Listeners map[IngressListenerKey]structs.IngressListener
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configSnapshotIngressGateway) IsEmpty() bool {
|
|
|
|
if c == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return len(c.Upstreams) == 0 &&
|
|
|
|
len(c.DiscoveryChain) == 0 &&
|
|
|
|
len(c.WatchedUpstreams) == 0 &&
|
|
|
|
len(c.WatchedUpstreamEndpoints) == 0
|
|
|
|
}
|
|
|
|
|
2020-04-16 23:24:11 +00:00
|
|
|
type IngressListenerKey struct {
|
|
|
|
Protocol string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *IngressListenerKey) RouteName() string {
|
2020-04-30 14:31:37 +00:00
|
|
|
return fmt.Sprintf("%d", k.Port)
|
2020-04-16 23:24:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 13:09:01 +00:00
|
|
|
func IngressListenerKeyFromGWService(s structs.GatewayService) IngressListenerKey {
|
|
|
|
return IngressListenerKey{Protocol: s.Protocol, Port: s.Port}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IngressListenerKeyFromListener(l structs.IngressListener) IngressListenerKey {
|
|
|
|
return IngressListenerKey{Protocol: l.Protocol, Port: l.Port}
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// ConfigSnapshot captures all the resulting config needed for a proxy instance.
|
|
|
|
// It is meant to be point-in-time coherent and is used to deliver the current
|
|
|
|
// config state to observers who need it to be pushed in (e.g. XDS server).
|
|
|
|
type ConfigSnapshot struct {
|
2020-08-27 17:20:58 +00:00
|
|
|
Kind structs.ServiceKind
|
|
|
|
Service string
|
|
|
|
ProxyID structs.ServiceID
|
|
|
|
Address string
|
|
|
|
Port int
|
|
|
|
ServiceMeta map[string]string
|
|
|
|
TaggedAddresses map[string]structs.ServiceAddress
|
|
|
|
Proxy structs.ConnectProxyConfig
|
|
|
|
Datacenter string
|
|
|
|
IntentionDefaultAllow bool
|
2020-01-24 15:04:58 +00:00
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
ServerSNIFn ServerSNIFunc
|
|
|
|
Roots *structs.IndexedCARoots
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
// connect-proxy specific
|
|
|
|
ConnectProxy configSnapshotConnectProxy
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
// terminating-gateway specific
|
|
|
|
TerminatingGateway configSnapshotTerminatingGateway
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
// mesh-gateway specific
|
|
|
|
MeshGateway configSnapshotMeshGateway
|
2018-10-03 12:36:38 +00:00
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
// ingress-gateway specific
|
|
|
|
IngressGateway configSnapshotIngressGateway
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns whether or not the snapshot has all required fields filled yet.
|
|
|
|
func (s *ConfigSnapshot) Valid() bool {
|
2019-06-24 19:05:36 +00:00
|
|
|
switch s.Kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2021-04-28 22:13:29 +00:00
|
|
|
if s.Proxy.Mode == structs.ProxyModeTransparent && !s.ConnectProxy.MeshConfigSet {
|
2021-04-06 18:19:59 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-08-27 17:20:58 +00:00
|
|
|
return s.Roots != nil &&
|
|
|
|
s.ConnectProxy.Leaf != nil &&
|
|
|
|
s.ConnectProxy.IntentionsSet
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
|
|
|
return s.Roots != nil
|
2020-08-27 17:20:58 +00:00
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
2020-03-09 20:59:02 +00:00
|
|
|
if s.ServiceMeta[structs.MetaWANFederationKey] == "1" {
|
|
|
|
if len(s.MeshGateway.ConsulServers) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 17:20:58 +00:00
|
|
|
return s.Roots != nil &&
|
|
|
|
(s.MeshGateway.WatchedServicesSet || len(s.MeshGateway.ServiceGroups) > 0)
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindIngressGateway:
|
|
|
|
return s.Roots != nil &&
|
2020-04-27 23:36:20 +00:00
|
|
|
s.IngressGateway.Leaf != nil &&
|
2021-08-24 13:09:01 +00:00
|
|
|
s.IngressGateway.GatewayConfigLoaded &&
|
2020-05-07 15:22:57 +00:00
|
|
|
s.IngressGateway.HostsSet
|
2019-06-24 19:05:36 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone makes a deep copy of the snapshot we can send to other goroutines
|
|
|
|
// without worrying that they will racily read or mutate shared maps etc.
|
|
|
|
func (s *ConfigSnapshot) Clone() (*ConfigSnapshot, error) {
|
|
|
|
snapCopy, err := copystructure.Copy(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
snap := snapCopy.(*ConfigSnapshot)
|
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
// nil these out as anything receiving one of these clones does not need them and should never "cancel" our watches
|
2019-06-18 00:52:01 +00:00
|
|
|
switch s.Kind {
|
2019-07-02 03:10:51 +00:00
|
|
|
case structs.ServiceKindConnectProxy:
|
|
|
|
snap.ConnectProxy.WatchedUpstreams = nil
|
2019-08-05 18:30:35 +00:00
|
|
|
snap.ConnectProxy.WatchedGateways = nil
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
|
|
|
snap.TerminatingGateway.WatchedServices = nil
|
|
|
|
snap.TerminatingGateway.WatchedIntentions = nil
|
|
|
|
snap.TerminatingGateway.WatchedLeaves = nil
|
2020-08-27 17:20:58 +00:00
|
|
|
snap.TerminatingGateway.WatchedConfigs = nil
|
|
|
|
snap.TerminatingGateway.WatchedResolvers = nil
|
2019-06-18 00:52:01 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
|
|
|
snap.MeshGateway.WatchedDatacenters = nil
|
|
|
|
snap.MeshGateway.WatchedServices = nil
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindIngressGateway:
|
|
|
|
snap.IngressGateway.WatchedUpstreams = nil
|
2020-05-07 15:22:57 +00:00
|
|
|
snap.IngressGateway.WatchedGateways = nil
|
2020-04-16 21:00:48 +00:00
|
|
|
snap.IngressGateway.WatchedDiscoveryChains = nil
|
2020-05-07 15:22:57 +00:00
|
|
|
snap.IngressGateway.LeafCertWatchCancel = nil
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return snap, nil
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
2020-04-16 21:00:48 +00:00
|
|
|
|
|
|
|
func (s *ConfigSnapshot) Leaf() *structs.IssuedCert {
|
|
|
|
switch s.Kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
|
|
|
return s.ConnectProxy.Leaf
|
|
|
|
case structs.ServiceKindIngressGateway:
|
|
|
|
return s.IngressGateway.Leaf
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|