trail

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package trail provides a comprehensive audit trail system for tracking all operations

Package trail provides a comprehensive audit trail system for tracking all operations

Package trail provides a comprehensive audit trail system for tracking all operations

Package trail provides a comprehensive audit trail system for tracking all operations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSVHeader

func CSVHeader() []string

CSVHeader returns the header row for CSV export

func ConvertAuditLogToAuditEvent

func ConvertAuditLogToAuditEvent(log *AuditLog) *audit.AuditEvent

ConvertAuditLogToAuditEvent converts an AuditLog to an AuditEvent

func CreateComplianceReport

func CreateComplianceReport(logs []*AuditLog, framework string, startTime, endTime time.Time) map[string]interface{}

CreateComplianceReport creates a compliance report for the specified framework

func ExportComplianceReport

func ExportComplianceReport(report map[string]interface{}, filePath string) error

ExportComplianceReport exports a compliance report to a file

func ExportLogsToCSV

func ExportLogsToCSV(logs []*AuditLog, filePath string) error

ExportLogsToCSV exports audit logs to a CSV file

func ExportLogsToJSON

func ExportLogsToJSON(logs []*AuditLog, filePath string) error

ExportLogsToJSON exports audit logs to a JSON file

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats a duration in a human-readable format

func GenerateAuditSummary

func GenerateAuditSummary(logs []*AuditLog) map[string]interface{}

GenerateAuditSummary generates a summary of audit logs

func GetFieldHistory

func GetFieldHistory(logs []*AuditLog, resourceType, resourceID, field string) []map[string]interface{}

GetFieldHistory gets the history of changes to a specific field

func HashLog

func HashLog(log *AuditLog) (string, error)

HashLog creates a hash of an audit log for integrity verification

func VerifyLogChain

func VerifyLogChain(logs []*AuditLog) (bool, []string)

VerifyLogChain verifies the integrity of a chain of audit logs

Types

type AuditLog

type AuditLog struct {
	// Unique identifier for the audit log
	ID string `json:"id"`

	// Time when the event occurred
	Timestamp time.Time `json:"timestamp"`

	// User who performed the action
	UserID string `json:"user_id,omitempty"`

	// Username of the user who performed the action
	Username string `json:"username,omitempty"`

	// Type of operation performed
	Operation string `json:"operation"`

	// Resource type that was acted upon
	ResourceType string `json:"resource_type"`

	// Specific resource identifier
	ResourceID string `json:"resource_id,omitempty"`

	// Human-readable description of the event
	Description string `json:"description"`

	// IP address where the action originated
	IPAddress string `json:"ip_address,omitempty"`

	// Status of the operation (success, failure, etc.)
	Status string `json:"status"`

	// Additional details about the operation
	Details map[string]interface{} `json:"details,omitempty"`

	// Previous state of the resource (for update operations)
	PreviousState map[string]interface{} `json:"previous_state,omitempty"`

	// New state of the resource (for update operations)
	NewState map[string]interface{} `json:"new_state,omitempty"`

	// Changes made during the operation
	Changes map[string]interface{} `json:"changes,omitempty"`

	// Verification information (for verification operations)
	Verification *VerificationInfo `json:"verification,omitempty"`

	// Compliance metadata
	Compliance *ComplianceInfo `json:"compliance,omitempty"`

	// Digital signature for tamper evidence
	Signature string `json:"signature,omitempty"`

	// Hash of the previous log entry (for chain of custody)
	PreviousHash string `json:"previous_hash,omitempty"`
}

AuditLog represents a comprehensive audit log entry

func ConvertAuditEventToAuditLog

func ConvertAuditEventToAuditLog(event *audit.AuditEvent) *AuditLog

ConvertAuditEventToAuditLog converts an AuditEvent to an AuditLog

func FilterLogs

func FilterLogs(logs []*AuditLog, filter func(*AuditLog) bool) []*AuditLog

FilterLogs filters a slice of audit logs based on criteria

func FindChanges

func FindChanges(logs []*AuditLog, field string) []*AuditLog

FindChanges finds all logs that contain changes to a specific field

func FromJSON

func FromJSON(jsonStr string) (*AuditLog, error)

FromJSON creates an audit log from a JSON string

func GetApprovalHistory

func GetApprovalHistory(logs []*AuditLog) []*AuditLog

GetApprovalHistory gets all approval operations

func GetFailedOperations

func GetFailedOperations(logs []*AuditLog) []*AuditLog

GetFailedOperations gets all failed operations

func GetRecentActivity

func GetRecentActivity(logs []*AuditLog, duration time.Duration) []*AuditLog

GetRecentActivity gets recent activity logs

func GetResourceHistory

func GetResourceHistory(logs []*AuditLog, resourceType, resourceID string) []*AuditLog

GetResourceHistory gets the complete history of a resource

func GetUserActivity

func GetUserActivity(logs []*AuditLog, userID, username string) []*AuditLog

GetUserActivity gets all activity for a specific user

func GetVerificationHistory

func GetVerificationHistory(logs []*AuditLog) []*AuditLog

GetVerificationHistory gets all verification operations

func NewAuditLog

func NewAuditLog(operation, resourceType, description string) *AuditLog

NewAuditLog creates a new audit log entry

func RedactSensitiveData

func RedactSensitiveData(log *AuditLog, fields []string) *AuditLog

RedactSensitiveData redacts sensitive data from an audit log

func SortLogs

func SortLogs(logs []*AuditLog, ascending bool) []*AuditLog

SortLogs sorts a slice of audit logs by timestamp

func (*AuditLog) Sign

func (l *AuditLog) Sign(key string) error

Sign signs the audit log for tamper evidence

func (*AuditLog) ToCSV

func (l *AuditLog) ToCSV() ([]string, error)

ToCSV converts the audit log to a CSV record

func (*AuditLog) ToJSON

func (l *AuditLog) ToJSON() (string, error)

ToJSON converts the audit log to a JSON string

func (*AuditLog) Verify

func (l *AuditLog) Verify(key string) (bool, error)

Verify verifies the signature of the audit log

func (*AuditLog) WithApproval

func (l *AuditLog) WithApproval(requiresApproval bool, status, approvedBy string) *AuditLog

WithApproval adds approval information to the compliance metadata

func (*AuditLog) WithChange

func (l *AuditLog) WithChange(field string, oldValue, newValue interface{}) *AuditLog

WithChange adds a change to the audit log

func (*AuditLog) WithChanges

func (l *AuditLog) WithChanges(changes map[string]interface{}) *AuditLog

WithChanges adds multiple changes to the audit log

func (*AuditLog) WithCompliance

func (l *AuditLog) WithCompliance(frameworks, controls []string, dataClassification, retentionPeriod string) *AuditLog

WithCompliance adds compliance information to the audit log

func (*AuditLog) WithDetail

func (l *AuditLog) WithDetail(key string, value interface{}) *AuditLog

WithDetail adds a detail to the audit log

func (*AuditLog) WithIP

func (l *AuditLog) WithIP(ipAddress string) *AuditLog

WithIP adds IP address information to the audit log

func (*AuditLog) WithResource

func (l *AuditLog) WithResource(resourceID string) *AuditLog

WithResource adds resource information to the audit log

func (*AuditLog) WithStates

func (l *AuditLog) WithStates(previousState, newState map[string]interface{}) *AuditLog

WithStates adds previous and new state information to the audit log

func (*AuditLog) WithStatus

func (l *AuditLog) WithStatus(status string) *AuditLog

WithStatus sets the status of the audit log

func (*AuditLog) WithUser

func (l *AuditLog) WithUser(userID, username string) *AuditLog

WithUser adds user information to the audit log

func (*AuditLog) WithVerification

func (l *AuditLog) WithVerification(verificationType, verifiedBy string, result string) *AuditLog

WithVerification adds verification information to the audit log

func (*AuditLog) WithVerificationMetadata

func (l *AuditLog) WithVerificationMetadata(key string, value interface{}) *AuditLog

WithVerificationMetadata adds metadata to the verification information

type AuditLogFilter

type AuditLogFilter func(*AuditLog) bool

AuditLogFilter defines a function type for filtering audit logs

func CombineFilters

func CombineFilters(filters ...AuditLogFilter) AuditLogFilter

CombineFilters combines multiple filters with AND logic

func CreateComplianceFilter

func CreateComplianceFilter(frameworks ...string) AuditLogFilter

CreateComplianceFilter creates a filter for logs by compliance framework

func CreateOperationFilter

func CreateOperationFilter(operations ...string) AuditLogFilter

CreateOperationFilter creates a filter for logs by operation

func CreateResourceFilter

func CreateResourceFilter(resourceType, resourceID string) AuditLogFilter

CreateResourceFilter creates a filter for logs by resource type and ID

func CreateStatusFilter

func CreateStatusFilter(statuses ...string) AuditLogFilter

CreateStatusFilter creates a filter for logs by status

func CreateTimeRangeFilter

func CreateTimeRangeFilter(startTime, endTime time.Time) AuditLogFilter

CreateTimeRangeFilter creates a filter for logs within a time range

func CreateUserFilter

func CreateUserFilter(userID, username string) AuditLogFilter

CreateUserFilter creates a filter for logs by user ID or username

type AuditTrailConfig

type AuditTrailConfig struct {
	// Whether to enable the audit trail
	Enabled bool `json:"enabled"`

	// Directory where audit logs are stored
	LogDirectory string `json:"log_directory"`

	// Whether to sign audit logs for tamper evidence
	SignLogs bool `json:"sign_logs"`

	// Secret key for signing logs
	SigningKey string `json:"signing_key,omitempty"`

	// Whether to maintain a hash chain for logs
	EnableHashChain bool `json:"enable_hash_chain"`

	// Whether to compress log files
	CompressLogs bool `json:"compress_logs"`

	// Maximum size of a single log file in MB
	MaxLogFileSize int `json:"max_log_file_size"`

	// Maximum number of log files to keep
	MaxLogFiles int `json:"max_log_files"`

	// Log retention period in days
	RetentionDays int `json:"retention_days"`

	// Fields to redact from logs
	RedactFields []string `json:"redact_fields"`

	// Whether to include previous and new state in logs
	IncludeState bool `json:"include_state"`

	// Whether to include compliance information
	IncludeCompliance bool `json:"include_compliance"`
}

AuditTrailConfig defines configuration options for the audit trail system

func DefaultAuditTrailConfig

func DefaultAuditTrailConfig() *AuditTrailConfig

DefaultAuditTrailConfig returns the default configuration for the audit trail

type AuditTrailManager

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

AuditTrailManager manages the audit trail system

func NewAuditTrailManager

func NewAuditTrailManager(config *AuditTrailConfig, auditManager *audit.AuditManager) (*AuditTrailManager, error)

NewAuditTrailManager creates a new audit trail manager

func (*AuditTrailManager) AddLogger

func (m *AuditTrailManager) AddLogger(logger Logger)

AddLogger adds a logger to the manager

func (*AuditTrailManager) Close

func (m *AuditTrailManager) Close() error

Close closes the audit trail manager and all loggers

func (*AuditTrailManager) ExportLogs

func (m *AuditTrailManager) ExportLogs(ctx context.Context, filters map[string]interface{}, startTime, endTime time.Time, format, filePath string) error

ExportLogs exports logs to a file in the specified format

func (*AuditTrailManager) GetLog

func (m *AuditTrailManager) GetLog(ctx context.Context, id string) (*AuditLog, error)

GetLog retrieves a specific log by ID

func (*AuditTrailManager) LogApproval

func (m *AuditTrailManager) LogApproval(ctx context.Context, userID, username, resourceType, resourceID, description, status string, details map[string]interface{}) error

LogApproval logs an approval operation

func (*AuditTrailManager) LogCompliance

func (m *AuditTrailManager) LogCompliance(ctx context.Context, userID, username, resourceType, resourceID, description string, frameworks, controls []string, dataClassification, retentionPeriod string) error

LogCompliance logs a compliance-related operation

func (*AuditTrailManager) LogCreate

func (m *AuditTrailManager) LogCreate(ctx context.Context, userID, username, resourceType, resourceID, description string, details map[string]interface{}) error

LogCreate logs a create operation

func (*AuditTrailManager) LogCustom

func (m *AuditTrailManager) LogCustom(ctx context.Context, operation, userID, username, resourceType, resourceID, description string, details map[string]interface{}) error

LogCustom logs a custom operation

func (*AuditTrailManager) LogDelete

func (m *AuditTrailManager) LogDelete(ctx context.Context, userID, username, resourceType, resourceID, description string, deletedState map[string]interface{}) error

LogDelete logs a delete operation

func (*AuditTrailManager) LogFromAuditEvent

func (m *AuditTrailManager) LogFromAuditEvent(ctx context.Context, event *audit.AuditEvent) error

LogFromAuditEvent converts and logs an AuditEvent

func (*AuditTrailManager) LogOperation

func (m *AuditTrailManager) LogOperation(ctx context.Context, log *AuditLog) error

LogOperation logs an operation to the audit trail

func (*AuditTrailManager) LogRead

func (m *AuditTrailManager) LogRead(ctx context.Context, userID, username, resourceType, resourceID, description string, details map[string]interface{}) error

LogRead logs a read operation

func (*AuditTrailManager) LogUpdate

func (m *AuditTrailManager) LogUpdate(ctx context.Context, userID, username, resourceType, resourceID, description string, previousState, newState map[string]interface{}) error

LogUpdate logs an update operation

func (*AuditTrailManager) LogVerification

func (m *AuditTrailManager) LogVerification(ctx context.Context, userID, username, resourceType, resourceID, description, verificationType, result string, metadata map[string]interface{}) error

LogVerification logs a verification operation

func (*AuditTrailManager) PurgeLogs

func (m *AuditTrailManager) PurgeLogs(ctx context.Context) error

PurgeLogs deletes logs older than the retention period

func (*AuditTrailManager) QueryLogs

func (m *AuditTrailManager) QueryLogs(ctx context.Context, filters map[string]interface{}, startTime, endTime time.Time, limit, offset int) ([]*AuditLog, error)

QueryLogs queries audit logs based on filters

func (*AuditTrailManager) RemoveLogger

func (m *AuditTrailManager) RemoveLogger(logger Logger)

RemoveLogger removes a logger from the manager

func (*AuditTrailManager) VerifyLogIntegrity

func (m *AuditTrailManager) VerifyLogIntegrity(ctx context.Context, startTime, endTime time.Time) (bool, []string, error)

VerifyLogIntegrity verifies the integrity of logs

type ComplianceInfo

type ComplianceInfo struct {
	// Compliance frameworks applicable to this operation
	Frameworks []string `json:"frameworks,omitempty"`

	// Specific compliance controls addressed
	Controls []string `json:"controls,omitempty"`

	// Retention period for this log entry
	RetentionPeriod string `json:"retention_period,omitempty"`

	// Classification of the data involved
	DataClassification string `json:"data_classification,omitempty"`

	// Whether this operation requires approval
	RequiresApproval bool `json:"requires_approval,omitempty"`

	// Approval status if approval is required
	ApprovalStatus string `json:"approval_status,omitempty"`

	// Entity that approved the operation
	ApprovedBy string `json:"approved_by,omitempty"`

	// Time when the operation was approved
	ApprovedAt time.Time `json:"approved_at,omitempty"`
}

ComplianceInfo contains compliance-related metadata

type FileLogger

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

FileLogger is a file-based implementation of Logger

func NewFileLogger

func NewFileLogger(filePath string, config *AuditTrailConfig) (*FileLogger, error)

NewFileLogger creates a new file-based logger

func (*FileLogger) Close

func (l *FileLogger) Close() error

Close closes the file logger

func (*FileLogger) GetLog

func (l *FileLogger) GetLog(ctx context.Context, id string) (*AuditLog, error)

GetLog retrieves a specific audit log by ID from the file

func (*FileLogger) Log

func (l *FileLogger) Log(ctx context.Context, log *AuditLog) error

Log logs an audit log entry to the file

func (*FileLogger) PurgeLogs

func (l *FileLogger) PurgeLogs(ctx context.Context, olderThan time.Time) error

PurgeLogs purges logs older than the specified time

func (*FileLogger) Query

func (l *FileLogger) Query(ctx context.Context, filters map[string]interface{}, startTime, endTime time.Time) ([]*AuditLog, error)

Query queries audit logs from files based on filters

type InMemoryLogger

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

InMemoryLogger is an in-memory implementation of Logger

func NewInMemoryLogger

func NewInMemoryLogger(config *AuditTrailConfig) *InMemoryLogger

NewInMemoryLogger creates a new in-memory logger

func (*InMemoryLogger) GetLog

func (l *InMemoryLogger) GetLog(ctx context.Context, id string) (*AuditLog, error)

GetLog retrieves a specific audit log by ID from memory

func (*InMemoryLogger) Log

func (l *InMemoryLogger) Log(ctx context.Context, log *AuditLog) error

Log logs an audit log entry to memory

func (*InMemoryLogger) PurgeLogs

func (l *InMemoryLogger) PurgeLogs(ctx context.Context, olderThan time.Time) error

PurgeLogs purges logs older than the specified time from memory

func (*InMemoryLogger) Query

func (l *InMemoryLogger) Query(ctx context.Context, filters map[string]interface{}, startTime, endTime time.Time) ([]*AuditLog, error)

Query queries audit logs from memory based on filters

type LineScanner

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

LineScanner is a simple line scanner for reading log files

func NewLineScanner

func NewLineScanner(reader io.Reader) *LineScanner

NewLineScanner creates a new line scanner

func (*LineScanner) Err

func (s *LineScanner) Err() error

Err returns the error, if any

func (*LineScanner) Scan

func (s *LineScanner) Scan() bool

Scan advances to the next line

func (*LineScanner) Text

func (s *LineScanner) Text() string

Text returns the current line

type LogPurger

type LogPurger interface {
	// PurgeLogs purges logs older than the specified time
	PurgeLogs(ctx context.Context, olderThan time.Time) error
}

LogPurger defines the interface for loggers that support log purging

type Logger

type Logger interface {
	// Log logs an audit log entry
	Log(ctx context.Context, log *AuditLog) error

	// Query queries audit logs based on filters
	Query(ctx context.Context, filters map[string]interface{}, startTime, endTime time.Time) ([]*AuditLog, error)

	// GetLog retrieves a specific audit log by ID
	GetLog(ctx context.Context, id string) (*AuditLog, error)
}

Logger defines the interface for audit trail loggers

type SyslogLogger

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

SyslogLogger is a syslog-based implementation of Logger

func NewSyslogLogger

func NewSyslogLogger(config *AuditTrailConfig) (*SyslogLogger, error)

NewSyslogLogger creates a new syslog-based logger

func (*SyslogLogger) GetLog

func (l *SyslogLogger) GetLog(ctx context.Context, id string) (*AuditLog, error)

GetLog retrieves a specific audit log by ID from syslog

func (*SyslogLogger) Log

func (l *SyslogLogger) Log(ctx context.Context, log *AuditLog) error

Log logs an audit log entry to syslog

func (*SyslogLogger) Query

func (l *SyslogLogger) Query(ctx context.Context, filters map[string]interface{}, startTime, endTime time.Time) ([]*AuditLog, error)

Query queries audit logs from syslog based on filters

type VerificationInfo

type VerificationInfo struct {
	// Type of verification performed
	VerificationType string `json:"verification_type"`

	// Entity that performed the verification
	VerifiedBy string `json:"verified_by"`

	// Time when the verification was performed
	VerifiedAt time.Time `json:"verified_at"`

	// Result of the verification
	Result string `json:"result"`

	// Additional verification metadata
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

VerificationInfo contains information about verification operations

Jump to

Keyboard shortcuts

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