Skip to main content

Module audit

Module audit 

Source
Expand description

Audit trail — optional local log of every intercepted command.

Records are JSON Lines (one JSON object per line). Each record contains the AuditEvent fields written by the caller, plus metadata collected automatically at write time (AuditMeta: user, host, cwd, git repo and branch, schema version) and optional caller-provided details (AuditExtra: policy sources, effective challenge, deny reason).

Writes are safe under concurrency: each record is serialized into one buffer and written with a single write() on an O_APPEND descriptor, under an exclusive flock on Unix, so lines from parallel writers never interleave. The file is created with mode 0600. When the log grows past DEFAULT_MAX_AUDIT_BYTES it is rotated: audit.log becomes audit.log.1, .1 becomes .2, … and only the newest DEFAULT_MAX_AUDIT_FILES generations are kept (configurable with the audit_max_files setting of the log’s configuration directory, see max_files_for). audit show reads every generation.

A stored command is capped at MAX_STORED_COMMAND_BYTES; longer commands are truncated and the record carries command_bytes and the command_sha256 of the full command, so a few huge commands can never flush the history.

Secrets are redacted before a command is stored (see redact_secrets): mysql -pSECRET, --password=…, PGPASSWORD=… and other *_PASSWORD / *_TOKEN / *_SECRET / *_KEY assignments, user:pass@ in URLs, Authorization: headers, curl -u user:pass and well-known token formats (AWS AKIA…, GitHub ghp_…, Slack xox…-) become ***. A redacted record carries command_redacted: true, and its command_sha256 is computed over the original command, so a known command can still be matched against the log without storing the secret.

Identity: user is the account name of the real uid (from the password database, falling back to $USER), uid is the numeric uid, and env_user records $USER when it differs from user.

Older records (without the newer fields) are still readable through AuditEntry.

Modules§

deny_reason
The values of AuditExtra::deny_reason.

Structs§

AuditEntry
Read model for one audit record. Every field is optional so records written by older versions (or by hand) still load.
AuditEvent
A single audit log entry, as written by the interception paths.
AuditExtra
Optional details a caller can attach with log_event_with.
AuditFilter
Filters for audit show.
AuditMeta
Metadata collected automatically by log_event for every record.
AuditRead
Parsed audit records plus the number of lines that could not be parsed.

Enums§

AuditOutcome
The outcome of a challenge interaction.

Constants§

AUDIT_SCHEMA_VERSION
Version of the on-disk record format written by this build.
DEFAULT_MAX_AUDIT_BYTES
Size after which audit.log is rotated to audit.log.1 (10 MiB).
DEFAULT_MAX_AUDIT_FILES
Rotated generations kept by default (audit.log.1 … audit.log.5).
MAX_AUDIT_FILES_LIMIT
Largest accepted audit_max_files.
MAX_STORED_COMMAND_BYTES
Longest command stored verbatim in a record (8 KiB). Longer commands are truncated (at a character boundary) and hashed.
OUTCOME_VALUES
The outcome names accepted by AuditOutcome::parse (for help texts).

Functions§

clear_log
Clear the audit log (and every rotated generation).
collapse_events
Collapse records that share an event_id (the pre-challenge Cancelled record followed by the final outcome), keeping the last one.
collect_meta
Collect the automatic metadata for a record.
collect_meta_for
collect_meta for command.
current_uid
Real uid of this process (Unix).
filter_entries
Apply a filter.
format_table
Render records as a human-readable table.
git_info
Find the git repository root and current branch for dir by reading .git/HEAD directly (no subprocess).
log_event
Append an audit event to the log file as a JSON line.
log_event_with
Append an audit event plus caller-provided details as one JSON line.
log_files
Every existing log generation, oldest first (audit.log.N … audit.log.1, then audit.log). Generations beyond the configured limit that are still on disk are included too.
max_files
The number of rotated generations kept for an audit_max_files setting (clamped like max_files_for).
max_files_for
How many rotated generations are kept for the log at audit_path.
now_timestamp
Get the current timestamp in ISO 8601 format.
parse_since
Parse a --since value into a comparable UTC timestamp.
read_entries
Read every log generation (see log_files), oldest first.
read_log
Read and return all audit log lines.
redact_secrets
Replace secrets in a command line with *** before it is stored.
sha256_hex
Lowercase hex SHA-256 of data (small self-contained implementation so the audit log needs no extra dependency).
unescape_invisible
Undo crate::context::escape_invisible.