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