Auditing and Monitoring MongoDB Access

Tutorial 4 of 5

1. Introduction

Goal of Tutorial

By the end of this tutorial, you will understand how to audit and monitor MongoDB access. Auditing is crucial for tracking and analyzing activities in your MongoDB database, which are essential tasks when it comes to maintaining security and troubleshooting issues.

What You Will Learn

You will learn how to:
* Enable auditing in MongoDB
* Configure auditing filters
* Use MongoDB built-in tools for monitoring

Prerequisites

Basic understanding of MongoDB and familiarity with JavaScript. You should also have MongoDB installed on your machine.

2. Step-by-Step Guide

Enabling MongoDB Auditing

MongoDB Enterprise allows you to log all system activity for your database. To enable auditing, you'll need to add parameters in the MongoDB configuration file (mongod.conf) as follows:

auditLog:
   destination: file
   format: JSON
   path: /var/mongodb/db/auditLog.json

The above configuration will log all activities in a JSON file named auditLog.json.

Configuring Audit Filters

MongoDB allows you to selectively audit actions based on several criteria. You can specify the filter in the auditLog section of the configuration file.

auditLog:
   filter: '{ atype: { $in: [ "insert", "delete", "update" ] } }'

This configuration will log only insert, delete, and update operations.

Monitoring MongoDB Access

MongoDB provides built-in tools such as MongoDB Atlas, which allows you to monitor your database instances. It includes charts, custom dashboards, and automated alerting.

3. Code Examples

Example: Enabling Auditing

Here is a basic example of how you would enable auditing in MongoDB.

auditLog:
   destination: file
   format: BSON
   path: /mongodb/data/db/auditLog.bson
   filter: '{ atype: "authenticate", "param.user": "myUser", "param.db": "myDatabase" }'

In this example, we are logging all authentication actions from "myUser" on "myDatabase".

Example: Monitoring with MongoDB Atlas

Once you have MongoDB Atlas set up, monitoring is pretty straightforward. Here is a basic example using the MongoDB Atlas API.

const {MongoClient} = require('mongodb');
async function main(){
   const uri = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/test";
   const client = new MongoClient(uri);
   await client.connect();
   await listDatabases(client);
   await client.close();
}
async function listDatabases(client){
   databasesList = await client.db().admin().listDatabases();
   console.log("Databases:");
   databasesList.databases.forEach(db => console.log(` - ${db.name}`));
};
main().catch(console.error);

This script connects to your MongoDB Atlas cluster and lists all the databases.

4. Summary

In this tutorial, we've covered how to enable and configure auditing in MongoDB and how to monitor MongoDB access using MongoDB Atlas. With these skills, you can track and analyze activities in your MongoDB database for better security and troubleshooting.

5. Practice Exercises

  1. Try to enable auditing on your MongoDB database, make it log only "query" and "createIndex" operations.
  2. Connect to MongoDB Atlas and list all collections in a specific database.

For further practice, consider exploring more about MongoDB Atlas and its various features related to database monitoring and security.