rules

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package rules holds the rule engine and the rule catalogue.

A rule is a single-file affair: the struct, its registration, its documentation, and its tests all live together, and the same metadata that drives the engine renders the `quaddoc rules` reference page. There is no separate docs step to forget.

Rules are given a Project, never a lone unit. Several checks are only answerable across the whole set: whether a bind source is shared between units (QD001 and QD002), whether siblings can resolve each other (QD030), whether a name collides (QD032). See docs/spec-review.md finding F3.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExitCode

func ExitCode(findings []Finding) int

ExitCode maps findings to a process exit status: 0 clean, 1 warnings only, 2 any error. Notes alone do not fail a build.

func Register

func Register(r *Rule)

Register adds a rule to the catalogue. It panics on a duplicate or malformed ID, because both are programming errors that should never reach a release.

Types

type Confidence

type Confidence string

Confidence records whether a finding was reasoned from the units alone or confirmed against the host. The same rule can produce either, and the wording differs: without host context we say a thing may be true, with it we say it is.

const (
	// Possible means the finding was derived from the units alone.
	Possible Confidence = "possible"
	// Confirmed means host context established the fact.
	Confirmed Confidence = "confirmed"
)

type Config

type Config struct {
	// Disabled lists rule IDs to skip entirely.
	Disabled map[string]bool
	// SeverityOverride maps a rule ID to the severity to report it at.
	SeverityOverride map[string]Severity
}

Config controls which rules run and at what severity.

type Context

type Context struct {
	Project *ir.Project
	// Host is never nil. When no context was gathered it is an
	// unknown-everything implementation, so rules need no nil checks.
	Host hostctx.Context
	// BindSourceUsage counts how many units mount each bind source. Computed
	// once and shared, which is what keeps QD001 and QD002 from
	// contradicting each other.
	BindSourceUsage map[string]int
	// contains filtered or unexported fields
}

Context is what a rule is given: the whole project, the host context if one was gathered, and the analysis shared between rules.

func (*Context) Severity

func (c *Context) Severity(ruleID string, def Severity) Severity

Severity returns the effective severity for a rule, after configuration overrides and host-context downgrades.

type Engine

type Engine struct {
	Config Config
	Host   hostctx.Context
}

Engine runs the catalogue over a project.

func (*Engine) Run

func (e *Engine) Run(p *ir.Project) []Finding

Run executes every enabled rule and returns the findings, ordered deterministically so output does not depend on map iteration.

type Finding

type Finding struct {
	RuleID     string     `json:"rule"`
	Severity   Severity   `json:"-"`
	SeverityJS string     `json:"severity"`
	Confidence Confidence `json:"confidence"`
	// Unit is the path of the unit the finding concerns.
	Unit string `json:"unit"`
	// Line is where in that unit, or 0 when the finding is about the unit
	// as a whole rather than a particular line.
	Line int `json:"line,omitempty"`
	// Message states what is wrong, in one sentence.
	Message string `json:"message"`
	// Remediation is copy-pasteable, or an explicit statement that no
	// mechanical fix exists and what decision the user must make instead.
	Remediation string `json:"remediation"`

	// Fix carries the structured detail the fix engine needs, so that it
	// applies exactly what the rule decided rather than re-deriving it from
	// the prose. Empty for findings with no mechanical fix.
	Fix map[string]string `json:"-"`
}

Finding is one reported problem.

type Rule

type Rule struct {
	// ID is the QD### identifier.
	ID string
	// Summary is a one-line description, shown in listings.
	Summary string
	// Rationale explains why this matters, in prose, for the reference page.
	Rationale string
	// Citation names the documentation or observed behaviour the rule
	// encodes. A rule without one does not ship: see CLAUDE.md.
	Citation string
	// DefaultSeverity applies unless configuration overrides it.
	DefaultSeverity Severity
	// NeedsHostContext marks rules whose findings are only confirmed with a
	// host context, and which must degrade gracefully without one.
	NeedsHostContext bool
	// Fixable marks rules whose remediation is mechanically applicable and
	// provably semantics-preserving.
	Fixable bool
	// Check runs the rule.
	Check func(*Context) []Finding
}

Rule is one check over a project.

Metadata lives beside the implementation so the reference documentation cannot drift from what the code does.

func All

func All() []*Rule

All returns every registered rule, ordered by ID.

func Lookup

func Lookup(id string) (*Rule, bool)

Lookup finds a rule by ID.

type Severity

type Severity int

Severity is how seriously a finding should be taken. It drives the exit code.

const (
	// Note is informational: worth knowing, not worth blocking on.
	Note Severity = iota
	// Warning is a probable problem, or a certain one with a mild effect.
	Warning
	// Error is a problem that will stop the unit working as intended.
	Error
)

func DowngradeForSELinux

func DowngradeForSELinux(mode hostctx.SELinuxMode, def Severity) (Severity, bool)

DowngradeForSELinux applies the ladder from ADR-0004 to a severity that depends on SELinux being enforced.

Under a permissive kernel the label is still wrong, it simply is not being enforced today, and turning enforcing back on would break the container. So the finding drops to a note rather than disappearing. When SELinux is absent from the kernel the finding is meaningless and is suppressed entirely; the caller drops findings for which this returns false.

func ParseSeverity

func ParseSeverity(s string) (Severity, bool)

ParseSeverity converts the textual form used in configuration files.

func Worst

func Worst(findings []Finding) (Severity, bool)

Worst returns the highest severity among findings, and whether there were any.

func (Severity) String

func (s Severity) String() string

Directories

Path Synopsis
Command genkeys regenerates the known Quadlet key set from the installed podman-systemd.unit(5) manual page.
Command genkeys regenerates the known Quadlet key set from the installed podman-systemd.unit(5) manual page.

Jump to

Keyboard shortcuts

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