2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-01-24 13:01:37 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2018-02-12 23:15:15 +00:00
|
|
|
"sync"
|
2018-01-24 13:01:37 +00:00
|
|
|
"time"
|
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-01-24 13:01:37 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/client/fingerprint"
|
2019-01-22 22:42:49 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/loader"
|
2019-01-23 14:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-10-11 00:08:57 +00:00
|
|
|
)
|
|
|
|
|
2018-01-24 13:01:37 +00:00
|
|
|
// FingerprintManager runs a client fingerprinters on a continuous basis, and
|
|
|
|
// updates the client when the node has changed
|
|
|
|
type FingerprintManager struct {
|
2018-10-04 19:08:20 +00:00
|
|
|
singletonLoader loader.PluginCatalog
|
|
|
|
getConfig func() *config.Config
|
|
|
|
node *structs.Node
|
|
|
|
nodeLock sync.Mutex
|
|
|
|
shutdownCh chan struct{}
|
2018-02-05 17:20:07 +00:00
|
|
|
|
2018-01-25 16:30:15 +00:00
|
|
|
// updateNodeAttributes is a callback to the client to update the state of its
|
2018-02-05 17:20:07 +00:00
|
|
|
// associated node
|
2018-12-01 16:10:39 +00:00
|
|
|
updateNodeAttributes func(*fingerprint.FingerprintResponse) *structs.Node
|
2018-01-25 16:30:15 +00:00
|
|
|
|
2020-05-15 15:09:01 +00:00
|
|
|
reloadableFps map[string]fingerprint.ReloadableFingerprint
|
|
|
|
|
2018-11-28 03:42:22 +00:00
|
|
|
logger log.Logger
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 23:02:52 +00:00
|
|
|
// NewFingerprintManager is a constructor that creates and returns an instance
|
|
|
|
// of FingerprintManager
|
2018-10-04 19:08:20 +00:00
|
|
|
func NewFingerprintManager(
|
|
|
|
singletonLoader loader.PluginCatalog,
|
|
|
|
getConfig func() *config.Config,
|
2018-02-05 23:02:52 +00:00
|
|
|
node *structs.Node,
|
|
|
|
shutdownCh chan struct{},
|
2018-12-01 16:10:39 +00:00
|
|
|
updateNodeAttributes func(*fingerprint.FingerprintResponse) *structs.Node,
|
2018-09-16 00:48:59 +00:00
|
|
|
logger log.Logger) *FingerprintManager {
|
2018-08-29 22:05:03 +00:00
|
|
|
|
2018-02-05 23:02:52 +00:00
|
|
|
return &FingerprintManager{
|
2018-10-04 19:08:20 +00:00
|
|
|
singletonLoader: singletonLoader,
|
2018-01-25 16:30:15 +00:00
|
|
|
getConfig: getConfig,
|
|
|
|
updateNodeAttributes: updateNodeAttributes,
|
|
|
|
node: node,
|
|
|
|
shutdownCh: shutdownCh,
|
2018-09-16 00:48:59 +00:00
|
|
|
logger: logger.Named("fingerprint_mgr"),
|
2020-05-15 15:09:01 +00:00
|
|
|
reloadableFps: make(map[string]fingerprint.ReloadableFingerprint),
|
2018-02-05 23:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:52:43 +00:00
|
|
|
// setNode updates the current client node
|
|
|
|
func (fm *FingerprintManager) setNode(node *structs.Node) {
|
|
|
|
fm.nodeLock.Lock()
|
|
|
|
defer fm.nodeLock.Unlock()
|
|
|
|
fm.node = node
|
|
|
|
}
|
|
|
|
|
2018-04-16 22:07:08 +00:00
|
|
|
// getNode returns the current client node
|
|
|
|
func (fm *FingerprintManager) getNode() *structs.Node {
|
|
|
|
fm.nodeLock.Lock()
|
|
|
|
defer fm.nodeLock.Unlock()
|
|
|
|
return fm.node
|
|
|
|
}
|
|
|
|
|
2018-03-19 16:50:18 +00:00
|
|
|
// Run starts the process of fingerprinting the node. It does an initial pass,
|
2020-10-12 12:47:05 +00:00
|
|
|
// identifying allowlisted and denylisted fingerprints/drivers. Then, for
|
2018-03-19 16:50:18 +00:00
|
|
|
// those which require periotic checking, it starts a periodic process for
|
|
|
|
// each.
|
2021-12-20 10:44:21 +00:00
|
|
|
func (fm *FingerprintManager) Run() error {
|
2018-03-19 16:50:18 +00:00
|
|
|
// First, set up all fingerprints
|
2021-12-20 10:44:21 +00:00
|
|
|
cfg := fm.getConfig()
|
2020-10-14 22:17:47 +00:00
|
|
|
// COMPAT(1.0) using inclusive language, whitelist is kept for backward compatibility.
|
2020-10-12 12:47:05 +00:00
|
|
|
allowlistFingerprints := cfg.ReadStringListToMap("fingerprint.allowlist", "fingerprint.whitelist")
|
|
|
|
allowlistFingerprintsEnabled := len(allowlistFingerprints) > 0
|
2020-10-14 22:17:47 +00:00
|
|
|
// COMPAT(1.0) using inclusive language, blacklist is kept for backward compatibility.
|
2020-10-12 12:47:05 +00:00
|
|
|
denylistFingerprints := cfg.ReadStringListToMap("fingerprint.denylist", "fingerprint.blacklist")
|
2018-01-24 13:01:37 +00:00
|
|
|
|
2021-12-20 10:44:21 +00:00
|
|
|
fm.logger.Debug("built-in fingerprints", "fingerprinters", fingerprint.BuiltinFingerprints())
|
2018-03-09 17:28:01 +00:00
|
|
|
|
2018-03-19 16:50:18 +00:00
|
|
|
var availableFingerprints []string
|
|
|
|
var skippedFingerprints []string
|
|
|
|
for _, name := range fingerprint.BuiltinFingerprints() {
|
2020-10-12 12:47:05 +00:00
|
|
|
// Skip modules that are not in the allowlist if it is enabled.
|
|
|
|
if _, ok := allowlistFingerprints[name]; allowlistFingerprintsEnabled && !ok {
|
2018-03-19 16:50:18 +00:00
|
|
|
skippedFingerprints = append(skippedFingerprints, name)
|
|
|
|
continue
|
|
|
|
}
|
2020-10-12 12:47:05 +00:00
|
|
|
// Skip modules that are in the denylist
|
|
|
|
if _, ok := denylistFingerprints[name]; ok {
|
2018-03-19 16:50:18 +00:00
|
|
|
skippedFingerprints = append(skippedFingerprints, name)
|
|
|
|
continue
|
|
|
|
}
|
2018-03-09 17:28:01 +00:00
|
|
|
|
2018-03-19 16:50:18 +00:00
|
|
|
availableFingerprints = append(availableFingerprints, name)
|
|
|
|
}
|
2018-01-24 13:01:37 +00:00
|
|
|
|
2021-12-20 10:44:21 +00:00
|
|
|
if err := fm.setupFingerprinters(availableFingerprints); err != nil {
|
2018-03-19 16:50:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(skippedFingerprints) != 0 {
|
2021-12-20 10:44:21 +00:00
|
|
|
fm.logger.Debug("fingerprint modules skipped due to allow/denylist",
|
2018-08-29 22:05:03 +00:00
|
|
|
"skipped_fingerprinters", skippedFingerprints)
|
2018-03-19 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:09:01 +00:00
|
|
|
// Reload will reload any registered ReloadableFingerprinters and immediately call Fingerprint
|
|
|
|
func (fm *FingerprintManager) Reload() {
|
|
|
|
for name, fp := range fm.reloadableFps {
|
|
|
|
fm.logger.Info("reloading fingerprinter", "fingerprinter", name)
|
|
|
|
fp.Reload()
|
|
|
|
if _, err := fm.fingerprint(name, fp); err != nil {
|
|
|
|
fm.logger.Warn("error fingerprinting after reload", "fingerprinter", name, "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 16:50:18 +00:00
|
|
|
// setupFingerprints is used to fingerprint the node to see if these attributes are
|
|
|
|
// supported
|
|
|
|
func (fm *FingerprintManager) setupFingerprinters(fingerprints []string) error {
|
|
|
|
var appliedFingerprints []string
|
|
|
|
|
|
|
|
for _, name := range fingerprints {
|
2018-09-16 00:48:59 +00:00
|
|
|
f, err := fingerprint.NewFingerprint(name, fm.logger)
|
2018-03-19 16:50:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
fm.logger.Error("error fingerprinting", "error", err, "fingerprinter", name)
|
2018-03-19 16:50:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
detected, err := fm.fingerprint(name, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// log the fingerprinters which have been applied
|
|
|
|
if detected {
|
|
|
|
appliedFingerprints = append(appliedFingerprints, name)
|
|
|
|
}
|
|
|
|
|
2022-09-26 19:14:19 +00:00
|
|
|
p, _ := f.Periodic()
|
2018-03-19 16:50:18 +00:00
|
|
|
if p {
|
2022-09-26 19:14:19 +00:00
|
|
|
go fm.runFingerprint(f, name)
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
2020-05-15 15:09:01 +00:00
|
|
|
|
|
|
|
if rfp, ok := f.(fingerprint.ReloadableFingerprint); ok {
|
|
|
|
fm.reloadableFps[name] = rfp
|
|
|
|
}
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
2018-03-19 16:50:18 +00:00
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
fm.logger.Debug("detected fingerprints", "node_attrs", appliedFingerprints)
|
2018-03-19 16:50:18 +00:00
|
|
|
return nil
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 16:50:18 +00:00
|
|
|
// runFingerprint runs each fingerprinter individually on an ongoing basis
|
2022-09-26 19:14:19 +00:00
|
|
|
func (fm *FingerprintManager) runFingerprint(f fingerprint.Fingerprint, name string) {
|
|
|
|
_, period := f.Periodic()
|
|
|
|
fm.logger.Debug("fingerprinting periodically", "fingerprinter", name, "initial_period", period)
|
2018-03-19 16:50:18 +00:00
|
|
|
|
|
|
|
timer := time.NewTimer(period)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
_, err := fm.fingerprint(name, f)
|
|
|
|
if err != nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
fm.logger.Debug("error periodic fingerprinting", "error", err, "fingerprinter", name)
|
2018-03-19 16:50:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-26 19:14:19 +00:00
|
|
|
_, period = f.Periodic()
|
|
|
|
timer.Reset(period)
|
2018-03-19 16:50:18 +00:00
|
|
|
case <-fm.shutdownCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fingerprint does an initial fingerprint of the client. If the fingerprinter
|
|
|
|
// is meant to be run continuously, a process is launched to perform this
|
|
|
|
// fingerprint on an ongoing basis in the background.
|
|
|
|
func (fm *FingerprintManager) fingerprint(name string, f fingerprint.Fingerprint) (bool, error) {
|
2018-12-01 16:10:39 +00:00
|
|
|
var response fingerprint.FingerprintResponse
|
2018-03-19 16:50:18 +00:00
|
|
|
|
|
|
|
fm.nodeLock.Lock()
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &fingerprint.FingerprintRequest{Config: fm.getConfig(), Node: fm.node}
|
2018-03-19 16:50:18 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
|
|
|
fm.nodeLock.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if node := fm.updateNodeAttributes(&response); node != nil {
|
2018-04-10 17:52:43 +00:00
|
|
|
fm.setNode(node)
|
2018-03-19 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Detected, nil
|
|
|
|
}
|