2022-03-15 14:07:40 +00:00
|
|
|
package serverlessplugin
|
|
|
|
|
|
|
|
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/golang/protobuf/proto"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-12-21 06:26:20 +00:00
|
|
|
// Extend updates indexed xDS structures to include patches for
|
2022-03-31 20:24:46 +00:00
|
|
|
// serverless integrations. It is responsible for constructing all of the
|
|
|
|
// patchers and forwarding xDS structs onto the appropriate patcher. If any
|
|
|
|
// portion of this function fails, it will record the error and continue. The
|
|
|
|
// behavior is appropriate since the unpatched xDS structures this receives are
|
|
|
|
// typically invalid.
|
2022-12-21 06:26:20 +00:00
|
|
|
func Extend(resources *xdscommon.IndexedResources, config xdscommon.ExtensionConfiguration) (*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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.IsUpstream() {
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
patcher := makePatcher(config)
|
|
|
|
if patcher == nil {
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !patcher.CanPatch(config.Kind) {
|
|
|
|
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:
|
2022-12-21 06:26:20 +00:00
|
|
|
if !config.MatchesUpstreamServiceSNI(nameOrSNI) {
|
2022-03-31 20:24:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newCluster, patched, err := patcher.PatchCluster(resource)
|
|
|
|
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:
|
2022-12-21 06:26:20 +00:00
|
|
|
newListener, patched, err := patchListener(config, resource, patcher)
|
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:
|
2022-12-21 06:26:20 +00:00
|
|
|
if !config.MatchesUpstreamServiceSNI(nameOrSNI) {
|
2022-04-01 14:32:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newRoute, patched, err := patcher.PatchRoute(resource)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-21 06:26:20 +00:00
|
|
|
func patchListener(config xdscommon.ExtensionConfiguration, l *envoy_listener_v3.Listener, p patcher) (proto.Message, bool, error) {
|
2022-05-05 20:39:39 +00:00
|
|
|
switch config.Kind {
|
|
|
|
case api.ServiceKindTerminatingGateway:
|
2022-12-21 06:26:20 +00:00
|
|
|
return patchTerminatingGatewayListener(config, l, p)
|
2022-05-05 20:39:39 +00:00
|
|
|
case api.ServiceKindConnectProxy:
|
2022-12-21 06:26:20 +00:00
|
|
|
return patchConnectProxyListener(config, l, p)
|
2022-05-05 20:39:39 +00:00
|
|
|
}
|
|
|
|
return l, false, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 06:26:20 +00:00
|
|
|
func patchTerminatingGatewayListener(config xdscommon.ExtensionConfiguration, l *envoy_listener_v3.Listener, p patcher) (proto.Message, bool, error) {
|
2022-03-31 20:24:46 +00:00
|
|
|
var resultErr error
|
|
|
|
patched := false
|
|
|
|
for _, filterChain := range l.FilterChains {
|
|
|
|
sni := getSNI(filterChain)
|
|
|
|
|
|
|
|
if sni == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-12-21 06:26:20 +00:00
|
|
|
newFilter, ok, err := p.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
|
|
|
|
}
|
|
|
|
|
2022-12-21 06:26:20 +00:00
|
|
|
func patchConnectProxyListener(config xdscommon.ExtensionConfiguration, l *envoy_listener_v3.Listener, p patcher) (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]
|
|
|
|
}
|
|
|
|
|
2022-12-21 06:26:20 +00:00
|
|
|
if envoyID != config.EnvoyID() {
|
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 {
|
2022-12-21 06:26:20 +00:00
|
|
|
newFilter, ok, err := p.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
|
|
|
}
|