2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
package extensioncommon
|
2022-03-31 20:24:46 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-23 11:55:06 +00:00
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
2022-03-31 20:24:46 +00:00
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
2023-05-23 11:55:06 +00:00
|
|
|
envoy_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
|
2022-03-31 20:24:46 +00:00
|
|
|
envoy_http_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
|
|
|
|
envoy_tls_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
|
2023-05-23 11:55:06 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/xdscommon"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-04-06 21:12:07 +00:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2023-05-23 11:55:06 +00:00
|
|
|
"strings"
|
2022-03-31 20:24:46 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
// MakeUpstreamTLSTransportSocket generates an Envoy transport socket for the given TLS context.
|
|
|
|
func MakeUpstreamTLSTransportSocket(tlsContext *envoy_tls_v3.UpstreamTlsContext) (*envoy_core_v3.TransportSocket, error) {
|
2022-03-31 20:24:46 +00:00
|
|
|
if tlsContext == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-04-06 21:12:07 +00:00
|
|
|
return MakeTransportSocket("tls", tlsContext)
|
2022-03-31 20:24:46 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
// MakeTransportSocket generates an Envoy transport socket from the given proto message.
|
|
|
|
func MakeTransportSocket(name string, config proto.Message) (*envoy_core_v3.TransportSocket, error) {
|
2023-01-11 14:39:10 +00:00
|
|
|
any, err := anypb.New(config)
|
2022-03-31 20:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &envoy_core_v3.TransportSocket{
|
|
|
|
Name: name,
|
|
|
|
ConfigType: &envoy_core_v3.TransportSocket_TypedConfig{
|
|
|
|
TypedConfig: any,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
// MakeEnvoyHTTPFilter generates an Envoy HTTP filter from the given proto message.
|
|
|
|
func MakeEnvoyHTTPFilter(name string, cfg proto.Message) (*envoy_http_v3.HttpFilter, error) {
|
2023-01-11 14:39:10 +00:00
|
|
|
any, err := anypb.New(cfg)
|
2022-03-31 20:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &envoy_http_v3.HttpFilter{
|
|
|
|
Name: name,
|
|
|
|
ConfigType: &envoy_http_v3.HttpFilter_TypedConfig{TypedConfig: any},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
// MakeFilter generates an Envoy listener filter from the given proto message.
|
|
|
|
func MakeFilter(name string, cfg proto.Message) (*envoy_listener_v3.Filter, error) {
|
2023-01-11 14:39:10 +00:00
|
|
|
any, err := anypb.New(cfg)
|
2022-03-31 20:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &envoy_listener_v3.Filter{
|
|
|
|
Name: name,
|
|
|
|
ConfigType: &envoy_listener_v3.Filter_TypedConfig{TypedConfig: any},
|
|
|
|
}, nil
|
|
|
|
}
|
2023-05-23 11:55:06 +00:00
|
|
|
|
|
|
|
// GetListenerEnvoyID returns the Envoy ID string parsed from the name of the given Listener. If none is found, it
|
|
|
|
// returns the empty string.
|
|
|
|
func GetListenerEnvoyID(l *envoy_listener_v3.Listener) string {
|
|
|
|
if id, _, found := strings.Cut(l.Name, ":"); found {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLocalAppCluster returns true if the given Cluster represents the local Cluster, which receives inbound traffic to
|
|
|
|
// the local proxy.
|
|
|
|
func IsLocalAppCluster(c *envoy_cluster_v3.Cluster) bool {
|
|
|
|
return c.Name == xdscommon.LocalAppClusterName
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRouteToLocalAppCluster takes a RouteConfiguration and returns true if all routes within it target the local
|
|
|
|
// Cluster. Note that because we currently target RouteConfiguration in PatchRoute, we have to check multiple individual
|
|
|
|
// Route resources.
|
|
|
|
func IsRouteToLocalAppCluster(r *envoy_route_v3.RouteConfiguration) bool {
|
|
|
|
clusterNames := RouteClusterNames(r)
|
|
|
|
_, match := clusterNames[xdscommon.LocalAppClusterName]
|
|
|
|
|
|
|
|
return match && len(clusterNames) == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInboundPublicListener returns true if the given Listener represents the inbound public Listener for the local
|
|
|
|
// service.
|
|
|
|
func IsInboundPublicListener(l *envoy_listener_v3.Listener) bool {
|
|
|
|
return GetListenerEnvoyID(l) == xdscommon.PublicListenerName
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOutboundTProxyListener returns true if the given Listener represents the outbound TProxy Listener for the local
|
|
|
|
// service.
|
|
|
|
func IsOutboundTProxyListener(l *envoy_listener_v3.Listener) bool {
|
|
|
|
return GetListenerEnvoyID(l) == xdscommon.OutboundListenerName
|
|
|
|
}
|