Documentation
¶
Overview ¶
Package config resolves TokenHound configuration once per run. Layers, lowest first: defaults, the project tokenhound.yaml at the scan root (or --config), the user file <user config dir>/tokenhound/config.yaml, TOKENHOUND_* environment variables and flags. Each set leaf replaces the one below it; lists replace.
Trust tiers (AD-24): the project file may come from a hostile repository, so network.*, alerts.*, hook.* and ping are ignored from it; it may not disable verification or set timeout, its limits (up to 4x their default) and verify.timeout may only rise above the trusted value, its grade.f_threshold_usd_per_day and verify.max_concurrency may only fall below it, its durations are bounded and its fail_on may only tighten the policy (all applied after every other layer); ${NAME} references in it stay literal and its paths must stay inside the project root. new_only: true is ignored from it too (it loosens fail_on). Every ignored value gives a warning that names the key and never the value.
Ignore files (docs/schema/ignore.v1.md): the project ignore file (ignore_file, or <root>/.tokenhound-ignore) and the user machine ignore file <user config dir>/tokenhound/ignore are parsed into Resolved.Ignore. A configured file that is missing, and a malformed rule, are issues "<file>:<line>: <message>" that never repeat the rule.
Errors and warnings never carry config values, environment values or foreign error text (AD-3). The package never imports the public tokenhound package: the public config package maps Resolved onto tokenhound.Options.
Index ¶
- Constants
- func DisplayPath(cwd, file string) string
- func EnvNames() []string
- func ParseBaseline(data []byte, display string) (*model.Baseline, []yamlschema.Issue)
- func ParseIgnore(data []byte, display string, scope model.IgnoreScope, prefix, source string) ([]model.IgnoreRule, []yamlschema.Issue)
- type Alerts
- type Error
- type FailOn
- type Flags
- type Grade
- type Hook
- type Input
- type Limits
- type Network
- type Path
- type Resolved
- type Tier
- type Verify
Constants ¶
const ( // ProjectFile is the project-tier config file at the scan root. ProjectFile = "tokenhound.yaml" // UserFile is the user-tier config file under the user config directory. UserFile = "tokenhound/config.yaml" // EnvPrefix starts every environment variable the config reads. EnvPrefix = "TOKENHOUND_" // SupportedVersion is the only config version this binary reads. SupportedVersion = 1 )
File names and limits.
const ( DefaultVerifyConcurrency = 8 DefaultVerifyTimeout = 10 * time.Second DefaultFThresholdUSD = 500 DefaultMaxFileBytes = 10 << 20 DefaultArchiveMaxBytes = 256 << 20 DefaultArchiveMaxDepth = 5 DefaultArchiveMaxEntries = 10000 DefaultDecodeMaxDepth = 3 DefaultHookMode = "block" DefaultHookOnError = "warn-allow" )
Defaults.
const ( // DetailProjectTier: the key is honoured only from the user tier. DetailProjectTier = "project_tier" // DetailFailOnLoosening: a project fail_on value would loosen the policy. DetailFailOnLoosening = "fail_on_loosening" // DetailGateLoosening: a project value would weaken the scan behind the gate // (verification off, a timeout, new_only on, a lower limit or verify timeout, a // higher grade threshold or verify.max_concurrency). DetailGateLoosening = "gate_loosening" // DetailEnvReference: a ${NAME} reference in the project tier is kept as a // literal (the value is used, not ignored). DetailEnvReference = "env_reference" // DetailAbsolutePath: a project path is absolute. DetailAbsolutePath = "absolute_path" // DetailParentTraversal: a project path climbs out of the root. DetailParentTraversal = "parent_traversal" // DetailProjectLimitCap: a project limit is above what the project tier may // raise it to (4x the default, at most the schema maximum). DetailProjectLimitCap = "project_limit_cap" )
Detail codes of config warnings.
const ( FlagNoVerify = "--no-verify" FlagFailOnStatus = "--fail-on-status" FlagFailOnWarnings = "--fail-on-warnings" FlagFailOnMinDollars = "--fail-on-min-dollars" FlagTimeout = "--timeout" FlagNewOnly = "--new-only" )
Flag names, for issues about flag values.
const ( // IgnoreFile is the default project ignore file at the scan root. IgnoreFile = ".tokenhound-ignore" // UserIgnoreFile is the user-tier machine ignore file under the user config // directory. UserIgnoreFile = "tokenhound/ignore" )
Ignore file names (docs/schema/ignore.v1.md).
const BaselineFile = ".tokenhound-baseline.json"
BaselineFile is the default baseline file at the scan root (docs/schema/baseline.v1.json).
Variables ¶
This section is empty.
Functions ¶
func DisplayPath ¶ added in v0.2.0
DisplayPath is how a file is named to the user: relative to cwd with forward slashes when it is inside cwd, else its absolute slash path.
func EnvNames ¶
func EnvNames() []string
EnvNames returns every environment variable the config reads, in table order.
func ParseBaseline ¶ added in v0.2.0
ParseBaseline parses and validates one baseline file. display names the file in issues, which read "<display>: <field>: <message>" (no line) and never contain a value from the file.
func ParseIgnore ¶ added in v0.2.0
func ParseIgnore(data []byte, display string, scope model.IgnoreScope, prefix, source string) ([]model.IgnoreRule, []yamlschema.Issue)
ParseIgnore parses one ignore file. display names the file in issues; scope is project or machine; prefix ("" or a slash path ending in "/") is prepended to the path and loc patterns of a project file that sits in a subdirectory of the root; source is recorded in each rule's Source. Issues read "<display>:<line>: <message>" and never contain the rule value.
Types ¶
type Alerts ¶
type Alerts struct {
SlackWebhookEnv string
TeamsWebhookEnv string
WebhookURLEnv string
OnStatuses []model.Status
Timeout time.Duration
}
Alerts is the alerts block (user tier only).
type Error ¶
type Error struct {
Issues []yamlschema.Issue
}
Error is a config that cannot be used. Each issue prints as "file:line: field: message" (or "NAME: field: message" for an environment variable or flag).
func BaselinePath ¶ added in v0.2.0
BaselinePath returns the baseline file of a resolved configuration: the baseline key from any tier (a project-tier value already kept lexically inside the root), or <root>/.tokenhound-baseline.json. A project-tier or default file that resolves through a symlink to a place outside root (the file itself, or the directory it would be written to) is an issue: a repository must not make TokenHound read or overwrite a file outside it. cwd names the file in the issue.
type Flags ¶
type Flags struct {
// Verify is false for --no-verify.
Verify *bool
FailOnStatus *string
FailOnWarnings *string
FailOnMinDollars *string
Timeout *string
// NewOnly is --new-only.
NewOnly *bool
}
Flags are the command line values that override config. A nil field is not set. String fields hold the raw flag text: lists are comma separated.
type Input ¶
type Input struct {
// Root is the project root: the scan root directory ("" means Cwd).
Root string
// ConfigFile replaces <Root>/tokenhound.yaml when set (--config); it is still
// project tier. A relative path is relative to Cwd and is displayed as given;
// relative paths inside it resolve against Root.
ConfigFile string
// Flags override everything else.
Flags Flags
// LookupEnv reads the environment (nil means os.LookupEnv).
LookupEnv func(string) (string, bool)
// UserConfigDir returns the user config directory (nil means os.UserConfigDir).
// An error skips the user tier.
UserConfigDir func() (string, error)
// ReadFile reads a config file or the CA bundle (nil means a bounded os read).
ReadFile func(string) ([]byte, error)
// Cwd resolves relative environment and flag paths ("" means os.Getwd).
Cwd string
// SkipBaseline leaves the baseline file unread (Resolved.Baseline nil, no
// baseline_missing warning): for the baseline verb, which must never read the
// baseline it replaces (AD-25), and for verbs that do not compare findings.
SkipBaseline bool
}
Input is what Resolve reads. Every lookup is injectable so tests are hermetic.
type Limits ¶
type Limits struct {
MaxFileBytes int64
ArchiveMaxBytes int64
ArchiveMaxDepth int
ArchiveMaxEntries int
DecodeMaxDepth int
}
Limits is the limits block.
type Resolved ¶
type Resolved struct {
Verify Verify
FailOn FailOn
Timeout time.Duration
Grade Grade
Limits Limits
Network Network
Hook Hook
Alerts Alerts
Ping bool
// Paths holds the resolved absolute path of each set path key (ignore_file,
// baseline, import.gitleaks, import.trufflehog, custom_detectors) and the tier
// it came from, so later stories can apply their own tier rules (Story 1.9 drops
// verify blocks from project-tier custom detectors).
Paths map[string]Path
// Ignore holds the parsed rules of the project ignore file (ignore_file, or
// <root>/.tokenhound-ignore) followed by those of the user machine ignore file
// <user config dir>/tokenhound/ignore.
Ignore []model.IgnoreRule
// NewOnly is new_only (--new-only): baselined findings do not count for fail_on.
NewOnly bool
// Baseline is the parsed baseline file (baseline, or
// <root>/.tokenhound-baseline.json); nil when the file is absent or SkipBaseline
// is set.
Baseline *model.Baseline
}
Resolved is the effective configuration.
func Defaults ¶
Defaults returns the defaults, with fail_on.warnings [timeout, file_too_large] when ci is set: a scan that was cut short, or that skipped a file because it is over limits.max_file_bytes (a key in it would go unseen), must not pass a CI gate. The project tier can only add to fail_on, never drop these (see docs/adr/0002).