Documentation
¶
Overview ¶
Package integrity verifies the plexd binary and hook scripts by SHA-256 checksum, and the SSH host key by its OpenSSH fingerprint.
Package integrity verifies the plexd binary and hook scripts by SHA-256 checksum, and the SSH host key by its OpenSSH fingerprint.
Index ¶
- Constants
- func HashFile(path string) (string, error)
- func HexChecksum(wire string) (string, error)
- func HostKeyFingerprint(path string) (string, error)
- func SelfChecksum() (string, error)
- func SelfPath() (string, error)
- func WireChecksum(hexDigest string) (string, error)
- type CheckResult
- type Config
- type Store
- type Verifier
- func (v *Verifier) BinaryChecksum() string
- func (v *Verifier) Run(ctx context.Context, nodeID string) error
- func (v *Verifier) VerifyBinary(ctx context.Context, nodeID string) error
- func (v *Verifier) VerifyHook(ctx context.Context, nodeID, hookPath, expectedChecksum string) (bool, error)
- func (v *Verifier) VerifyHooksDir(ctx context.Context, nodeID string)
- func (v *Verifier) VerifyHostKey(ctx context.Context, nodeID string) error
- type ViolationReporter
Constants ¶
const DefaultVerifyInterval = 5 * time.Minute
DefaultVerifyInterval is the default interval between integrity verification runs.
Variables ¶
This section is empty.
Functions ¶
func HashFile ¶
HashFile computes the SHA-256 checksum of the file at path using streaming I/O. While it reads, the file can still be renamed, on Windows too (openForHash).
func HexChecksum ¶ added in v0.28.0
HexChecksum is the inverse of WireChecksum: it decodes a checksum in the wire form, the 32 raw bytes in standard-padded base64, into the hex digest HashFile returns. A digest the control plane delivered, such as a published hook version's, can then be compared with one this package computed and pinned the way a computed one is.
Anything that does not decode to exactly 32 bytes is refused, the empty string and a hex digest among them, because such a value names no SHA-256 digest and would pin a hook no script can match.
func HostKeyFingerprint ¶ added in v0.4.0
HostKeyFingerprint parses the OpenSSH private key at path and renders its public half as the canonical `SHA256:<base64>` fingerprint — the form `ssh-keygen -l` prints, the one the capability manifest already carries, and the one the integrity contract's fingerprint fields accept.
The fingerprint, not a file digest, is what identifies a host key: the same key re-serialised is a different PEM but the same identity, and it is the identity a peer pins.
func SelfChecksum ¶ added in v0.2.0
SelfChecksum computes the SHA-256 checksum of the currently running binary.
func SelfPath ¶ added in v0.36.0
SelfPath returns the path of the currently running binary with every symlink resolved. plexd up uses it when Config.BinaryPath is empty, so it is the store key of the binary's baseline and the artifact_id of its reports: an install that moves the binary to a new resolved path starts a new baseline rather than raising a violation.
func WireChecksum ¶ added in v0.4.0
WireChecksum re-encodes a hex digest from HashFile into the form the control plane's checksum fields carry: the 32 raw bytes in standard-padded base64.
Hex is this package's own currency — it is what the baseline store holds and what hook comparisons run on — but on the wire those fields are declared `format: byte`, so a hex string is decoded as base64 and yields 48 bytes instead of 32. The capability manifest refuses that outright, and while the heartbeat contract also documents a hex form, the deployed control plane answers it with 400 `binary_checksum_empty`. Base64 is the one encoding both operations accept, so it is the one this agent sends.
Types ¶
type CheckResult ¶
type CheckResult struct {
// Path is the filesystem path that was verified.
Path string
// Expected is the hex-encoded SHA-256 checksum that was expected.
Expected string
// Actual is the hex-encoded SHA-256 checksum that was computed.
Actual string
// OK is true when Expected matches Actual (or when establishing a new baseline).
OK bool
}
CheckResult holds the outcome of a file integrity check.
func VerifyFile ¶
func VerifyFile(path, expectedChecksum string, requireChecksum bool) (CheckResult, error)
VerifyFile computes the SHA-256 checksum of the file at path and compares it against expectedChecksum. When requireChecksum is true and expectedChecksum is empty, an error is returned (hooks must have a control-plane-provided checksum). When requireChecksum is false and expectedChecksum is empty, the computed checksum is returned as a new baseline with OK=true.
type Config ¶
type Config struct {
// Enabled controls whether Run makes the baseline checks of the binary,
// the hooks directory and the SSH host key. nil means use default (true);
// explicit false disables them. VerifyHook, which the executor calls
// before a hook runs, does not read it.
//
// It is a tri-state for the reason actions.Config.Enabled is: with a plain
// bool an operator's `enabled: false` is indistinguishable from an omitted
// key, and defaulting would turn it back on.
Enabled *bool `yaml:"enabled"`
// BinaryPath is the path to the plexd binary to verify. Empty means plexd
// up verifies the running executable, resolved through symlinks
// (SelfPath). The path is the baseline's store key and the report's
// artifact_id.
BinaryPath string `yaml:"binary_path"`
// HooksDir is the directory containing hook scripts to verify. Empty
// means no sweep and no watcher: the hooks directory is checked only
// where this field or PLEXD_HOOKS_DIR names one. It does not mirror
// actions.hooks_dir.
HooksDir string `yaml:"hooks_dir"`
// HostKeyPath is the SSH host key whose fingerprint is verified. It is
// derived from the agent's data dir rather than configured, so it is kept
// off the YAML surface.
HostKeyPath string `yaml:"-"`
// VerifyInterval is the interval between integrity verification runs.
// Must be at least 30s when enabled.
// Default: 5m
VerifyInterval time.Duration `yaml:"verify_interval"`
// WatchEnabled controls whether inotify file watching is active.
// When enabled, file changes in HooksDir trigger immediate checksum
// recomputation instead of waiting for the next periodic verification.
// It has no effect while HooksDir is empty.
// nil means use default (true); explicit false disables the watcher.
WatchEnabled *bool `yaml:"watch_enabled"`
}
Config holds the configuration for integrity verification.
func (*Config) ApplyDefaults ¶
func (c *Config) ApplyDefaults()
ApplyDefaults sets default values for zero-valued fields.
func (*Config) IsEnabled ¶ added in v0.36.0
IsEnabled returns the effective Enabled setting: true unless explicitly set to false.
func (*Config) IsWatchEnabled ¶ added in v0.36.0
IsWatchEnabled returns the effective WatchEnabled setting: true unless explicitly set to false.
func (Config) MarshalYAML ¶ added in v0.36.0
MarshalYAML renders the effective Enabled and WatchEnabled values so a dump of the live config never reports either switch as null. config.dump marshals the whole agent config, and a null there reads as "unset", which for these fields reads as off while it means on.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store persists known-good checksums as a JSON file in the agent's data directory.
func NewStore ¶
NewStore creates a Store backed by dataDir/checksums.json. If the file does not exist, an empty store is created.
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier orchestrates integrity verification for the plexd binary, the hook scripts, and the SSH host key.
func NewVerifier ¶
func NewVerifier(cfg Config, store *Store, reporter ViolationReporter, logger *slog.Logger) *Verifier
NewVerifier creates a Verifier with the given configuration, store, reporter, and logger.
func (*Verifier) BinaryChecksum ¶
BinaryChecksum returns the last computed binary checksum (thread-safe). Returns an empty string before any verification has run.
func (*Verifier) Run ¶
Run verifies the binary, the hooks directory, and the SSH host key once at start and then on every tick of VerifyInterval. When the watch is enabled and HooksDir is set, it also monitors the hooks directory via inotify for real-time change detection. Run returns at once when verification is disabled, and otherwise blocks until the context is cancelled and the watcher has stopped, so a caller that waits for Run waits for the watcher too.
func (*Verifier) VerifyBinary ¶
VerifyBinary computes the binary checksum, compares against the stored baseline, and reports a violation on mismatch. On first run (no baseline), the checksum is stored as the new baseline. Once the control plane has accepted a report, the observed checksum becomes the baseline, so one change is reported once; a mismatch whose digests cannot be put on the wire is adopted without a report.
func (*Verifier) VerifyHook ¶
func (v *Verifier) VerifyHook(ctx context.Context, nodeID, hookPath, expectedChecksum string) (bool, error)
VerifyHook verifies a hook script against the expected checksum from the control plane. Returns true if the hook is safe to execute, false if there is a mismatch. An error is returned if the expected checksum is empty (hooks require a checksum).
func (*Verifier) VerifyHooksDir ¶
VerifyHooksDir computes checksums for all files in the hooks directory, compares against stored baselines, and reports violations on mismatch. Violations are attributed to the scanning detector; the fsnotify watcher enters the same sweep through verifyHooksDir with the inotify detector. An empty HooksDir, or one that does not exist, is a no-op.
func (*Verifier) VerifyHostKey ¶ added in v0.4.0
VerifyHostKey computes the SSH host key's fingerprint, compares it against the stored baseline, and reports a violation on mismatch. On first run the fingerprint becomes the baseline, as for the binary.
A key that changes under a running agent is the tamper signal: the SSH server keeps serving the key it loaded at startup, so a divergence on disk means something replaced it. The fingerprint identifies the key, not the file — the same key re-serialised keeps its fingerprint, which is why this is not a checksum comparison. An unset HostKeyPath or an absent file is a no-op: a node that never started the tunnel has no key to watch. Once the control plane has accepted a report, the observed fingerprint becomes the baseline.
type ViolationReporter ¶
type ViolationReporter interface {
ReportViolations(ctx context.Context, nodeID string, reports []api.IntegrityViolationReport) error
}
ViolationReporter abstracts control plane violation reporting for testability.
The contract's ingest endpoint takes a batch, so the interface does too: a directory sweep that finds three tampered hooks delivers them as one request rather than three.