audit

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 21 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 audit provides audit logging types and storage.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("audit entry not found")

ErrNotFound means no audit entry exists under the identifier asked for. The API answers 404, and it asks by cause rather than by reading the message, which is how every other layer here reports a thing that is not there.

Functions

func JobIDFrom

func JobIDFrom(
	ctx context.Context,
) string

JobIDFrom returns the job a request created, or the empty string when it created none.

func LocalUser added in v0.3.0

func LocalUser() string

LocalUser is the name of the user of the operating system who runs a local command, or "unknown".

func RecordJobID

func RecordJobID(
	ctx context.Context,
	jobID string,
)

RecordJobID notes the job a request created. Called by the job client for every dispatch; a context with no sink — a background task, an agent, a test — is left alone.

The first job wins. A broadcast is one job across many agents, and a request that somehow dispatches twice is better described by the job it started with than by the last one it happened to make.

func RecordLocal added in v0.3.0

func RecordLocal(
	ctx context.Context,
	record Recorder,
	action string,
	target string,
	summary string,
)

RecordLocal writes, with record, the entry of a change that a local command made, with the user of the operating system as the actor. A write that fails does not fail the command, which already made the change. A nil record writes nothing.

func RecordSteps added in v0.3.0

func RecordSteps(
	ctx context.Context,
	jobID string,
	hostname string,
	steps []types.Step,
)

RecordSteps notes the steps of one host that answered the job of a request. Called by the job client for each response that it collects, also when the job failed on the host. Only the steps of the job that RecordJobID recorded are kept, and a context with no sink is left alone.

func SensitiveFields added in v0.3.0

func SensitiveFields() map[string]bool

SensitiveFields are the names of the fixed denylist: the names whose values the summary redacts in each request. A guard of the DSLs reads it: a string input field with one of these names holds a secret, and is a secret.Value. It is a copy.

func Summarize

func Summarize(
	body []byte,
	contentType string,
) string

Summarize returns what a request asked for, in a form an audit log can keep: the JSON body with sensitive values replaced and the whole thing capped.

A body that is not JSON is described rather than stored, because an audit entry is not the place to keep an arbitrary upload. An empty body summarizes to nothing at all, so a request with no body adds no field.

func WithJobIDSink

func WithJobIDSink(
	ctx context.Context,
) context.Context

WithJobIDSink returns a context that collects the job ID a request creates.

Types

type Actor added in v0.3.0

type Actor string

Actor is the kind of who made a change that did not go through the API.

const (
	// ActorSystem is osapi itself: auto_admit, delete_after.
	ActorSystem Actor = "system"
	// ActorJoinToken is a join token that admitted a machine.
	ActorJoinToken Actor = "join-token"
	// ActorLocal is a person who ran a command on the controller's host,
	// named by the user of the operating system.
	ActorLocal Actor = "local"
	// ActorAgent is a machine that asked for a change itself, named by its
	// machine ID: an enrollment request, a renewal of its NATS user, or a
	// hostname that it reported.
	ActorAgent Actor = "agent"
)

The actors of a change that did not go through the API. A request through the API has none: its User is the subject of its token.

type Entry

type Entry struct {
	// ID is the unique identifier for this audit entry.
	ID string `json:"id"`
	// Timestamp is when the request was processed.
	Timestamp time.Time `json:"timestamp"`
	// User is who did it: the authenticated subject (from JWT sub claim) of
	// a request, or the name of the actor of another kind.
	User string `json:"user"`
	// Actor is the kind of who did it: empty for a request through the API,
	// or system, join-token or local (osapi-io/osapi#1425).
	Actor Actor `json:"actor,omitempty"`
	// Action is what a change that did not go through the API did, such as
	// agent.admit or jointoken.create.
	Action string `json:"action,omitempty"`
	// Target is what the action was on, such as a machine ID or a token ID.
	Target string `json:"target,omitempty"`
	// Roles are the roles from the JWT token.
	Roles []types.Role `json:"roles"`
	// Targets are the target scope from the JWT token, empty when the token
	// acts on each host.
	Targets []string `json:"targets,omitempty"`
	// Method is the HTTP method (GET, POST, PUT, DELETE).
	Method string `json:"method"`
	// Path is the request URL path.
	Path string `json:"path"`
	// OperationID is the OpenAPI operation ID (if available).
	OperationID string `json:"operation_id,omitempty"`
	// SourceIP is the client's IP address.
	SourceIP string `json:"source_ip"`
	// ResponseCode is the HTTP response status code.
	ResponseCode int `json:"response_code"`
	// DurationMs is the request processing time in milliseconds.
	DurationMs int64 `json:"duration_ms"`
	// TraceID is the OpenTelemetry trace ID for correlation.
	TraceID string `json:"trace_id,omitempty"`
	// JobID is the job the request created, empty for a request that created
	// none. It is what joins an audit entry to the job's own status timeline.
	JobID string `json:"job_id,omitempty"`
	// Steps are the calls of other behaviors that the job's method made, for
	// each host that answered before the request returned, also when one
	// failed. Empty when no host made a call.
	Steps []HostSteps `json:"steps,omitempty"`
	// RequestSummary is what the request asked for, with sensitive values
	// replaced. Recorded for methods that change something, because "who ran
	// command/shell against which host" without the command is not an answer.
	RequestSummary string `json:"request_summary,omitempty"`
}

Entry represents a single audit log record.

func Change added in v0.3.0

func Change(
	ctx context.Context,
	actor Actor,
	name string,
	action string,
	target string,
	code int,
	summary string,
) Entry

Change is the entry of a change that did not go through the API: actor and name (user) did action on target, with the outcome code, an HTTP status such as 200 or 403, and summary, which holds no secret.

type HostSteps added in v0.3.0

type HostSteps struct {
	// Hostname is the host that answered the job.
	Hostname string `json:"hostname"`
	// Steps are the calls, in the order that the method made them.
	Steps []types.Step `json:"steps"`
}

HostSteps are the calls of other behaviors that the job's method made on one host.

func StepsFrom added in v0.3.0

func StepsFrom(
	ctx context.Context,
) []HostSteps

StepsFrom returns the steps of each host that answered the job of a request, sorted by hostname, or nil when no host made a call.

type Recorder added in v0.3.0

type Recorder func(ctx context.Context, e Entry) error

Recorder writes an entry to the audit log of the controller. A local command gets one from its deps, or nil when the controller keeps no log.

type Store

type Store interface {
	// Write persists an audit entry.
	Write(
		ctx context.Context,
		entry Entry,
	) error

	// Get retrieves a single audit entry by ID.
	Get(
		ctx context.Context,
		id string,
	) (*Entry, error)

	// List retrieves audit entries with pagination.
	// Returns the entries, total count, and any error.
	List(
		ctx context.Context,
		limit int,
		offset int,
	) ([]Entry, int, error)

	// ListAll retrieves all audit entries without pagination.
	ListAll(
		ctx context.Context,
	) ([]Entry, error)
}

Store defines the interface for audit log persistence.

type Stream added in v0.3.0

type Stream interface {
	Publish(ctx context.Context, subject string, data []byte) error
	StreamInfo(ctx context.Context, name string) (*jetstream.StreamInfo, error)
	StreamLastMsg(ctx context.Context, name string, subject string) (*jetstream.RawStreamMsg, error)
	StreamRead(ctx context.Context, name string, from uint64, count int) ([]jetstream.Msg, error)
}

Stream is the access to NATS that StreamStore uses: it publishes each entry to the audit stream, and reads the stream back. A *natsclient.Client is one.

type StreamStore

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

StreamStore implements Store backed by a NATS JetStream stream.

func NewStreamStore

func NewStreamStore(
	logger *slog.Logger,
	nc Stream,
	stream string,
	subject string,
) *StreamStore

NewStreamStore creates a new StreamStore.

func (*StreamStore) Get

func (s *StreamStore) Get(
	ctx context.Context,
	id string,
) (*Entry, error)

Get returns one audit entry, in an audit.get span.

func (*StreamStore) List

func (s *StreamStore) List(
	ctx context.Context,
	limit int,
	offset int,
) ([]Entry, int, error)

List returns a page of audit entries and the total, in an audit.list span.

func (*StreamStore) ListAll

func (s *StreamStore) ListAll(
	ctx context.Context,
) ([]Entry, error)

ListAll returns every audit entry, for an export, in an audit.export span.

func (*StreamStore) Write

func (s *StreamStore) Write(
	ctx context.Context,
	entry Entry,
) error

Write persists an audit entry by publishing to the stream.

type Writer added in v0.3.0

type Writer interface {
	Write(ctx context.Context, entry Entry) error
}

Writer is where an entry goes: the audit stream. A *StreamStore is one.

Directories

Path Synopsis
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.

Jump to

Keyboard shortcuts

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