How to use Encryption to Protect MongoDB data

Agus Syafaat

With many kinds of data stored in the database, sometimes we may be dealing with confidential data, which might include credit card data, financial record, personal information. Those PII (Personal Identifiable Information) data is subject to regulation eg: PCI DSS, HIPAA, or GDPR which we need to protect and ensure confidentiality, integrity and availability.

Data Encryption is part of architecting MongoDB for security implementation in production environments. The aim of data encryption is adding more safeguards for security of the data, especially from insider threats. We lock down the service and ports of the database, maintain an access control list of who can access and perform which operations into the database, and enable encryption to protect from sniffing during network transmission, or when the data is stored. In this blog, we will discuss how to use encryption in MongoDB.

Data Encryption in Transit

Data Encryption in transit ensures MongoDB data is secured between the clients (i.e., application server) and the database server and in between the database servers in the MongoDB ReplicaSet or ShardedCluster architecture. MongoDB uses SSL/TLS certificates, either generated as self signed certificates or certificates which are issued by the Certificate Authority.

The best way is to use the certificate from a certificate authority, because it will allow MongoDB drivers to check the host with the Certificate Authority which means there will be a validation of server identity to avoid man-in-the-middle attack. You can still use the self signed certificate in a trusted network.

MongoDB SSL/TLS encryption must use the TLS/SSL ciphers with a minimum of 128-bit key. Starting from MongoDB version 4.2 and above, there is a new parameter called net.tls. It provides the same functionality as net.ssl. The configuration in mongod.conf file as shown below:

net:
   tls:
      mode: requireTLS
      certificateKeyFile: /etc/ssl/mongodb.pem

 

While if we want to add Client Certificate Validation, we just need to add parameter CAFile as following:

      

net:
         tls:
            mode: requireTLS
            certificateKeyFile: /etc/ssl/mongodb.pem
            CAFile: /etc/ssl/caClientCert.pem

With the above configuration, MongoDB SSL/TLS connections require valid certificates from the clients and the client must specify SSL/TLS connection and present the certificate key files.

 In the above configuration, we use net.tls which exists on MongoDB 4.2. For the above version, we can use net.ssl configuration as shown below:

net:
   ssl:
      mode: requireSSL
      PEMKeyFile: /etc/ssl/mongodb.pem

And adding Client Certificate Validation is similar with the net.tls configuration. Just add the parameter CAFile as shown below:

net:
   ssl:
      mode: requireSSL
      PEMKeyFile: /etc/ssl/mongodb.pem
      CAFile: /etc/ssl/caClientCert.pem

 

Data Encryption at Rest

Talking about data encryption at rest, there are several methods of MongoDB data encryption which are: 

  • Database Storage Engine encryption

MongoDB provides native encryption on the WiredTiger storage engine. The data rest encryption requires two keys protection for the data, which are master key used for encrypting the data and master key used to encrypt the database keys. The encryption uses AES256-CBC Advanced Encryption Standard. It uses asymmetric keys which is the same key for encrypting and decrypting the data. It is available only in Enterprise Edition starting from version 3.2 and above.

Percona Server for MongoDB has data encryption at rest which comes as part of the open source server, introduced from version 3.6. The current release does not include the Key Management Interoperability Protocol (KMIP) or Amazon KMS. We can use a local keyfile or a third party key management server such as Hashicorp Vault.

The parameter in Percona Server for MongoDB related to encryption is encryptionCipherMode which we can configure by choosing one of the following cipher modes:

  • AES256-CBC

  • AES256-GCM

The default cipher is AES256-CBC if you did not explicitly apply one of the above. We can enable data at rest encryption on the new Percona Server for MongoDB installation, but it does not support existing MongoDB services.

  • Disk/Storage Encryption

The storage encryption is the encryption of the storage media. We can use Linux based disk encryption such as LUKS to encrypt the data volume of the disk, or if we use a cloud environment, there might be an encryption option. For instance, in AWS it is possible to have encrypted block storage as well as S3 storage. 

  • API based Encryption

The API based encryption uses third party encryption software or the application provides an algorithm to encrypt the data before it is stored in the MongoDB database. The entire process is handled by the application layer.

ClusterControl
The only management system you’ll ever need to take control of your open source database infrastructure.