Documentation
¶
Overview ¶
Package audit provides structured security audit logging capabilities (reserved module).
Index ¶
- Constants
- Variables
- func RegisterSchema(s *schema.Schema) error
- func SanitizeMetadata(input any) map[string]any
- type Actor
- type Audit
- type AuditRecord
- type AuditRecordedPayload
- type Config
- type EventFilter
- type ListFilter
- type ListResult
- type Module
- type Option
- type OrgResolver
- type Outcome
- type TargetResource
Constants ¶
const (
EventAuditRecorded auth.EventName = "audit.record.created"
)
Event names emitted or tracked by the audit plugin.
const (
ModelAuditRecords = "audit_records"
)
const PluginID = "audit"
PluginID identifies this plugin in the GoModularAuth runtime.
Variables ¶
var ( // ErrStoreRequired indicates that an AuditStore implementation was not supplied in configuration. ErrStoreRequired = errors.New("audit: audit store is required") // ErrEventRequired indicates an attempt to write an audit record with an empty event identifier. ErrEventRequired = errors.New("audit: event identifier cannot be empty") // ErrRecordNotFound indicates that the requested audit record does not exist in storage. ErrRecordNotFound = errors.New("audit: record not found") // ErrNilRecord indicates that an audit record pointer provided for persistence was nil. ErrNilRecord = errors.New("audit: record cannot be nil") // ErrInvalidFilter indicates invalid query or pagination criteria. ErrInvalidFilter = errors.New("audit: invalid filter criteria") )
Sentinel errors returned by the audit plugin and store implementations.
Functions ¶
func RegisterSchema ¶
RegisterSchema registers the audit_records model in the compiled schema.
func SanitizeMetadata ¶
SanitizeMetadata takes arbitrary metadata or payload and produces a scrubbed, safe map[string]any strictly free of plaintext secrets, tokens, OTPs and passwords.
Types ¶
type Actor ¶
type Actor struct {
Subject string `json:"subject"`
Type auth.PrincipalType `json:"type"`
IPAddress string `json:"ip_address,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
}
Actor encapsulates the security principal initiating or associated with the audited action.
type Audit ¶
type Audit interface {
// Record manually registers an explicit audit entry, ensuring sanitization before persistence.
Record(ctx context.Context, record *AuditRecord) error
// Query searches the audit log with customizable filtering and pagination criteria.
Query(ctx context.Context, filter ListFilter) (*ListResult, error)
// GetByID retrieves a single audit entry by its unique identifier.
GetByID(ctx context.Context, id string) (*AuditRecord, error)
// QueryByActor returns audit entries performed by or associated with a specific actor subject.
QueryByActor(ctx context.Context, subject string, limit int) ([]*AuditRecord, error)
// QueryByTarget returns audit entries affecting a specific target resource type and identifier.
QueryByTarget(ctx context.Context, targetType, targetID string, limit int) ([]*AuditRecord, error)
// QueryByOrganization returns paginated audit records scoped to a multi-tenant organization.
QueryByOrganization(ctx context.Context, orgID string, filter ListFilter) (*ListResult, error)
}
Audit represents the strongly typed public capability provided by the audit plugin. Applications retrieve this capability via auth.Get[plugins.Audit]().
type AuditRecord ¶
type AuditRecord struct {
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Event string `json:"event"`
Action string `json:"action"`
ActorSubject string `json:"actor_subject"`
ActorType string `json:"actor_type"`
TargetType string `json:"target_type"`
TargetID string `json:"target_id"`
Outcome Outcome `json:"outcome"`
OrganizationID string `json:"organization_id,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
}
AuditRecord represents an immutable audit log entry capturing a security-relevant event.
func (*AuditRecord) Validate ¶
func (r *AuditRecord) Validate() error
Validate ensures all mandatory audit record fields are populated and sets defaults.
type AuditRecordedPayload ¶
type AuditRecordedPayload struct {
RecordID string `json:"record_id"`
Event string `json:"event"`
Timestamp time.Time `json:"timestamp"`
}
AuditRecordedPayload describes an audit log creation event.
type Config ¶
type Config struct {
Filter EventFilter
AsyncWorkers int
BufferSize int
BatchSize int
FlushInterval time.Duration
OrgResolver OrgResolver
}
Config holds settings for the audit plugin.
type EventFilter ¶
EventFilter is a predicate function to decide if an event should be audited. Returns true to audit, false to drop.
type ListFilter ¶
type ListFilter struct {
ActorSubject string `json:"actor_subject,omitempty"`
ActorType string `json:"actor_type,omitempty"`
TargetType string `json:"target_type,omitempty"`
TargetID string `json:"target_id,omitempty"`
Event string `json:"event,omitempty"`
Outcome Outcome `json:"outcome,omitempty"`
OrganizationID string `json:"organization_id,omitempty"`
Since time.Time `json:"since,omitempty"`
Until time.Time `json:"until,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
}
ListFilter defines search and pagination parameters for querying audit records.
func (*ListFilter) Normalize ¶
func (f *ListFilter) Normalize()
Normalize validates and applies reasonable defaults to filter parameters.
type ListResult ¶
type ListResult struct {
Records []*AuditRecord `json:"records"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
ListResult wraps the matching audit entries and total count for pagination.
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module implements auth.PluginModule for structured security auditing.
func (*Module) Init ¶
Init configures infrastructure, registers the typed Capability in GoModularAuth, and subscribes the audit consumer to all dispatched events.
func (*Module) RegisterSchema ¶
RegisterSchema contributes the audit_records model to the compiled schema.
type Option ¶
type Option func(*Config)
Option configures audit plugin behavior.
func WithAsync ¶
WithAsync enables asynchronous background worker processing for non-blocking audit logging.
func WithFilter ¶
func WithFilter(f EventFilter) Option
WithFilter configures a predicate to selectively filter out events from being audited.
func WithOrgResolver ¶
func WithOrgResolver(resolver OrgResolver) Option
WithOrgResolver configures a custom organization ID extractor for tenant scoping.
type OrgResolver ¶
OrgResolver extracts an organization ID from context or event payload.
type Outcome ¶
type Outcome string
Outcome classifies the result of an audited operation.
const ( // OutcomeSuccess denotes an operation that completed successfully. OutcomeSuccess Outcome = "success" // OutcomeFailure denotes an operation that failed, was rejected, or unauthorized. OutcomeFailure Outcome = "failure" // OutcomeUnknown denotes an operation whose explicit result was indeterminate. OutcomeUnknown Outcome = "unknown" )
type TargetResource ¶
TargetResource specifies the subject or resource affected by an audited event.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package migrations contains SQL schema migrations for the audit plugin.
|
Package migrations contains SQL schema migrations for the audit plugin. |
|
Package plugins exposes capability aliases for GoModularAuth plugins.
|
Package plugins exposes capability aliases for GoModularAuth plugins. |