audit

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 16 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

Index

Constants

View Source
const (
	AuditSchemaVersion = "audit.identrail.io/v1"
)

Variables

This section is empty.

Functions

func ActorFromContext

func ActorFromContext(ctx context.Context) (string, bool)

ActorFromContext returns the actor when present.

func CorrelationIDFromContext

func CorrelationIDFromContext(ctx context.Context) (string, bool)

CorrelationIDFromContext returns a correlation ID when present.

func EnsureCorrelationID

func EnsureCorrelationID(ctx context.Context) (context.Context, string)

EnsureCorrelationID ensures a correlation ID exists on the returned context.

func FingerprintAPIKey

func FingerprintAPIKey(raw string) string

FingerprintAPIKey returns a stable, non-secret identifier suitable for audit logs. Deprecated: Use Fingerprinter.APIKey for keyed HMAC-SHA256 fingerprints.

func FingerprintIdentifier

func FingerprintIdentifier(raw string) string

FingerprintIdentifier returns a stable, non-secret identifier suitable for correlating principals/resources without logging raw IDs. Deprecated: Use Fingerprinter.Identifier for keyed HMAC-SHA256 fingerprints.

func WithActor

func WithActor(ctx context.Context, actor string) context.Context

WithActor stores a stable, non-secret actor identifier on the context.

func WithCorrelationID

func WithCorrelationID(ctx context.Context, correlationID string) context.Context

WithCorrelationID stores a correlation ID on the context.

func WithFingerprinter

func WithFingerprinter(ctx context.Context, fingerprinter *Fingerprinter) context.Context

WithFingerprinter stores an audit fingerprinter for action events.

func WithSink

func WithSink(ctx context.Context, sink AuditSink) context.Context

WithSink stores an audit sink on the context. Callers should treat sink write errors as non-fatal to business logic.

func WriteAction

func WriteAction(ctx context.Context, event AuditEvent)

WriteAction writes an action audit event when a sink is configured on the context. Sink write failures are intentionally ignored by callers.

Types

type AsyncAuditSink

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

AsyncAuditSink decouples request-path audit writes from slower downstream sinks.

func NewAsyncAuditSink

func NewAsyncAuditSink(sink AuditSink, buffer int) *AsyncAuditSink

NewAsyncAuditSink wraps a sink with a bounded in-memory queue.

func (*AsyncAuditSink) Close

func (a *AsyncAuditSink) Close() error

func (*AsyncAuditSink) WithWriteErrorHandler

func (a *AsyncAuditSink) WithWriteErrorHandler(fn func(error)) *AsyncAuditSink

WithWriteErrorHandler sets a callback invoked each time the background goroutine fails to deliver an event to the downstream sink. Use it to log forwarding errors immediately instead of waiting for Close to surface them.

func (*AsyncAuditSink) Write

func (a *AsyncAuditSink) Write(ctx context.Context, event AuditEvent) error

type AuditAuthzDecision

type AuditAuthzDecision struct {
	PolicySetID   string                 `json:"policy_set_id,omitempty"`
	PolicyVersion *int                   `json:"policy_version,omitempty"`
	PolicySource  string                 `json:"policy_source,omitempty"`
	RolloutMode   string                 `json:"rollout_mode,omitempty"`
	Allowed       bool                   `json:"allowed"`
	Stage         string                 `json:"stage,omitempty"`
	Reason        string                 `json:"reason,omitempty"`
	Input         AuditAuthzInputSummary `json:"input"`
}

AuditAuthzDecision captures one centralized authorization decision.

type AuditAuthzInputSummary

type AuditAuthzInputSummary struct {
	SubjectType    string `json:"subject_type,omitempty"`
	SubjectIDHash  string `json:"subject_id_hash,omitempty"`
	Action         string `json:"action,omitempty"`
	ResourceType   string `json:"resource_type,omitempty"`
	ResourceIDHash string `json:"resource_id_hash,omitempty"`
	TenantID       string `json:"tenant_id,omitempty"`
	WorkspaceID    string `json:"workspace_id,omitempty"`
}

AuditAuthzInputSummary captures one sanitized authorization decision input.

type AuditEvent

type AuditEvent struct {
	SchemaVersion string    `json:"schema_version"`
	EventID       string    `json:"event_id"`
	Timestamp     time.Time `json:"timestamp"`

	// Service/component/category provide stable routing fields for collectors.
	Service   string `json:"service,omitempty"`
	Component string `json:"component,omitempty"`
	Category  string `json:"category,omitempty"`

	// Kind distinguishes API request envelopes from action-level control-plane events.
	// Examples: "api_request", "action".
	Kind string `json:"kind,omitempty"`

	// CorrelationID ties multiple records back to the same request/workflow.
	CorrelationID string `json:"correlation_id,omitempty"`

	// Actor is a stable, non-secret identifier for the principal that triggered the action.
	Actor string `json:"actor,omitempty"`

	// Action is an operation name for action audit records.
	Action string `json:"action,omitempty"`

	// Scope context.
	TenantID    string `json:"tenant_id,omitempty"`
	WorkspaceID string `json:"workspace_id,omitempty"`

	// Resource context.
	ResourceType string `json:"resource_type,omitempty"`
	ResourceID   string `json:"resource_id,omitempty"`

	// Outcome is a coarse result indicator such as "success", "denied", "not_found", "error".
	Outcome string `json:"outcome,omitempty"`
	Error   string `json:"error,omitempty"`

	// API request fields (Kind=api_request).
	Method     string              `json:"method,omitempty"`
	Path       string              `json:"path,omitempty"`
	Status     int                 `json:"status,omitempty"`
	ClientIP   string              `json:"client_ip,omitempty"`
	DurationMS int64               `json:"duration_ms,omitempty"`
	UserAgent  string              `json:"user_agent,omitempty"`
	APIKeyID   string              `json:"api_key_id,omitempty"`
	Authz      *AuditAuthzDecision `json:"authz,omitempty"`
}

AuditEvent captures one structured audit record for external export.

The existing API request audit log is represented as Kind=api_request with the HTTP fields populated. Control-plane action audit records use Kind=action and populate Action/Resource/Scope fields.

func NormalizeEvent

func NormalizeEvent(ctx context.Context, event AuditEvent) AuditEvent

NormalizeEvent applies the stable audit event envelope shared by all sinks.

type AuditSink

type AuditSink interface {
	Write(ctx context.Context, event AuditEvent) error
	Close() error
}

AuditSink defines the export target for audit events.

func SinkFromContext

func SinkFromContext(ctx context.Context) (AuditSink, bool)

SinkFromContext returns the configured sink when present.

type FileAuditSink

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

FileAuditSink writes audit events in JSON lines format.

func NewFileAuditSink

func NewFileAuditSink(path string) (*FileAuditSink, error)

NewFileAuditSink creates or appends to a local audit file with restrictive permissions.

func (*FileAuditSink) Close

func (s *FileAuditSink) Close() error

func (*FileAuditSink) Write

func (s *FileAuditSink) Write(_ context.Context, event AuditEvent) error

type Fingerprinter

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

Fingerprinter produces keyed HMAC-SHA256 fingerprints for audit identifiers.

func FingerprinterFromContext

func FingerprinterFromContext(ctx context.Context) (*Fingerprinter, bool)

FingerprinterFromContext returns the configured fingerprinter when present.

func NewFingerprinter

func NewFingerprinter(secret string) *Fingerprinter

NewFingerprinter creates a Fingerprinter that uses the given secret as the HMAC-SHA256 key. The secret must be non-empty in production; an empty value causes a panic to prevent accidental use of weak fingerprints.

func (*Fingerprinter) APIKey

func (f *Fingerprinter) APIKey(raw string) string

APIKey returns a stable, keyed fingerprint for API keys in audit logs.

func (*Fingerprinter) Identifier

func (f *Fingerprinter) Identifier(raw string) string

Identifier returns a stable, keyed fingerprint suitable for correlating principals/resources in audit logs without exposing raw IDs.

type HTTPAuditSink

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

HTTPAuditSink posts audit events to a remote collector endpoint.

func NewHTTPAuditSink

func NewHTTPAuditSink(endpoint string, timeout time.Duration, hmacSecret string, maxRetries int, retryBackoff time.Duration) (*HTTPAuditSink, error)

NewHTTPAuditSink builds an HTTP audit sink with URL safety checks.

func (*HTTPAuditSink) Close

func (s *HTTPAuditSink) Close() error

func (*HTTPAuditSink) Write

func (s *HTTPAuditSink) Write(ctx context.Context, event AuditEvent) error

type MultiAuditSink

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

MultiAuditSink fans out one event to multiple sinks.

func NewMultiAuditSink

func NewMultiAuditSink(sinks ...AuditSink) *MultiAuditSink

NewMultiAuditSink creates a fanout sink.

func (*MultiAuditSink) Close

func (m *MultiAuditSink) Close() error

func (*MultiAuditSink) Write

func (m *MultiAuditSink) Write(ctx context.Context, event AuditEvent) error

type NopAuditSink

type NopAuditSink struct{}

NopAuditSink discards audit events when no export target is configured.

func (NopAuditSink) Close

func (NopAuditSink) Close() error

func (NopAuditSink) Write

Jump to

Keyboard shortcuts

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