2019-04-17 20:50:31 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2020-01-17 07:03:02 +00:00
|
|
|
"fmt"
|
2019-04-17 20:50:31 +00:00
|
|
|
"net"
|
2020-05-19 01:22:25 +00:00
|
|
|
"net/url"
|
2021-10-07 19:17:31 +00:00
|
|
|
"os"
|
2019-04-17 20:50:31 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/certutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/tlsutil"
|
|
|
|
|
2019-04-17 20:50:31 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ListenerAcceptDeadline = 500 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client is used to lookup a client certificate.
|
|
|
|
type Client interface {
|
|
|
|
ClientLookup(context.Context, *tls.CertificateRequestInfo) (*tls.Certificate, error)
|
2020-01-17 07:03:02 +00:00
|
|
|
ServerName() string
|
|
|
|
CACert(ctx context.Context) *x509.Certificate
|
2019-04-17 20:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handler exposes functions for looking up TLS configuration and handing
|
|
|
|
// off a connection for a cluster listener application.
|
|
|
|
type Handler interface {
|
|
|
|
ServerLookup(context.Context, *tls.ClientHelloInfo) (*tls.Certificate, error)
|
2019-06-20 19:14:58 +00:00
|
|
|
CALookup(context.Context) ([]*x509.Certificate, error)
|
2019-04-17 20:50:31 +00:00
|
|
|
|
|
|
|
// Handoff is used to pass the connection lifetime off to
|
|
|
|
// the handler
|
|
|
|
Handoff(context.Context, *sync.WaitGroup, chan struct{}, *tls.Conn) error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
type ClusterHook interface {
|
|
|
|
AddClient(alpn string, client Client)
|
|
|
|
RemoveClient(alpn string)
|
|
|
|
AddHandler(alpn string, handler Handler)
|
|
|
|
StopHandler(alpn string)
|
|
|
|
TLSConfig(ctx context.Context) (*tls.Config, error)
|
|
|
|
Addr() net.Addr
|
2020-01-17 07:03:02 +00:00
|
|
|
GetDialerFunc(ctx context.Context, alpnProto string) func(string, time.Duration) (net.Conn, error)
|
2019-06-20 19:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 20:50:31 +00:00
|
|
|
// Listener is the source of truth for cluster handlers and connection
|
|
|
|
// clients. It dynamically builds the cluster TLS information. It's also
|
|
|
|
// responsible for starting tcp listeners and accepting new cluster connections.
|
|
|
|
type Listener struct {
|
|
|
|
handlers map[string]Handler
|
|
|
|
clients map[string]Client
|
|
|
|
shutdown *uint32
|
|
|
|
shutdownWg *sync.WaitGroup
|
|
|
|
server *http2.Server
|
|
|
|
|
2021-10-07 19:17:31 +00:00
|
|
|
networkLayer NetworkLayer
|
|
|
|
cipherSuites []uint16
|
|
|
|
advertise net.Addr
|
|
|
|
logger log.Logger
|
|
|
|
l sync.RWMutex
|
|
|
|
tlsConnectionLoggingLevel log.Level
|
2019-04-17 20:50:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 20:10:26 +00:00
|
|
|
func NewListener(networkLayer NetworkLayer, cipherSuites []uint16, logger log.Logger, idleTimeout time.Duration) *Listener {
|
2019-04-17 20:50:31 +00:00
|
|
|
// Create the HTTP/2 server that will be shared by both RPC and regular
|
|
|
|
// duties. Doing it this way instead of listening via the server and gRPC
|
|
|
|
// allows us to re-use the same port via ALPN. We can just tell the server
|
|
|
|
// to serve a given conn and which handler to use.
|
|
|
|
h2Server := &http2.Server{
|
|
|
|
// Our forwarding connections heartbeat regularly so anything else we
|
|
|
|
// want to go away/get cleaned up pretty rapidly
|
2020-07-27 20:10:26 +00:00
|
|
|
IdleTimeout: idleTimeout,
|
2019-04-17 20:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Listener{
|
|
|
|
handlers: make(map[string]Handler),
|
|
|
|
clients: make(map[string]Client),
|
|
|
|
shutdown: new(uint32),
|
|
|
|
shutdownWg: &sync.WaitGroup{},
|
|
|
|
server: h2Server,
|
|
|
|
|
2021-10-07 19:17:31 +00:00
|
|
|
networkLayer: networkLayer,
|
|
|
|
cipherSuites: cipherSuites,
|
|
|
|
logger: logger,
|
|
|
|
tlsConnectionLoggingLevel: log.LevelFromString(os.Getenv("VAULT_CLUSTER_TLS_SESSION_LOG_LEVEL")),
|
2019-04-17 20:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 01:22:25 +00:00
|
|
|
func (cl *Listener) SetAdvertiseAddr(addr string) error {
|
|
|
|
u, err := url.ParseRequestURI(addr)
|
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to parse advertise address: %w", err)
|
2020-05-19 01:22:25 +00:00
|
|
|
}
|
|
|
|
cl.advertise = &NetAddr{
|
|
|
|
Host: u.Host,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
func (cl *Listener) Addr() net.Addr {
|
2020-05-19 01:22:25 +00:00
|
|
|
if cl.advertise != nil {
|
|
|
|
return cl.advertise
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:03:02 +00:00
|
|
|
addrs := cl.Addrs()
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return addrs[0]
|
2019-06-20 19:14:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 07:03:02 +00:00
|
|
|
func (cl *Listener) Addrs() []net.Addr {
|
|
|
|
return cl.networkLayer.Addrs()
|
2019-04-17 20:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddClient adds a new client for an ALPN name
|
|
|
|
func (cl *Listener) AddClient(alpn string, client Client) {
|
|
|
|
cl.l.Lock()
|
|
|
|
cl.clients[alpn] = client
|
|
|
|
cl.l.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveClient removes the client for the specified ALPN name
|
|
|
|
func (cl *Listener) RemoveClient(alpn string) {
|
|
|
|
cl.l.Lock()
|
|
|
|
delete(cl.clients, alpn)
|
|
|
|
cl.l.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddHandler registers a new cluster handler for the provided ALPN name.
|
|
|
|
func (cl *Listener) AddHandler(alpn string, handler Handler) {
|
|
|
|
cl.l.Lock()
|
|
|
|
cl.handlers[alpn] = handler
|
|
|
|
cl.l.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopHandler stops the cluster handler for the provided ALPN name, it also
|
|
|
|
// calls stop on the handler.
|
|
|
|
func (cl *Listener) StopHandler(alpn string) {
|
|
|
|
cl.l.Lock()
|
|
|
|
handler, ok := cl.handlers[alpn]
|
|
|
|
delete(cl.handlers, alpn)
|
|
|
|
cl.l.Unlock()
|
|
|
|
if ok {
|
|
|
|
handler.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 21:21:56 +00:00
|
|
|
// Handler returns the handler for the provided ALPN name
|
|
|
|
func (cl *Listener) Handler(alpn string) (Handler, bool) {
|
2019-07-09 07:46:08 +00:00
|
|
|
cl.l.RLock()
|
2019-04-17 21:21:56 +00:00
|
|
|
handler, ok := cl.handlers[alpn]
|
2019-07-09 07:46:08 +00:00
|
|
|
cl.l.RUnlock()
|
2019-04-17 21:21:56 +00:00
|
|
|
return handler, ok
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:50:31 +00:00
|
|
|
// Server returns the http2 server that the cluster listener is using
|
|
|
|
func (cl *Listener) Server() *http2.Server {
|
|
|
|
return cl.server
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSConfig returns a tls config object that uses dynamic lookups to correctly
|
|
|
|
// authenticate registered handlers/clients
|
|
|
|
func (cl *Listener) TLSConfig(ctx context.Context) (*tls.Config, error) {
|
|
|
|
serverLookup := func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
cl.logger.Debug("performing server cert lookup")
|
|
|
|
|
|
|
|
cl.l.RLock()
|
|
|
|
defer cl.l.RUnlock()
|
|
|
|
for _, v := range clientHello.SupportedProtos {
|
|
|
|
if handler, ok := cl.handlers[v]; ok {
|
|
|
|
return handler.ServerLookup(ctx, clientHello)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.logger.Warn("no TLS certs found for ALPN", "ALPN", clientHello.SupportedProtos)
|
|
|
|
return nil, errors.New("unsupported protocol")
|
|
|
|
}
|
|
|
|
|
|
|
|
clientLookup := func(requestInfo *tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
cl.logger.Debug("performing client cert lookup")
|
|
|
|
|
|
|
|
cl.l.RLock()
|
|
|
|
defer cl.l.RUnlock()
|
|
|
|
for _, client := range cl.clients {
|
|
|
|
cert, err := client.ClientLookup(ctx, requestInfo)
|
|
|
|
if err == nil && cert != nil {
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.logger.Warn("no client information found")
|
|
|
|
return nil, errors.New("no client cert found")
|
|
|
|
}
|
|
|
|
|
|
|
|
serverConfigLookup := func(clientHello *tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
caPool := x509.NewCertPool()
|
|
|
|
|
|
|
|
ret := &tls.Config{
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
GetCertificate: serverLookup,
|
|
|
|
GetClientCertificate: clientLookup,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
RootCAs: caPool,
|
|
|
|
ClientCAs: caPool,
|
|
|
|
NextProtos: clientHello.SupportedProtos,
|
|
|
|
CipherSuites: cl.cipherSuites,
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.l.RLock()
|
|
|
|
defer cl.l.RUnlock()
|
|
|
|
for _, v := range clientHello.SupportedProtos {
|
|
|
|
if handler, ok := cl.handlers[v]; ok {
|
2019-06-20 19:14:58 +00:00
|
|
|
caList, err := handler.CALookup(ctx)
|
2019-04-17 20:50:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
for _, ca := range caList {
|
|
|
|
caPool.AddCert(ca)
|
|
|
|
}
|
2019-04-17 20:50:31 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.logger.Warn("no TLS config found for ALPN", "ALPN", clientHello.SupportedProtos)
|
|
|
|
return nil, errors.New("unsupported protocol")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tls.Config{
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
GetCertificate: serverLookup,
|
|
|
|
GetClientCertificate: clientLookup,
|
|
|
|
GetConfigForClient: serverConfigLookup,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
CipherSuites: cl.cipherSuites,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the tcp listeners and will accept connections until stop is
|
2019-06-20 19:14:58 +00:00
|
|
|
// called. This function blocks so should be called in a goroutine.
|
2019-04-17 20:50:31 +00:00
|
|
|
func (cl *Listener) Run(ctx context.Context) error {
|
|
|
|
// Get our TLS config
|
|
|
|
tlsConfig, err := cl.TLSConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
cl.logger.Error("failed to get tls configuration when starting cluster listener", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The server supports all of the possible protos
|
|
|
|
tlsConfig.NextProtos = []string{"h2", consts.RequestForwardingALPN, consts.PerfStandbyALPN, consts.PerformanceReplicationALPN, consts.DRReplicationALPN}
|
|
|
|
|
2020-01-17 07:03:02 +00:00
|
|
|
for _, ln := range cl.networkLayer.Listeners() {
|
2019-04-17 20:50:31 +00:00
|
|
|
// closeCh is used to shutdown the spawned goroutines once this
|
|
|
|
// function returns
|
|
|
|
closeCh := make(chan struct{})
|
|
|
|
|
2020-01-17 07:03:02 +00:00
|
|
|
localLn := ln
|
2019-04-17 20:50:31 +00:00
|
|
|
|
|
|
|
// Wrap the listener with TLS
|
2020-01-17 07:03:02 +00:00
|
|
|
tlsLn := tls.NewListener(localLn, tlsConfig)
|
2019-04-17 20:50:31 +00:00
|
|
|
|
|
|
|
if cl.logger.IsInfo() {
|
|
|
|
cl.logger.Info("serving cluster requests", "cluster_listen_address", tlsLn.Addr())
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.shutdownWg.Add(1)
|
|
|
|
// Start our listening loop
|
|
|
|
go func(closeCh chan struct{}, tlsLn net.Listener) {
|
|
|
|
defer func() {
|
2022-01-20 13:34:40 +00:00
|
|
|
cl.shutdownWg.Done()
|
2019-04-17 20:50:31 +00:00
|
|
|
tlsLn.Close()
|
|
|
|
close(closeCh)
|
|
|
|
}()
|
|
|
|
|
2021-05-12 15:47:38 +00:00
|
|
|
// baseDelay is the initial delay after an Accept() error before attempting again
|
|
|
|
const baseDelay = 5 * time.Millisecond
|
|
|
|
|
|
|
|
// maxDelay is the maximum delay after an Accept() error before attempting again.
|
|
|
|
// In the case that this function is error-looping, it will delay the shutdown check.
|
|
|
|
// Therefore, changes to maxDelay may have an effect on the latency of shutdown.
|
|
|
|
const maxDelay = 1 * time.Second
|
|
|
|
|
|
|
|
var loopDelay time.Duration
|
2019-04-17 20:50:31 +00:00
|
|
|
for {
|
|
|
|
if atomic.LoadUint32(cl.shutdown) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the deadline for the accept call. If it passes we'll get
|
|
|
|
// an error, causing us to check the condition at the top
|
|
|
|
// again.
|
2020-01-17 07:03:02 +00:00
|
|
|
localLn.SetDeadline(time.Now().Add(ListenerAcceptDeadline))
|
2019-04-17 20:50:31 +00:00
|
|
|
|
|
|
|
// Accept the connection
|
|
|
|
conn, err := tlsLn.Accept()
|
|
|
|
if err != nil {
|
2021-05-13 22:02:25 +00:00
|
|
|
err, ok := err.(net.Error)
|
|
|
|
if ok && !err.Timeout() {
|
2019-04-17 20:50:31 +00:00
|
|
|
cl.logger.Debug("non-timeout error accepting on cluster port", "error", err)
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
2021-05-13 22:02:25 +00:00
|
|
|
if ok && err.Timeout() {
|
|
|
|
loopDelay = 0
|
|
|
|
continue
|
|
|
|
}
|
2021-05-12 15:47:38 +00:00
|
|
|
|
|
|
|
if loopDelay == 0 {
|
|
|
|
loopDelay = baseDelay
|
|
|
|
} else {
|
|
|
|
loopDelay *= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if loopDelay > maxDelay {
|
|
|
|
loopDelay = maxDelay
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(loopDelay)
|
2019-04-17 20:50:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-12 15:47:38 +00:00
|
|
|
// No error, reset loop delay
|
|
|
|
loopDelay = 0
|
|
|
|
|
2019-04-17 20:50:31 +00:00
|
|
|
if conn == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type assert to TLS connection and handshake to populate the
|
|
|
|
// connection state
|
|
|
|
tlsConn := conn.(*tls.Conn)
|
|
|
|
|
|
|
|
// Set a deadline for the handshake. This will cause clients
|
|
|
|
// that don't successfully auth to be kicked out quickly.
|
|
|
|
// Cluster connections should be reliable so being marginally
|
|
|
|
// aggressive here is fine.
|
|
|
|
err = tlsConn.SetDeadline(time.Now().Add(30 * time.Second))
|
|
|
|
if err != nil {
|
|
|
|
if cl.logger.IsDebug() {
|
|
|
|
cl.logger.Debug("error setting deadline for cluster connection", "error", err)
|
|
|
|
}
|
|
|
|
tlsConn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tlsConn.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
if cl.logger.IsDebug() {
|
|
|
|
cl.logger.Debug("error handshaking cluster connection", "error", err)
|
|
|
|
}
|
|
|
|
tlsConn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:17:31 +00:00
|
|
|
cl.logTLSSessionStart(tlsConn.RemoteAddr().String(), tlsConn.ConnectionState())
|
|
|
|
|
2019-04-17 20:50:31 +00:00
|
|
|
// Now, set it back to unlimited
|
|
|
|
err = tlsConn.SetDeadline(time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
if cl.logger.IsDebug() {
|
|
|
|
cl.logger.Debug("error setting deadline for cluster connection", "error", err)
|
|
|
|
}
|
|
|
|
tlsConn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.l.RLock()
|
|
|
|
handler, ok := cl.handlers[tlsConn.ConnectionState().NegotiatedProtocol]
|
|
|
|
cl.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
cl.logger.Debug("unknown negotiated protocol on cluster port")
|
|
|
|
tlsConn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := handler.Handoff(ctx, cl.shutdownWg, closeCh, tlsConn); err != nil {
|
|
|
|
cl.logger.Error("error handling cluster connection", "error", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(closeCh, tlsLn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-02 19:13:00 +00:00
|
|
|
// Stop stops the cluster listener
|
2019-04-17 20:50:31 +00:00
|
|
|
func (cl *Listener) Stop() {
|
|
|
|
// Set the shutdown flag. This will cause the listeners to shut down
|
|
|
|
// within the deadline in clusterListenerAcceptDeadline
|
|
|
|
atomic.StoreUint32(cl.shutdown, 1)
|
|
|
|
cl.logger.Info("forwarding rpc listeners stopped")
|
|
|
|
|
|
|
|
// Wait for them all to shut down
|
|
|
|
cl.shutdownWg.Wait()
|
|
|
|
cl.logger.Info("rpc listeners successfully shut down")
|
|
|
|
}
|
2020-01-17 07:03:02 +00:00
|
|
|
|
|
|
|
// GetDialerFunc returns a function that looks up the TLS information for the
|
|
|
|
// provided alpn name and calls the network layer's dial function.
|
|
|
|
func (cl *Listener) GetDialerFunc(ctx context.Context, alpn string) func(string, time.Duration) (net.Conn, error) {
|
|
|
|
return func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
tlsConfig, err := cl.TLSConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
cl.logger.Error("failed to get tls configuration", "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tlsConfig == nil {
|
|
|
|
return nil, errors.New("no tls config found")
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.l.RLock()
|
|
|
|
client, ok := cl.clients[alpn]
|
|
|
|
cl.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no client configured for alpn: %q", alpn)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverName := client.ServerName()
|
|
|
|
if serverName != "" {
|
|
|
|
tlsConfig.ServerName = serverName
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert := client.CACert(ctx)
|
|
|
|
if caCert != nil {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(caCert)
|
|
|
|
tlsConfig.RootCAs = pool
|
|
|
|
tlsConfig.ClientCAs = pool
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig.NextProtos = []string{alpn}
|
2021-09-30 01:25:15 +00:00
|
|
|
cl.logger.Debug("creating rpc dialer", "address", addr, "alpn", alpn, "host", tlsConfig.ServerName)
|
2020-01-17 07:03:02 +00:00
|
|
|
|
2021-10-07 19:17:31 +00:00
|
|
|
conn, err := cl.networkLayer.Dial(addr, timeout, tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cl.logTLSSessionStart(conn.RemoteAddr().String(), conn.ConnectionState())
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cl *Listener) logTLSSessionStart(peerAddress string, state tls.ConnectionState) {
|
|
|
|
if cl.tlsConnectionLoggingLevel != log.NoLevel {
|
|
|
|
cipherName, _ := tlsutil.GetCipherName(state.CipherSuite)
|
|
|
|
cl.logger.Log(cl.tlsConnectionLoggingLevel, "TLS connection established", "peer", peerAddress, "negotiated_protocol", state.NegotiatedProtocol, "cipher_suite", cipherName)
|
|
|
|
for _, chain := range state.VerifiedChains {
|
|
|
|
for _, cert := range chain {
|
|
|
|
cl.logger.Log(cl.tlsConnectionLoggingLevel, "Peer certificate", "is_ca", cert.IsCA, "serial_number", cert.SerialNumber.String(), "subject", cert.Subject.String(),
|
|
|
|
"signature_algorithm", cert.SignatureAlgorithm.String(), "public_key_algorithm", cert.PublicKeyAlgorithm.String(), "public_key_size", certutil.GetPublicKeySize(cert.PublicKey))
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 07:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkListener is used by the network layer to define a net.Listener for use
|
|
|
|
// in the cluster listener.
|
|
|
|
type NetworkListener interface {
|
|
|
|
net.Listener
|
|
|
|
|
|
|
|
SetDeadline(t time.Time) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkLayer is the network abstraction used in the cluster listener.
|
|
|
|
// Abstracting the network layer out allows us to swap the underlying
|
|
|
|
// implementations for tests.
|
|
|
|
type NetworkLayer interface {
|
|
|
|
Addrs() []net.Addr
|
|
|
|
Listeners() []NetworkListener
|
|
|
|
Dial(address string, timeout time.Duration, tlsConfig *tls.Config) (*tls.Conn, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkLayerSet is used for returning a slice of layers to a caller.
|
|
|
|
type NetworkLayerSet interface {
|
|
|
|
Layers() []NetworkLayer
|
|
|
|
}
|
2020-05-19 01:22:25 +00:00
|
|
|
|
|
|
|
type NetAddr struct {
|
|
|
|
Host string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NetAddr) String() string {
|
|
|
|
return c.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*NetAddr) Network() string {
|
|
|
|
return "tcp"
|
|
|
|
}
|