2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
package metadata
|
2016-02-20 01:26:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-03-21 23:36:44 +00:00
|
|
|
"regexp"
|
2016-02-20 01:26:45 +00:00
|
|
|
"strconv"
|
2017-08-14 14:36:07 +00:00
|
|
|
"strings"
|
2016-02-20 01:26:45 +00:00
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2016-02-20 01:26:45 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
2016-03-27 00:58:12 +00:00
|
|
|
// Key is used in maps and for equality tests. A key is based on endpoints.
|
|
|
|
type Key struct {
|
2016-03-28 19:53:19 +00:00
|
|
|
name string
|
2016-03-27 00:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal compares two Key objects
|
|
|
|
func (k *Key) Equal(x *Key) bool {
|
2016-03-28 19:53:19 +00:00
|
|
|
return k.name == x.name
|
2016-03-27 00:58:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
// Server is used to return details of a consul server
|
|
|
|
type Server struct {
|
2022-08-31 21:38:42 +00:00
|
|
|
Name string // <node>.<dc>
|
|
|
|
ShortName string // <node>
|
|
|
|
ID string
|
|
|
|
Datacenter string
|
|
|
|
Segment string
|
|
|
|
Port int
|
|
|
|
SegmentAddrs map[string]string
|
|
|
|
SegmentPorts map[string]int
|
|
|
|
WanJoinPort int
|
|
|
|
LanJoinPort int
|
2022-08-25 16:44:58 +00:00
|
|
|
ExternalGRPCPort int
|
|
|
|
ExternalGRPCTLSPort int
|
2022-08-31 21:38:42 +00:00
|
|
|
Bootstrap bool
|
|
|
|
Expect int
|
|
|
|
Build version.Version
|
|
|
|
Version int
|
|
|
|
RaftVersion int
|
|
|
|
Addr net.Addr
|
|
|
|
Status serf.MemberStatus
|
|
|
|
ReadReplica bool
|
|
|
|
FeatureFlags map[string]int
|
2017-05-10 21:25:48 +00:00
|
|
|
|
|
|
|
// If true, use TLS when connecting to this server
|
|
|
|
UseTLS bool
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 00:58:12 +00:00
|
|
|
// Key returns the corresponding Key
|
2016-03-30 00:39:19 +00:00
|
|
|
func (s *Server) Key() *Key {
|
2016-03-27 00:58:12 +00:00
|
|
|
return &Key{
|
2016-03-28 19:53:19 +00:00
|
|
|
name: s.Name,
|
2016-03-27 00:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
// String returns a string representation of Server
|
|
|
|
func (s *Server) String() string {
|
2016-03-27 08:32:04 +00:00
|
|
|
var addrStr, networkStr string
|
2016-03-27 02:30:04 +00:00
|
|
|
if s.Addr != nil {
|
2016-03-27 08:32:04 +00:00
|
|
|
addrStr = s.Addr.String()
|
|
|
|
networkStr = s.Addr.Network()
|
2016-03-27 02:30:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 18:37:25 +00:00
|
|
|
return fmt.Sprintf("%s (Addr: %s/%s) (DC: %s)", s.Name, networkStr, addrStr, s.Datacenter)
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
var versionFormat = regexp.MustCompile(`\d+\.\d+\.\d+`)
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
// IsConsulServer returns true if a serf member is a consul server
|
|
|
|
// agent. Returns a bool and a pointer to the Server.
|
|
|
|
func IsConsulServer(m serf.Member) (bool, *Server) {
|
2016-02-20 01:26:45 +00:00
|
|
|
if m.Tags["role"] != "consul" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
datacenter := m.Tags["dc"]
|
2017-08-14 14:36:07 +00:00
|
|
|
segment := m.Tags["segment"]
|
2016-02-20 01:26:45 +00:00
|
|
|
_, bootstrap := m.Tags["bootstrap"]
|
2017-05-10 21:25:48 +00:00
|
|
|
_, useTLS := m.Tags["use_tls"]
|
|
|
|
|
2016-02-20 01:26:45 +00:00
|
|
|
expect := 0
|
2018-01-28 18:40:13 +00:00
|
|
|
expectStr, ok := m.Tags["expect"]
|
2016-02-20 01:26:45 +00:00
|
|
|
var err error
|
|
|
|
if ok {
|
2018-01-28 18:40:13 +00:00
|
|
|
expect, err = strconv.Atoi(expectStr)
|
2016-02-20 01:26:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
portStr := m.Tags["port"]
|
|
|
|
port, err := strconv.Atoi(portStr)
|
2016-02-20 01:26:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
segmentAddrs := make(map[string]string)
|
|
|
|
segmentPorts := make(map[string]int)
|
2020-01-29 18:21:38 +00:00
|
|
|
featureFlags := make(map[string]int)
|
2017-08-14 14:36:07 +00:00
|
|
|
for name, value := range m.Tags {
|
2017-09-01 00:39:46 +00:00
|
|
|
if strings.HasPrefix(name, "sl_") {
|
|
|
|
addr, port, err := net.SplitHostPort(value)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-01-28 18:40:13 +00:00
|
|
|
segmentPort, err := strconv.Atoi(port)
|
2017-08-14 14:36:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2017-09-01 00:39:46 +00:00
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
segmentName := strings.TrimPrefix(name, "sl_")
|
|
|
|
segmentAddrs[segmentName] = addr
|
|
|
|
segmentPorts[segmentName] = segmentPort
|
2021-05-19 19:07:24 +00:00
|
|
|
} else if strings.HasPrefix(name, featureFlagPrefix) {
|
|
|
|
featureName := strings.TrimPrefix(name, featureFlagPrefix)
|
2020-01-29 18:21:38 +00:00
|
|
|
featureState, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
featureFlags[featureName] = featureState
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
buildVersion, err := Build(&m)
|
2017-03-21 23:36:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
wanJoinPort := 0
|
|
|
|
wanJoinPortStr, ok := m.Tags["wan_join_port"]
|
2017-03-15 19:26:54 +00:00
|
|
|
if ok {
|
2018-01-28 18:40:13 +00:00
|
|
|
wanJoinPort, err = strconv.Atoi(wanJoinPortStr)
|
2017-03-15 19:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 16:44:58 +00:00
|
|
|
var externalGRPCPort, externalGRPCTLSPort int
|
|
|
|
externalGRPCPortStr, foundGRPC := m.Tags["grpc_port"]
|
|
|
|
externalGRPCTLSPortStr, foundGRPCTLS := m.Tags["grpc_tls_port"]
|
|
|
|
if foundGRPC {
|
|
|
|
externalGRPCPort, _ = strconv.Atoi(externalGRPCPortStr)
|
|
|
|
}
|
|
|
|
if foundGRPCTLS {
|
|
|
|
externalGRPCTLSPort, _ = strconv.Atoi(externalGRPCTLSPortStr)
|
|
|
|
}
|
|
|
|
// If either port tag was found, check to ensure that at least one port was valid.
|
|
|
|
if foundGRPC || foundGRPCTLS {
|
|
|
|
if externalGRPCPort < 1 && externalGRPCTLSPort < 1 {
|
2022-07-07 18:55:41 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
vsnStr := m.Tags["vsn"]
|
|
|
|
vsn, err := strconv.Atoi(vsnStr)
|
2016-02-20 01:26:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
raftVsn := 0
|
2018-01-28 18:48:21 +00:00
|
|
|
raftVsnStr, ok := m.Tags["raft_vsn"]
|
2017-03-14 17:43:43 +00:00
|
|
|
if ok {
|
2018-01-28 18:48:21 +00:00
|
|
|
raftVsn, err = strconv.Atoi(raftVsnStr)
|
2017-03-14 17:43:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2017-02-22 20:53:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 00:41:36 +00:00
|
|
|
// Check if the server is a non voter
|
2020-11-17 15:53:57 +00:00
|
|
|
// DEPRECATED - remove looking for the nonvoter tag eventually once we don't have to support
|
|
|
|
// read replicas running v1.8.x and below.
|
2018-09-20 00:41:36 +00:00
|
|
|
_, nonVoter := m.Tags["nonvoter"]
|
2020-11-17 15:53:57 +00:00
|
|
|
_, readReplica := m.Tags["read_replica"]
|
2018-09-20 00:41:36 +00:00
|
|
|
|
2016-02-20 01:26:45 +00:00
|
|
|
addr := &net.TCPAddr{IP: m.Addr, Port: port}
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
parts := &Server{
|
2022-08-25 16:44:58 +00:00
|
|
|
Name: m.Name,
|
|
|
|
ShortName: strings.TrimSuffix(m.Name, "."+datacenter),
|
|
|
|
ID: m.Tags["id"],
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Segment: segment,
|
|
|
|
Port: port,
|
|
|
|
SegmentAddrs: segmentAddrs,
|
|
|
|
SegmentPorts: segmentPorts,
|
|
|
|
WanJoinPort: wanJoinPort,
|
|
|
|
LanJoinPort: int(m.Port),
|
|
|
|
ExternalGRPCPort: externalGRPCPort,
|
|
|
|
ExternalGRPCTLSPort: externalGRPCTLSPort,
|
|
|
|
Bootstrap: bootstrap,
|
|
|
|
Expect: expect,
|
|
|
|
Addr: addr,
|
|
|
|
Build: *buildVersion,
|
|
|
|
Version: vsn,
|
|
|
|
RaftVersion: raftVsn,
|
|
|
|
Status: m.Status,
|
|
|
|
UseTLS: useTLS,
|
2020-11-17 15:53:57 +00:00
|
|
|
// DEPRECATED - remove nonVoter check once support for that tag is removed
|
|
|
|
ReadReplica: nonVoter || readReplica,
|
2020-01-29 18:21:38 +00:00
|
|
|
FeatureFlags: featureFlags,
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
|
|
|
return true, parts
|
|
|
|
}
|
2021-05-19 19:07:24 +00:00
|
|
|
|
2021-09-29 19:45:11 +00:00
|
|
|
// TODO(ACL-Legacy-Compat): remove in phase 2
|
2021-09-22 22:55:53 +00:00
|
|
|
const TagACLs = "acls"
|
|
|
|
|
2021-05-19 19:07:24 +00:00
|
|
|
const featureFlagPrefix = "ft_"
|
|
|
|
|
|
|
|
// AddFeatureFlags to the tags. The tags map is expected to be a serf.Config.Tags.
|
|
|
|
// The feature flags are encoded in the tags so that IsConsulServer can decode them
|
|
|
|
// and populate the Server.FeatureFlags map.
|
|
|
|
func AddFeatureFlags(tags map[string]string, flags ...string) {
|
|
|
|
for _, flag := range flags {
|
|
|
|
tags[featureFlagPrefix+flag] = "1"
|
|
|
|
}
|
|
|
|
}
|
2023-05-23 19:37:01 +00:00
|
|
|
|
|
|
|
func GetIP(addr net.Addr) []byte {
|
|
|
|
switch a := addr.(type) {
|
|
|
|
case *net.UDPAddr:
|
|
|
|
return []byte(a.IP.String())
|
|
|
|
case *net.TCPAddr:
|
|
|
|
return []byte(a.IP.String())
|
|
|
|
}
|
|
|
|
return []byte{}
|
|
|
|
}
|