2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
package peerstream
|
|
|
|
|
|
|
|
import (
|
2022-07-21 17:03:27 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/stream"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbpeering"
|
|
|
|
"github.com/hashicorp/consul/proto/private/pbpeerstream"
|
2022-07-08 17:01:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(peering): fix up these interfaces to be more testable now that they are
|
|
|
|
// extracted from private peering
|
|
|
|
|
2022-07-21 17:03:27 +00:00
|
|
|
const (
|
|
|
|
defaultOutgoingHeartbeatInterval = 15 * time.Second
|
|
|
|
defaultIncomingHeartbeatTimeout = 2 * time.Minute
|
|
|
|
)
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
type Server struct {
|
|
|
|
Config
|
2022-08-29 20:32:26 +00:00
|
|
|
|
|
|
|
Tracker *Tracker
|
2022-07-08 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Backend Backend
|
|
|
|
GetStore func() StateStore
|
|
|
|
Logger hclog.Logger
|
2022-08-01 14:33:18 +00:00
|
|
|
ForwardRPC func(structs.RPCInfo, func(*grpc.ClientConn) error) (bool, error)
|
2022-07-08 17:01:13 +00:00
|
|
|
ACLResolver ACLResolver
|
|
|
|
// Datacenter of the Consul server this gRPC server is hosted on
|
|
|
|
Datacenter string
|
|
|
|
ConnectEnabled bool
|
2022-07-21 17:03:27 +00:00
|
|
|
|
|
|
|
// outgoingHeartbeatInterval is how often we send a heartbeat.
|
|
|
|
outgoingHeartbeatInterval time.Duration
|
|
|
|
|
2022-08-26 20:49:03 +00:00
|
|
|
// incomingHeartbeatTimeout is how long we'll wait between receiving heartbeats before we close the connection.
|
|
|
|
incomingHeartbeatTimeout time.Duration
|
2022-07-08 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate mockery --name ACLResolver --inpackage
|
|
|
|
type ACLResolver interface {
|
|
|
|
ResolveTokenAndDefaultMeta(string, *acl.EnterpriseMeta, *acl.AuthorizerContext) (resolver.Result, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(cfg Config) *Server {
|
|
|
|
requireNotNil(cfg.Backend, "Backend")
|
|
|
|
requireNotNil(cfg.GetStore, "GetStore")
|
|
|
|
requireNotNil(cfg.Logger, "Logger")
|
|
|
|
// requireNotNil(cfg.ACLResolver, "ACLResolver") // TODO(peering): reenable check when ACLs are required
|
|
|
|
if cfg.Datacenter == "" {
|
|
|
|
panic("Datacenter is required")
|
|
|
|
}
|
2022-07-21 17:03:27 +00:00
|
|
|
if cfg.outgoingHeartbeatInterval == 0 {
|
|
|
|
cfg.outgoingHeartbeatInterval = defaultOutgoingHeartbeatInterval
|
|
|
|
}
|
2022-08-26 20:49:03 +00:00
|
|
|
if cfg.incomingHeartbeatTimeout == 0 {
|
|
|
|
cfg.incomingHeartbeatTimeout = defaultIncomingHeartbeatTimeout
|
2022-07-21 17:03:27 +00:00
|
|
|
}
|
2022-07-08 17:01:13 +00:00
|
|
|
return &Server{
|
2022-08-29 20:32:26 +00:00
|
|
|
Config: cfg,
|
|
|
|
Tracker: NewTracker(cfg.incomingHeartbeatTimeout),
|
2022-07-08 17:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireNotNil(v interface{}, name string) {
|
|
|
|
if v == nil {
|
|
|
|
panic(name + " is required")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ pbpeerstream.PeerStreamServiceServer = (*Server)(nil)
|
|
|
|
|
|
|
|
func (s *Server) Register(grpcServer *grpc.Server) {
|
|
|
|
pbpeerstream.RegisterPeerStreamServiceServer(grpcServer, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Backend interface {
|
|
|
|
Subscribe(req *stream.SubscribeRequest) (*stream.Subscription, error)
|
|
|
|
|
|
|
|
// IsLeader indicates whether the consul server is in a leader state or not.
|
|
|
|
IsLeader() bool
|
|
|
|
|
|
|
|
// SetLeaderAddress is called on a raft.LeaderObservation in a go routine
|
|
|
|
// in the consul server; see trackLeaderChanges()
|
|
|
|
SetLeaderAddress(string)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
GetLeaderAddress() string
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
ValidateProposedPeeringSecret(id string) (bool, error)
|
2022-08-08 07:41:00 +00:00
|
|
|
PeeringSecretsWrite(req *pbpeering.SecretsWriteRequest) error
|
2022-07-08 17:01:13 +00:00
|
|
|
PeeringTerminateByID(req *pbpeering.PeeringTerminateByIDRequest) error
|
|
|
|
PeeringTrustBundleWrite(req *pbpeering.PeeringTrustBundleWriteRequest) error
|
|
|
|
CatalogRegister(req *structs.RegisterRequest) error
|
|
|
|
CatalogDeregister(req *structs.DeregisterRequest) error
|
2022-08-22 14:22:11 +00:00
|
|
|
PeeringWrite(req *pbpeering.PeeringWriteRequest) error
|
2022-07-08 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StateStore provides a read-only interface for querying Peering data.
|
|
|
|
type StateStore interface {
|
|
|
|
PeeringRead(ws memdb.WatchSet, q state.Query) (uint64, *pbpeering.Peering, error)
|
|
|
|
PeeringReadByID(ws memdb.WatchSet, id string) (uint64, *pbpeering.Peering, error)
|
|
|
|
PeeringList(ws memdb.WatchSet, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.Peering, error)
|
|
|
|
PeeringTrustBundleRead(ws memdb.WatchSet, q state.Query) (uint64, *pbpeering.PeeringTrustBundle, error)
|
|
|
|
PeeringTrustBundleList(ws memdb.WatchSet, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.PeeringTrustBundle, error)
|
2022-08-01 14:33:18 +00:00
|
|
|
PeeringSecretsRead(ws memdb.WatchSet, peerID string) (*pbpeering.PeeringSecrets, error)
|
2022-07-08 17:01:13 +00:00
|
|
|
ExportedServicesForPeer(ws memdb.WatchSet, peerID, dc string) (uint64, *structs.ExportedServiceList, error)
|
|
|
|
ServiceDump(ws memdb.WatchSet, kind structs.ServiceKind, useKind bool, entMeta *acl.EnterpriseMeta, peerName string) (uint64, structs.CheckServiceNodes, error)
|
|
|
|
CheckServiceNodes(ws memdb.WatchSet, serviceName string, entMeta *acl.EnterpriseMeta, peerName string) (uint64, structs.CheckServiceNodes, error)
|
|
|
|
NodeServices(ws memdb.WatchSet, nodeNameOrID string, entMeta *acl.EnterpriseMeta, peerName string) (uint64, *structs.NodeServices, error)
|
|
|
|
CAConfig(ws memdb.WatchSet) (uint64, *structs.CAConfiguration, error)
|
|
|
|
TrustBundleListByService(ws memdb.WatchSet, service, dc string, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.PeeringTrustBundle, error)
|
2022-09-29 19:37:19 +00:00
|
|
|
ServiceList(ws memdb.WatchSet, entMeta *acl.EnterpriseMeta, peerName string) (uint64, structs.ServiceList, error)
|
2022-09-21 15:55:19 +00:00
|
|
|
ConfigEntry(ws memdb.WatchSet, kind, name string, entMeta *acl.EnterpriseMeta) (uint64, structs.ConfigEntry, error)
|
2022-07-08 17:01:13 +00:00
|
|
|
AbandonCh() <-chan struct{}
|
|
|
|
}
|