config

package
v0.166.3 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 19 Imported by: 1 module, 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 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.

See https://github.com/draugr-dev/draugr/issues/129.

Index

Constants

View Source
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.

View Source
const FileName = "draugr.config.yaml"

FileName is the config a project may carry beside its Saga.

View Source
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.

View Source
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

func CurrentKey(key string) string

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

func Digest(data []byte) string

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

func Discovered(workDir string) []string

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

func Explicit(flag string) string

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

func Files(explicit, workDir string) []string

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 Get

func Get(doc []byte, key string) (string, bool)

Get returns the value at a dotted key, and whether it was there.

func On added in v0.158.1

func On(b *bool) bool

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

func ProjectFile(workDir string) string

ProjectFile is where Load looks for a project's file: FileName in the working directory.

func Set

func Set(doc []byte, key, value string) ([]byte, error)

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

func Unset(doc []byte, key string) ([]byte, error)

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

func Parse(data []byte, path string) (File, error)

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"`
}

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.

func (*Problems) Error added in v0.158.1

func (p *Problems) Error() string

Error renders the file, its problems one per line, and the hint.

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

type Resolved struct {
	File    File
	Sources []Source
}

Resolved is the configuration in effect, and the files it came from, nearest last.

func Load

func Load(explicit, workDir string) (Resolved, error)

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

func Resolve(sources []Source) Resolved

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.

func (Resolved) OfflineBy added in v0.161.0

func (r Resolved) OfflineBy() string

OfflineBy is the file whose `offline: true` is in force, or "" when no file turns it on: the most specific one that says true, which is the one `draugr config show` names for it.

func (Resolved) Overrides added in v0.158.1

func (r Resolved) Overrides() []Override

Overrides lists the settings a source makes that do not take effect: a false for a one-way setting that a less specific source turned on.

type ScanSettings added in v0.161.0

type ScanSettings struct {
	// Jobs caps how many scan jobs run at once, as --jobs does. Zero is one per CPU, the built-in
	// default. A pointer, so a more specific file's `0` restores the default over a less specific
	// file's cap.
	Jobs *int `yaml:"jobs,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.

Jump to

Keyboard shortcuts

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