2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-07-26 06:25:33 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-08-15 13:42:42 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2016-07-26 06:25:33 +00:00
|
|
|
"encoding/json"
|
2016-08-15 13:42:42 +00:00
|
|
|
"errors"
|
2016-07-26 06:25:33 +00:00
|
|
|
"fmt"
|
2016-08-15 13:42:42 +00:00
|
|
|
"math/big"
|
|
|
|
mathrand "math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-04-04 17:02:44 +00:00
|
|
|
"strings"
|
2016-08-15 13:42:42 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2019-04-17 20:50:31 +00:00
|
|
|
"github.com/hashicorp/vault/vault/cluster"
|
2016-07-26 06:25:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Storage path where the local cluster name and identifier are stored
|
2016-08-09 15:28:42 +00:00
|
|
|
coreLocalClusterInfoPath = "core/cluster/local/info"
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-01-04 21:44:03 +00:00
|
|
|
corePrivateKeyTypeP521 = "p521"
|
|
|
|
corePrivateKeyTypeED25519 = "ed25519"
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
// Internal so as not to log a trace message
|
|
|
|
IntNoForwardingHeaderName = "X-Vault-Internal-No-Request-Forwarding"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-09-17 00:50:28 +00:00
|
|
|
ErrCannotForward = errors.New("cannot forward request; no connection or address not known")
|
|
|
|
ErrCannotForwardLocalOnly = errors.New("cannot forward local-only request")
|
2016-07-26 06:25:33 +00:00
|
|
|
)
|
|
|
|
|
2019-02-06 02:01:18 +00:00
|
|
|
type ClusterLeaderParams struct {
|
|
|
|
LeaderUUID string
|
|
|
|
LeaderRedirectAddr string
|
|
|
|
LeaderClusterAddr string
|
|
|
|
}
|
|
|
|
|
2016-07-26 06:25:33 +00:00
|
|
|
// Structure representing the storage entry that holds cluster information
|
|
|
|
type Cluster struct {
|
|
|
|
// Name of the cluster
|
|
|
|
Name string `json:"name" structs:"name" mapstructure:"name"`
|
|
|
|
|
|
|
|
// Identifier of the cluster
|
|
|
|
ID string `json:"id" structs:"id" mapstructure:"id"`
|
|
|
|
}
|
|
|
|
|
2017-01-06 20:42:18 +00:00
|
|
|
// Cluster fetches the details of the local cluster. This method errors out
|
|
|
|
// when Vault is sealed.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Core) Cluster(ctx context.Context) (*Cluster, error) {
|
2016-08-10 19:09:16 +00:00
|
|
|
var cluster Cluster
|
|
|
|
|
2016-07-26 06:25:33 +00:00
|
|
|
// Fetch the storage entry. This call fails when Vault is sealed.
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := c.barrier.Get(ctx, coreLocalClusterInfoPath)
|
2016-07-26 06:25:33 +00:00
|
|
|
if err != nil {
|
2016-08-10 19:22:12 +00:00
|
|
|
return nil, err
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
|
|
|
if entry == nil {
|
2016-08-10 19:09:16 +00:00
|
|
|
return &cluster, nil
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the cluster information
|
|
|
|
if err = jsonutil.DecodeJSON(entry.Value, &cluster); err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode cluster details: %w", err)
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 15:03:50 +00:00
|
|
|
// Set in config file
|
|
|
|
if c.clusterName != "" {
|
|
|
|
cluster.Name = c.clusterName
|
|
|
|
}
|
|
|
|
|
2016-07-26 06:25:33 +00:00
|
|
|
return &cluster, nil
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
// This sets our local cluster cert and private key based on the advertisement.
|
|
|
|
// It also ensures the cert is in our local cluster cert pool.
|
2017-03-02 15:03:49 +00:00
|
|
|
func (c *Core) loadLocalClusterTLS(adv activeAdvertisement) (retErr error) {
|
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
2018-02-23 19:47:07 +00:00
|
|
|
c.localClusterCert.Store(([]byte)(nil))
|
|
|
|
c.localClusterParsedCert.Store((*x509.Certificate)(nil))
|
|
|
|
c.localClusterPrivateKey.Store((*ecdsa.PrivateKey)(nil))
|
2017-03-02 15:03:49 +00:00
|
|
|
|
|
|
|
c.requestForwardingConnectionLock.Lock()
|
|
|
|
c.clearForwardingClients()
|
|
|
|
c.requestForwardingConnectionLock.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
switch {
|
2016-08-19 18:49:11 +00:00
|
|
|
case adv.ClusterAddr == "":
|
|
|
|
// Clustering disabled on the server, don't try to look for params
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case adv.ClusterKeyParams == nil:
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("no key params found loading local cluster TLS information")
|
2016-08-19 18:49:11 +00:00
|
|
|
return fmt.Errorf("no local cluster key params found")
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
case adv.ClusterKeyParams.X == nil, adv.ClusterKeyParams.Y == nil, adv.ClusterKeyParams.D == nil:
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to parse local cluster key due to missing params")
|
2016-08-15 13:42:42 +00:00
|
|
|
return fmt.Errorf("failed to parse local cluster key")
|
2016-08-19 15:03:53 +00:00
|
|
|
|
2016-08-19 18:49:11 +00:00
|
|
|
case adv.ClusterKeyParams.Type != corePrivateKeyTypeP521:
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("unknown local cluster key type", "key_type", adv.ClusterKeyParams.Type)
|
2016-08-15 13:42:42 +00:00
|
|
|
return fmt.Errorf("failed to find valid local cluster key type")
|
2016-08-19 18:49:11 +00:00
|
|
|
|
|
|
|
case adv.ClusterCert == nil || len(adv.ClusterCert) == 0:
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("no local cluster cert found")
|
2016-08-19 18:49:11 +00:00
|
|
|
return fmt.Errorf("no local cluster cert found")
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
|
2018-02-23 19:47:07 +00:00
|
|
|
c.localClusterPrivateKey.Store(&ecdsa.PrivateKey{
|
2016-08-15 13:42:42 +00:00
|
|
|
PublicKey: ecdsa.PublicKey{
|
|
|
|
Curve: elliptic.P521(),
|
|
|
|
X: adv.ClusterKeyParams.X,
|
|
|
|
Y: adv.ClusterKeyParams.Y,
|
|
|
|
},
|
|
|
|
D: adv.ClusterKeyParams.D,
|
2018-02-23 19:47:07 +00:00
|
|
|
})
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2018-02-23 19:47:07 +00:00
|
|
|
locCert := make([]byte, len(adv.ClusterCert))
|
|
|
|
copy(locCert, adv.ClusterCert)
|
|
|
|
c.localClusterCert.Store(locCert)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2018-02-23 19:47:07 +00:00
|
|
|
cert, err := x509.ParseCertificate(adv.ClusterCert)
|
2016-08-15 13:42:42 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed parsing local cluster certificate", "error", err)
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("error parsing local cluster certificate: %w", err)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 19:47:07 +00:00
|
|
|
c.localClusterParsedCert.Store(cert)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 06:25:33 +00:00
|
|
|
// setupCluster creates storage entries for holding Vault cluster information.
|
2016-07-26 13:18:38 +00:00
|
|
|
// Entries will be created only if they are not already present. If clusterName
|
|
|
|
// is not supplied, this method will auto-generate it.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Core) setupCluster(ctx context.Context) error {
|
2017-01-06 20:42:18 +00:00
|
|
|
// Prevent data races with the TLS parameters
|
|
|
|
c.clusterParamsLock.Lock()
|
|
|
|
defer c.clusterParamsLock.Unlock()
|
|
|
|
|
2016-07-26 06:25:33 +00:00
|
|
|
// Check if storage index is already present or not
|
2018-01-19 06:44:44 +00:00
|
|
|
cluster, err := c.Cluster(ctx)
|
2016-07-26 06:25:33 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to get cluster details", "error", err)
|
2016-07-26 06:25:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-08-10 19:09:16 +00:00
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
var modified bool
|
|
|
|
|
2016-08-10 19:09:16 +00:00
|
|
|
if cluster == nil {
|
|
|
|
cluster = &Cluster{}
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 19:09:16 +00:00
|
|
|
if cluster.Name == "" {
|
|
|
|
// If cluster name is not supplied, generate one
|
|
|
|
if c.clusterName == "" {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Debug("cluster name not found/set, generating new")
|
2016-08-10 19:09:16 +00:00
|
|
|
clusterNameBytes, err := uuid.GenerateRandomBytes(4)
|
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to generate cluster name", "error", err)
|
2016-08-10 19:09:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2016-08-10 19:09:16 +00:00
|
|
|
c.clusterName = fmt.Sprintf("vault-cluster-%08x", clusterNameBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster.Name = c.clusterName
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsDebug() {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Debug("cluster name set", "name", cluster.Name)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2016-08-10 19:09:16 +00:00
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
|
2020-05-13 02:00:59 +00:00
|
|
|
// This is the first point at which the stored (or newly generated)
|
|
|
|
// cluster name is known.
|
|
|
|
c.metricSink.SetDefaultClusterName(cluster.Name)
|
|
|
|
|
2016-08-10 19:09:16 +00:00
|
|
|
if cluster.ID == "" {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Debug("cluster ID not found, generating new")
|
2016-08-10 19:09:16 +00:00
|
|
|
// Generate a clusterID
|
2016-08-15 13:42:42 +00:00
|
|
|
cluster.ID, err = uuid.GenerateUUID()
|
2016-07-26 06:25:33 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to generate cluster identifier", "error", err)
|
2016-07-26 06:25:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsDebug() {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Debug("cluster ID set", "id", cluster.ID)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2016-08-10 19:09:16 +00:00
|
|
|
modified = true
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
// If we're using HA, generate server-to-server parameters
|
|
|
|
if c.ha != nil {
|
|
|
|
// Create a private key
|
2018-02-23 19:47:07 +00:00
|
|
|
if c.localClusterPrivateKey.Load().(*ecdsa.PrivateKey) == nil {
|
2018-09-18 03:03:00 +00:00
|
|
|
c.logger.Debug("generating cluster private key")
|
2019-10-17 17:33:00 +00:00
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P521(), c.secureRandomReader)
|
2016-08-19 15:03:53 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to generate local cluster key", "error", err)
|
2016-08-19 15:03:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:47:07 +00:00
|
|
|
c.localClusterPrivateKey.Store(key)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
// Create a certificate
|
2018-02-23 19:47:07 +00:00
|
|
|
if c.localClusterCert.Load().([]byte) == nil {
|
2016-08-19 15:03:53 +00:00
|
|
|
host, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-01 23:16:47 +00:00
|
|
|
host = fmt.Sprintf("fw-%s", host)
|
2022-06-15 00:53:19 +00:00
|
|
|
c.logger.Debug("generating local cluster certificate", "host", host)
|
2016-08-19 15:03:53 +00:00
|
|
|
template := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: host,
|
|
|
|
},
|
|
|
|
DNSNames: []string{host},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement | x509.KeyUsageCertSign,
|
|
|
|
SerialNumber: big.NewInt(mathrand.Int63()),
|
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
// 30 years of single-active uptime ought to be enough for anybody
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
|
|
|
BasicConstraintsValid: true,
|
2018-09-04 16:29:18 +00:00
|
|
|
IsCA: true,
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2018-02-23 19:47:07 +00:00
|
|
|
certBytes, err := x509.CreateCertificate(rand.Reader, template, template, c.localClusterPrivateKey.Load().(*ecdsa.PrivateKey).Public(), c.localClusterPrivateKey.Load().(*ecdsa.PrivateKey))
|
2016-08-19 15:03:53 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("error generating self-signed cert", "error", err)
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("unable to generate local cluster certificate: %w", err)
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-03-02 15:03:49 +00:00
|
|
|
parsedCert, err := x509.ParseCertificate(certBytes)
|
2016-08-19 15:03:53 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("error parsing self-signed cert", "error", err)
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("error parsing generated certificate: %w", err)
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2018-02-23 19:47:07 +00:00
|
|
|
c.localClusterCert.Store(certBytes)
|
|
|
|
c.localClusterParsedCert.Store(parsedCert)
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 19:09:16 +00:00
|
|
|
if modified {
|
|
|
|
// Encode the cluster information into as a JSON string
|
|
|
|
rawCluster, err := json.Marshal(cluster)
|
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to encode cluster details", "error", err)
|
2016-08-10 19:09:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-07-26 06:25:33 +00:00
|
|
|
|
2016-08-10 19:09:16 +00:00
|
|
|
// Store it
|
2019-01-31 14:25:18 +00:00
|
|
|
err = c.barrier.Put(ctx, &logical.StorageEntry{
|
2016-08-10 19:09:16 +00:00
|
|
|
Key: coreLocalClusterInfoPath,
|
|
|
|
Value: rawCluster,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to store cluster details", "error", err)
|
2016-08-10 19:09:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:58:10 +00:00
|
|
|
c.clusterID.Store(cluster.ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Core) loadCluster(ctx context.Context) error {
|
|
|
|
cluster, err := c.Cluster(ctx)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Error("failed to get cluster details", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.clusterID.Store(cluster.ID)
|
2016-08-10 19:09:16 +00:00
|
|
|
return nil
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2019-02-15 02:14:56 +00:00
|
|
|
// startClusterListener starts cluster request listeners during unseal. It
|
2016-08-19 15:03:53 +00:00
|
|
|
// is assumed that the state lock is held while this is run. Right now this
|
2019-02-15 02:14:56 +00:00
|
|
|
// only starts cluster listeners. Once the listener is started handlers/clients
|
|
|
|
// can start being registered to it.
|
2018-01-19 09:11:59 +00:00
|
|
|
func (c *Core) startClusterListener(ctx context.Context) error {
|
2019-06-27 17:00:03 +00:00
|
|
|
if c.ClusterAddr() == "" {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("clustering disabled, not starting listeners")
|
2016-08-15 13:42:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:59:56 +00:00
|
|
|
if c.getClusterListener() != nil {
|
2019-06-20 19:14:58 +00:00
|
|
|
c.logger.Warn("cluster listener is already started")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
if c.clusterListenerAddrs == nil || len(c.clusterListenerAddrs) == 0 {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Warn("clustering not disabled but no addresses to listen on")
|
2016-08-19 15:03:53 +00:00
|
|
|
return fmt.Errorf("cluster addresses not found")
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Debug("starting cluster listeners")
|
2016-08-19 15:03:53 +00:00
|
|
|
|
2020-01-17 07:03:02 +00:00
|
|
|
networkLayer := c.clusterNetworkLayer
|
|
|
|
|
|
|
|
if networkLayer == nil {
|
2022-06-27 15:39:53 +00:00
|
|
|
tcpLogger := c.logger.Named("cluster-listener.tcp")
|
|
|
|
networkLayer = cluster.NewTCPLayer(c.clusterListenerAddrs, tcpLogger)
|
|
|
|
c.AddLogger(tcpLogger)
|
2020-01-17 07:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 15:39:53 +00:00
|
|
|
listenerLogger := c.logger.Named("cluster-listener")
|
2020-07-27 20:10:26 +00:00
|
|
|
c.clusterListener.Store(cluster.NewListener(networkLayer,
|
|
|
|
c.clusterCipherSuites,
|
2022-06-27 15:39:53 +00:00
|
|
|
listenerLogger,
|
2023-03-22 18:51:37 +00:00
|
|
|
5*c.clusterHeartbeatInterval,
|
|
|
|
c.grpcMinConnectTimeout))
|
2019-02-15 02:14:56 +00:00
|
|
|
|
2022-06-27 15:39:53 +00:00
|
|
|
c.AddLogger(listenerLogger)
|
|
|
|
|
2019-09-03 15:59:56 +00:00
|
|
|
err := c.getClusterListener().Run(ctx)
|
2019-04-04 17:02:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-27 17:00:03 +00:00
|
|
|
if strings.HasSuffix(c.ClusterAddr(), ":0") {
|
2019-04-04 17:02:44 +00:00
|
|
|
// If we listened on port 0, record the port the OS gave us.
|
2020-01-17 07:03:02 +00:00
|
|
|
c.clusterAddr.Store(fmt.Sprintf("https://%s", c.getClusterListener().Addr()))
|
2019-04-04 17:02:44 +00:00
|
|
|
}
|
2020-05-19 01:22:25 +00:00
|
|
|
|
|
|
|
if len(c.ClusterAddr()) != 0 {
|
|
|
|
if err := c.getClusterListener().SetAdvertiseAddr(c.ClusterAddr()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 17:02:44 +00:00
|
|
|
return nil
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
func (c *Core) ClusterAddr() string {
|
2019-06-27 17:00:03 +00:00
|
|
|
return c.clusterAddr.Load().(string)
|
2019-06-20 19:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 15:59:56 +00:00
|
|
|
func (c *Core) getClusterListener() *cluster.Listener {
|
|
|
|
cl := c.clusterListener.Load()
|
|
|
|
if cl == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cl.(*cluster.Listener)
|
|
|
|
}
|
|
|
|
|
2019-02-15 02:14:56 +00:00
|
|
|
// stopClusterListener stops any existing listeners during seal. It is
|
2016-08-15 13:42:42 +00:00
|
|
|
// assumed that the state lock is held while this is run.
|
|
|
|
func (c *Core) stopClusterListener() {
|
2019-09-03 15:59:56 +00:00
|
|
|
clusterListener := c.getClusterListener()
|
|
|
|
if clusterListener == nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Debug("clustering disabled, not stopping listeners")
|
2016-08-19 15:03:53 +00:00
|
|
|
return
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("stopping cluster listeners")
|
2016-08-19 15:03:53 +00:00
|
|
|
|
2019-09-03 15:59:56 +00:00
|
|
|
clusterListener.Stop()
|
2020-04-21 20:45:07 +00:00
|
|
|
c.clusterListener.Store((*cluster.Listener)(nil))
|
2016-11-11 21:43:33 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("cluster listeners successfully shut down")
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
func (c *Core) SetClusterListenerAddrs(addrs []*net.TCPAddr) {
|
|
|
|
c.clusterListenerAddrs = addrs
|
2019-06-27 17:00:03 +00:00
|
|
|
if c.ClusterAddr() == "" && len(addrs) == 1 {
|
|
|
|
c.clusterAddr.Store(fmt.Sprintf("https://%s", addrs[0].String()))
|
2017-10-24 20:58:53 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 14:38:48 +00:00
|
|
|
func (c *Core) SetClusterHandler(handler http.Handler) {
|
|
|
|
c.clusterHandler = handler
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2023-01-06 22:06:54 +00:00
|
|
|
|
|
|
|
func (c *Core) ClusterID() string {
|
|
|
|
return c.clusterID.Load()
|
|
|
|
}
|