audit

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMetadata

func AddMetadata(ctx context.Context, key string, value any)

AddMetadata adds a single key-value pair to the audit metadata If metadata doesn't exist yet, it creates a new map

func AddMetadataBatch

func AddMetadataBatch(ctx context.Context, metadata map[string]any)

AddMetadataBatch adds multiple key-value pairs to the audit metadata If metadata doesn't exist yet, it creates a new map

func GetMetadata

func GetMetadata(ctx context.Context) map[string]any

GetMetadata retrieves the audit metadata from the context Returns nil if no metadata has been set

func SetMetadata

func SetMetadata(ctx context.Context, metadata map[string]any)

SetMetadata stores additional audit metadata Handlers can call this to add custom fields to the audit event

func SetResource

func SetResource(ctx context.Context, resource *Resource)

SetResource stores resource information for audit logging Handlers should call this to specify which resource is being acted upon

Types

type ActionCategory

type ActionCategory string

ActionCategory represents the category of audit action

const (
	CategoryResource      ActionCategory = "resource"
	CategoryAuth          ActionCategory = "auth"
	CategoryObservability ActionCategory = "observability"
)

type ActionDefinition

type ActionDefinition struct {
	// Method is the HTTP method (GET, POST, PUT, DELETE, etc.)
	Method string
	// Pattern is the route pattern to match (using Go 1.22+ ServeMux patterns)
	// Examples: "/api/v1/namespaces/{namespace}/projects", "POST /api/v1/components/{id}"
	Pattern string
	// Action is the semantic action name for audit logging
	Action string
	// Category is the action category
	Category ActionCategory
}

ActionDefinition defines how to map an HTTP route to an audit action

type ActionResolver

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

ActionResolver resolves HTTP requests to audit action definitions

func NewActionResolver

func NewActionResolver(definitions []ActionDefinition) *ActionResolver

NewActionResolver creates a new action resolver with the given definitions

func (*ActionResolver) Resolve

func (r *ActionResolver) Resolve(req *http.Request) *ActionDefinition

Resolve attempts to match an HTTP request to an action definition Returns the matching definition or nil if no match is found

type Actor

type Actor struct {
	Type         string                 `json:"type"`                   // e.g., "user", "service_account", "anonymous"
	ID           string                 `json:"id"`                     // User ID, service account ID, or "anonymous"
	Entitlements map[string]interface{} `json:"entitlements,omitempty"` // Optional entitlements associated with the actor
}

Actor represents who performed the action

type AuditData

type AuditData struct {
	Resource *Resource
	Metadata map[string]any
}

AuditData is a mutable container for audit information set by handlers

type Event

type Event struct {
	EventID   string         `json:"event_id"`           // Unique identifier (UUID v7)
	Timestamp time.Time      `json:"timestamp"`          // When the action occurred
	Actor     Actor          `json:"actor"`              // Who performed the action
	Action    string         `json:"action"`             // Semantic action name (e.g., "create_project")
	Category  ActionCategory `json:"category"`           // Action category
	Resource  *Resource      `json:"resource"`           // Target resource (can be nil for non-resource actions)
	Result    Result         `json:"result"`             // Outcome
	RequestID string         `json:"request_id"`         // Correlation ID linking to access log
	SourceIP  string         `json:"source_ip"`          // Client IP address
	Service   string         `json:"service"`            // Emitting service (e.g., "openchoreo-api")
	Metadata  map[string]any `json:"metadata,omitempty"` // Additional context (optional)
}

Event represents a complete audit log event

type Logger

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

Logger handles emitting audit log events using structured logging

func NewLogger

func NewLogger(slogger *slog.Logger, serviceName string) *Logger

NewLogger creates a new audit logger

func (*Logger) LogEvent

func (l *Logger) LogEvent(event *Event)

LogEvent emits an audit log event using slog

type Middleware

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

Middleware handles audit logging for HTTP requests

func NewMiddleware

func NewMiddleware(logger *Logger, resolver *ActionResolver) *Middleware

NewMiddleware creates a new audit middleware

func (*Middleware) Handler

func (m *Middleware) Handler(next http.Handler) http.Handler

Handler returns the HTTP middleware handler

type Resource

type Resource struct {
	Type string `json:"type"`           // e.g., "project", "component", "environment"
	ID   string `json:"id,omitempty"`   // Resource identifier
	Name string `json:"name,omitempty"` // Resource name (if different from ID)
}

Resource represents the target resource of an action

func GetResource

func GetResource(ctx context.Context) *Resource

GetResource retrieves the resource information from the context Returns nil if no resource has been set

type Result

type Result string

Result represents the outcome of an action

const (
	ResultSuccess Result = "success"
	ResultFailure Result = "failure"
	ResultDenied  Result = "denied"
)

Jump to

Keyboard shortcuts

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