2023-01-06 17:13:40 +00:00
|
|
|
package builtinextensiontemplate
|
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"
|
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"
|
2022-03-31 20:24:46 +00:00
|
|
|
|
2022-03-15 14:07:40 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/xdscommon"
|
2022-03-31 20:24:46 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2022-03-15 14:07:40 +00:00
|
|
|
)
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
type EnvoyExtension struct {
|
|
|
|
Constructor PluginConstructor
|
|
|
|
plugin Plugin
|
|
|
|
ready bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ xdscommon.EnvoyExtension = (*EnvoyExtension)(nil)
|
|
|
|
|
|
|
|
// Validate ensures the data in ExtensionConfiguration can successfuly be used
|
|
|
|
// to apply the specified Envoy extension.
|
|
|
|
func (envoyExtension *EnvoyExtension) Validate(config xdscommon.ExtensionConfiguration) error {
|
|
|
|
plugin, err := envoyExtension.Constructor(config)
|
|
|
|
|
|
|
|
envoyExtension.plugin = plugin
|
|
|
|
envoyExtension.ready = err == nil
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-21 06:26:20 +00:00
|
|
|
// Extend updates indexed xDS structures to include patches for
|
2023-01-06 17:13:40 +00:00
|
|
|
// built-in extensions. It is responsible for applying Plugins to
|
|
|
|
// the the appropriate xDS resources. If any portion of this function fails,
|
|
|
|
// it will attempt continue and return an error. The caller can then determine
|
|
|
|
// if it is better to use a partially applied extension or error out.
|
|
|
|
func (envoyExtension *EnvoyExtension) Extend(resources *xdscommon.IndexedResources, config xdscommon.ExtensionConfiguration) (*xdscommon.IndexedResources, error) {
|
|
|
|
if !envoyExtension.ready {
|
|
|
|
panic("envoy extension used without being properly constructed")
|
|
|
|
}
|
|
|
|
|
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-06 17:13:40 +00:00
|
|
|
if !envoyExtension.plugin.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.ClusterType,
|
|
|
|
xdscommon.ListenerType,
|
2022-04-01 14:32:38 +00:00
|
|
|
xdscommon.RouteType,
|
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-06 17:13:40 +00:00
|
|
|
newCluster, patched, err := envoyExtension.plugin.PatchCluster(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-06 17:13:40 +00:00
|
|
|
newListener, patched, err := envoyExtension.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
|
|
|
|
// name must match the upstream service's SNI.
|
|
|
|
if config.IsUpstream() && !config.MatchesUpstreamServiceSNI(nameOrSNI) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// There aren't routes for inbound services.
|
|
|
|
if !config.IsUpstream() {
|
2022-04-01 14:32:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
newRoute, patched, err := envoyExtension.plugin.PatchRoute(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-06 17:13:40 +00:00
|
|
|
func (envoyExtension EnvoyExtension) patchListener(config xdscommon.ExtensionConfiguration, 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-06 17:13:40 +00:00
|
|
|
func (envoyExtension EnvoyExtension) patchTerminatingGatewayListener(config xdscommon.ExtensionConfiguration, l *envoy_listener_v3.Listener) (proto.Message, bool, error) {
|
|
|
|
// 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-06 17:13:40 +00:00
|
|
|
newFilter, ok, err := envoyExtension.plugin.PatchFilter(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)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
filters = append(filters, newFilter)
|
|
|
|
patched = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filterChain.Filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, patched, resultErr
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
func (envoyExtension EnvoyExtension) patchConnectProxyListener(config xdscommon.ExtensionConfiguration, 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-06 17:13:40 +00:00
|
|
|
// If the Envoy extension configuration is for an upstream service, the listener's
|
|
|
|
// name must match the upstream service's EnvoyID.
|
|
|
|
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-06 17:13:40 +00:00
|
|
|
newFilter, ok, err := envoyExtension.plugin.PatchFilter(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
filters = append(filters, newFilter)
|
|
|
|
patched = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filterChain.Filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, patched, resultErr
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|