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"
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
type configSnapshotConnectProxy struct {
|
|
|
|
ConfigSnapshotUpstreams
|
|
|
|
|
|
|
|
WatchedServiceChecks map[structs.ServiceID][]structs.CheckType // TODO: missing garbage collection
|
|
|
|
PreparedQueryEndpoints map[string]structs.CheckServiceNodes // DEPRECATED:see:WatchedUpstreamEndpoints
|
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 &&
|
|
|
|
len(c.DiscoveryChain) == 0 &&
|
|
|
|
len(c.WatchedUpstreams) == 0 &&
|
|
|
|
len(c.WatchedUpstreamEndpoints) == 0 &&
|
|
|
|
len(c.WatchedGateways) == 0 &&
|
|
|
|
len(c.WatchedGatewayEndpoints) == 0 &&
|
|
|
|
len(c.WatchedServiceChecks) == 0 &&
|
2020-01-24 15:04:58 +00:00
|
|
|
len(c.PreparedQueryEndpoints) == 0
|
2019-10-17 21:46:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
type configSnapshotTerminatingGateway struct {
|
|
|
|
// WatchedServices is a map of service id to a cancel function. This cancel
|
|
|
|
// 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.
|
|
|
|
WatchedServices map[structs.ServiceID]context.CancelFunc
|
|
|
|
|
|
|
|
// WatchedIntentions is a map of service id to a cancel function.
|
|
|
|
// 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.
|
|
|
|
WatchedIntentions map[structs.ServiceID]context.CancelFunc
|
|
|
|
|
|
|
|
// WatchedLeaves is a map of ServiceID to a cancel function.
|
|
|
|
// 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.
|
|
|
|
WatchedLeaves map[structs.ServiceID]context.CancelFunc
|
|
|
|
|
|
|
|
// ServiceLeaves is a map of ServiceID to a leaf cert.
|
|
|
|
// Terminating gateways will present different certificates depending
|
|
|
|
// on the service that the caller is trying to reach.
|
|
|
|
ServiceLeaves map[structs.ServiceID]*structs.IssuedCert
|
|
|
|
|
2020-04-14 14:59:23 +00:00
|
|
|
// WatchedResolvers is a map of ServiceID to a cancel function.
|
|
|
|
// 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.
|
|
|
|
WatchedResolvers map[structs.ServiceID]context.CancelFunc
|
|
|
|
|
|
|
|
// ServiceResolvers is a map of service id to an associated
|
|
|
|
// service-resolver config entry for that service.
|
|
|
|
ServiceResolvers map[structs.ServiceID]*structs.ServiceResolverConfigEntry
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
// ServiceGroups is a map of service id to the service instances of that
|
|
|
|
// service in the local datacenter.
|
|
|
|
ServiceGroups map[structs.ServiceID]structs.CheckServiceNodes
|
2020-04-27 22:25:37 +00:00
|
|
|
|
|
|
|
// GatewayServices is a map of service id to the config entry association
|
|
|
|
// between the gateway and a service. TLS configuration stored here is
|
|
|
|
// used for TLS origination from the gateway to the linked service.
|
|
|
|
GatewayServices map[structs.ServiceID]structs.GatewayService
|
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 &&
|
|
|
|
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-04-27 22:25:37 +00:00
|
|
|
len(c.WatchedResolvers) == 0 &&
|
|
|
|
len(c.GatewayServices) == 0
|
2020-04-10 18:06:08 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
type configSnapshotMeshGateway struct {
|
2020-03-09 20:59:02 +00:00
|
|
|
// WatchedServices is a map of service id to a cancel function. This cancel
|
|
|
|
// 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-02-19 16:57:55 +00:00
|
|
|
WatchedServices map[structs.ServiceID]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
|
|
|
|
|
|
|
// ServiceGroups is a map of service id to the service instances of that
|
|
|
|
// service in the local datacenter.
|
2020-02-19 16:57:55 +00:00
|
|
|
ServiceGroups map[structs.ServiceID]structs.CheckServiceNodes
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
// ServiceResolvers is a map of service id to an associated
|
|
|
|
// service-resolver config entry for that service.
|
2020-02-19 16:57:55 +00:00
|
|
|
ServiceResolvers map[structs.ServiceID]*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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
for dc, _ := range c.GatewayGroups {
|
|
|
|
dcs = append(dcs, dc)
|
|
|
|
}
|
|
|
|
for dc, _ := range c.FedStateGateways {
|
|
|
|
if _, ok := c.GatewayGroups[dc]; !ok {
|
|
|
|
dcs = append(dcs, dc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 &&
|
|
|
|
len(c.ConsulServers) == 0
|
2019-10-17 21:46:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
type configSnapshotIngressGateway struct {
|
|
|
|
ConfigSnapshotUpstreams
|
|
|
|
// 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
|
2020-04-16 21:00:48 +00:00
|
|
|
|
|
|
|
// WatchedDiscoveryChains is a map of upstream.Identifier() -> CancelFunc's
|
|
|
|
// in order to cancel any watches when the ingress gateway configuration is
|
|
|
|
// changed. Ingress gateways need this because discovery chain watches are
|
|
|
|
// added and removed through the lifecycle of single proxycfg.state instance.
|
|
|
|
WatchedDiscoveryChains map[string]context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configSnapshotIngressGateway) IsEmpty() bool {
|
|
|
|
if c == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return len(c.Upstreams) == 0 &&
|
|
|
|
len(c.DiscoveryChain) == 0 &&
|
|
|
|
len(c.WatchedDiscoveryChains) == 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
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-18 00:52:01 +00:00
|
|
|
Kind structs.ServiceKind
|
|
|
|
Service string
|
2020-01-24 15:04:58 +00:00
|
|
|
ProxyID structs.ServiceID
|
2019-06-18 00:52:01 +00:00
|
|
|
Address string
|
|
|
|
Port int
|
2020-03-09 20:59:02 +00:00
|
|
|
ServiceMeta map[string]string
|
2019-06-18 00:52:01 +00:00
|
|
|
TaggedAddresses map[string]structs.ServiceAddress
|
|
|
|
Proxy structs.ConnectProxyConfig
|
|
|
|
Datacenter string
|
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
|
|
|
// Skip intentions for now as we don't push those down yet, just pre-warm them.
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2019-06-18 00:52:01 +00:00
|
|
|
return s.Roots != nil && s.ConnectProxy.Leaf != nil
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
|
|
|
return s.Roots != nil
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 21:55:13 +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 &&
|
|
|
|
s.IngressGateway.Leaf != nil
|
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
|
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
|
|
|
|
snap.IngressGateway.WatchedDiscoveryChains = 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
|
|
|
|
}
|
|
|
|
}
|