audittest

package
v0.0.64 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 9 Imported by: ? 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 audittest checks a component's audit output against the rules the audit package's event builder and audit handler middleware enforce.

A component runs the checker in its unit tests against every event type it declares. It logs the events through a logger built with audit.HandlerMiddleware and a buffer as the writer, then checks the buffer:

var buf bytes.Buffer
logger := slog.New(audit.HandlerMiddleware(audit.WithWriter(&buf))(slog.Default().Handler()))
// ... exercise the code that logs audit events through logger ...

checker := audittest.NewChecker(
	audittest.WithLegacyTypes("widget_update"),
	audittest.WithSecrets("s3cr3t-api-key"),
)
checker.AssertConforms(t, buf.Bytes())

Each failed rule is reported as a Violation that names the check, so a failure points at the rule that was broken. The checks are:

A component that does not yet follow every rule, such as one whose events predate the audit package's event builder, can turn checks off with WithSkippedChecks while it closes the gaps.

Stability

This package is Alpha stability. The API may change without notice.

Index

Constants

View Source
const (
	CheckJSON             = "json"
	CheckMessage          = "message"
	CheckLevel            = "level"
	CheckLogger           = "logger"
	CheckEnvelopeKeys     = "envelope_keys"
	CheckType             = "type"
	CheckOutcome          = "outcome"
	CheckReason           = "reason"
	CheckSchemaVersion    = "schema_version"
	CheckSize             = "size"
	CheckActorLength      = "actor_length"
	CheckRequestID        = "request_id"
	CheckPrincipalKind    = "principal_kind"
	CheckUserID           = "user_id"
	CheckAnonymousOutcome = "anonymous_outcome"
	CheckSecret           = "secret"
)

Names of the checks, as reported in Violation.Check.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

Checker checks audit lines. Create one with NewChecker.

func NewChecker

func NewChecker(opts ...Option) *Checker

NewChecker returns a Checker.

func (*Checker) AssertConforms

func (c *Checker) AssertConforms(tb testing.TB, output []byte)

AssertConforms reports a test error for every violation on every line of output, the bytes the audit handler middleware wrote. It also fails when output holds no audit line, so a test cannot pass by logging nothing.

func (*Checker) CheckLine

func (c *Checker) CheckLine(line []byte) error

CheckLine checks one audit line and returns its violations joined, or nil when it conforms. The line may or may not end with its newline.

func (*Checker) CheckOutput

func (c *Checker) CheckOutput(output []byte) error

CheckOutput checks every line of output and returns the violations joined, or nil when every line conforms. Like Checker.AssertConforms, it fails when output holds no audit line, so a test cannot pass by logging nothing.

func (*Checker) Violations

func (c *Checker) Violations(line []byte) []Violation

Violations returns every rule one audit line breaks. The line may or may not end with its newline.

type Option

type Option func(*Checker)

Option configures a Checker.

func WithExtraOutcomes

func WithExtraOutcomes(outcomes ...string) Option

WithExtraOutcomes lists outcomes the component declared to its event builder with audit.WithExtraOutcomes, which pass the outcome check.

func WithLegacyTypes

func WithLegacyTypes(types ...string) Option

WithLegacyTypes lists event types the component shipped before the <area>.<resource>.<verb> naming rule. A shipped type is never renamed, because SIEM rules match on it, so these pass the type check.

func WithSecrets

func WithSecrets(values ...string) Option

WithSecrets lists secret values, such as a token or key the test used, that must not appear anywhere in an audit line. Empty values are ignored. No violation's detail prints a secret: a value that holds one is shown as "[redacted]". A CheckSecret violation names a secret by its number, counting from 1 over the non-empty values in the order given, across every WithSecrets option.

func WithSkippedChecks

func WithSkippedChecks(checks ...string) Option

WithSkippedChecks turns off the named checks, given as Check constants, for a component whose output does not yet follow every rule, such as one that does not build its events with the audit package's event builder. Each skipped check is a gap to close; skip as few as the component needs.

type Violation

type Violation struct {
	// Check is the name of the broken check, one of the Check constants.
	Check string
	// Detail says how the line broke it.
	Detail string
}

Violation is one broken rule on one audit line.

func (Violation) Error

func (v Violation) Error() string

Error implements error.

Jump to

Keyboard shortcuts

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