2022-07-13 15:33:48 +00:00
|
|
|
package internal
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2021-02-16 15:17:54 +00:00
|
|
|
"time"
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2022-11-07 16:34:30 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2021-02-16 15:17:54 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2020-09-09 20:37:43 +00:00
|
|
|
|
2022-10-11 22:00:32 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2022-11-07 16:34:30 +00:00
|
|
|
|
2022-10-11 22:00:32 +00:00
|
|
|
agentmiddleware "github.com/hashicorp/consul/agent/grpc-middleware"
|
2020-09-09 20:37:43 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
2021-08-24 21:28:44 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2020-09-09 20:37:43 +00:00
|
|
|
)
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// ClientConnPool creates and stores a connection for each datacenter.
|
|
|
|
type ClientConnPool struct {
|
2021-08-24 21:28:44 +00:00
|
|
|
dialer dialer
|
|
|
|
servers ServerLocator
|
|
|
|
gwResolverDep gatewayResolverDep
|
|
|
|
conns map[string]*grpc.ClientConn
|
|
|
|
connsLock sync.Mutex
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
type ServerLocator interface {
|
2021-08-24 21:28:44 +00:00
|
|
|
// ServerForGlobalAddr returns server metadata for a server with the specified globally unique address.
|
|
|
|
ServerForGlobalAddr(globalAddr string) (*metadata.Server, error)
|
|
|
|
|
2021-06-01 22:31:52 +00:00
|
|
|
// Authority returns the target authority to use to dial the server. This is primarily
|
2020-09-09 22:46:58 +00:00
|
|
|
// needed for testing multiple agents in parallel, because gRPC requires the
|
|
|
|
// resolver to be registered globally.
|
2021-06-01 22:31:52 +00:00
|
|
|
Authority() string
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
// gatewayResolverDep is just a holder for a function pointer that can be
|
|
|
|
// updated lazily after the structs are instantiated (but before first use)
|
|
|
|
// and all structs with a reference to this struct will see the same update.
|
|
|
|
type gatewayResolverDep struct {
|
|
|
|
// GatewayResolver is a function that returns a suitable random mesh
|
|
|
|
// gateway address for dialing servers in a given DC. This is only
|
|
|
|
// needed if wan federation via mesh gateways is enabled.
|
|
|
|
GatewayResolver func(string) string
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// TLSWrapper wraps a non-TLS connection and returns a connection with TLS
|
|
|
|
// enabled.
|
|
|
|
type TLSWrapper func(dc string, conn net.Conn) (net.Conn, error)
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
// ALPNWrapper is a function that is used to wrap a non-TLS connection and
|
|
|
|
// returns an appropriate TLS connection or error. This taks a datacenter and
|
|
|
|
// node name as argument to configure the desired SNI value and the desired
|
|
|
|
// next proto for configuring ALPN.
|
|
|
|
type ALPNWrapper func(dc, nodeName, alpnProto string, conn net.Conn) (net.Conn, error)
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
type dialer func(context.Context, string) (net.Conn, error)
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
type ClientConnPoolConfig struct {
|
|
|
|
// Servers is a reference for how to figure out how to dial any server.
|
|
|
|
Servers ServerLocator
|
|
|
|
|
|
|
|
// SrcAddr is the source address for outgoing connections.
|
|
|
|
SrcAddr *net.TCPAddr
|
|
|
|
|
|
|
|
// TLSWrapper is the specifics of wrapping a socket when doing an TYPE_BYTE+TLS
|
|
|
|
// wrapped RPC request.
|
|
|
|
TLSWrapper TLSWrapper
|
|
|
|
|
|
|
|
// ALPNWrapper is the specifics of wrapping a socket when doing an ALPN+TLS
|
|
|
|
// wrapped RPC request (typically only for wan federation via mesh
|
|
|
|
// gateways).
|
|
|
|
ALPNWrapper ALPNWrapper
|
|
|
|
|
|
|
|
// UseTLSForDC is a function to determine if dialing a given datacenter
|
|
|
|
// should use TLS.
|
|
|
|
UseTLSForDC func(dc string) bool
|
|
|
|
|
|
|
|
// DialingFromServer should be set to true if this connection pool is owned
|
|
|
|
// by a consul server instance.
|
|
|
|
DialingFromServer bool
|
|
|
|
|
|
|
|
// DialingFromDatacenter is the datacenter of the consul agent using this
|
|
|
|
// pool.
|
|
|
|
DialingFromDatacenter string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientConnPool create new GRPC client pool to connect to servers using
|
|
|
|
// GRPC over RPC.
|
|
|
|
func NewClientConnPool(cfg ClientConnPoolConfig) *ClientConnPool {
|
|
|
|
c := &ClientConnPool{
|
|
|
|
servers: cfg.Servers,
|
2020-09-09 22:46:58 +00:00
|
|
|
conns: make(map[string]*grpc.ClientConn),
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
2021-08-24 21:28:44 +00:00
|
|
|
c.dialer = newDialer(cfg, &c.gwResolverDep)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGatewayResolver is only to be called during setup before the pool is used.
|
|
|
|
func (c *ClientConnPool) SetGatewayResolver(gatewayResolver func(string) string) {
|
|
|
|
c.gwResolverDep.GatewayResolver = gatewayResolver
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// ClientConn returns a grpc.ClientConn for the datacenter. If there are no
|
|
|
|
// existing connections in the pool, a new one will be created, stored in the pool,
|
|
|
|
// then returned.
|
|
|
|
func (c *ClientConnPool) ClientConn(datacenter string) (*grpc.ClientConn, error) {
|
2021-07-22 18:58:08 +00:00
|
|
|
return c.dial(datacenter, "server")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: godoc
|
|
|
|
func (c *ClientConnPool) ClientConnLeader() (*grpc.ClientConn, error) {
|
|
|
|
return c.dial("local", "leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientConnPool) dial(datacenter string, serverType string) (*grpc.ClientConn, error) {
|
2020-09-09 22:46:58 +00:00
|
|
|
c.connsLock.Lock()
|
|
|
|
defer c.connsLock.Unlock()
|
2020-09-09 20:37:43 +00:00
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
target := fmt.Sprintf("consul://%s/%s.%s", c.servers.Authority(), serverType, datacenter)
|
|
|
|
if conn, ok := c.conns[target]; ok {
|
2020-09-09 20:37:43 +00:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:15:02 +00:00
|
|
|
conn, err := grpc.Dial(
|
2021-07-22 18:58:08 +00:00
|
|
|
target,
|
2020-09-09 20:37:43 +00:00
|
|
|
// use WithInsecure mode here because we handle the TLS wrapping in the
|
|
|
|
// custom dialer based on logic around whether the server has TLS enabled.
|
2022-11-07 16:34:30 +00:00
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
2020-09-09 22:46:58 +00:00
|
|
|
grpc.WithContextDialer(c.dialer),
|
2020-09-09 20:37:43 +00:00
|
|
|
grpc.WithDisableRetry(),
|
2022-10-11 22:00:32 +00:00
|
|
|
grpc.WithStatsHandler(agentmiddleware.NewStatsHandler(metrics.Default(), metricsLabels)),
|
2021-02-16 15:17:54 +00:00
|
|
|
// Keep alive parameters are based on the same default ones we used for
|
|
|
|
// Yamux. These are somewhat arbitrary but we did observe in scale testing
|
|
|
|
// that the gRPC defaults (servers send keepalives only every 2 hours,
|
|
|
|
// clients never) seemed to result in TCP drops going undetected until
|
|
|
|
// actual updates needed to be sent which caused unnecessary delays for
|
|
|
|
// deliveries. These settings should be no more work for servers than
|
|
|
|
// existing yamux clients but hopefully allow TCP drops to be detected
|
|
|
|
// earlier and so have a smaller chance of going unnoticed until there are
|
|
|
|
// actual updates to send out from the servers. The servers have a policy to
|
|
|
|
// not accept pings any faster than once every 15 seconds to protect against
|
|
|
|
// abuse.
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: 30 * time.Second,
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}))
|
2020-09-09 20:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
c.conns[target] = conn
|
2020-09-09 20:37:43 +00:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDialer returns a gRPC dialer function that conditionally wraps the connection
|
2020-09-09 22:46:58 +00:00
|
|
|
// with TLS based on the Server.useTLS value.
|
2021-08-24 21:28:44 +00:00
|
|
|
func newDialer(cfg ClientConnPoolConfig, gwResolverDep *gatewayResolverDep) func(context.Context, string) (net.Conn, error) {
|
|
|
|
return func(ctx context.Context, globalAddr string) (net.Conn, error) {
|
|
|
|
server, err := cfg.Servers.ServerForGlobalAddr(globalAddr)
|
2020-09-09 20:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
if cfg.DialingFromServer &&
|
|
|
|
gwResolverDep.GatewayResolver != nil &&
|
|
|
|
cfg.ALPNWrapper != nil &&
|
|
|
|
server.Datacenter != cfg.DialingFromDatacenter {
|
|
|
|
// NOTE: TLS is required on this branch.
|
|
|
|
conn, _, err := pool.DialRPCViaMeshGateway(
|
|
|
|
ctx,
|
|
|
|
server.Datacenter,
|
|
|
|
server.ShortName,
|
|
|
|
cfg.SrcAddr,
|
|
|
|
tlsutil.ALPNWrapper(cfg.ALPNWrapper),
|
|
|
|
pool.ALPN_RPCGRPC,
|
|
|
|
cfg.DialingFromServer,
|
|
|
|
gwResolverDep.GatewayResolver,
|
|
|
|
)
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d := net.Dialer{LocalAddr: cfg.SrcAddr, Timeout: pool.DefaultDialTimeout}
|
|
|
|
conn, err := d.DialContext(ctx, "tcp", server.Addr.String())
|
2020-09-09 22:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
if server.UseTLS && cfg.UseTLSForDC(server.Datacenter) {
|
|
|
|
if cfg.TLSWrapper == nil {
|
2020-09-15 18:11:48 +00:00
|
|
|
conn.Close()
|
2020-09-09 20:37:43 +00:00
|
|
|
return nil, fmt.Errorf("TLS enabled but got nil TLS wrapper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch the connection into TLS mode
|
|
|
|
if _, err := conn.Write([]byte{byte(pool.RPCTLS)}); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the connection in a TLS client
|
2021-08-24 21:28:44 +00:00
|
|
|
tlsConn, err := cfg.TLSWrapper(server.Datacenter, conn)
|
2020-09-09 20:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conn = tlsConn
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
_, err = conn.Write([]byte{byte(pool.RPCGRPC)})
|
2020-09-09 20:37:43 +00:00
|
|
|
if err != nil {
|
2020-09-15 18:11:48 +00:00
|
|
|
conn.Close()
|
2020-09-09 20:37:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
}
|