audit

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package audit provides audit logging for the ToolHive Registry Server. It uses the shared audit library from toolhive-core to emit structured audit events for API operations.

Index

Constants

View Source
const (
	ResourceTypeSource   = "source"
	ResourceTypeRegistry = "registry"
	ResourceTypeEntry    = "entry"
)

Target field values for resource types.

View Source
const (
	EventSourceCreate   = "source.create"
	EventSourceUpdate   = "source.update"
	EventSourceDelete   = "source.delete"
	EventRegistryCreate = "registry.create"
	EventRegistryUpdate = "registry.update"
	EventRegistryDelete = "registry.delete"
	EventEntryPublish   = "entry.publish"
	EventEntryDelete    = "entry.delete"
	EventEntryClaims    = "entry.claims.update"
)

Event types for audit logging — write operations.

View Source
const (
	EventSourceList          = "source.list"
	EventSourceRead          = "source.read"
	EventSourceEntriesList   = "source.entries.list"
	EventRegistryList        = "registry.list"
	EventRegistryRead        = "registry.read"
	EventRegistryEntriesList = "registry.entries.list"
	EventUserInfo            = "user.info"
)

Event types for audit logging — read operations.

View Source
const ComponentRegistryAPI = "toolhive-registry-api"

ComponentRegistryAPI is the component name for the registry server (distinct from toolhive's "toolhive-api").

View Source
const (
	// EventAuthUnauthenticated is emitted when a request fails authentication
	// (HTTP 401). Captured by the pre-auth middleware so that auth failures
	// are visible to SIEM systems even though the post-auth audit middleware
	// never fires for rejected requests.
	EventAuthUnauthenticated = "auth.unauthenticated"
)

Event types for audit logging — security events.

Variables

This section is empty.

Functions

func AuthFailureMiddleware

func AuthFailureMiddleware(cfg *config.AuditConfig, logger *Logger, publicPaths []string) func(http.Handler) http.Handler

AuthFailureMiddleware returns HTTP middleware that emits audit events for authentication failures (HTTP 401). It must be installed BEFORE the auth middleware so it can observe auth rejections.

When cfg is nil or disabled, the middleware is a no-op pass-through.

func EventTypeFromRequest

func EventTypeFromRequest(method, path string, status int) string

EventTypeFromRequest determines the audit event type from the HTTP method, request path, and response status code. The status code is used to distinguish creates (201) from updates (200) for PUT upsert endpoints. It returns an empty string if the request does not map to a known auditable operation.

func Middleware

func Middleware(cfg *config.AuditConfig, logger *Logger) func(http.Handler) http.Handler

Middleware returns HTTP middleware that emits audit events for operations on /v1/ endpoints. It must be installed after auth and role resolution middleware so that JWT claims are available in the context.

When cfg is nil or disabled, the middleware is a no-op pass-through.

func OutcomeFromStatus

func OutcomeFromStatus(status int) string

OutcomeFromStatus maps an HTTP status code to an audit outcome string.

func SourceFromRequest

func SourceFromRequest(r *http.Request) audit.EventSource

SourceFromRequest extracts the client IP and User-Agent for audit events.

For the IP address, it uses r.RemoteAddr as the primary source because Chi's middleware.RealIP runs earlier in the middleware chain and already resolves X-Forwarded-For / X-Real-IP into RemoteAddr. The raw X-Forwarded-For header is preserved in Extra for forensic context.

func TargetFromRequest

func TargetFromRequest(method, path string) map[string]string

TargetFromRequest extracts a rich target map from the request path, including explicit resource_type and resource_name fields alongside the raw method and path. This gives SIEM systems structured fields to query without needing to regex-parse the URL.

Types

type Logger

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

Logger wraps a dedicated *slog.Logger for audit events and manages the underlying writer so it can be closed on shutdown.

func NewLogger

func NewLogger(logFile string) (*Logger, error)

NewLogger creates a dedicated audit logger. If logFile is non-empty, events are written to that file (created/appended); otherwise they go to stdout. The returned Logger must be closed via Close() on shutdown.

func (*Logger) Close

func (l *Logger) Close() error

Close releases any resources held by the logger (e.g., open file handles).

func (*Logger) Slog

func (l *Logger) Slog() *slog.Logger

Slog returns the underlying *slog.Logger for use with AuditEvent.LogTo().

Jump to

Keyboard shortcuts

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