2020-08-07 22:30:32 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha512"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
|
|
"github.com/hashicorp/consul/lib"
|
|
|
|
"github.com/hashicorp/consul/types"
|
2020-08-07 23:24:51 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-08-07 22:30:32 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/shirou/gopsutil/host"
|
|
|
|
)
|
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
// newNodeIDFromConfig will pull the persisted node ID, if any, or create a random one
|
2020-08-07 22:30:32 +00:00
|
|
|
// and persist it.
|
2020-08-07 23:24:51 +00:00
|
|
|
func newNodeIDFromConfig(config *config.RuntimeConfig, logger hclog.Logger) (types.NodeID, error) {
|
2020-08-07 22:30:32 +00:00
|
|
|
if config.NodeID != "" {
|
2020-08-07 23:24:51 +00:00
|
|
|
nodeID := strings.ToLower(string(config.NodeID))
|
|
|
|
if _, err := uuid.ParseUUID(nodeID); err != nil {
|
|
|
|
return "", fmt.Errorf("specified NodeID is invalid: %w", err)
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
2020-08-07 23:24:51 +00:00
|
|
|
return types.NodeID(nodeID), nil
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For dev mode we have no filesystem access so just make one.
|
2020-08-07 23:24:51 +00:00
|
|
|
if config.DataDir == "" {
|
|
|
|
id, err := makeNodeID(logger, config.DisableHostNodeID)
|
|
|
|
return types.NodeID(id), err
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
// Load saved state, if any. Since a user could edit this, we also validate it.
|
|
|
|
filename := filepath.Join(config.DataDir, "node-id")
|
|
|
|
if _, err := os.Stat(filename); err == nil {
|
|
|
|
rawID, err := ioutil.ReadFile(filename)
|
2020-08-07 22:30:32 +00:00
|
|
|
if err != nil {
|
2020-08-07 23:24:51 +00:00
|
|
|
return "", err
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeID := strings.TrimSpace(string(rawID))
|
|
|
|
nodeID = strings.ToLower(nodeID)
|
2020-08-07 23:24:51 +00:00
|
|
|
if _, err = uuid.ParseUUID(nodeID); err != nil {
|
|
|
|
return "", fmt.Errorf("NodeID in %s is invalid: %w", filename, err)
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
2020-08-07 23:24:51 +00:00
|
|
|
return types.NodeID(nodeID), nil
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
id, err := makeNodeID(logger, config.DisableHostNodeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to create a NodeID: %w", err)
|
|
|
|
}
|
|
|
|
if err := lib.EnsurePath(filename, false); err != nil {
|
|
|
|
return "", err
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
2020-08-07 23:24:51 +00:00
|
|
|
if err := ioutil.WriteFile(filename, []byte(id), 0600); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to write NodeID to disk: %w", err)
|
|
|
|
}
|
|
|
|
return types.NodeID(id), nil
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// makeNodeID will try to find a host-specific ID, or else will generate a
|
|
|
|
// random ID. The returned ID will always be formatted as a GUID. We don't tell
|
|
|
|
// the caller whether this ID is random or stable since the consequences are
|
|
|
|
// high for us if this changes, so we will persist it either way. This will let
|
|
|
|
// gopsutil change implementations without affecting in-place upgrades of nodes.
|
2020-08-07 23:24:51 +00:00
|
|
|
func makeNodeID(logger hclog.Logger, disableHostNodeID bool) (string, error) {
|
|
|
|
if disableHostNodeID {
|
|
|
|
return uuid.GenerateUUID()
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get a stable ID associated with the host itself.
|
|
|
|
info, err := host.Info()
|
|
|
|
if err != nil {
|
2020-08-07 23:24:51 +00:00
|
|
|
logger.Debug("Couldn't get a unique ID from the host", "error", err)
|
|
|
|
return uuid.GenerateUUID()
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash the input to make it well distributed. The reported Host UUID may be
|
|
|
|
// similar across nodes if they are on a cloud provider or on motherboards
|
|
|
|
// created from the same batch.
|
2020-08-10 15:49:46 +00:00
|
|
|
buf := sha512.Sum512([]byte(strings.ToLower(info.HostID)))
|
|
|
|
id := fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
2020-08-07 22:30:32 +00:00
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
|
|
|
buf[10:16])
|
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
logger.Debug("Using unique ID from host as node ID", "id", id)
|
2020-08-07 22:30:32 +00:00
|
|
|
return id, nil
|
|
|
|
}
|