Documentation
¶
Overview ¶
Package toolexec runs the external tools Draugr orchestrates, and reports what it ran.
It exists so that every external invocation, scanners today, SBOM generation and whatever comes next, narrates itself the same way. Without that, a run is a black box: you can see that something failed but not the command, the directory, how long it took, or what the tool itself said about it.
Index ¶
- Constants
- Variables
- func Bound(ctx context.Context) int
- func CanBoundMemory() bool
- func DefaultJobs() int
- func Divide(ctx context.Context, commands int) (concurrent int, env []string)
- func LimitsThreads(ctx context.Context) bool
- func MemoryLimit() uint64
- func NextGoCommand(rest []string) (string, bool)
- func OwnPeakMemory() (uint64, bool)
- func Parallelism() int
- func PeakMemory() (uint64, bool)
- func Run(ctx context.Context, dir string, argv []string) ([]byte, error)
- func RunCombined(ctx context.Context, dir string, argv []string) ([]byte, error)
- func RunCombinedWithEnv(ctx context.Context, dir string, argv, env []string) ([]byte, error)
- func RunWithEnv(ctx context.Context, dir string, argv, env []string) ([]byte, error)
- func RunWithLog(ctx context.Context, dir string, argv, env []string, stream Log) (stdout, stderr []byte, err error)
- func RunWithStderr(ctx context.Context, dir string, argv, env []string) (stdout, stderr []byte, err error)
- func SharesWorkers(ctx context.Context) bool
- func WithJobs(ctx context.Context, n int) context.Context
- func WithMemoryBound(ctx context.Context, b MemoryBound) context.Context
- func WithMemoryPeak(ctx context.Context, p *MemoryPeak) context.Context
- func WithSharedWorkers(ctx context.Context) context.Context
- func WithThreadLimit(ctx context.Context) context.Context
- func Workers(ctx context.Context, def int) int
- type Log
- type MemoryBound
- type MemoryPeak
Constants ¶
const MemoryBoundAccepts = "a size such as 512MiB or 8GiB, or 0 for none"
MemoryBoundAccepts is what a memory bound takes, in the words a refusal of one uses.
Variables ¶
var ErrCannotStart = errors.New("could not start")
ErrCannotStart is a tool that was found and could not start: the shell's exit 127 for a command or interpreter it could not find, 126 for one it could not execute. A wrapper script whose Python environment is gone exits so, and so does a binary built for another platform. The tool never read its target, so a failure carrying this is about the machine, not the target.
var ErrKilled = errors.New("was killed")
ErrKilled is a tool ended by SIGKILL while the scan was still running. Nothing in the tool sends it, and the kernel does when memory runs out, killing the process using the most. A scan that was canceled kills its tools the same way, so a kill after the scan's context ended is not this.
var ErrOverMemoryBound = errors.New("used more memory than its bound")
ErrOverMemoryBound is a tool Draugr stopped because it used more memory than the run's bound (WithMemoryBound). It is ErrKilled too: the tool was ended by SIGKILL for its memory, and what a caller does about a kill, such as naming what went unread, it does about this.
Functions ¶
func Bound ¶ added in v0.175.0
Bound is the parallelism a tool run under ctx is given where the run limits tool threads (WithThreadLimit), and the pool size Workers holds a tool to: the bound divided among the jobs running at once (WithJobs), rounded down and at least one. Without that, each of j jobs would give its tool the whole bound, j times what the machine was sized for. A job running alone keeps it all.
func CanBoundMemory ¶ added in v0.191.0
func CanBoundMemory() bool
CanBoundMemory reports whether this platform enforces a memory bound. Where it does not, a run given one says so and runs its tools unbounded.
func DefaultJobs ¶ added in v0.175.0
func DefaultJobs() int
DefaultJobs is how many scan jobs a run starts at once when nobody chose: one per CPU this process may use, or one per memoryPerJob of the memory it may use, whichever is fewer, and at least one. The CPUs are GOMAXPROCS, which the Go runtime lowers to a container's CPU limit, and the memory is the lower of the machine's and a cgroup's limit, so a pod given four CPUs on a 64-core node runs four jobs rather than 64.
func Divide ¶ added in v0.166.3
Divide shares a build's parallelism among the `go` commands one tool runs at once, for a tool that runs up to commands of them. The parallelism is the job's bound (Bound) where the run limits tool threads (WithThreadLimit), and otherwise the CPUs this process may use, which is the -p one `go` command takes by default. It returns how many commands the tool should run at once, commands or that parallelism where it is fewer and at least one, and the environment that tool runs with: GOFLAGS with -p at the parallelism divided by that many, rounded down and at least one, after the caller's other flags. Each command then builds with at most that -p, so together they run as many compile processes as one `go` command would rather than that many for each.
Without the limit the machine is still divided, because the commands together still have every CPU, as one build would: more compile processes than CPUs have no CPU to add, and each holds its package's memory while it waits for one.
The environment is for the tool's run to pass as its own. Under the limit the bound is layered over it, and GOMAXPROCS stays the whole bound, because it sizes the tool's own threads and not its builds. A caller whose GOFLAGS sets -p keeps it, and the environment is then empty: that caller chose each command's parallelism, and the count returned still bounds how many commands run.
func LimitsThreads ¶ added in v0.185.0
LimitsThreads reports whether ctx asks tool runs to be held to their job's share of the bound (WithThreadLimit).
func MemoryLimit ¶ added in v0.178.0
func MemoryLimit() uint64
MemoryLimit is the memory this process may use, in bytes: the machine's, or a cgroup's limit where that is lower. Zero where the operating system reports neither.
func NextGoCommand ¶ added in v0.186.0
NextGoCommand returns the first non-blank line of rest, trimmed, where it is a go command a reader can run, as `go mod tidy` or `go mod download golang.org/x/text`, which the go command prints indented on the line after a message that offers it.
func OwnPeakMemory ¶ added in v0.178.0
OwnPeakMemory is the largest resident memory, in bytes, this process itself has reached, the tools it ran left out, and false where the operating system does not report it. It covers the life of this process.
Draugr's own memory is part of what a scan takes: it holds every finding the tools report, and reads and decodes their reports. A figure for the tools alone reads the same whether Draugr took a little of the machine or most of it.
func Parallelism ¶ added in v0.166.2
func Parallelism() int
Parallelism is the bound a tool run under the limit (WithThreadLimit) is given as GOMAXPROCS and as -p in GOFLAGS, where the caller set neither, computed once per process from the machine's cores and memory.
func PeakMemory ¶ added in v0.166.2
PeakMemory is the largest resident memory, in bytes, of any single process this one has run and waited for, including the processes those started and waited for, and false where the operating system does not report it. It is the largest one process reached, not a sum, and it covers the life of this process.
func Run ¶
Run executes argv and returns its stdout. dir sets the working directory; empty inherits the current one. Some tools resolve their target relative to the working directory rather than from an argument (gosec loads Go packages via `./...`), so for those the checkout has to be the cwd, not just a path passed in.
No shell is involved, exec.CommandContext, not "sh -c". And argv is built from typed config by the caller, never from user shell input.
func RunCombined ¶ added in v0.52.0
RunCombined is Run capturing stderr alongside stdout.
For a tool asked a question rather than told to scan. Several write their answer to stderr, `nuclei -templates-version` prints its whole reply there and nothing at all to stdout, so a caller reading only stdout gets an empty string and concludes the wrong thing. Keeping this separate from Run is deliberate: a scanner's stdout is a report to be parsed, and folding stderr into it would corrupt the parse for every tool that logs while it works.
func RunCombinedWithEnv ¶ added in v0.126.0
RunCombinedWithEnv is RunCombined with extra environment variables layered over the parent's.
For a tool that is told where to work through the environment and answers on stderr. notation reads its trust policy from NOTATION_CONFIG and says why verification failed on stderr, with nothing on stdout to parse, so a caller needs both halves at once.
func RunWithEnv ¶ added in v0.45.0
RunWithEnv is Run with extra environment variables layered over the parent's, each "K=V".
A tool that shells out to another tool cannot be told which context to work in through argv, kube-bench invokes kubectl itself, and kubectl takes its cluster from the environment. This is how a scanner points such a tool at the right target rather than whatever the machine happens to be configured for.
func RunWithLog ¶ added in v0.178.0
func RunWithLog(ctx context.Context, dir string, argv, env []string, stream Log) (stdout, stderr []byte, err error)
RunWithLog is RunWithStderr holding only the lines of stderr log keeps, and repeating only those it shows.
func RunWithStderr ¶ added in v0.135.1
func RunWithStderr(ctx context.Context, dir string, argv, env []string) (stdout, stderr []byte, err error)
RunWithStderr is RunWithEnv returning the tool's stderr beside its stdout, kept apart.
For a tool that reports on stdout and logs on stderr what it could not do, then exits 0. Trivy logs each Terraform module it failed to load and scans on without it, and nothing in its report says so. A caller that reads only stdout sees a clean scan of less than was there.
func SharesWorkers ¶ added in v0.178.0
SharesWorkers reports whether ctx asks tools to hold their own worker pools to the job's share (WithSharedWorkers).
func WithJobs ¶ added in v0.175.0
WithJobs returns a context saying that n scan jobs, the one running under it included, run at once, so the bound its tools get under the limit is a share of the machine's rather than all of it.
func WithMemoryBound ¶ added in v0.191.0
func WithMemoryBound(ctx context.Context, b MemoryBound) context.Context
WithMemoryBound returns a context under which every tool run is stopped once its resident memory, with that of the processes it starts, passes b. A zero bound is none.
It is a choice for the operator, off by default: with no bound a tool may use all the memory the machine gives it, and the kernel kills it only when that runs out. A bound is for a run inside a limit whose owner ends every process once one is killed, and for a run that should leave the rest of the machine its memory.
func WithMemoryPeak ¶ added in v0.178.0
func WithMemoryPeak(ctx context.Context, p *MemoryPeak) context.Context
WithMemoryPeak returns a context under which every tool run reports its process's peak to p, as well as to any MemoryPeak ctx already carries. A job's peak is then its own, and a run's covers its jobs.
func WithSharedWorkers ¶ added in v0.178.0
WithSharedWorkers returns a context under which a tool that sizes a worker pool of its own is held to its job's share of the bound for that pool (Workers), rather than left at its own default.
It is a choice for the operator, off by default. A pool such as Semgrep's analysis processes or Trivy's files in flight is sized to the machine whatever else runs, so beside other jobs it holds more memory and CPU than its share. Holding it to the share lowers both, and it makes a long run slower: the share is fixed when the tool starts, so a run that starts beside many short jobs keeps a small pool after they finish.
func WithThreadLimit ¶ added in v0.185.0
WithThreadLimit returns a context under which every tool run is given its job's share of the bound (Bound) as GOMAXPROCS and as -p in GOFLAGS, where the caller set neither, and a tool that runs several `go` commands at once divides that share among them (Divide).
It is a choice for the operator, off by default. Each tool otherwise sizes itself to the machine, which is the fastest a scan runs, and on a machine with many cores and little memory per core the jobs running at once can together run out of memory. The limit trades speed for that memory: a tool beside many other jobs may run on one thread.
func Workers ¶ added in v0.178.0
Workers is how many workers a tool runs under ctx where its own default is def: def, or the bound (Bound) where that is fewer, and at least one.
For a tool that sizes a pool of its own, of processes, threads or goroutines, which GOMAXPROCS does not reach on its own: a Python or OCaml tool's processes, or the files a Go tool reads at once. The bound is what the jobs running at once share, whatever their scanner.
Types ¶
type Log ¶ added in v0.178.0
type Log struct {
// Keep, when not nil, is asked of each line as the tool writes it, without its newline, and a
// line it refuses is dropped there and then. Nil holds every line.
//
// A tool asked for its debug or trace log, so that Draugr can read the one line that records a
// file it skipped, writes a line or more for every file it reads. Held whole, that log grows
// with the target, and the lines a caller reads are a handful of it.
Keep func(line []byte) bool
// Shown, when not nil, receives the lines held and returns the part Draugr may relay into its
// own log and quote in the error. The caller still receives every line held.
//
// Gitleaks at trace level logs the path of each file it skips, which is the only record of the
// skip, and in the same stream the value of each candidate secret it set aside. Relaying that
// stream as it is would copy those values into `--log-level trace` output and any `--log-file`.
Shown func([]byte) []byte
}
Log says which lines of a tool's stderr a run holds, and which of those Draugr repeats.
type MemoryBound ¶ added in v0.191.0
type MemoryBound struct {
// Bytes is the bound. Zero means none.
Bytes uint64
// Text is the bound as it was written, such as 8GiB, which an error quotes.
Text string
}
MemoryBound is the most resident memory one tool run may use, with the processes it starts, before Draugr stops it.
func MemoryBoundOf ¶ added in v0.191.0
func MemoryBoundOf(ctx context.Context) MemoryBound
MemoryBoundOf is the bound tool runs under ctx are held to, zero where there is none.
func ParseMemoryBound ¶ added in v0.191.0
func ParseMemoryBound(s string) (MemoryBound, error)
ParseMemoryBound reads a bound written as a whole number of KiB, MiB, GiB or TiB, such as 8GiB. "0" and "" mean no bound. The units are binary only: "8G" and "8GB" are refused rather than read as one of the two sizes each could mean.
type MemoryPeak ¶ added in v0.178.0
type MemoryPeak struct {
// contains filtered or unexported fields
}
MemoryPeak is the largest resident memory, in bytes, any one tool process run under a context carrying it (WithMemoryPeak) reached, together with the processes it waited for. The zero value has seen no process. It is safe for concurrent use.
func (*MemoryPeak) Bytes ¶ added in v0.178.0
func (p *MemoryPeak) Bytes() uint64
Bytes is the largest resident memory seen, zero where no process run under it reported one.