Documentation
¶
Overview ¶
Package policy is the gk guard policy engine.
Rules implement the Rule interface and register themselves via the package Registry. The engine evaluates every registered rule in parallel against an Input (the repo + context being checked) and aggregates Violations.
Design notes:
- Rules are stateless. Per-call state lives on Input / Violation.
- Severity is a stable integer — lower numeric value means more severe so Violations sort naturally (Error < Warn < Info).
- `gk guard check` (GD-08) is the CLI driver that builds Input, runs the Registry, and exits with a code derived from the worst Severity.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Input ¶
type Input struct {
// Runner is a git runner scoped to the target repo. Rules that need
// git data (commit lists, diff ranges, log output) use this.
Runner git.Runner
// WorkDir is the repo root; rules that spawn non-git subprocesses
// (scanners, linters) pass this as cwd.
WorkDir string
// Staged restricts evaluation to staged changes only. Pre-commit hook
// sets this true.
Staged bool
// History widens evaluation to every commit reachable from any ref.
// The default scope is the current content of the tracked files.
History bool
// Verify asks rules that can test a finding against its issuer (live
// credential checks) to do so. It adds network calls, so it is opt-in.
Verify bool
// ArchiveReaders is how many git processes read committed archives at
// once. Each holds the archive it rebuilds in memory. Zero means the
// rule's default.
ArchiveReaders int
}
Input is the context passed to every Rule's Evaluate method. Rules pick only the fields they need.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds rule instances. Callers construct a Registry via NewRegistry and register the rules they need.
func (*Registry) Evaluate ¶
Evaluate runs every registered rule in parallel and returns all Violations merged. Rule errors are collected separately — one failing rule does not abort the others. The Violations slice is sorted by (Severity asc, RuleID asc, File asc, Line asc) so the worst items come first and output is deterministic.
type Rule ¶
type Rule interface {
// Name returns a stable identifier used as RuleID in Violations and
// surfaced in config (.gk.yaml `policies.<name>:`).
Name() string
// Evaluate runs the rule against Input and returns zero or more
// Violations. An error is returned only for infrastructure failures
// (git command failed, filesystem error); policy breaches are
// expressed as Violations, not errors.
Evaluate(ctx context.Context, in Input) ([]Violation, error)
}
Rule is the contract every policy implementation satisfies.
type Severity ¶
type Severity int
Severity ranks a Violation. Lower values mean more severe so sorting a slice of Violations puts the most important items first.
type Verification ¶ added in v0.155.0
type Verification struct {
Provider string
Status string
Account string
AccountName string
URL string
ARN string
Identity string
IdentityName string
Code string
}
Verification is the outcome of a live credential check. Provider is aws, slack, or gcp. Status is one of valid, rejected, unknown, or unverifiable. Account is the AWS account, Slack team ID, or GCP project; ARN is the AWS caller; Identity is the Slack user ID. AccountName, URL, and IdentityName are the Slack workspace name, workspace URL, and user name. Code is the issuer's error code or a local reason, and on a valid GCP key the restriction it hit.
type Violation ¶
type Violation struct {
RuleID string // stable identifier matching Rule.Name()
Severity Severity // rule-assigned severity
File string // empty when the violation is not file-local
Line int // 0 when N/A
Commit string // introducing commit SHA for history scans; empty otherwise
Message string // human-readable one-liner
Hint string // optional fix suggestion
// Verification is set when Input.Verify ran a live check for this finding.
Verification *Verification
}
Violation describes a single policy breach. Severity is assigned by the rule; the engine does not override it.