policy

package
v0.166.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 6 Imported by: 0 modules, 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 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 NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) Evaluate

func (r *Registry) Evaluate(ctx context.Context, in Input) ([]Violation, []error)

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.

func (*Registry) Get

func (r *Registry) Get(name string) (Rule, bool)

Get returns the Rule registered under name, or (nil, false) if absent.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns every registered rule name sorted alphabetically.

func (*Registry) Register

func (r *Registry) Register(rule Rule) error

Register adds a Rule. Re-registering a Rule with an existing Name returns an error so callers catch accidental duplication. Rules written in tests can bypass this by calling Replace.

func (*Registry) Replace

func (r *Registry) Replace(rule Rule)

Replace installs a Rule under its Name, overwriting any existing entry. Useful for tests that stub out a built-in rule.

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.

const (
	SeverityError Severity = iota // 0 — blocks the check
	SeverityWarn                  // 1 — surfaced, does not block
	SeverityInfo                  // 2 — informational only
)

func (Severity) String

func (s Severity) String() string

String returns the stable lowercase name used in JSON output and logs.

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.

Directories

Path Synopsis
Package rules holds the built-in gk guard policy rule implementations.
Package rules holds the built-in gk guard policy rule implementations.

Jump to

Keyboard shortcuts

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