audit

package
v0.1.35 Latest Latest
Warning

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

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

Documentation

Overview

Package audit records kafui operations (state-changing by default, optionally all) to a local JSONL file for accountability and review. It is the audit half of the enforcement seam: the guarded datasource emits one Record per audited operation with its classified result.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultPath

func DefaultPath() string

DefaultPath returns the default audit log path (~/.kafui/audit.log).

func ResolveUser

func ResolveUser() string

ResolveUser returns the acting local identity: the OS user, falling back to "Unknown". (A SASL-username fallback is a future refinement.)

Types

type FileWriter

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

FileWriter appends one JSON line per record to a file. It is safe for concurrent use.

ponytail: no rotation here — audit volume for a single-user local tool is small. If it ever needs capping, reuse pkg/ui/shared's rotatingWriter rather than adding a size check inline.

func NewFileWriter

func NewFileWriter(path string) (*FileWriter, error)

NewFileWriter opens (creating parent dirs and the file if needed) path for append with 0600 permissions.

func (*FileWriter) Close

func (w *FileWriter) Close() error

Close closes the underlying file.

func (*FileWriter) Write

func (w *FileWriter) Write(rec Record) error

Write appends rec as a single JSON line.

type Level

type Level string

Level selects which operations are audited.

const (
	LevelAlterOnly Level = "alter_only" // only state-changing operations (default)
	LevelAll       Level = "all"        // reads too
)

type Record

type Record struct {
	Timestamp string         `json:"timestamp"` // ISO-8601 UTC
	User      string         `json:"user"`
	Cluster   string         `json:"cluster,omitempty"`
	Resources []Resource     `json:"resources"`
	Operation string         `json:"operation"`
	Params    map[string]any `json:"params,omitempty"`
	Result    Result         `json:"result"`
	Error     string         `json:"error,omitempty"`
}

Record is one audit-log line.

type Resource

type Resource struct {
	Type    string   `json:"type"`
	ID      string   `json:"id,omitempty"`
	Alter   bool     `json:"alter"`
	Actions []string `json:"actions"`
}

Resource is one resource an operation touched.

type Result

type Result string

Result classifies the outcome of an audited operation.

const (
	ResultSuccess         Result = "success"
	ResultAccessDenied    Result = "access_denied"
	ResultValidationError Result = "validation_error"
	ResultExecutionError  Result = "execution_error"
	ResultUnknownError    Result = "unknown_error"
)

func Classify

func Classify(err error) Result

Classify maps an operation error to a Result by unwrapping the typed domain errors in pkg/api. A nil error is success.

type Service

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

Service records audit entries subject to the enabled flag and level. When disabled it is a no-op, so call sites are unconditional. Write failures are logged and never propagated: auditing must never fail or delay the operation.

func NewService

func NewService(enabled bool, level Level, w Writer, log *slog.Logger) *Service

NewService builds an audit service. A nil writer or logger is tolerated (logger falls back to slog.Default). Pass enabled=false for a no-op service.

func (*Service) Enabled

func (s *Service) Enabled() bool

Enabled reports whether the service records anything.

func (*Service) Record

func (s *Service) Record(rec Record)

Record stamps the record with timestamp/user and writes it, honoring the configured level. Read-only operations are skipped at alter_only level. Nil service or writer is a no-op.

type Writer

type Writer interface {
	Write(Record) error
}

Writer appends audit records somewhere durable.

Jump to

Keyboard shortcuts

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