peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2022-10-10 19:45:30 +00:00
|
|
|
"container/ring"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-05-27 00:55:16 +00:00
|
|
|
"sync"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-10-10 19:45:30 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
2022-07-12 23:18:05 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
2022-09-29 03:27:11 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2022-09-20 13:46:20 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/stream"
|
2022-07-13 15:33:48 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-external/services/peerstream"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"github.com/hashicorp/consul/agent/rpc/peering"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2022-09-20 13:46:20 +00:00
|
|
|
"github.com/hashicorp/consul/ipaddr"
|
2022-09-29 03:27:11 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbpeering"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
type PeeringBackend struct {
|
2022-05-23 18:30:58 +00:00
|
|
|
// TODO(peering): accept a smaller interface; maybe just funcs from the server that we actually need: DC, IsLeader, etc
|
2022-07-08 17:01:13 +00:00
|
|
|
srv *Server
|
|
|
|
|
|
|
|
leaderAddrLock sync.RWMutex
|
|
|
|
leaderAddr string
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
var _ peering.Backend = (*PeeringBackend)(nil)
|
|
|
|
var _ peerstream.Backend = (*PeeringBackend)(nil)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
|
|
|
// NewPeeringBackend returns a peering.Backend implementation that is bound to the given server.
|
2022-07-08 17:01:13 +00:00
|
|
|
func NewPeeringBackend(srv *Server) *PeeringBackend {
|
|
|
|
return &PeeringBackend{
|
|
|
|
srv: srv,
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
// SetLeaderAddress is called on a raft.LeaderObservation in a go routine
|
|
|
|
// in the consul server; see trackLeaderChanges()
|
|
|
|
func (b *PeeringBackend) SetLeaderAddress(addr string) {
|
|
|
|
b.leaderAddrLock.Lock()
|
|
|
|
b.leaderAddr = addr
|
|
|
|
b.leaderAddrLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLeaderAddress provides the best hint for the current address of the
|
|
|
|
// leader. There is no guarantee that this is the actual address of the
|
|
|
|
// leader.
|
|
|
|
func (b *PeeringBackend) GetLeaderAddress() string {
|
|
|
|
b.leaderAddrLock.RLock()
|
|
|
|
defer b.leaderAddrLock.RUnlock()
|
|
|
|
return b.leaderAddr
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 03:27:11 +00:00
|
|
|
// GetTLSMaterials returns the TLS materials for the dialer to dial the acceptor using TLS.
|
|
|
|
// It returns the server name to validate, and the CA certificate to validate with.
|
2022-09-29 21:49:58 +00:00
|
|
|
func (b *PeeringBackend) GetTLSMaterials(generatingToken bool) (string, []string, error) {
|
|
|
|
if generatingToken {
|
|
|
|
if !b.srv.config.ConnectEnabled {
|
|
|
|
return "", nil, fmt.Errorf("connect.enabled must be set to true in the server's configuration when generating peering tokens")
|
|
|
|
}
|
|
|
|
if b.srv.config.GRPCTLSPort <= 0 && !b.srv.tlsConfigurator.GRPCServerUseTLS() {
|
|
|
|
return "", nil, fmt.Errorf("TLS for gRPC must be enabled when generating peering tokens")
|
|
|
|
}
|
2022-09-29 03:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
roots, err := b.srv.getCARoots(nil, b.srv.fsm.State())
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, fmt.Errorf("failed to fetch roots: %w", err)
|
|
|
|
}
|
2022-09-29 21:49:58 +00:00
|
|
|
if len(roots.Roots) == 0 || roots.TrustDomain == "" {
|
2022-09-29 03:27:11 +00:00
|
|
|
return "", nil, fmt.Errorf("CA has not finished initializing")
|
|
|
|
}
|
|
|
|
|
|
|
|
serverName := connect.PeeringServerSAN(b.srv.config.Datacenter, roots.TrustDomain)
|
|
|
|
|
|
|
|
var caPems []string
|
|
|
|
for _, r := range roots.Roots {
|
|
|
|
caPems = append(caPems, lib.EnsureTrailingNewline(r.RootCert))
|
|
|
|
}
|
|
|
|
|
|
|
|
return serverName, caPems, nil
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 19:45:30 +00:00
|
|
|
// GetLocalServerAddresses looks up server or mesh gateway addresses from the state store for a peer to dial.
|
|
|
|
func (b *PeeringBackend) GetLocalServerAddresses() ([]string, error) {
|
|
|
|
store := b.srv.fsm.State()
|
|
|
|
|
|
|
|
useGateways, err := b.PeerThroughMeshGateways(nil)
|
|
|
|
if err != nil {
|
|
|
|
// For inbound traffic we prefer to fail fast if we can't determine whether we should peer through MGW.
|
|
|
|
// This prevents unexpectedly sharing local server addresses when a user only intended to peer through gateways.
|
|
|
|
return nil, fmt.Errorf("failed to determine if peering should happen through mesh gateways: %w", err)
|
|
|
|
}
|
|
|
|
if useGateways {
|
|
|
|
return meshGatewayAdresses(store, nil, true)
|
|
|
|
}
|
|
|
|
return serverAddresses(store)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDialAddresses returns: the addresses to cycle through when dialing a peer's servers,
|
2022-11-10 18:54:00 +00:00
|
|
|
// an optional buffer of just gateway addresses, and an optional error.
|
2022-10-10 19:45:30 +00:00
|
|
|
// The resulting ring buffer is front-loaded with the local mesh gateway addresses if they are present.
|
2022-11-10 18:54:00 +00:00
|
|
|
func (b *PeeringBackend) GetDialAddresses(logger hclog.Logger, ws memdb.WatchSet, peerID string) (*ring.Ring, *ring.Ring, error) {
|
2022-10-10 19:45:30 +00:00
|
|
|
newRing, err := b.fetchPeerServerAddresses(ws, peerID)
|
|
|
|
if err != nil {
|
2022-11-10 18:54:00 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to refresh peer server addresses, will continue to use initial addresses: %w", err)
|
2022-10-10 19:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gatewayRing, err := b.maybeFetchGatewayAddresses(ws)
|
|
|
|
if err != nil {
|
|
|
|
// If we couldn't fetch the mesh gateway addresses we fall back to dialing the remote server addresses.
|
2022-11-10 18:54:00 +00:00
|
|
|
logger.Warn("failed to refresh local gateway addresses, will attempt to dial peer directly", "error", err)
|
|
|
|
return newRing, nil, nil
|
2022-10-10 19:45:30 +00:00
|
|
|
}
|
|
|
|
if gatewayRing != nil {
|
|
|
|
// The ordering is important here. We always want to start with the mesh gateway
|
|
|
|
// addresses and fallback to the remote addresses, so we append the server addresses
|
2022-11-10 18:54:00 +00:00
|
|
|
// in newRing to gatewayRing. We also need a new ring to prevent mixing up pointers
|
|
|
|
// with the gateway-only buffer
|
|
|
|
compositeRing := ring.New(gatewayRing.Len() + newRing.Len())
|
|
|
|
gatewayRing.Do(func(s any) {
|
|
|
|
compositeRing.Value = s.(string)
|
|
|
|
compositeRing = compositeRing.Next()
|
|
|
|
})
|
|
|
|
|
|
|
|
newRing.Do(func(s any) {
|
|
|
|
compositeRing.Value = s.(string)
|
|
|
|
compositeRing = compositeRing.Next()
|
|
|
|
})
|
|
|
|
newRing = compositeRing
|
2022-10-10 19:45:30 +00:00
|
|
|
}
|
2022-11-10 18:54:00 +00:00
|
|
|
return newRing, gatewayRing, nil
|
2022-10-10 19:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetchPeerServerAddresses will return a ring buffer with the latest peer server addresses.
|
|
|
|
// If the peering is no longer active or does not have addresses, then we return an error.
|
|
|
|
func (b *PeeringBackend) fetchPeerServerAddresses(ws memdb.WatchSet, peerID string) (*ring.Ring, error) {
|
|
|
|
_, peering, err := b.Store().PeeringReadByID(ws, peerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch peer %q: %w", peerID, err)
|
|
|
|
}
|
|
|
|
if !peering.IsActive() {
|
|
|
|
return nil, fmt.Errorf("there is no active peering for %q", peerID)
|
|
|
|
}
|
2022-11-08 20:55:18 +00:00
|
|
|
return bufferFromAddresses(peering.GetAddressesToDial())
|
2022-10-10 19:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybeFetchGatewayAddresses will return a ring buffer with the latest gateway addresses if the
|
|
|
|
// local datacenter is configured to peer through mesh gateways and there are local gateways registered.
|
|
|
|
// If neither of these are true then we return a nil buffer.
|
|
|
|
func (b *PeeringBackend) maybeFetchGatewayAddresses(ws memdb.WatchSet) (*ring.Ring, error) {
|
|
|
|
useGateways, err := b.PeerThroughMeshGateways(ws)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to determine if peering should happen through mesh gateways: %w", err)
|
|
|
|
}
|
|
|
|
if useGateways {
|
|
|
|
addresses, err := meshGatewayAdresses(b.srv.fsm.State(), ws, false)
|
2022-10-10 19:54:36 +00:00
|
|
|
if err != nil {
|
2022-10-10 19:45:30 +00:00
|
|
|
return nil, fmt.Errorf("error fetching local mesh gateway addresses: %w", err)
|
|
|
|
}
|
2022-10-10 19:54:36 +00:00
|
|
|
return bufferFromAddresses(addresses)
|
2022-10-10 19:45:30 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerThroughMeshGateways determines if the config entry to enable peering control plane
|
|
|
|
// traffic through a mesh gateway is set to enable.
|
|
|
|
func (b *PeeringBackend) PeerThroughMeshGateways(ws memdb.WatchSet) (bool, error) {
|
|
|
|
_, rawEntry, err := b.srv.fsm.State().ConfigEntry(ws, structs.MeshConfig, structs.MeshConfigMesh, acl.DefaultEnterpriseMeta())
|
2022-09-20 13:46:20 +00:00
|
|
|
if err != nil {
|
2022-10-10 19:45:30 +00:00
|
|
|
return false, fmt.Errorf("failed to read mesh config entry: %w", err)
|
|
|
|
}
|
|
|
|
mesh, ok := rawEntry.(*structs.MeshConfigEntry)
|
|
|
|
if rawEntry != nil && !ok {
|
|
|
|
return false, fmt.Errorf("got unexpected type for mesh config entry: %T", rawEntry)
|
2022-09-20 13:46:20 +00:00
|
|
|
}
|
2022-10-10 19:45:30 +00:00
|
|
|
return mesh.PeerThroughMeshGateways(), nil
|
|
|
|
|
|
|
|
}
|
2022-09-20 13:46:20 +00:00
|
|
|
|
2022-10-10 19:54:36 +00:00
|
|
|
func bufferFromAddresses(addresses []string) (*ring.Ring, error) {
|
|
|
|
// IMPORTANT: The address ring buffer must always be length > 0
|
|
|
|
if len(addresses) == 0 {
|
|
|
|
return nil, fmt.Errorf("no known addresses")
|
|
|
|
}
|
2022-10-10 19:45:30 +00:00
|
|
|
ring := ring.New(len(addresses))
|
|
|
|
for _, addr := range addresses {
|
|
|
|
ring.Value = addr
|
|
|
|
ring = ring.Next()
|
2022-09-20 13:46:20 +00:00
|
|
|
}
|
2022-10-10 19:54:36 +00:00
|
|
|
return ring, nil
|
2022-09-20 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 19:45:30 +00:00
|
|
|
func meshGatewayAdresses(state *state.Store, ws memdb.WatchSet, wan bool) ([]string, error) {
|
|
|
|
_, nodes, err := state.ServiceDump(ws, structs.ServiceKindMeshGateway, true, acl.DefaultEnterpriseMeta(), structs.DefaultPeerKeyword)
|
2022-09-20 13:46:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to dump gateway addresses: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var addrs []string
|
|
|
|
for _, node := range nodes {
|
2022-10-10 19:45:30 +00:00
|
|
|
_, addr, port := node.BestAddress(wan)
|
2022-09-20 13:46:20 +00:00
|
|
|
addrs = append(addrs, ipaddr.FormatAddressPort(addr, port))
|
|
|
|
}
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return nil, fmt.Errorf("servers are configured to PeerThroughMeshGateways, but no mesh gateway instances are registered")
|
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2022-10-19 20:33:25 +00:00
|
|
|
func parseNodeAddr(node *structs.ServiceNode) string {
|
|
|
|
// Prefer the wan address
|
|
|
|
if v, ok := node.TaggedAddresses[structs.TaggedAddressWAN]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return node.Address
|
|
|
|
}
|
|
|
|
|
2022-09-20 13:46:20 +00:00
|
|
|
func serverAddresses(state *state.Store) ([]string, error) {
|
2022-10-19 22:26:15 +00:00
|
|
|
_, nodes, err := state.ServiceNodes(nil, structs.ConsulServiceName, structs.DefaultEnterpriseMetaInDefaultPartition(), structs.DefaultPeerKeyword)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var addrs []string
|
|
|
|
for _, node := range nodes {
|
2022-10-19 20:33:25 +00:00
|
|
|
addr := parseNodeAddr(node)
|
|
|
|
|
2022-08-25 16:44:58 +00:00
|
|
|
// Prefer the TLS port if it is defined.
|
|
|
|
grpcPortStr := node.ServiceMeta["grpc_tls_port"]
|
|
|
|
if v, err := strconv.Atoi(grpcPortStr); err == nil && v > 0 {
|
2022-10-19 20:33:25 +00:00
|
|
|
addrs = append(addrs, addr+":"+grpcPortStr)
|
2022-08-25 16:44:58 +00:00
|
|
|
continue
|
2022-07-08 17:01:13 +00:00
|
|
|
}
|
2022-08-25 16:44:58 +00:00
|
|
|
// Fallback to the standard port if TLS is not defined.
|
|
|
|
grpcPortStr = node.ServiceMeta["grpc_port"]
|
|
|
|
if v, err := strconv.Atoi(grpcPortStr); err == nil && v > 0 {
|
2022-10-19 20:33:25 +00:00
|
|
|
addrs = append(addrs, addr+":"+grpcPortStr)
|
2022-08-25 16:44:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Skip node if neither defined.
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
2022-07-12 15:09:00 +00:00
|
|
|
if len(addrs) == 0 {
|
|
|
|
return nil, fmt.Errorf("a grpc bind port must be specified in the configuration for all servers")
|
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeToken encodes a peering token as a bas64-encoded representation of JSON (for now).
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) EncodeToken(tok *structs.PeeringToken) ([]byte, error) {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
jsonToken, err := json.Marshal(tok)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal token: %w", err)
|
|
|
|
}
|
|
|
|
return []byte(base64.StdEncoding.EncodeToString(jsonToken)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeToken decodes a peering token from a base64-encoded JSON byte array (for now).
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) DecodeToken(tokRaw []byte) (*structs.PeeringToken, error) {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
tokJSONRaw, err := base64.StdEncoding.DecodeString(string(tokRaw))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode token: %w", err)
|
|
|
|
}
|
|
|
|
var tok structs.PeeringToken
|
|
|
|
if err := json.Unmarshal(tokJSONRaw, &tok); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tok, nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (s *PeeringBackend) Subscribe(req *stream.SubscribeRequest) (*stream.Subscription, error) {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
return s.srv.publisher.Subscribe(req)
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) Store() peering.Store {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
return b.srv.fsm.State()
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) EnterpriseCheckPartitions(partition string) error {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
return b.enterpriseCheckPartitions(partition)
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) EnterpriseCheckNamespaces(namespace string) error {
|
2022-06-08 16:55:18 +00:00
|
|
|
return b.enterpriseCheckNamespaces(namespace)
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) IsLeader() bool {
|
2022-05-23 18:30:58 +00:00
|
|
|
return b.srv.IsLeader()
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) CheckPeeringUUID(id string) (bool, error) {
|
|
|
|
state := b.srv.fsm.State()
|
2022-06-21 18:04:08 +00:00
|
|
|
if _, existing, err := state.PeeringReadByID(nil, id); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if existing != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
func (b *PeeringBackend) ValidateProposedPeeringSecret(id string) (bool, error) {
|
|
|
|
return b.srv.fsm.State().ValidateProposedPeeringSecretUUID(id)
|
|
|
|
}
|
|
|
|
|
2022-08-08 07:41:00 +00:00
|
|
|
func (b *PeeringBackend) PeeringSecretsWrite(req *pbpeering.SecretsWriteRequest) error {
|
2022-08-01 14:33:18 +00:00
|
|
|
_, err := b.srv.raftApplyProtobuf(structs.PeeringSecretsWriteType, req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) PeeringWrite(req *pbpeering.PeeringWriteRequest) error {
|
|
|
|
_, err := b.srv.raftApplyProtobuf(structs.PeeringWriteType, req)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(peering): This needs RPC metrics interceptor since it's not triggered by an RPC.
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) PeeringTerminateByID(req *pbpeering.PeeringTerminateByIDRequest) error {
|
|
|
|
_, err := b.srv.raftApplyProtobuf(structs.PeeringTerminateByIDType, req)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) PeeringTrustBundleWrite(req *pbpeering.PeeringTrustBundleWriteRequest) error {
|
|
|
|
_, err := b.srv.raftApplyProtobuf(structs.PeeringTrustBundleWriteType, req)
|
2022-05-26 19:24:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) CatalogRegister(req *structs.RegisterRequest) error {
|
|
|
|
_, err := b.srv.leaderRaftApply("Catalog.Register", structs.RegisterRequestType, req)
|
2022-05-12 21:04:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
func (b *PeeringBackend) CatalogDeregister(req *structs.DeregisterRequest) error {
|
|
|
|
_, err := b.srv.leaderRaftApply("Catalog.Deregister", structs.DeregisterRequestType, req)
|
2022-06-13 17:52:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-12 23:18:05 +00:00
|
|
|
|
|
|
|
func (b *PeeringBackend) ResolveTokenAndDefaultMeta(token string, entMeta *acl.EnterpriseMeta, authzCtx *acl.AuthorizerContext) (resolver.Result, error) {
|
|
|
|
return b.srv.ResolveTokenAndDefaultMeta(token, entMeta, authzCtx)
|
|
|
|
}
|