Client-Side Encryption¶
On this page
New in MongoDB 4.2, client-side encryption allows administrators and developers to encrypt specific fields in MongoDB documents before inserting them into the database.
With client-side encryption, developers can encrypt fields client-side without any server-side configuration or directives. Client-side encryption supports workloads where applications must guarantee that unauthorized parties, including server administrators, cannot read the encrypted data.
Warning
Enabling Client Side Encryption reduces the maximum write batch size and may have a negative performance impact.
Installation¶
Client-side encryption requires the installation of additional packages.
libmongocrypt¶
Libmongocrypt is a C library used by the driver for client-side encryption. To use client-side encryption, you must install the libmongocrypt binary on the machine running your Ruby program.
To download a pre-built binary:
- Download a tarball of all libmongocrypt variations here.
- Extract the file you downloaded. You will see a list of directories, each corresponding to an operating system. Find the directory that matches your operating system and open it.
- Inside that folder, open the folder called “nocrypto.” In either the lib or lb64 folder, you will find the libmongocrypt.so or libmongocrypt.dylib or libmongocrypt.dll file, depending on your OS.
- Move that file to wherever you want to keep it on your machine. You may delete the other files included in the tarball.
To build the binary from source:
- Follow the instructions in the README in the libmongocrypt GitHub repo.
Once you have the libmongocrypt binary on your machine, specify the path to the binary using the LIBMONGOCRYPT_PATH environment variable. It is recommended that you add this variable to your rc files. For example:
mongocryptd¶
Mongocryptd is a daemon that tells the driver which fields to encrypt in a given operation. It is only required for automatic encryption, which is an enterprise-only feature. If you only intend to use explicit encryption, you may skip this step.
Mongocryptd comes pre-packaged with enterprise builds of the MongoDB server (versions 4.2 and newer). For installation instructions, see the MongoDB manual.
In order to configure mongocryptd (for example, which port it listens on or the
path used to spawn the daemon), it is necessary to pass different options to the
Mongo::Client
performing automatic encryption. See the :extra_options
section of this tutorial for more information.
Automatic Encryption¶
Automatic encryption is a feature that allows users to configure a
Mongo::Client
instance to always encrypt specific document fields when
performing database operations. Once the Mongo::Client
is configured, it
will automatically encrypt any field that requires encryption before writing
it to the database, and it will automatically decrypt those fields when reading
them.
Client-side encryption implements envelope encryption, which is the practice of encrypting data with a data key, which is in turn encrypted using a master key. Thus, using client-side encryption with MongoDB involves three main steps:
- Create a master key
- Create a data key (and encrypt it using the master key)
- Encrypt data using the data key
The example below demonstrates how to follow these steps with a local master key in order to perform automatic encryption.
Note
Automatic encryption is an enterprise only feature that only applies to
operations on a collection. Automatic encryption is not supported for operations
on a database or view, and operations that are not bypassed will result in
error (see Auto Encryption Whitelist
). To bypass automatic encryption for all operations, set bypass_auto_encryption
to true in auto_encryption_options
.
Note
Automatic encryption requires the authenticated user to have the listCollections privilege action.
The example above demonstrates using automatic encryption with a local master key. For more information about using the AWS Key Management Service to create a master key and create data keys, see the following sections of this tutorial:
Explicit Encryption¶
Explicit encryption is a feature that allows users to encrypt and decrypt individual pieces of data such as strings, integers, or symbols. Explicit encryption is a community feature and does not require an enterprise build of the MongoDB server to use. To perform all explicit encryption and decryption operations, use an instance of the ClientEncryption class.
Client-side encryption implements envelope encryption, which is the practice of encrypting data with a data key, which is in turn encrypted using a master key. Thus, using client-side encryption with MongoDB involves three main steps:
- Create a master key
- Create a data key (and encrypt it using the master key)
- Encrypt data using the data key
The example below demonstrates how to follow these steps with a local master key in order to perform explicit encryption.
The example above demonstrates using explicit encryption with a local master key. For more information about using the AWS Key Management Service to create a master key and create data keys, see the following sections of this tutorial:
Creating a Master Key¶
Both automatic encryption and explicit encryption require an encryption master key. This master key is used to encrypt data keys, which are in turn used to encrypt user data. The master key can be generated in one of two ways: by creating a local key, or by creating a key in the Amazon Web Services Key Management Service (AWS KMS).
Local Master Key¶
A local master key is a 96-byte binary string. It should be persisted on your machine as an environment variable or in a text file.
Warning
Using a local master key is insecure and not recommended if you plan to use client-side encryption in production.
Run the following code to generate a local master key using Ruby:
AWS Master Key¶
It is recommended that you use Amazon’s Key Management Service to create and store your master key. To do so, follow steps 1 and 2 of the :drivers:`"Convert to a Remote Master Key" section</security/client-side-field-level-encryption-local-key-to-kms/#convert-to-a-remote-master-key>` in the MongoDB Client-Side Encryption documentation.
For more information about creating a master key, see the :drivers:`Create a Master Key </security/client-side-field-level-encryption-guide/#a-create-a-master-key>` section of the MongoDB manual.
Creating a Data Key¶
Once you have created a master key, create a data key by calling the
#create_data_key
method on an instance of the Mongo::ClientEncryption
class. This method generates a new data key and inserts it into the key vault
collection, which is the MongoDB collection in which you choose to store your
data keys. The #create_data_key
method returns id of the newly-created
data key in the form of a BSON::Binary object.
Create a Data Key Using a Local Master Key¶
If you have created a local master key, you may use it to generate a new data key with the following code snippet:
Warning
Using a local master key is insecure and not recommended if you plan to use client-side encryption in production.
See the Local Master Key section for more information about generating a new local master key.
Create a Data Key Using an AWS Master Key¶
If you have created an AWS master key, note the access key ID and the secret access key of the IAM user that has permissions to use the key. Additionally, note the AWS region and the Amazon Resource Number (ARN) of your master key. You will use that information to generate a data key.
See the AWS Master Key section of this tutorial for more information about generating a new master key on AWS and finding the information you need to create data keys.
For more information about creating a data key, see the :drivers:`Create a Data Encryption Key </security/client-side-field-level-encryption-guide/#b-create-a-data-encryption-key>` section of the MongoDB manual.
Auto-Encryption Options¶
Automatic encryption can be configured on a Mongo::Client
using the
auto_encryption_options
option Hash
. This section provides an overview
of the fields inside auto_encryption_options
and explains how to choose their
values.
:key_vault_client
¶
The key vault client is a Mongo::Client
instance that will be used to connect
to the MongoDB collection containing your encryption data keys. For example, if
your key vault was hosted on a MongoDB instance at localhost:30000
:
If your data keys are stored in the same MongoDB instance that stores your encrypted data, you may leave this option blank, and the top-level client will be used to insert and fetch data keys.
:key_vault_namespace
¶
The key vault namespace is a String
in the format "database_name.collection_name"
,
where database_name
and collection_name
are the name of the database and
collection in which you would like to store your data keys. For example, if your data
keys are stored in the admin
database in the datakeys
collection:
There is no default key vault namespace, and this option must be provided.
:schema_map
¶
A schema map is a Hash with information about which fields to automatically encrypt and decrypt.
The code snippet at the top of this tutorial demonstrates creating a schema
map using a Ruby Hash
. While this will work, schema maps can grow quite
large and it could be unweildy to include them in your Ruby code. Instead, it is
recommended that you store them in a separate JSON (JavaScript Object Notation)
file.
Before creating the JSON file, Base64-encode the UUID of the your data key.
Then, create a new JSON file containing your schema map in the format defined by the JSON Schema Draft 4 standard syntax. You can read more about formatting your schema map in the Automatic Encryption Rules section of the MongoDB manual.
When you intend to use your schema map, convert it to a Ruby Hash
using the
BSON::ExtJSON
module in the bson
Ruby gem.
Note
It is also possible to supply a schema map as a validator on a MongoDB collection.
This is referred to as a “remote schema map,” while providing the schema map as
an option on the Mongo::Client
is called a “local schema map.”
Supplying a local schema map provides more security than relying on JSON schemas obtained from the server. It protects against a malicious server advertising a false JSON schema, which could trick the client into sending unencrypted data that should be encrypted.
See Server-Side Field Level Encryption Enforcement in the MongoDB manual for more information about using the schema map to create a JSON schema validator on your collection.
:bypass_auto_encryption
¶
The :bypass_auto_encryption
option is a Boolean
that specifies whether the
Mongo::Client
should skip encryption when writing to the database. If
:bypass_auto_encryption
is true
, the client will still perform automatic
decryption of any previously-encrypted data.
:extra_options
¶
:extra_options
is a Hash
of options related to spawning mongocryptd.
Every option in this Hash
has a default value, so it is only necessary to
provide the options whose defaults you want to override.
:mongocryptd_spawn_args
- This is anArray<String>
containing arguments for spawning mongocryptd. The Ruby driver will pass these arguments to mongocryptd on spawning the daemon. Possible arguments are:"--idleShutdownTimeoutSecs"
- The number of seconds mongocryptd must remain idle before it shuts itself down. The default value is 60."--port"
- The port at which mongocryptd will listen for connections. The default is 27020.
:mongocryptd_uri
- The URI that the driver will use to connect to mongocryptd. By default, this is"mongodb://localhost:27020"
.:mongocryptd_spawn_path
- The path to the mongocryptd executable. The default is"mongocryptd"
.:mongocryptd_bypass_spawn
- ABoolean
indicating whether the driver should skip spawning mongocryptd.
For example, if you would like to run mongocryptd on port 30000, provide
extra_options
as follows:
Warning
The contents of :extra_options
is subject to change in future versions
of the client-side encryption API.