2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2016-08-19 18:03:53 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2019-02-15 05:14:56 +03:00
|
|
|
"bytes"
|
2017-12-02 01:08:38 +03:00
|
|
|
"context"
|
2019-02-15 05:14:56 +03:00
|
|
|
"crypto/ecdsa"
|
2016-08-19 18:03:53 +03:00
|
|
|
"crypto/tls"
|
2017-03-02 18:03:49 +03:00
|
|
|
"crypto/x509"
|
2019-02-15 05:14:56 +03:00
|
|
|
"errors"
|
2016-08-19 18:03:53 +03:00
|
|
|
"fmt"
|
2022-05-20 23:49:11 +03:00
|
|
|
"math"
|
2016-08-19 18:03:53 +03:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-04-16 21:02:20 +03:00
|
|
|
"github.com/armon/go-metrics"
|
2019-02-15 05:14:56 +03:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2016-08-19 18:03:53 +03:00
|
|
|
"github.com/hashicorp/vault/helper/forwarding"
|
2019-04-17 23:50:31 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
|
|
"github.com/hashicorp/vault/vault/cluster"
|
2019-02-15 05:14:56 +03:00
|
|
|
"github.com/hashicorp/vault/vault/replication"
|
2016-08-19 18:03:53 +03:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"google.golang.org/grpc"
|
2017-05-26 20:32:13 +03:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2016-08-19 18:03:53 +03:00
|
|
|
)
|
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
type requestForwardingHandler struct {
|
|
|
|
fws *http2.Server
|
|
|
|
fwRPCServer *grpc.Server
|
|
|
|
logger log.Logger
|
|
|
|
ha bool
|
|
|
|
core *Core
|
|
|
|
stopCh chan struct{}
|
2018-09-18 06:03:00 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
type requestForwardingClusterClient struct {
|
|
|
|
core *Core
|
|
|
|
}
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
// NewRequestForwardingHandler creates a cluster handler for use with request
|
|
|
|
// forwarding.
|
2019-11-07 01:36:47 +03:00
|
|
|
func NewRequestForwardingHandler(c *Core, fws *http2.Server, perfStandbySlots chan struct{}, perfStandbyRepCluster *replication.Cluster) (*requestForwardingHandler, error) {
|
2017-02-17 07:09:39 +03:00
|
|
|
// Resolve locally to avoid races
|
|
|
|
ha := c.ha != nil
|
|
|
|
|
2018-01-25 04:23:08 +03:00
|
|
|
fwRPCServer := grpc.NewServer(
|
2017-05-26 20:32:13 +03:00
|
|
|
grpc.KeepaliveParams(keepalive.ServerParameters{
|
2020-07-27 23:10:26 +03:00
|
|
|
Time: 2 * c.clusterHeartbeatInterval,
|
2017-05-26 20:32:13 +03:00
|
|
|
}),
|
2018-06-29 19:52:23 +03:00
|
|
|
grpc.MaxRecvMsgSize(math.MaxInt32),
|
|
|
|
grpc.MaxSendMsgSize(math.MaxInt32),
|
2017-05-26 20:32:13 +03:00
|
|
|
)
|
2017-02-17 07:09:39 +03:00
|
|
|
|
2017-05-24 17:38:48 +03:00
|
|
|
if ha && c.clusterHandler != nil {
|
2018-01-25 04:23:08 +03:00
|
|
|
RegisterRequestForwardingServer(fwRPCServer, &forwardedRequestRPCServer{
|
2018-09-18 06:03:00 +03:00
|
|
|
core: c,
|
|
|
|
handler: c.clusterHandler,
|
|
|
|
perfStandbySlots: perfStandbySlots,
|
|
|
|
perfStandbyRepCluster: perfStandbyRepCluster,
|
2019-06-27 20:00:03 +03:00
|
|
|
raftFollowerStates: c.raftFollowerStates,
|
2017-02-17 07:09:39 +03:00
|
|
|
})
|
|
|
|
}
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
return &requestForwardingHandler{
|
|
|
|
fws: fws,
|
|
|
|
fwRPCServer: fwRPCServer,
|
|
|
|
ha: ha,
|
|
|
|
logger: c.logger.Named("request-forward"),
|
|
|
|
core: c,
|
|
|
|
stopCh: make(chan struct{}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientLookup satisfies the ClusterClient interface and returns the ha tls
|
|
|
|
// client certs.
|
|
|
|
func (c *requestForwardingClusterClient) ClientLookup(ctx context.Context, requestInfo *tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
parsedCert := c.core.localClusterParsedCert.Load().(*x509.Certificate)
|
|
|
|
if parsedCert == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
currCert := c.core.localClusterCert.Load().([]byte)
|
|
|
|
if len(currCert) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
localCert := make([]byte, len(currCert))
|
|
|
|
copy(localCert, currCert)
|
|
|
|
|
|
|
|
for _, subj := range requestInfo.AcceptableCAs {
|
|
|
|
if bytes.Equal(subj, parsedCert.RawIssuer) {
|
|
|
|
return &tls.Certificate{
|
|
|
|
Certificate: [][]byte{localCert},
|
|
|
|
PrivateKey: c.core.localClusterPrivateKey.Load().(*ecdsa.PrivateKey),
|
|
|
|
Leaf: c.core.localClusterParsedCert.Load().(*x509.Certificate),
|
|
|
|
}, nil
|
|
|
|
}
|
2018-06-17 01:21:33 +03:00
|
|
|
}
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:03:02 +03:00
|
|
|
func (c *requestForwardingClusterClient) ServerName() string {
|
|
|
|
parsedCert := c.core.localClusterParsedCert.Load().(*x509.Certificate)
|
|
|
|
if parsedCert == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedCert.Subject.CommonName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *requestForwardingClusterClient) CACert(ctx context.Context) *x509.Certificate {
|
|
|
|
return c.core.localClusterParsedCert.Load().(*x509.Certificate)
|
|
|
|
}
|
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
// ServerLookup satisfies the ClusterHandler interface and returns the server's
|
|
|
|
// tls certs.
|
|
|
|
func (rf *requestForwardingHandler) ServerLookup(ctx context.Context, clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
currCert := rf.core.localClusterCert.Load().([]byte)
|
|
|
|
if len(currCert) == 0 {
|
|
|
|
return nil, fmt.Errorf("got forwarding connection but no local cert")
|
2016-08-19 18:03:53 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
localCert := make([]byte, len(currCert))
|
|
|
|
copy(localCert, currCert)
|
|
|
|
|
|
|
|
return &tls.Certificate{
|
|
|
|
Certificate: [][]byte{localCert},
|
|
|
|
PrivateKey: rf.core.localClusterPrivateKey.Load().(*ecdsa.PrivateKey),
|
|
|
|
Leaf: rf.core.localClusterParsedCert.Load().(*x509.Certificate),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CALookup satisfies the ClusterHandler interface and returns the ha ca cert.
|
2019-06-20 22:14:58 +03:00
|
|
|
func (rf *requestForwardingHandler) CALookup(ctx context.Context) ([]*x509.Certificate, error) {
|
2019-02-15 05:14:56 +03:00
|
|
|
parsedCert := rf.core.localClusterParsedCert.Load().(*x509.Certificate)
|
|
|
|
|
|
|
|
if parsedCert == nil {
|
|
|
|
return nil, fmt.Errorf("forwarding connection client but no local cert")
|
|
|
|
}
|
|
|
|
|
2019-06-20 22:14:58 +03:00
|
|
|
return []*x509.Certificate{parsedCert}, nil
|
2019-02-15 05:14:56 +03:00
|
|
|
}
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
// Handoff serves a request forwarding connection.
|
|
|
|
func (rf *requestForwardingHandler) Handoff(ctx context.Context, shutdownWg *sync.WaitGroup, closeCh chan struct{}, tlsConn *tls.Conn) error {
|
|
|
|
if !rf.ha {
|
|
|
|
tlsConn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
rf.logger.Debug("got request forwarding connection")
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
shutdownWg.Add(2)
|
|
|
|
// quitCh is used to close the connection and the second
|
|
|
|
// goroutine if the server closes before closeCh.
|
|
|
|
quitCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-quitCh:
|
|
|
|
case <-closeCh:
|
|
|
|
case <-rf.stopCh:
|
|
|
|
}
|
|
|
|
tlsConn.Close()
|
|
|
|
shutdownWg.Done()
|
|
|
|
}()
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
go func() {
|
|
|
|
rf.fws.ServeConn(tlsConn, &http2.ServeConnOpts{
|
|
|
|
Handler: rf.fwRPCServer,
|
|
|
|
BaseConfig: &http.Server{
|
|
|
|
ErrorLog: rf.logger.StandardLogger(nil),
|
|
|
|
},
|
|
|
|
})
|
2018-01-25 04:23:08 +03:00
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
// close the quitCh which will close the connection and
|
|
|
|
// the other goroutine.
|
|
|
|
close(quitCh)
|
|
|
|
shutdownWg.Done()
|
2016-08-19 18:03:53 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
// Stop stops the request forwarding server and closes connections.
|
|
|
|
func (rf *requestForwardingHandler) Stop() error {
|
2019-02-19 23:03:02 +03:00
|
|
|
// Give some time for existing RPCs to drain.
|
2019-04-17 23:50:31 +03:00
|
|
|
time.Sleep(cluster.ListenerAcceptDeadline)
|
2019-02-15 05:14:56 +03:00
|
|
|
close(rf.stopCh)
|
|
|
|
rf.fwRPCServer.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts the listeners and servers necessary to handle forwarded requests
|
|
|
|
func (c *Core) startForwarding(ctx context.Context) error {
|
2019-04-17 23:50:31 +03:00
|
|
|
c.logger.Debug("request forwarding setup function")
|
|
|
|
defer c.logger.Debug("leaving request forwarding setup function")
|
2019-02-15 05:14:56 +03:00
|
|
|
|
|
|
|
// Clean up in case we have transitioned from a client to a server
|
|
|
|
c.requestForwardingConnectionLock.Lock()
|
|
|
|
c.clearForwardingClients()
|
|
|
|
c.requestForwardingConnectionLock.Unlock()
|
|
|
|
|
2020-03-31 20:47:39 +03:00
|
|
|
clusterListener := c.getClusterListener()
|
|
|
|
if c.ha == nil || clusterListener == nil {
|
2019-04-17 23:50:31 +03:00
|
|
|
c.logger.Debug("request forwarding not setup")
|
2019-02-15 05:14:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-07 01:36:47 +03:00
|
|
|
perfStandbyRepCluster, perfStandbySlots, err := c.perfStandbyClusterHandler()
|
2019-02-15 05:14:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:47:39 +03:00
|
|
|
handler, err := NewRequestForwardingHandler(c, clusterListener.Server(), perfStandbySlots, perfStandbyRepCluster)
|
2019-02-15 05:14:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:47:39 +03:00
|
|
|
clusterListener.AddHandler(consts.RequestForwardingALPN, handler)
|
2019-02-15 05:14:56 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Core) stopForwarding() {
|
2020-03-31 20:47:39 +03:00
|
|
|
clusterListener := c.getClusterListener()
|
|
|
|
if clusterListener != nil {
|
|
|
|
clusterListener.StopHandler(consts.RequestForwardingALPN)
|
|
|
|
clusterListener.StopHandler(consts.PerfStandbyALPN)
|
2019-02-15 05:14:56 +03:00
|
|
|
}
|
2019-07-24 23:32:57 +03:00
|
|
|
c.removeAllPerfStandbySecondaries()
|
2019-02-15 05:14:56 +03:00
|
|
|
}
|
|
|
|
|
2016-08-19 18:03:53 +03:00
|
|
|
// refreshRequestForwardingConnection ensures that the client/transport are
|
|
|
|
// alive and that the current active address value matches the most
|
|
|
|
// recently-known address.
|
2018-01-19 12:11:59 +03:00
|
|
|
func (c *Core) refreshRequestForwardingConnection(ctx context.Context, clusterAddr string) error {
|
2022-05-20 23:49:11 +03:00
|
|
|
c.logger.Debug("refreshing forwarding connection", "clusterAddr", clusterAddr)
|
|
|
|
defer c.logger.Debug("done refreshing forwarding connection", "clusterAddr", clusterAddr)
|
2017-03-02 02:16:47 +03:00
|
|
|
|
2016-08-19 18:03:53 +03:00
|
|
|
c.requestForwardingConnectionLock.Lock()
|
|
|
|
defer c.requestForwardingConnectionLock.Unlock()
|
|
|
|
|
2017-03-02 02:16:47 +03:00
|
|
|
// Clean things up first
|
|
|
|
c.clearForwardingClients()
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2017-03-02 02:16:47 +03:00
|
|
|
// If we don't have anything to connect to, just return
|
2016-08-19 18:03:53 +03:00
|
|
|
if clusterAddr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterURL, err := url.Parse(clusterAddr)
|
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("error parsing cluster address attempting to refresh forwarding connection", "error", err)
|
2016-08-19 18:03:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-15 05:14:56 +03:00
|
|
|
parsedCert := c.localClusterParsedCert.Load().(*x509.Certificate)
|
|
|
|
if parsedCert == nil {
|
|
|
|
c.logger.Error("no request forwarding cluster certificate found")
|
|
|
|
return errors.New("no request forwarding cluster certificate found")
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:59:56 +03:00
|
|
|
clusterListener := c.getClusterListener()
|
2020-01-17 10:03:02 +03:00
|
|
|
if clusterListener == nil {
|
|
|
|
c.logger.Error("no cluster listener configured")
|
|
|
|
return nil
|
2019-02-15 05:14:56 +03:00
|
|
|
}
|
|
|
|
|
2020-01-17 10:03:02 +03:00
|
|
|
clusterListener.AddClient(consts.RequestForwardingALPN, &requestForwardingClusterClient{
|
|
|
|
core: c,
|
|
|
|
})
|
|
|
|
|
2017-05-24 16:34:59 +03:00
|
|
|
// Set up grpc forwarding handling
|
|
|
|
// It's not really insecure, but we have to dial manually to get the
|
|
|
|
// ALPN header right. It's just "insecure" because GRPC isn't managing
|
|
|
|
// the TLS state.
|
2018-01-19 12:11:59 +03:00
|
|
|
dctx, cancelFunc := context.WithCancel(ctx)
|
2023-05-29 18:02:27 +03:00
|
|
|
c.rpcClientConn, err = grpc.DialContext(dctx, clusterURL.Host,
|
2020-01-17 10:03:02 +03:00
|
|
|
grpc.WithDialer(clusterListener.GetDialerFunc(ctx, consts.RequestForwardingALPN)),
|
2017-05-26 20:32:13 +03:00
|
|
|
grpc.WithInsecure(), // it's not, we handle it in the dialer
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
2020-07-27 23:10:26 +03:00
|
|
|
Time: 2 * c.clusterHeartbeatInterval,
|
2018-02-06 21:52:35 +03:00
|
|
|
}),
|
|
|
|
grpc.WithDefaultCallOptions(
|
|
|
|
grpc.MaxCallRecvMsgSize(math.MaxInt32),
|
|
|
|
grpc.MaxCallSendMsgSize(math.MaxInt32),
|
2023-05-29 18:02:27 +03:00
|
|
|
))
|
2017-05-24 16:34:59 +03:00
|
|
|
if err != nil {
|
2017-05-24 22:06:56 +03:00
|
|
|
cancelFunc()
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("err setting up forwarding rpc client", "error", err)
|
2017-05-24 16:34:59 +03:00
|
|
|
return err
|
2016-08-19 18:03:53 +03:00
|
|
|
}
|
2018-01-19 15:22:31 +03:00
|
|
|
c.rpcClientConnContext = dctx
|
2017-05-24 22:06:56 +03:00
|
|
|
c.rpcClientConnCancelFunc = cancelFunc
|
|
|
|
c.rpcForwardingClient = &forwardingClient{
|
|
|
|
RequestForwardingClient: NewRequestForwardingClient(c.rpcClientConn),
|
2018-09-04 19:29:18 +03:00
|
|
|
core: c,
|
2020-07-27 23:10:26 +03:00
|
|
|
echoTicker: time.NewTicker(c.clusterHeartbeatInterval),
|
2018-09-04 19:29:18 +03:00
|
|
|
echoContext: dctx,
|
2017-05-24 22:06:56 +03:00
|
|
|
}
|
2017-05-26 20:32:13 +03:00
|
|
|
c.rpcForwardingClient.startHeartbeat()
|
2016-08-19 18:03:53 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-19 21:49:11 +03:00
|
|
|
func (c *Core) clearForwardingClients() {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Debug("clearing forwarding clients")
|
|
|
|
defer c.logger.Debug("done clearing forwarding clients")
|
2017-03-02 02:16:47 +03:00
|
|
|
|
2016-08-19 21:49:11 +03:00
|
|
|
if c.rpcClientConnCancelFunc != nil {
|
|
|
|
c.rpcClientConnCancelFunc()
|
|
|
|
c.rpcClientConnCancelFunc = nil
|
|
|
|
}
|
|
|
|
if c.rpcClientConn != nil {
|
|
|
|
c.rpcClientConn.Close()
|
|
|
|
c.rpcClientConn = nil
|
|
|
|
}
|
2017-05-24 22:06:56 +03:00
|
|
|
|
|
|
|
c.rpcClientConnContext = nil
|
2017-03-02 02:16:47 +03:00
|
|
|
c.rpcForwardingClient = nil
|
2019-02-06 05:01:18 +03:00
|
|
|
|
2019-09-03 18:59:56 +03:00
|
|
|
clusterListener := c.getClusterListener()
|
|
|
|
if clusterListener != nil {
|
|
|
|
clusterListener.RemoveClient(consts.RequestForwardingALPN)
|
2019-02-15 05:14:56 +03:00
|
|
|
}
|
2019-02-06 05:01:18 +03:00
|
|
|
c.clusterLeaderParams.Store((*ClusterLeaderParams)(nil))
|
2016-08-19 21:49:11 +03:00
|
|
|
}
|
|
|
|
|
2016-08-19 18:03:53 +03:00
|
|
|
// ForwardRequest forwards a given request to the active node and returns the
|
|
|
|
// response.
|
2016-08-27 00:53:47 +03:00
|
|
|
func (c *Core) ForwardRequest(req *http.Request) (int, http.Header, []byte, error) {
|
2021-10-04 20:55:15 +03:00
|
|
|
// checking if the node is perfStandby here to avoid a deadlock between
|
|
|
|
// Core.stateLock and Core.requestForwardingConnectionLock
|
|
|
|
isPerfStandby := c.PerfStandby()
|
2016-08-19 18:03:53 +03:00
|
|
|
c.requestForwardingConnectionLock.RLock()
|
|
|
|
defer c.requestForwardingConnectionLock.RUnlock()
|
|
|
|
|
2017-05-24 16:34:59 +03:00
|
|
|
if c.rpcForwardingClient == nil {
|
|
|
|
return 0, nil, nil, ErrCannotForward
|
|
|
|
}
|
2016-08-19 18:03:53 +03:00
|
|
|
|
2021-04-16 21:02:20 +03:00
|
|
|
defer metrics.MeasureSince([]string{"ha", "rpc", "client", "forward"}, time.Now())
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
origPath := req.URL.Path
|
|
|
|
defer func() {
|
|
|
|
req.URL.Path = origPath
|
|
|
|
}()
|
|
|
|
|
|
|
|
req.URL.Path = req.Context().Value("original_request_path").(string)
|
|
|
|
|
2017-05-24 16:34:59 +03:00
|
|
|
freq, err := forwarding.GenerateForwardedRequest(req)
|
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("error creating forwarding RPC request", "error", err)
|
2017-05-24 16:34:59 +03:00
|
|
|
return 0, nil, nil, fmt.Errorf("error creating forwarding RPC request")
|
|
|
|
}
|
|
|
|
if freq == nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("got nil forwarding RPC request")
|
2017-05-24 16:34:59 +03:00
|
|
|
return 0, nil, nil, fmt.Errorf("got nil forwarding RPC request")
|
|
|
|
}
|
2021-04-15 17:23:26 +03:00
|
|
|
resp, err := c.rpcForwardingClient.ForwardRequest(req.Context(), freq)
|
2017-05-24 16:34:59 +03:00
|
|
|
if err != nil {
|
2021-04-16 21:02:20 +03:00
|
|
|
metrics.IncrCounter([]string{"ha", "rpc", "client", "forward", "errors"}, 1)
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("error during forwarded RPC request", "error", err)
|
2017-05-24 16:34:59 +03:00
|
|
|
return 0, nil, nil, fmt.Errorf("error during forwarding RPC request")
|
|
|
|
}
|
2016-08-27 00:53:47 +03:00
|
|
|
|
2017-05-24 16:34:59 +03:00
|
|
|
var header http.Header
|
|
|
|
if resp.HeaderEntries != nil {
|
|
|
|
header = make(http.Header)
|
|
|
|
for k, v := range resp.HeaderEntries {
|
2017-11-02 15:31:50 +03:00
|
|
|
header[k] = v.Values
|
2016-08-27 00:53:47 +03:00
|
|
|
}
|
2016-08-19 18:03:53 +03:00
|
|
|
}
|
2017-05-24 16:34:59 +03:00
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
// If we are a perf standby and the request was forwarded to the active node
|
|
|
|
// we should attempt to wait for the WAL to ship to offer best effort read after
|
|
|
|
// write guarantees
|
2021-10-04 20:55:15 +03:00
|
|
|
if isPerfStandby && resp.LastRemoteWal > 0 {
|
2018-09-18 06:03:00 +03:00
|
|
|
WaitUntilWALShipped(req.Context(), c, resp.LastRemoteWal)
|
|
|
|
}
|
|
|
|
|
2017-05-24 16:34:59 +03:00
|
|
|
return int(resp.StatusCode), header, resp.Body, nil
|
2016-08-19 18:03:53 +03:00
|
|
|
}
|