Skip to main content

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement:
As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB.

The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations.


Solution 1: Diagnostic Logs
Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was the operation that was done on our document (e.g. create, read).
This is the most basic operation that even if is generated by diagnostic logs can be used as audit as long as we don’t need what changes were done on each document. In diagnostics logs we will not be able to find this information.
Don’t forget to check the maximum value for retention policy and push data to a storage before this period.

Solution 2: Change feed
These features of Azure Cosmos DB allow us to stream to an external system any change operations that happen on our collections. It is built in inside Azure Cosmos DB, allowing us to catch all the changes that were done on our collection without taking into account what system have done the change.
The feed generated by Cosmos DB can be ingested by multiple systems like:

  • Azure Function
  • App Services
  • Azure Notification Hubs
  • Azure HDInsight
  • Apache Spark
  • Apache Storm
  • Azure Stream Analytics
  • Azure Cosmos DB
  • Azure Storage (Table, Blobs)
  • Azure Data Lake

For audit system, we might want to push the data inside the most cheap storage solution available – Azure Storage Blob or Azure Data Lake. Why? When we collect audit data, we do not need to do any action except storing the data itself for later use.
When data processing is required, we can use Azure Functions or any stream analytics solution available to ingest and crunch the audit data. From scalability perspective, a consumer is available for each collection partition, enabling us to scale-up the solution with multiple nodes for the same collection. These is done using the lease and consumer groups’ concept that it is used for Azure Event Hub.

The changed fee is available not only in the write region, but also in the read regions, giving us the possibility to decide in what region (replica) we want to do this action. By default, only Create and Update operations are written in the feed. The delete operations can be written also but you need to specify this (set a specific flag). Once you activate it, you will catch also the deletion of documents that have the TTL set and are deleted automatically (expires).
Another cool feature of change feed is that there is no expiration time available. You can requests the change feed for the last 12 months anytime, because there is no retention policy.

There some things related to change feed that you should know before jumping to this solution as audit system. First of all you will not be able to know who made the change. This means that you will know the state change of a document, but without adding extra fields in the document it is impossible to know who made the change to it.

The biggest problem from the audit perspective related to change feed is that not all the changes can be found in the feed. Only the most recent change in the document will be available, allowing us to know what document was change, but without being able to know how many changes were done on the document.
Change feed can be an interesting feature if we need to audit an Azure Cosmos DB, but we need to ensure that all business requirements related to the audit system can be covered.

Solution 3: Custom proxy
The first two solution are out of the box solution available inside Azure Cosmos DB. They are offering good support, but might lack of some information that you need when you want to fulfil all the requirements of an audit system.
For example using the Diagnostics Logs of Azure Cosmos DB you know who done a change and the operation type, but without knowing what were the changes done on a document. Using change feed you will know that a document suffered some changes, but without knowing what were the changes and who done them.
Adding a proxy on top of Azure Cosmos DB would fulfil all the requirements that you can have on an audit system, but it will kill the performance. Having a proxy on top of Azure Cosmos DB means that the proxy need to be thin and to have a minimum impact from latency perspective.
This proxy might be required when you expose the Azure Cosmos DB repository to multiple system, where you don’t have control what actions are done by each external system.

Solution 4: Triggers
Azure Cosmos DB has a good support for triggers and are extremely powerful. Can be used with success to collect audit information as long as you do not need user information (the person who done the trigger). There is no way to find information related to it. Beside this you need to define triggers for each collection and operations, this increase complexity and chances to make a mistake.

Conclusion
Each of this solution comes with pros and cons. The 3rd one is the most powerful, but if you ask me I would do anything possible to avoid having a proxy between system and Azure Cosmos DB. This would increase latency and could become a bottleneck easily.
Change feed it is a nice and useful when you need to stream changes, but might not cover all the changes that are happening on your storage. Additional to this you might lose information related to who made the change.

My main preference is using Diagnostics Logs, that could provide enough information to fulfil the audit requirements. If this is not an option than using a proxy or something similar might be the only way for you

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP