Learning Byte / Learn

Please note, this video is best viewed with 1080p quality selected. You can use the player bar to navigate to chapters within the video.

Chapters

0:36 The Basics

1:27 Queryable Encryption

2:43 Demo: Encrypt a Document with Queryable Encryption Using a MongoDB Driver and a Local Key

6:51 Learn More

Resources

To learn more and leave feedback:


Video Transcript (English)

Hello and welcome to the MongoDB Queryable Encryption Learning Byte where you can pick up knowledge on MongoDB in 20 minutes or less. I'm Cynthia Braund, senior product manager of data security here at MongoDB. In this Learning Byte, we'll explore the benefits of queryable encryption and how it secures sensitive data while maintaining the ability to query it. And I'll walk you through how to set up queryable encryption in MongoDB using a compatible MongoDB driver.

We'll go the different types of encryption, where they're used, and what they protect against. Encryption in transit, otherwise known as TLS, encrypts data while it's in flight over a network and protects from eavesdropping on that network. Encryption at rest protects data while it's stored in a database and is a fairly common practice nowadays. This protects against theft of disk drives or other storage media.

Once a database starts up and is running, that data is no longer protected. In use encryption solutions like queryable encryption protect data at the client layer, meaning that data is protected in use, in memory, in logs, and in backups as well as in transit and at rest. The drawbacks of encrypting data at the client side is that once you encrypt that data, it isn't very usable. And that's why we developed queryable encryption.

The unique aspect of queryable encryption is how it allows the database to process queries on encrypted data without ever decrypting it, and without the server knowing what that data is. Take this example of a billing database where we need to find all customer records with Social Security Number 901-10-4312. For those of you not in the US, a Social Security number is a government issued unique identifier given to people in the US. This query is issued by the client and you can see it looks like a normal MongoDB query.

That query hits the MongoDB driver. The driver knows the field should be encrypted, so it reaches out to a key provider, usually a Cloud KMS, and gets the key and encrypts that data. When the query is received by the server, it doesn't know that the query is asking for Social Security number 901-10-4312. All it knows is that it's supposed to return three documents.

The encrypted documents are returned back to the driver where they're decrypted and sent to the application. For more information on how it works, please see the query encryption documentation found at the end of this video, or by searching queryable encryption on the MongoDB docs site.

Let's take a look at how to set up, insert, and query data with MongoDB Query Encryption and a compatible MongoDB driver. Before we dive into our demo, please review the following prerequisites to set up your environment.

For this demo, imagine you're a developer at a medical billing company and your security team tells you you need to encrypt any PII, or PHI in your applications. Since you store Social Security Number and billing addresses in your application, you'll need to encrypt those. Your business users, however, still need to be able to query on Social Security Number. So you'll need to encrypt them in a way that preserves that functionality.

In this demo, we're going to set up encryption keys using what we call a local key, just a key sitting on your local file system. We're showing this because it does not require access to a key management system and it's easiest to use for a simple testing. However, never use a local key for a production app. It's not secure and leaves your keys and data vulnerable.

Queryable Encryption supports AWS KMS, GCP Cloud KMS, Azure Key Vault, and KMIP, such as Hashicorp Vault Enterprise for secure key management. Before we get started, I've set up some application variables, but you can hardcode these as well. First, let's start by creating a customer master key by generating a 9-6 byte CMK and save it to your file system as the file customer-master-key.TXT. Here, I'm showing how to do it in Python.

In this step, we'll specify KMS provider options. Retrieve the contents of the customer master key file and pass the CMK value to your KMS provider. The client uses these settings to discover the CMK. Set the provider name to local to inform the driver you are using a local key provider.

Next, set automatic encryption options. These include the KMS provider credentials object, defining the previous step, the namespace of of your key vault collection, and the path to your automatic encryption shared library. I have many of these set as variables, but you can also hardcode them. Here are the KMS provider credentials and auto encryption options using the helper methods I just showed you.

Then create the Mongo client to be used for encryption and decryption. Instantiate a MongoDB client object with the automatic encryption settings. Specify the fields you want to encrypt by creating an encrypted fields map for your collection. In this demo, we are encrypting Social Security Number with a query type of equality, and the billing object will be encrypted but not queryable.

Now you can create your collection. Create your collection using the Create encrypted collection helper method. This will automatically create the data encryption keys for you and will ensure that the proper indexes are created. Next, use your queryable encryption enabled Mongo client instance to insert an encrypted document into the medical records .patient's namespace. Finally, retrieve the encrypted document you inserted by querying on the encrypted feed it. And let's run the program. On here, you can see the data, Social Security Number, and the billing object are not encrypted because we're a application that has access to the keys.

Switching to Atlas, you see what the data looks like if queried from the database. Here's the record we just inserted into the patient's collection in our medical record database. Social Security Number and billing objects are a series of asterisks, which is how Atlas UI displays encrypted data. 

In this Learning Byte we discussed how queryable encryption addresses modern data protection challenges, by securing your data end to end while maintaining the ability to query that data without decrypting it, and showed you how to set up.

For more information on queryable encryption, or how to provide feedback on the product, please refer to the following resources below.