package
0.0.0-20241030091535-cc1b11756418
Repository: https://github.com/onsdigital/go-ns.git
Documentation: pkg.go.dev

# README

Auditing

Creating an auditor

To create a new auditor simply provide a Kafka producer (see go-ns/kafka) for the topic you wish to send your audit events to and the name of the service auditing the even.

auditor = audit.New(auditProducer, "dp-dataset-api")

You can also create a nop auditor which satisfies the Auditor interface but does nothing when called.

auditor = &audit.NopAuditor{}

Recording events

To record an event simply call Auditor.Record() passing in the appropriate arguments for the event you wish to record. The following example is a typical use case for recording an audit event.

// audit params is map holding additional useful information for the event
auditParams := common.Params{"key":"value"}

// audit that an action has been attempted
if err := auditor.Record(ctx, "my_action", audit.Attempted, auditParams); err != nil {
    // handle error
}

// attempt do carry out the action
err := func() error {
    // business logic...
}()

if err != nil {
    // attempted action unsuccessful - record unsuccessful event
    if err := auditor.Record(ctx, "my_action", audit.Unsuccessful, auditParams); err != nil {
        // handle error
    } 
    // handle error
}

// action completed successfully - record success event
if err := auditor.Record(ctx, "my_action", audit.Successful, auditParams); err != nil {
    // handle error
} 

Auditor.Record() will automatically extract requestID/correlationID, User-Identity & Caller-Identity from the supplied context (if they exist) and add them to the audit event and log parameters.

If Auditor.Record() fails to record the event it will log the error (including requestID/correlationID, User-Identity & Caller-Identity if they are available) before returning.

# Packages

No description provided by the author

# Functions

GetParameters populates audit parameters with path variable values.
LogActionFailure adds auditting data to log.Data before calling LogError.
LogError creates a structured error message when auditing fails.
New creates a new Auditor with the namespace, producer and token provided.
NewAuditError creates new audit.Error with default field values where necessary and orders the params alphabetically.
ToLogData convert common.Params to log.Data.

# Constants

List of audit messages.
List of audit messages.
List of audit messages.
List of audit messages.
List of audit messages.

# Variables

EventSchema defines the avro schema for an audit event.

# Structs

Auditor provides functions for interception HTTP requests and populating the context with a base audit event and recording audit events.
AuditorServiceMock is wrapper around the generated mock implementation of audit.AuditorService which can be configured to return an error when specified audit action / result values are passed into the Record method, also providesconvenience test methods for asserting calls & params made to the mock.
Error represents containing details of an attempt to audit and action that failed.
Event holds data about the action being attempted.
NopAuditor is an no op implementation of the AuditorService.
OutboundProducerMock is a mock implementation of OutboundProducer.

# Interfaces

AuditorService defines the behaviour of an auditor.
OutboundProducer defines a producer for sending outbound audit events to a kafka topic.