4.6.0 • Published 2 years ago

hapi-audit-rest v4.6.0

Weekly downloads
7
License
MIT
Repository
github
Last release
2 years ago

hapi-audit-rest

npm version Build Status Known Vulnerabilities Coverage Status

Small opinionated Hapi.js plugin that generates audit logs for RESTful APIs.

Contents

Requirements

Works with Hapi.js v18 or higher, Node.js v14 or higher. For compatibility with Node.js v12 check version 3.

Installation

npm i -S hapi-audit-rest

Testing

npm test

About

This plugin creates audit log documents based on REST semantics.

HTTP methodDescriptionAudit Log Document
GETRetrieve resourcesAction
POSTCreate a new resourceMutation - Create
PUTUpdate a resourceMutation - Update
DELETEDelete a resourceMutation - Delete

Mutations track old and new state of a resource to effectively reason about state changes.

For every request an event is emitted with an audit log document.

Quickstart

await server.register({
    plugin: require("hapi-audit-rest"),
});

Audit Log Document Schemas

Action Schema

{
    application: String
    type: String,
    body: {
        entity: String,
        entityId: String|Number|Null,
        action: String,
        username: String|Null,
        data: Object|Null,
        timestamp: String,
    },
    outcome: String
};

Mutation Schema

{
    application: String
    type: String,
    body: {
        entity: String,
        entityId: String|Number|Null,
        action: String,
        username: String|Null,
        originalValues: Object|Array|Null,
        newValues: Object|Array|Null,
        timestamp: String,
    },
    outcome: String
};

Example Audit Log Documents

Consider a CRUD API on users.

GET Requests

// emitted data on GET /api/users?page=1&limit=10&sort=asc&column=id
{
    application: "my-app",
    type: "SEARCH",
    body: {
        entity: "/api/users",
        entityId: null,
        action: "SEARCH",
        username: null, // or the username if authenticated
        timestamp: "2021-02-13T18:11:25.917Z",
        data: {
            page: 1,
            limit: 10,
            sort: 'asc',
            column: 'id'
        },
    },
    outcome: "Success",
};

// emitted data on GET /api/users/1
{
    application: "my-app",
    type: "SEARCH",
    body: {
        entity: "/api/users/1",
        entityId: "1",
        action: "SEARCH",
        username: null, // or the username if authenticated
        timestamp: "2021-02-13T18:11:25.917Z",
        data: {},
    },
    outcome: "Success",
};

POST Requests

// consider the payload
const user = {
    username: "user",
    firstName: "first name",
    lastName: "last name",
};

// emitted data on POST /api/users, with payload user, created user with id returned in response
{
    application: "my-app",
    type: "MUTATION",
    body: {
        entity: "/api/users",
        entityId: 1,
        action: "CREATE",
        username: null, // or the username if authenticated
        originalValues: null,
        newValues: {
            id: 1,
            username: "user",
            firstName: "first name",
            lastName: "last name",
        },
        timestamp: "2021-02-20T20:53:04.821Z",
    },
    outcome: "Success",
};

DELETE Requests

// emitted data on DELETE /api/users/1
{
    application: "my-app",
    type: "MUTATION",
    body: {
        entity: "/api/users/1",
        entityId: 1,
        action: "DELETE",
        username: null, // or the username if authenticated
        originalValues: {
            id: 1,
            username: "user",
            firstName: "first name",
            lastName: "last name",
        },
        newValues: null,
        timestamp: "2021-02-20T20:53:04.821Z",
    },
    outcome: "Success",
};

PUT Requests

// consider the payload
const user = {
    firstName: "updated first",
};
// emitted data on PUT /api/users/1
{
    application: "my-app",
    type: "MUTATION",
    body: {
        entity: "/api/users/1",
        entityId: 1,
        action: "UPDATE",
        username: null, // or the username if authenticated
        originalValues: {
            id: 1,
            username: "user",
            firstName: "first name",
            lastName: "last name",
        },
        newValues: {
            firstName: "updated first", // use option fetchNewValues for the whole updated entity object
        },
        timestamp: "2021-02-20T20:53:04.821Z",
    },
    outcome: "Success",
};

API

Plugin registration options

await server.register({
    plugin: require("hapi-audit-rest"),
    options: {
        // plugin registration options
    },
});
NameTypeDefaultMandatoryDescription
debugBooleanfalsenoDisplay errors on std error stream.
diffFuncFunctionprovidednoExternal function to diff old and new values, Must return an array with two elements: old values and new values, with that order. The default implementation returns fetched old and new values. Signature function (oldValues, newValues) {return [oldValues, newValues]}
isCacheEnabledBooleantruenoEnable/Disable internal cache. Use cache only if running an one instance server (default enabled). If a GET by id is triggered before an update (PUT), old values will be loaded from cache instead of requiring an extra GET by id API call.
clientIdStringmy-appnoApplication instance name or auth client id.
usernameKeyStringnoThe path/key to the username stored in request.auth.credentials object.
cacheExpiresInNumber Positive Integer900000 (15mins)noTime (msecs) until cache expires (when cacheEnabled = false). Minimum 60000 (1 minute).
isAuditableFunctionprovidednoChecks if current request is auditable. The default implementation audits all requests.Signature function (request) {return Boolean}
eventHandlerFunctionprovidednoHandler for the emitted events. The default implementations prints the audit log to stdout. You will have to implement this function in order to do something with the audit log.Signature function ({ auditLog, routeEndpoint })
setEntityFunctionprovidednoCreates the entity name of the audit log. The default implementation returns the endpoint path.Signature function (path) {return String}
isEnabledBooleantruenoEnable/Disable plugin initialization and functionality.
extAllFunction-noA global override entrypoint to extend any value of any created audit log document, invoked on pre-response. Signature function (request, auditLog) {return Object}

Handle common cases

Common use cases for isAuditable option:

await server.register({
    plugin: require("hapi-audit-rest"),
    options: {
        isAuditable: ({ auth: { isAuthenticated }, method, url: { pathname } }) => {
            // do not audit unauthenticated requests
            if (!isAuthenticated) {
                return false
            }
            
            // do not audit GET requests
            if (method === "get") {
                return false
            }
            
            // do not audit requests when path does not start from /api
            if (!pathname.startsWith("/api")) {
                return false
            }
            
            // return true to audit all other cases
            return true
        }
    },
});

Common use cases for setEntity option:

await server.register({
    plugin: require("hapi-audit-rest"),
    options: {
        // use the standard pattern of an api i.e. /api/v1.0/users, to refine the entity name
        // will have 'entity: users' in audit log
        setEntity: (path) => path.split("/")[3], 
        }
    },
});

Plugin route options

// at any route
options: {
   plugins: {
      "hapi-audit-rest": {
        // plugin route options
      }
   }
}
NameTypeDefaultMandatoryDescription
extFunctionnoAn extension point per route, invoked on pre-response, to customize audit log document values: on GETasync (request) => AuditActionon POSTasync (request, { newVals }) => AuditMutationon PUTasync (request, { oldVals, newVals, diff }) => AuditMutationdiff: ({diffOnly, skipDiff}) => [originalValues, newValues] on DELETEasync (request, { oldVals }) => AuditMutationon PUT/POST/DELETE and isAction=true async (request) => AuditActionMust return an object (AuditAction or AuditMutation) with any of the following properties to override the default values: Audit Actiontype Stringentity StringentityId String/Number/Nullaction Stringdata Object/NullAudit Mutationentity StringentityId String/Number/Nullaction StringoriginalValues Object/Array/NullnewValues Object/Array/Null
isActionBooleanfalsenoEnable/Disable creation of action audit log documents for PUT/POST/DELETE requests instead of mutation.
setInjectedPathFunctionnoOn PUT requests, old and/or new values are fetched by injecting a GET by id request, based on PUT route path. When GET by id route path differs, it must be provided. Signature function (request) {return String}
fetchNewValuesBooleanfalsenoOn PUT requests, the incoming payload will be used as newValues. In case there are any model inconsistencies, this option will inject a GET by id request to fetch the newValues.

Disable plugin on route

By default the plugin applies to all registered routes. Should you need to exclude any, apply to the route:

options: {
   plugins: {
      "hapi-audit-rest": false,
   },
}

Flows & Audit Log Data

To effectively track old and new state of a resource, the plugin implements internal flows based on the following semantics:

HTTP methodScopeDescription
GETcollectionRetrieve all resources in a collection
GETresourceRetrieve a single resource
POSTresourceCreate a new resource in a collection
PUTresourceUpdate a resource
DELETEresourceDelete a resource

To override audit log document defaults use the route extension point. To completely override any created audit log document use the global override registration option extend all.

GET - scope collection

get_collection_flow

An action audit log document is created, on pre-response lifecycle if the request succeeds with the following defaults:

{
    application: "my-app",		// or the clientId if specified
    type: "SEARCH",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: null,
        action: "SEARCH",
        username: null,			// or the username if authenticated
        timestamp: Date.now(),
        data: request.query,
    },
    outcome: "Success",
};

GET - scope resource

get_resource_flow

An action audit log document is created, on pre-response lifecycle if the request succeeds with the following defaults:

{
    application: "my-app",		// or the clientId if specified
    type: "SEARCH",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: request.params.id,
        action: "SEARCH",
        username: null,			// or the username if authenticated
        timestamp: Date.now(),
        data: request.query,
    },
    outcome: "Success",
};

The response is cached if cashing enabled.

POST - scope resource

mutation (default)

post_resource_mutation_flow_2

A mutation audit log document is created on pre-response lifecycle if the request succeeds with the following defaults:

{
    application: "my-app",		// or the clientId if specified
    type: "MUTATION",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: request.response.source.id || request.payload.id,
        action: "CREATE",
        username: null,			// or the username if authenticated
        originalValues: null,
        newValues: request.response.source || request.payload,	// the response or the payload if response null
        timestamp: Date.now()",
    },
    outcome: "Success",
};
  • POST mutations rely to request payload or response payload to track the new resource state. If request is streamed to an upstream server this will result to an error.
action

post_put_delete_resource_action_flow_2

In cases that it is not meaningful to audit a mutation, an action audit log document can be created by setting isAction route parameter.

{
    application: "my-app",		// or the clientId if specified
    type: "SEARCH",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: request.params.id || request.payload.id,
        action: "SEARCH",
        username: null,			// or the username if authenticated
        timestamp: Date.now(),
        data: request.payload,	// or null if request streamed
    },
    outcome: "Success",
};

PUT - scope resource

mutation (default)

put_resource_mutation_flow_2

A mutation audit log document is created on pre-response lifecycle if the request succeeds with the following defaults:

{
    application: "my-app",		// or the clientId if specified
    type: "MUTATION",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: request.params.id || newValues.id,	// where newValues is either the request payload (default) or the resource data fetched after update when fetchNewValues=true or request streamed
        action: "UPDATE",
        username: null,			// or the username if authenticated
        originalValues: $,		// values fetched with injected GET by id call (or loaded from cache)
        newValues: request.payload || newValues,	// newValues = values fetched by injected GET by id call when fetchNewValues=true or request streamed
        timestamp: Date.now()",
    },
    outcome: "Success",
};

PUT mutations are the most complex.

  • Before the update, the original resource state is retrieved by inspecting the cache. If not in cache a GET by id request is injected based on the current request path (custom path can be set on route with setInjectedPath).
  • After the update, the new resource state is retrieved from the request payload. If the request is streamed or the fetchNewValues option is set, a GET by id request will be injected to fetch the new resource state.
action

In cases that it is not meaningful to audit a mutation, an action audit log document can be created by setting isAction route parameter.

{
    application: "my-app",		// or the clientId if specified
    type: "SEARCH",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: request.params.id || request.payload.id,
        action: "SEARCH",
        username: null,			// or the username if authenticated
        timestamp: Date.now(),
        data: request.payload,	// or null if request streamed
    },
    outcome: "Success",
};

DELETE - scope resource

mutation (default)

delete_resource_mutation_flow

A mutation audit log document is created on pre-response lifecycle if the request succeeds with the following defaults:

{
    application: "my-app",		// or the clientId if specified
    type: "MUTATION",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: request.params.id || originalValues.id,	// where originalValues = resource state before delete
        action: "DELETE",
        username: null,			// or the username if authenticated
        originalValues: $,		// values fetched with injected GET by id request before delete
        newValues: null,
        timestamp: Date.now()",
    },
    outcome: "Success",
};

DELETE mutations retrieve old resource state by injecting a GET by id request before the delete operation.

action

In cases that it is not meaningful to audit a mutation, an action audit log document can be created by setting isAction route parameter.

{
    application: "my-app",		// or the clientId if specified
    type: "SEARCH",
    body: {
        entity: $,				// as specified by setEntity function
        entityId: request.params.id || request.payload.id,
        action: "SEARCH",
        username: null,			// or the username if authenticated
        timestamp: Date.now(),
        data: request.payload,	// or null if request streamed
    },
    outcome: "Success",
};

Error handling

When an error occurs, it is logged using the request.log(tags, [data]) method:

  • tags: "error", "hapi-audit-rest"
  • data: error.message

The server isntance can interact with log information:

server.events.on({ name: "request", channels: "app" }, (request, event, tags) => {
    if (tags.error && tags["hapi-audit-rest"]) {
        console.log(event); // do something with error data
    }
});

If debug option is enabled (disabled by default), the error message will be printed to stderr for convenience.

License

hapi-audit-rest is licensed under a MIT License.

4.6.0

2 years ago

4.5.0

2 years ago

4.4.0

2 years ago

4.1.0

2 years ago

4.0.1

2 years ago

4.3.0

2 years ago

4.2.0

2 years ago

3.5.2

4 years ago

4.0.0

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.0

4 years ago

3.3.0

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.13.0

4 years ago

1.12.1

4 years ago

1.12.0

4 years ago

1.11.3

4 years ago

1.11.2

4 years ago

1.11.1

4 years ago

1.11.0

4 years ago

1.10.1

4 years ago

1.10.0

4 years ago

1.9.0

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago