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:
- CheckJSON: the line is one JSON object.
- CheckMessage, CheckLevel, CheckLogger: the message is "audit_event", the level is "AUDIT", and logger=audit is present.
- CheckEnvelopeKeys: the line has no top-level key outside the audit envelope, such as a trace id stamped by another handler.
- CheckType: the type is three dot-separated lower snake case segments, <area>.<resource>.<verb>, or is one of the component's shipped legacy types.
- CheckOutcome, CheckReason: the outcome is in the closed set or one declared with WithExtraOutcomes, and the reason, when present, is a bounded lower snake case token.
- CheckSchemaVersion: metadata.extra.schema_version is the integer audit.SchemaVersion.
- CheckSize, CheckActorLength: the line, newline included, is at most audit.MaxLineBytes, and every subjects value is a string that takes at most audit.MaxActorStringBytes once JSON-encoded.
- CheckRequestID: a request-scoped event has metadata.extra.request_id. An event whose actor is a system actor, and a refusal summary (a denied event whose metadata.extra.suppressed is a positive whole count), are not request scoped.
- CheckPrincipalKind, CheckUserID, CheckAnonymousOutcome: subjects has a known principal_kind; it has a string user_id for every kind except anonymous, and an anonymous event has no user_id of any type; an anonymous event's outcome is denied or error.
- CheckSecret: no value the test marks as secret appears anywhere in the line.
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 ¶
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 (*Checker) AssertConforms ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithExtraOutcomes lists outcomes the component declared to its event builder with audit.WithExtraOutcomes, which pass the outcome check.
func WithLegacyTypes ¶
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 ¶
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 ¶
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.