Documentation
¶
Overview ¶
Package config is Draugr's machine- and organization-level configuration, kept apart from the Saga on purpose.
A Saga describes an application: which repositories it has, how exposed a component is, which controls must pass. Those are facts about the software, they belong in its repository, and they are meant to differ between projects.
This file describes the machine or the organization running the scan: which build of a scanner to install, and what a control should default to before any project says otherwise. Those want to be *uniform*, and putting them in a Saga makes them diverge silently. A descriptor that can pin its own scanner version is a descriptor that can downgrade one until a finding disappears.
Index ¶
- Constants
- func CurrentKey(key string) string
- func DeepMerge(dst, src saga.ControllerSettings) saga.ControllerSettings
- func Digest(data []byte) string
- func Discovered(workDir string) []string
- func Explicit(flag string) string
- func Files(explicit, workDir string) []string
- func Get(doc []byte, key string) (string, bool)
- func On(b *bool) bool
- func OneWayKeys() []string
- func ProjectFile(workDir string) string
- func Set(doc []byte, key, value string) ([]byte, error)
- func Unset(doc []byte, key string) ([]byte, error)
- type CacheSettings
- type File
- type LogSettings
- type NotSetError
- type OutputSettings
- type Override
- type Problems
- type PublishSettings
- type Resolved
- type ScanSettings
- type Source
- type Supplied
- type ToolSettings
Constants ¶
const EnvVar = "DRAUGR_CONFIG"
EnvVar names a config file explicitly, which is the organization-wide lever: set it in a runner image and every pipeline picks up the same defaults with no per-repository change.
const FileName = "draugr.config.yaml"
FileName is the config a project may carry beside its Saga.
const KeysHint = "`draugr config keys` lists every key"
KeysHint closes a report of a key the file cannot hold, naming the command that lists every key this build has.
const KeysURL = "https://draugr.dev/docs/latest/reference/cli/#draugr-config"
KeysURL is where every key of the file is documented.
Variables ¶
This section is empty.
Functions ¶
func CurrentKey ¶ added in v0.160.0
CurrentKey is a dotted key with the older `controllers` spelling of its first segment replaced by `controls`, the name the section is read under.
func DeepMerge ¶
func DeepMerge(dst, src saga.ControllerSettings) saga.ControllerSettings
DeepMerge lays src over dst, recursing into nested maps so an override replaces only the keys it names. Exported because the same rule applies when these defaults meet a Saga's.
func Digest ¶ added in v0.162.0
Digest is the content digest of a file's bytes, in the form a report gives every other digest.
func Discovered ¶ added in v0.160.0
Discovered lists the files that exist where Load looks when no file is named, least specific first. With a file named, these are the ones it skips.
func Explicit ¶ added in v0.154.0
Explicit is the file named by `--config`, or by DRAUGR_CONFIG when the flag is empty, or "" when neither names one. Reading and editing both resolve it here, so `config set` writes the file a scan reads.
func Files ¶ added in v0.158.1
Files lists the files Load reads, least specific first: the explicit file alone when one is named, whether or not it exists, or else each discovered file that exists.
func On ¶ added in v0.158.1
On reports whether a boolean setting is present and true. An absent one is off.
func OneWayKeys ¶ added in v0.161.0
func OneWayKeys() []string
OneWayKeys lists the settings that only turn on across files, by the key a file writes them under.
func ProjectFile ¶ added in v0.158.1
ProjectFile is where Load looks for a project's file: FileName in the working directory.
func Set ¶
Set writes key=value into a configuration document, returning the new bytes.
The document is edited as a node tree rather than decoded and re-encoded, so **comments survive**. A `config set` that silently deleted the explanation somebody wrote next to a version pin would teach people not to use it, and they would go back to hand-editing the file this command exists to keep valid.
The key is a dotted path, `tools.trivy.version`, `controls.sca.mend.policy`. Intermediate mappings are created as needed, in block style, and an empty `{}` on the way becomes one too.
A value starting with `[` or `{` is a YAML flow list or mapping, `[library]` or `{config: x}`; anything else is one scalar, so a URL or a value holding `: ` is written as typed. The rule reads only the first character, because a value whose meaning depends on whether it happens to parse is a value nobody can predict.
func Unset ¶
Unset removes a key, and any mapping it leaves empty. A key the document does not hold is a *NotSetError and no document, so the caller writes nothing.
The cleanup matters: a `controls.sca.mend` left behind as an empty mapping after its last setting is removed is not the same file as one that never mentioned mend, and the difference shows up later as a scanner block that exists and configures nothing.
A key under `controls` also removes the same key under the older `controllers`, and the other way round, because both spellings load as one section.
Types ¶
type CacheSettings ¶ added in v0.66.0
type CacheSettings struct {
// Dir enables caching and says where. Empty leaves caching off, which stays the default:
// a cache is a promise that an unchanged input has an unchanged answer, and that is a promise
// somebody should opt into.
Dir string `yaml:"dir,omitempty"`
// TTL is how long an entry stays usable. Unset leaves the built-in default; `0s` written in
// the file means no expiry, as `--cache-ttl 0` does. A pointer, so the two are told apart and
// a more specific file's `0s` overrides a less specific file's duration.
TTL *time.Duration `yaml:"ttl,omitempty"`
// ReadOnly serves entries without writing them, for a run whose results the next run should
// not trust. Layered one way: a more specific file's `false` does not clear a less specific
// file's `true`, and Resolved.Overrides reports the `false` that was outranked.
ReadOnly *bool `yaml:"readOnly,omitempty"`
// RequireDigest refuses to cache an image named only by a tag, which can be rebuilt under the
// same name. Layered one way, as ReadOnly is.
RequireDigest *bool `yaml:"requireDigest,omitempty"`
}
CacheSettings configures result caching.
type File ¶
type File struct {
// Tools pins the build of each external scanner. Provisioning rather than behavior, and
// deliberately not readable from a Saga.
Tools map[string]ToolSettings `yaml:"tools,omitempty"`
// Controls are default settings merged *underneath* the Saga's, so a project overrides only
// the keys it cares about and inherits the rest. The same name and shape as a descriptor's
// `config.controls`.
Controls map[string]saga.ControllerSettings `yaml:"controls,omitempty"`
// Controllers is the older spelling, still read so no file breaks.
//
// Deprecated: write `controls`. Folded into Controls when a file is parsed, so nothing else has
// to know both names.
Controllers map[string]saga.ControllerSettings `yaml:"controllers,omitempty"`
// Cache is where scan results are reused between runs, and for how long.
//
// Here rather than in a Saga for the same reason as Tools: a cache directory is a fact about
// a machine or a runner image, not about the application being described. Two projects on one
// runner want the same cache; one project on two runners does not want its descriptor
// naming a path that exists on only one of them.
Cache CacheSettings `yaml:"cache,omitempty"`
// Publish holds defaults for where a run is delivered.
//
// Here rather than in a Saga because an endpoint is a fact about an organization, not about
// an application: forty projects pointing at one install would otherwise repeat the URL in
// forty descriptors and change it in forty places when the host moves.
Publish PublishSettings `yaml:"publish,omitempty"`
// Output is how a report is rendered for whoever reads it here.
//
// A rendering preference belongs to a person or a machine, not to an application: an auditor
// wants the evidence on every scan they run, and a pipeline wants the same listing for every
// repository it builds. Putting either in a descriptor asserts it on everyone else who scans
// that application.
Output OutputSettings `yaml:"output,omitempty"`
// Scan is how a scan runs on the machine doing it.
//
// How many scanners can run at once is a fact about a machine's CPUs and memory, not about the
// application: one descriptor scanned on a laptop and on a large runner wants a different answer
// from each.
Scan ScanSettings `yaml:"scan,omitempty"`
// Log is what Draugr writes to stderr about its own run.
//
// A log format is chosen by whatever reads the log, a person at a terminal or a collector in a
// pipeline, and the same descriptor is scanned by both.
Log LogSettings `yaml:"log,omitempty"`
// Offline says this machine makes no network calls, as --offline and $DRAUGR_OFFLINE do.
//
// Layered one way, as Cache.ReadOnly is: a more specific file's `false` does not clear a less
// specific file's `true`, and Resolved.Overrides reports the `false` that was outranked. A file
// may turn it on, because offline only narrows what a run does and every check it stops reports
// itself as not checked.
Offline *bool `yaml:"offline,omitempty"`
}
File is a parsed configuration.
func Parse ¶
Parse decodes and validates a configuration.
Strict: an unknown key is an error rather than something ignored. A misspelled setting that is silently dropped is a setting somebody believes is in force, and this file exists to make behavior uniform, a typo that quietly opts one machine out defeats the point of having it.
A failure is a *Problems naming every key at fault in the file's own terms, rather than the Go types it decodes into, returned beside whatever of the file did decode.
type LogSettings ¶ added in v0.161.0
type LogSettings struct {
// Level is the least severe record written: trace, debug, info, warn or error.
Level string `yaml:"level,omitempty"`
// Format is console, json or text.
Format string `yaml:"format,omitempty"`
}
LogSettings configures the log Draugr writes to stderr. Each has a `--log-*` flag that overrides it.
type NotSetError ¶ added in v0.160.0
type NotSetError struct {
// Key is the dotted key that was asked for.
Key string
}
NotSetError is Unset's answer for a key the document does not hold.
func (*NotSetError) Error ¶ added in v0.160.0
func (e *NotSetError) Error() string
type OutputSettings ¶ added in v0.96.0
type OutputSettings struct {
// View is what a console report shows: "findings" (a row each, with what argued with the band
// under it), "actions" (a row per thing to do) or "compact" (one line each).
View string `yaml:"view,omitempty"`
// Evidence also prints what stands behind the verdict, tool provenance, what each control
// measured against, the scanned revision, what the run cost. A pointer, so a more specific
// file's `false` is a setting that overrides a less specific file's `true`.
Evidence *bool `yaml:"evidence,omitempty"`
// Top caps how many rows the fix list shows. Zero means the built-in default rather than
// "show none": a file that omits a field is not asking for an empty list.
Top int `yaml:"top,omitempty"`
// OfferInstall is whether `draugr scan` on a terminal offers to install the scanners it is
// missing before it runs. Unset means it offers; answering "never" writes false.
OfferInstall *bool `yaml:"offerInstall,omitempty"`
// Tips is whether a console scan prints its contextual tips and its progress line, both of
// which --no-tips turns off. Unset means it prints them. A pointer, so a more specific file's
// `true` overrides a less specific file's `false`.
Tips *bool `yaml:"tips,omitempty"`
// Palette is the set of colors a terminal that shows full color is drawn in: "standard", the
// colors the HTML report and the website share, or "light-background", which keeps every
// color readable on a white terminal. Empty is the standard palette.
Palette string `yaml:"palette,omitempty"`
}
OutputSettings configures how the console renders a run. Each has a `--flag` that overrides it, except OfferInstall, which answers a question only a terminal is asked.
func (OutputSettings) Offers ¶ added in v0.154.0
func (o OutputSettings) Offers() bool
Offers reports whether a scan on a terminal may offer to install missing scanners.
type Override ¶ added in v0.158.1
type Override struct {
// Key is the dotted setting, e.g. "cache.readOnly".
Key string
// Path is the file whose value does not take effect.
Path string
// By is the less specific file whose value stays in force.
By string
}
Override is a setting a file makes that a less specific file outranks.
type Problems ¶ added in v0.158.1
type Problems struct {
// Path is the file.
Path string
// List holds one problem per line, each led by the dotted key it is about.
List []string
// Hint is where to look next, printed after the list.
Hint string
}
Problems is every reason one configuration file cannot be used, each naming the key it is about.
type PublishSettings ¶ added in v0.105.0
type PublishSettings struct {
// APIURL is where the draugr-api publisher posts when $DRAUGR_API_URL is unset.
//
// Who wrote it decides how far it is trusted, because the publisher sends its token there.
// From ~/.draugr/config.yaml, or the file --config or $DRAUGR_CONFIG names, it is the machine's
// answer. From the draugr.config.yaml in the directory being scanned, which is committed with the
// repository and changes by pull request, it is held to the rule for a url: in the Saga: used
// only on the machine's server's origin, or one $DRAUGR_API_ORIGINS lists.
//
// No token here, and there never will be. This file is committed alongside a project, and a
// credential in it is a credential in somebody's git history.
APIURL string `yaml:"apiUrl,omitempty"`
}
PublishSettings holds defaults for the publishers a run delivers to.
type Resolved ¶
Resolved is the configuration in effect, and the files it came from, nearest last.
func Load ¶
Load reads the configuration in effect.
An explicit path. `--config` or DRAUGR_CONFIG. Is used *alone*. Explicit means explicit: a runner image that names a config expects that config, not that one layered over whatever happens to be in the working directory.
Otherwise the home file is layered under the project file, so a personal default can be overridden by a repository that has an opinion. Missing files are not an error; a machine with no configuration is the normal case. Every file is read before an error is returned, so two broken files are reported together.
func Resolve ¶ added in v0.158.1
Resolve layers sources, least specific first, into the configuration in effect: the rule Load applies, for a caller that reads and checks the files itself.
func (Resolved) MergeControls ¶ added in v0.162.0
func (r Resolved) MergeControls(controls map[string]saga.ControllerSettings, components []saga.Component) (map[string]saga.ControllerSettings, []Supplied)
MergeControls lays a descriptor's controls over the control defaults in effect, and names the file behind each default that reached the result. controls is the descriptor's config.controls, and components its components, read for the controls they turn on.
A block supplies settings to a control the descriptor turns on, at the top level or on a component, and turns a control on itself only with `enabled: true`. A machine-wide scanner option would otherwise add its control to every scan on the machine, and the descriptor is what decides which controls run. For a control only components turn on, the defaults are laid under an `enabled: false` top-level block, so the components that turn it on inherit them and the rest stay off, as a descriptor written that way behaves.
The merge and the attribution are one function so they cannot disagree about which value won. A default reaches the result where the descriptor sets nothing at that setting or above it, and it is the most specific file's, the rule Resolve applies between files. A default the descriptor overrode, or one for a control nothing turns on, is attributed to nobody: it changed nothing the descriptor's digest is taken over. Files are returned least specific first, and a file that supplied nothing is left out.
type ScanSettings ¶ added in v0.161.0
type ScanSettings struct {
// Jobs caps how many scan jobs run at once, as --jobs does. Zero is the built-in default, one
// per CPU or per 2 GiB of memory, whichever is fewer. A pointer, so a more specific file's `0` restores the default over a less specific
// file's cap.
Jobs *int `yaml:"jobs,omitempty"`
// LimitToolThreads holds each tool's threads and Go build processes to its job's share of the
// machine, as --limit-tool-threads does. Off by default, when each tool sizes itself to the
// machine. A pointer, so a more specific file's `false` turns off a less specific file's `true`.
LimitToolThreads *bool `yaml:"limitToolThreads,omitempty"`
// share of the machine, as --share-workers does, and turns on LimitToolThreads with it. Off by
// default. A pointer, so a more specific file's `false` turns off a less specific file's `true`.
ShareWorkers *bool `yaml:"shareWorkers,omitempty"`
// ToolMemory is the most resident memory one tool run may use, with the processes it starts,
// before Draugr stops it, as --tool-memory sets: a whole number of KiB, MiB, GiB or TiB, such as
// 8GiB. Unset or 0 means no bound. A pointer, so a more specific file's `0` removes a less
// specific file's bound.
ToolMemory *string `yaml:"toolMemory,omitempty"`
}
ScanSettings configures how a scan runs.
type Source ¶
type Source struct {
Path string
File File
// Digest is the sha256 of the file's bytes as they were read, as `sha256:<hex>`. Empty for a
// source built in memory.
Digest string
}
Source records where a loaded file came from, so a reader can be told which one to edit.
type Supplied ¶ added in v0.162.0
type Supplied struct {
Source Source
// Settings are dotted from the control's name, as `secrets.gitleaks.history`, in order.
Settings []string
}
Supplied is a file whose control defaults reached a descriptor, and the settings that did.
type ToolSettings ¶
type ToolSettings struct {
// Version is the release to install, e.g. "0.69.3".
Version string `yaml:"version,omitempty"`
}
ToolSettings pins one external tool.