Skip to content

Latest commit

 

History

History
160 lines (115 loc) · 5.92 KB

README.md

File metadata and controls

160 lines (115 loc) · 5.92 KB

Audit.NET.DynamoDB

Amazon Dynamo DB provider for Audit.NET library (An extensible framework to audit executing operations in .NET).

Store the audit events in Dynamo DB tables using the AWSSDK.DynamoDBv2 library.

Install

NuGet Package To install the package run the following command on the Package Manager Console:

PM> Install-Package Audit.NET.DynamoDB

NuGet Status NuGet Count

Usage

Please see the Audit.NET Readme

Configuration

Set the static Audit.Core.Configuration.DataProvider property to set the Dynamo DB data provider, or call the UseDynamoDB method on the fluent configuration. This should be done before any AuditScope creation, i.e. during application startup.

For example:

Audit.Core.Configuration.DataProvider = new DynamoDataProvider()
{
    Client = new Lazy<IAmazonDynamoDB>(() => new AmazonDynamoDBClient(new AmazonDynamoDBConfig() 
    { 
        ServiceURL = "http://localhost:8000" 
    })),
    TableNameBuilder = ev => "MyTable"
};

Or even shorter using the constructor overload that accepts a fluent API:

Audit.Core.Configuration.DataProvider = new DynamoDataProvider(config => config
    .UseUrl("http://localhost:8000")
    .Table("MyTable"));

Or by using the global setup extension UseDynamoDB():

Audit.Core.Configuration.Setup()
    .UseDynamoDB(config => config
        .UseUrl("http://localhost:8000")
        .Table(ev => ev.EventType));

You can provide the table name setting as a string or as a function of the Audit Event.

Provider Options

  • Client: The DynamoDB client creator AmazonDynamoDBClient.
  • TableNameBuilder: A function of the audit event that returns the Table Name to use.
  • CustomAttributes: A dictionary with additional fields to be included in the document and as custom fields on the audit event.

Fluent API Methods

The provider options can be set with a fluent API described by the following methods:

Connection level
  • WithClient(): Use the given DynamoDB client instance (AmazonDynamoDBClient).
  • UseConfig(): Alternative to WithClient(), to use a DynamoDB client with the given settings (AmazonDynamoDBConfig).
  • UseUrl(): Alternative to use a DynamoDB client only specifying the service URL.
Table level
  • Table(): To specify the table name (as a string or a function of the audit event).
Attributes level
  • SetAttribute(): To specify additional top-level attributes on the document before saving.

Query events

This provider implements GetEvent and GetEventAsync methods to obtain an audit event by id:

var event = dynamoDataProvider.GetEvent((Primitive)1234);

The eventId parameter on the generic GetEvent(object eventId) must be of type Primitive, DynamoDBEntry or an array of any of these two types. The first (or only) element must be the Hash key, and the second element should be the range key (or NULL if not using a range).

There are more convenient overloads of the GetEvent/GetEventAsync methods that accepts the Primitives without needing to cast the parameters:

// Get event with the given HASH and RANGE
var event = dynamoDataProvider.GetEvent("A001-005283", 2018);
// Get event with the given HASH
var event = dynamoDataProvider.GetEvent("A001-005283");

Constraints

This provider has the following constraints:

  • The table to store the audit events must exists on DynamoDB.
  • Its indexes must consist of top-level properties of the audit event. (Note you can add properties to the AuditEvent as Custom Fields with the SetAttribute() method on the provider configuration)

The following is an example of a table creation using the AWSSDK.DynamoDBv2 library:

var config = new AmazonDynamoDBConfig() { ServiceURL = "http://localhost:8000" };
var client = new AmazonDynamoDBClient(config);

await client.CreateTableAsync(new CreateTableRequest()
{
    TableName = "AuditEvents",
    KeySchema = new List<KeySchemaElement>()
    {
        new KeySchemaElement("EventId", KeyType.HASH),
        new KeySchemaElement("EventType", KeyType.RANGE)
    },
    AttributeDefinitions = new List<AttributeDefinition>()
    {
        new AttributeDefinition("EventId", ScalarAttributeType.S),
        new AttributeDefinition("EventType", ScalarAttributeType.S)
    },
    ProvisionedThroughput = new ProvisionedThroughput(1, 1)
});

In this case, the primary key is defined as a Hash and a Range key, with EventId being the hash, and EventType being the range. Both must be top-level properties of the Audit Event, but since the EventId is not a built-in property, you can configure it as a Custom Field:

Audit.Core.Configuration.Setup()
    .UseDynamoDB(config => config
        .UseUrl(url)
        .Table("AuditEvents")
        .SetAttribute("EventId", ev => Guid.NewGuid()));

Or you can use a global Custom Action instead with the same outcome:

Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
    scope.SetCustomField("EventId", Guid.NewGuid());
});