host

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package host is what the agent hands a behavior's provider builder: the platform it runs on and the shared things a provider reaches the host through. It is apart from package behavior so that the SDK, which imports behavior, does not pull in what only the agent needs.

Index

Constants

This section is empty.

Variables

View Source
var ErrCommandDisabled = errors.New(
	"command execution is disabled on this agent: set agent.command.enabled to true to allow it",
)

ErrCommandDisabled is the refusal of a command that a caller supplies, such as command exec or docker exec, on an agent where agent.command.enabled is false: Host.CommandEnabled.

Functions

func Build

func Build(
	b *behavior.Behavior,
	h Host,
) (any, error)

Build builds b's provider for h's platform, with b's settings in the host its builder gets, after building the provider of each behavior b requires for the same host. It returns nil and no error when b declares no provider for the platform. The agent builds every behavior's provider with it at startup, and the provider tests build theirs with it on a fake host.

func FromGohai

func FromGohai(
	ctx context.Context,
) bool

FromGohai reports whether gohai runs the command of ctx, through Sources. The tests of a behavior read it: a read of a host fact goes through gohai.

func Require

func Require[P any](
	h Host,
	b *behavior.Behavior,
) P

Require is the provider of a behavior that h's behavior declared with behavior.Requires, as P, its provider type. It is the zero value when that behavior has no provider on this platform, which a builder answers with behavior.ErrUnsupported.

func Settings

func Settings[T any](
	h Host,
) T

Settings is h's settings as T, the struct the behavior passed to behavior.Config. Inside a provider builder the behavior's own variable cannot be named, so this is how a builder reads them.

Types

type Exec

type Exec interface {
	// RunCmd runs name with args as the agent's own user.
	RunCmd(
		ctx context.Context,
		name string,
		args []string,
	) (string, error)
	// RunReadCmd runs name with args as the agent's own user to read the
	// host, as gohai runs a command: stdout is the data and stderr is in the
	// error, with LC_ALL=C, a timeout and a limit on the output. gohai's
	// runner of the host is this.
	RunReadCmd(
		ctx context.Context,
		name string,
		args []string,
	) ([]byte, error)
	// RunPrivilegedCmd runs name with args, through sudo when the agent's
	// privilege escalation is on. A behavior declares each program it runs
	// this way with behavior.Sudo.
	RunPrivilegedCmd(
		ctx context.Context,
		name string,
		args []string,
	) (string, error)
	// RunPrivilegedCmdWithStdin is RunPrivilegedCmd with stdin written to
	// the program's standard input: how a secret reaches a program, since
	// one in its arguments is visible to every user on the host.
	RunPrivilegedCmdWithStdin(
		ctx context.Context,
		name string,
		args []string,
		stdin string,
	) (string, error)
	// RunPrivilegedCmdFull is RunPrivilegedCmd with the standard output,
	// the standard error and the exit code apart, for a command whose
	// caller gets all three, such as docker exec. A command that exits
	// with a code other than 0 is not an error: err is a failure to run it.
	// stdin, when it is not empty, goes to the program's standard input, as
	// RunPrivilegedCmdWithStdin gives it.
	RunPrivilegedCmdFull(
		ctx context.Context,
		name string,
		args []string,
		stdin string,
	) (stdout string, stderr string, exitCode int, err error)
	// RunCmdFull runs name with args as the agent's own user, in cwd when it
	// is not empty, for at most timeout seconds, and returns its standard
	// output, standard error, exit code and duration apart. A program that
	// exits non-zero is a result, not an error.
	RunCmdFull(
		ctx context.Context,
		name string,
		args []string,
		cwd string,
		timeout int,
	) (*exec.CmdResult, error)
}

Exec is what a provider needs of the agent's command runner. It is declared here, where providers use it, rather than taken from the agent's own package, so a behavior names no type it cannot import and a test can fake it.

type Host

type Host struct {
	// Platform is the agent's platform as gohai's family.Detect names it, such as
	// debian or darwin.
	Platform behavior.PlatformName
	// Container is true when the agent runs in a container.
	Container bool
	// CommandEnabled is agent.command.enabled: whether the agent runs a
	// command that a caller supplies. A method that runs one, such as
	// docker exec, refuses when it is false.
	CommandEnabled bool
	// Hostname is the agent's hostname.
	Hostname string
	Logger   *slog.Logger
	// FS is the filesystem a provider reads and writes through.
	FS avfs.VFS
	// Exec runs commands, with sudo for RunPrivilegedCmd when enabled.
	Exec Exec
	// FileState records the files osapi deployed, in the bucket
	// FileStateBucket. It is nil when the agent has no store for it.
	FileState provider.FileState
	// FileStateBucket is the KV bucket of FileState.
	FileStateBucket string
	// Objects reads the objects that osapi stores, such as a file uploaded
	// with osapi client file upload. It is nil when the agent has no object
	// store.
	Objects Objects
	// Settings is the behavior's decoded settings, as declared with
	// behavior.Config, or its defaults before the config is read.
	Settings any
	// Required are the providers of the behaviors this one requires, by
	// behavior name, built for the same host. Require reads one.
	Required map[string]any
	// Facts are the agent's facts, by their JSON names, for a template that a
	// provider renders with behavior.Render. The agent gives the facts it
	// collected last; a test gives none, or fixed ones. Nil is no facts.
	Facts func() map[string]any
	// Now is the host's clock. The agent gives time.Now; a test gives a
	// fixed time, so an example that reads the time, such as an uptime from
	// the boot time, has one answer. Time reads it.
	Now func() time.Time
	// Resolver resolves names for gohai, such as the canonical FQDN. The
	// agent gives none, which is the system resolver; a test gives one that
	// answers without the network.
	Resolver source.Resolver
}

Host is one agent's host, as a provider builder sees it.

func (Host) Sources

func (h Host) Sources() source.Sources

Sources are the sources a gohai collector reads this host through: its filesystem, its commands, its clock, its platform as the family, and its resolver. A nil field is the real host's.

func (Host) Time

func (h Host) Time() time.Time

Time is the time on the host: h.Now, or time.Now when the host gives none.

type Objects

type Objects interface {
	// Object is the content of the object name, and the content type that
	// its uploader gave it: raw, or template. An object that is not there
	// is an error that wraps behavior.ErrNotFound.
	Object(
		ctx context.Context,
		name string,
	) (content []byte, contentType types.ContentType, err error)
}

Objects reads the objects of the object store of the fleet.

Directories

Path Synopsis
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL