2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
package extensioncommon
|
2022-03-15 14:07:40 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-31 20:24:46 +00:00
|
|
|
"fmt"
|
2022-05-05 20:39:39 +00:00
|
|
|
"strings"
|
2022-03-31 20:24:46 +00:00
|
|
|
|
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
2022-04-01 14:32:38 +00:00
|
|
|
envoy_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
2023-01-27 19:43:16 +00:00
|
|
|
envoy_tcp_proxy_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/tcp_proxy/v3"
|
|
|
|
envoy_resource_v3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
|
2022-03-31 20:24:46 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-01-27 19:43:16 +00:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2022-03-31 20:24:46 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2023-02-06 17:14:35 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/xdscommon"
|
2022-03-15 14:07:40 +00:00
|
|
|
)
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// BasicExtension is the interface that each user of BasicEnvoyExtender must implement. It
|
|
|
|
// is responsible for modifying the xDS structures based on only the state of
|
|
|
|
// the extension.
|
|
|
|
type BasicExtension interface {
|
|
|
|
// CanApply determines if the extension can mutate resources for the given xdscommon.ExtensionConfiguration.
|
|
|
|
CanApply(*RuntimeConfig) bool
|
|
|
|
|
|
|
|
// PatchRoute patches a route to include the custom Envoy configuration
|
|
|
|
// required to integrate with the built in extension template.
|
|
|
|
PatchRoute(*RuntimeConfig, *envoy_route_v3.RouteConfiguration) (*envoy_route_v3.RouteConfiguration, bool, error)
|
2023-01-06 17:13:40 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// PatchCluster patches a cluster to include the custom Envoy configuration
|
|
|
|
// required to integrate with the built in extension template.
|
|
|
|
PatchCluster(*RuntimeConfig, *envoy_cluster_v3.Cluster) (*envoy_cluster_v3.Cluster, bool, error)
|
2023-01-06 17:13:40 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// PatchFilter patches an Envoy filter to include the custom Envoy
|
|
|
|
// configuration required to integrate with the built in extension template.
|
|
|
|
PatchFilter(*RuntimeConfig, *envoy_listener_v3.Filter) (*envoy_listener_v3.Filter, bool, error)
|
|
|
|
}
|
2023-01-06 17:13:40 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
var _ EnvoyExtender = (*BasicEnvoyExtender)(nil)
|
2023-01-06 17:13:40 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// BasicEnvoyExtender provides convenience functions for iterating and applying modifications
|
|
|
|
// to Envoy resources.
|
|
|
|
type BasicEnvoyExtender struct {
|
|
|
|
Extension BasicExtension
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (envoyExtension *BasicEnvoyExtender) Validate(config *RuntimeConfig) error {
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-06 17:13:40 +00:00
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (envoyExtender *BasicEnvoyExtender) Extend(resources *xdscommon.IndexedResources, config *RuntimeConfig) (*xdscommon.IndexedResources, error) {
|
2022-03-31 20:24:46 +00:00
|
|
|
var resultErr error
|
|
|
|
|
2022-05-05 20:39:39 +00:00
|
|
|
switch config.Kind {
|
|
|
|
case api.ServiceKindTerminatingGateway, api.ServiceKindConnectProxy:
|
|
|
|
default:
|
2022-12-21 06:26:20 +00:00
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
if !envoyExtender.Extension.CanApply(config) {
|
2022-12-21 06:26:20 +00:00
|
|
|
return resources, nil
|
2022-03-31 20:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, indexType := range []string{
|
|
|
|
xdscommon.ListenerType,
|
2022-04-01 14:32:38 +00:00
|
|
|
xdscommon.RouteType,
|
2023-01-27 19:43:16 +00:00
|
|
|
xdscommon.ClusterType,
|
2022-03-31 20:24:46 +00:00
|
|
|
} {
|
|
|
|
for nameOrSNI, msg := range resources.Index[indexType] {
|
|
|
|
switch resource := msg.(type) {
|
|
|
|
case *envoy_cluster_v3.Cluster:
|
2023-01-06 17:13:40 +00:00
|
|
|
// If the Envoy extension configuration is for an upstream service, the Cluster's
|
|
|
|
// name must match the upstream service's SNI.
|
|
|
|
if config.IsUpstream() && !config.MatchesUpstreamServiceSNI(nameOrSNI) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the extension's config is for an an inbound listener, the Cluster's name
|
|
|
|
// must be xdscommon.LocalAppClusterName.
|
|
|
|
if !config.IsUpstream() && nameOrSNI == xdscommon.LocalAppClusterName {
|
2022-03-31 20:24:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
newCluster, patched, err := envoyExtender.Extension.PatchCluster(config, resource)
|
2022-03-31 20:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("error patching cluster: %w", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if patched {
|
|
|
|
resources.Index[xdscommon.ClusterType][nameOrSNI] = newCluster
|
|
|
|
}
|
|
|
|
|
|
|
|
case *envoy_listener_v3.Listener:
|
2023-01-30 21:35:26 +00:00
|
|
|
newListener, patched, err := envoyExtender.patchListener(config, resource)
|
2022-03-31 20:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("error patching listener: %w", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if patched {
|
|
|
|
resources.Index[xdscommon.ListenerType][nameOrSNI] = newListener
|
|
|
|
}
|
|
|
|
|
2022-04-01 14:32:38 +00:00
|
|
|
case *envoy_route_v3.RouteConfiguration:
|
2023-01-06 17:13:40 +00:00
|
|
|
// If the Envoy extension configuration is for an upstream service, the route's
|
2023-01-27 19:43:16 +00:00
|
|
|
// name must match the upstream service's Envoy ID.
|
|
|
|
matchesEnvoyID := config.EnvoyID() == nameOrSNI
|
|
|
|
if config.IsUpstream() && !config.MatchesUpstreamServiceSNI(nameOrSNI) && !matchesEnvoyID {
|
2023-01-06 17:13:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// There aren't routes for inbound services.
|
|
|
|
if !config.IsUpstream() {
|
2022-04-01 14:32:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
newRoute, patched, err := envoyExtender.Extension.PatchRoute(config, resource)
|
2022-04-01 14:32:38 +00:00
|
|
|
if err != nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("error patching route: %w", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if patched {
|
|
|
|
resources.Index[xdscommon.RouteType][nameOrSNI] = newRoute
|
|
|
|
}
|
2022-03-31 20:24:46 +00:00
|
|
|
default:
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("unsupported type was skipped: %T", resource))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources, resultErr
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (envoyExtension BasicEnvoyExtender) patchListener(config *RuntimeConfig, l *envoy_listener_v3.Listener) (proto.Message, bool, error) {
|
2022-05-05 20:39:39 +00:00
|
|
|
switch config.Kind {
|
|
|
|
case api.ServiceKindTerminatingGateway:
|
2023-01-06 17:13:40 +00:00
|
|
|
return envoyExtension.patchTerminatingGatewayListener(config, l)
|
2022-05-05 20:39:39 +00:00
|
|
|
case api.ServiceKindConnectProxy:
|
2023-01-06 17:13:40 +00:00
|
|
|
return envoyExtension.patchConnectProxyListener(config, l)
|
2022-05-05 20:39:39 +00:00
|
|
|
}
|
|
|
|
return l, false, nil
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (b BasicEnvoyExtender) patchTerminatingGatewayListener(config *RuntimeConfig, l *envoy_listener_v3.Listener) (proto.Message, bool, error) {
|
2023-01-06 17:13:40 +00:00
|
|
|
// We don't support directly targeting terminating gateways with extensions.
|
|
|
|
if !config.IsUpstream() {
|
|
|
|
return l, false, nil
|
|
|
|
}
|
|
|
|
|
2022-03-31 20:24:46 +00:00
|
|
|
var resultErr error
|
|
|
|
patched := false
|
|
|
|
for _, filterChain := range l.FilterChains {
|
|
|
|
sni := getSNI(filterChain)
|
|
|
|
|
|
|
|
if sni == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
// The filter chain's SNI must match the upstream service's SNI.
|
2022-12-21 06:26:20 +00:00
|
|
|
if !config.MatchesUpstreamServiceSNI(sni) {
|
2022-03-31 20:24:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var filters []*envoy_listener_v3.Filter
|
|
|
|
|
|
|
|
for _, filter := range filterChain.Filters {
|
2023-01-30 21:35:26 +00:00
|
|
|
newFilter, ok, err := b.Extension.PatchFilter(config, filter)
|
2022-03-31 20:24:46 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("error patching listener filter: %w", err))
|
|
|
|
filters = append(filters, filter)
|
2023-01-31 16:49:45 +00:00
|
|
|
continue
|
2022-03-31 20:24:46 +00:00
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
filters = append(filters, newFilter)
|
|
|
|
patched = true
|
2023-01-31 16:49:45 +00:00
|
|
|
} else {
|
|
|
|
filters = append(filters, filter)
|
2022-03-31 20:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
filterChain.Filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, patched, resultErr
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (b BasicEnvoyExtender) patchConnectProxyListener(config *RuntimeConfig, l *envoy_listener_v3.Listener) (proto.Message, bool, error) {
|
2022-05-05 20:39:39 +00:00
|
|
|
var resultErr error
|
|
|
|
|
|
|
|
envoyID := ""
|
|
|
|
if i := strings.IndexByte(l.Name, ':'); i != -1 {
|
|
|
|
envoyID = l.Name[:i]
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:43:16 +00:00
|
|
|
if config.IsUpstream() && envoyID == xdscommon.OutboundListenerName {
|
2023-01-30 21:35:26 +00:00
|
|
|
return b.patchTProxyListener(config, l)
|
2023-01-27 19:43:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
// If the Envoy extension configuration is for an upstream service, the listener's
|
2023-01-27 19:43:16 +00:00
|
|
|
// name must match the upstream service's EnvoyID or be the outbound listener.
|
2023-01-06 17:13:40 +00:00
|
|
|
if config.IsUpstream() && envoyID != config.EnvoyID() {
|
|
|
|
return l, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the Envoy extension configuration is for inbound resources, the
|
|
|
|
// listener must be named xdscommon.PublicListenerName.
|
|
|
|
if !config.IsUpstream() && envoyID != xdscommon.PublicListenerName {
|
2022-05-05 20:39:39 +00:00
|
|
|
return l, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var patched bool
|
|
|
|
|
|
|
|
for _, filterChain := range l.FilterChains {
|
|
|
|
var filters []*envoy_listener_v3.Filter
|
|
|
|
|
|
|
|
for _, filter := range filterChain.Filters {
|
2023-01-30 21:35:26 +00:00
|
|
|
newFilter, ok, err := b.Extension.PatchFilter(config, filter)
|
2023-01-27 19:43:16 +00:00
|
|
|
if err != nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("error patching listener filter: %w", err))
|
|
|
|
filters = append(filters, filter)
|
2023-01-31 16:49:45 +00:00
|
|
|
continue
|
2023-01-27 19:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
filters = append(filters, newFilter)
|
|
|
|
patched = true
|
2023-01-31 16:49:45 +00:00
|
|
|
} else {
|
|
|
|
filters = append(filters, filter)
|
2023-01-27 19:43:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
filterChain.Filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, patched, resultErr
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (b BasicEnvoyExtender) patchTProxyListener(config *RuntimeConfig, l *envoy_listener_v3.Listener) (proto.Message, bool, error) {
|
2023-01-27 19:43:16 +00:00
|
|
|
var resultErr error
|
|
|
|
patched := false
|
|
|
|
|
|
|
|
vip := config.Upstreams[config.ServiceName].VIP
|
|
|
|
|
|
|
|
for _, filterChain := range l.FilterChains {
|
|
|
|
var filters []*envoy_listener_v3.Filter
|
|
|
|
|
|
|
|
match := filterChainTProxyMatch(vip, filterChain)
|
|
|
|
if !match {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filter := range filterChain.Filters {
|
2023-01-30 21:35:26 +00:00
|
|
|
newFilter, ok, err := b.Extension.PatchFilter(config, filter)
|
2022-05-05 20:39:39 +00:00
|
|
|
if err != nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("error patching listener filter: %w", err))
|
|
|
|
filters = append(filters, filter)
|
2023-01-31 16:49:45 +00:00
|
|
|
continue
|
2022-05-05 20:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
filters = append(filters, newFilter)
|
|
|
|
patched = true
|
2023-01-31 16:49:45 +00:00
|
|
|
} else {
|
|
|
|
filters = append(filters, filter)
|
2022-05-05 20:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
filterChain.Filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, patched, resultErr
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:43:16 +00:00
|
|
|
func filterChainTProxyMatch(vip string, filterChain *envoy_listener_v3.FilterChain) bool {
|
|
|
|
for _, prefixRange := range filterChain.FilterChainMatch.PrefixRanges {
|
|
|
|
// Since we always set the address prefix as the full VIP (rather than a prefix), we can just check if they are
|
|
|
|
// equal to find the matching filter chain.
|
|
|
|
if vip == prefixRange.AddressPrefix {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterClusterNames(filter *envoy_listener_v3.Filter) map[string]struct{} {
|
|
|
|
clusterNames := make(map[string]struct{})
|
|
|
|
if filter == nil {
|
|
|
|
return clusterNames
|
|
|
|
}
|
|
|
|
|
|
|
|
if config := envoy_resource_v3.GetHTTPConnectionManager(filter); config != nil {
|
|
|
|
// If it's using RDS, the cluster names will be in the route, rather than in the http filter's route config, so
|
|
|
|
// we don't return any cluster names in this case. They can be gathered from the route.
|
|
|
|
if config.GetRds() != nil {
|
|
|
|
return clusterNames
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := config.GetRouteConfig()
|
|
|
|
|
|
|
|
clusterNames = RouteClusterNames(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config := GetTCPProxy(filter); config != nil {
|
|
|
|
clusterNames[config.GetCluster()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clusterNames
|
|
|
|
}
|
|
|
|
|
|
|
|
func RouteClusterNames(route *envoy_route_v3.RouteConfiguration) map[string]struct{} {
|
|
|
|
if route == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterNames := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, virtualHost := range route.VirtualHosts {
|
|
|
|
for _, route := range virtualHost.Routes {
|
|
|
|
r := route.GetRoute()
|
|
|
|
if r == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c := r.GetCluster(); c != "" {
|
|
|
|
clusterNames[r.GetCluster()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wc := r.GetWeightedClusters(); wc != nil {
|
|
|
|
for _, c := range wc.GetClusters() {
|
|
|
|
if c.Name != "" {
|
|
|
|
clusterNames[c.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clusterNames
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTCPProxy(filter *envoy_listener_v3.Filter) *envoy_tcp_proxy_v3.TcpProxy {
|
|
|
|
if typedConfig := filter.GetTypedConfig(); typedConfig != nil {
|
|
|
|
config := &envoy_tcp_proxy_v3.TcpProxy{}
|
|
|
|
if err := anypb.UnmarshalTo(typedConfig, config, proto.UnmarshalOptions{}); err == nil {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-31 20:24:46 +00:00
|
|
|
func getSNI(chain *envoy_listener_v3.FilterChain) string {
|
|
|
|
var sni string
|
|
|
|
|
|
|
|
if chain == nil {
|
|
|
|
return sni
|
|
|
|
}
|
|
|
|
|
|
|
|
if chain.FilterChainMatch == nil {
|
|
|
|
return sni
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(chain.FilterChainMatch.ServerNames) == 0 {
|
|
|
|
return sni
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain.FilterChainMatch.ServerNames[0]
|
2022-03-15 14:07:40 +00:00
|
|
|
}
|