- Security >
- Encryption >
- In-Use Encryption >
- Introduction >
- Reference >
- Encryption Schemas
Encryption Schemas¶
On this page
Overview¶
Note
Enterprise Feature
The automatic feature of field level encryption is only available in MongoDB Enterprise 4.2 or later, and MongoDB Atlas 4.2 or later clusters.
New in version 4.2.
Encryption schemas contain user-specified rules that identify which fields must be encrypted and how to encrypt those fields. Applications must specify the automatic encryption rules using a strict subset of the JSON Schema Draft 4 standard syntax and the following encryption-specific keywords:
- Encrypt specifies the encryption options to use when encrypting the current field.
- Encrypt Metadata specifies inheritable encryption options.
For the MongoDB 4.2+ shell, use the Mongo constructor
to create the database connection with the automatic encryption rules
included as part of the
configuration object.
See Connect to a MongoDB Cluster with Automatic Client-Side Encryption Enabled
for an example.
For the official MongoDB 4.2+ compatible drivers, use the
driver-specific database connection constructor (MongoClient)
to create the database connection with the automatic encryption rules
included as part of the
configuration object. To learn more about -specific
MongoClient options, see the mongo client page.
Important
Don’t Specify Document Validation Keywords In Your Encryption Schema
Do not specify document validation keywords in the automatic encryption rules. To define document validation rules, configure schema validation.
Definition¶
-
encrypt¶ Object
Indicates that
<fieldName>must be encrypted. Theencryptobject has the following requirements:encryptcannot have any sibling fields in the<fieldName>object.encryptmust be the only child of the<fieldName>object.encryptcannot be specified within any subschema of theitemsoradditionalItemskeywords. Specifically, automatic does not support encrypting individual elements of an array.
The
encryptobject can contain only the following fields:Including any other field to the
encryptobject results in errors when issuing automatically encrypted read or write operationsIf
keyIdoralgorithmare omitted, the Install and Configure mongocryptd checks the full tree of parent fields and attempts to construct those options from the nearestencryptMetadataobject that specifies the option.bsonTypecannot be inherited and may be required depending on the value ofalgorithm.If
mongocryptdcannot construct the fullencryptobject using the fields specified to the object and any requiredencryptMetadata-inherited keys, automatic encryption fails and returns an error.
-
encrypt.algorithm¶ String
Indicates which encryption algorithm to use when encrypting values of
<fieldName>. Supports the following algorithms only:AEAD_AES_256_CBC_HMAC_SHA_512-RandomAEAD_AES_256_CBC_HMAC_SHA_512-Deterministic
For complete documentation on the encryption algorithms, see Fields and Encryption Types.
If omitted, <csfle-reference-mongocryptd> checks the full tree of parent fields for the nearest
encryptMetadata.algorithmkey and inherits that value. If no parentalgorithmexists, automatic field level encryption fails and returns an error.- If
encrypt.algorithmor its inherited value isAEAD_AES_256_CBC_HMAC_SHA_512-Deterministic, theencryptobject requires theencrypt.bsonTypefield. - If
encrypt.algorithmor its inherited value isAEAD_AES_256_CBC_HMAC_SHA_512-Random, theencryptobject may include theencrypt.bsonTypefield.
-
encrypt.bsonType¶ String | Array of Strings
The BSON type of the field being encrypted. Required if
encrypt.algorithmisAEAD_AES_256_CBC_HMAC_SHA_512-Deterministic.If
encrypt.algorithmor its inherited value isAEAD_AES_256_CBC_HMAC_SHA_512-Deterministic,bsonTypemust specify a single type.bsonTypedoes not support any of the following BSON types with the deterministic encryption algorithm:doubledecimal128boolobjectarrayjavascriptWithScope(Deprecated)
If
encrypt.algorithmor its inherited value isAED_AES_256_CBC_HMAC_SHA_512-Random,bsonTypeis optional and may specify an array of supported bson types. For fields withbsonTypeofarrayorobject, the client encrypts the entire array or object and not their individual elements.encrypt.bsonTypedoes not support the following types regardless ofencrypt.algorithmor its inherited value:minKeymaxKeynullundefined
-
encrypt.keyId¶ Array of single UUID
The UUID of the to use for encrypting field values. The UUID is a BSON binary data element of subtype
4.Specify one string inside the array.
If omitted, Install and Configure mongocryptd checks the full tree of parent fields for the nearest
encryptMetadata.keyIdkey and inherits that value. If no parentkeyIdexists, automatic field level encryption fails and returns an error.The
keyIdor its inherited value must exist in the specified as part of the auto encryption configuration options. If the specified does not exist, automatic encryption fails.Official MongoDB 4.2+ compatible drivers have language-specific requirements for specifying the UUID. Defer to the driver documentation for complete documentation on implementing client-side field level encryption.
-
encryptMetadata¶ Object
Defines encryption options which an
encryptobject nested in the siblingpropertiesmay inherit. If anencryptis missing an option required to support encryption,mongocryptdsearches the entire tree of parent objects to locate anencryptMetadataobject that specifies the missing option.encryptMetadatamust be specified in subschemas withbsonType: "object".encryptMetadatacannot be specified to any subschema of theitemsoradditionalItemskeywords. Specifically, automatic does not support encrypting individual elements of an array.The
encryptMetadataobject can contain only the following fields. Including any other field to theencryptobject results in errors when issuing automatically encrypted read or write operations:
-
encryptMetadata.algorithm¶ String
The encryption algorithm to use to encrypt a given field. If an
encryptobject is missing thealgorithmfield,mongocryptdsearches the entire tree of parent objects to locate anencryptMetadataobject that specifiesencryptMetadata.algorithm.Supports the following algorithms only:
AEAD_AES_256_CBC_HMAC_SHA_512-RandomAEAD_AES_256_CBC_HMAC_SHA_512-Deterministic
For complete documentation on the encryption algorithms, see Fields and Encryption Types.
If specifying
AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic, anyencryptobject inheriting that value must specifyencrypt.bsonType.
-
encryptMetadata.keyId¶ Array of single UUID
The UUID of a . The UUID is a BSON binary data element of subtype
4.Specify one string inside the array.
If an
encryptobject is missing thekeyIdfield,mongocryptdsearches the entire tree of parent objects to locate anencryptMetadataobject that specifiesencryptMetadata.keyId.The must exist in the specified as part of the auto encryption configuration options. The specified configuration options must also include appropriate access to the Key Management Service (KMS) and
(CMK) used to create the data key. Automaticencryption fails if the does not exist or if the client cannot decrypt the key with the specified KMS and CMK.
Official MongoDB 4.2+ compatible drivers have language-specific requirements for specifying the UUID. Defer to the driver documentation for complete documentation on implementing client-side field level encryption.
Examples¶
Encryption Schema - Multiple Fields¶
Consider a collection MedCo.patients where each document has
the following structure:
The following fields contains personally identifiable information (PII) that may be queried:
passportIdbloodTypeinsurance.policyNumberinsurance.provider
The deterministic encryption algorithm guarantees that the encrypted output of a value remains static. This allows queries for a specific value to return meaningful results at the cost of increased susceptibility to frequency analysis recovery. The deterministic encryption algorithm therefore meets both the encryption and queryability requirements of the data.
The following fields contain legally protected personally identifiable information (PII) that may never be queried:
medicalRecords
The randomized encryption algorithm guarantees that the encrypted output of a value is always unique. This prevents queries for a specific field value from returning meaningful results while supporting the highest possible protection of the field contents. The randomized encryption algorithm therefore meets both the encryption and queryability requirements of the data.
The following schema specifies automatic encryption rules which meet the
above requirements for the MedCo.patients collection:
The above automatic encryption rules mark the passportId,
bloodType, insurance.policyNumber, insurance.provider,
and medicalRecords fields for encryption.
- The
passportId,bloodType,insurance.policyNumber, andproviderfields require deterministic encryption using the specified key. - The
medicalRecordsfield requires randomized encryption using the specified key.
While does not support encrypting
individual array elements, randomized encryption supports encrypting the
entire array field rather than individual elements in the field. The
example automatic encryption rules specify randomized encryption for the
medicalRecords field to encrypt the entire array. If the automatic
encryption rules specified encrypt or
encryptMetadata within medicalRecords.items or
medicalRecords.additionalItems, automatic field level encryption
fails and returns an errors.
The official MongoDB 4.2+ compatible drivers, mongosh,
and the 4.2 or later legacy mongo shell require
specifying the automatic encryption rules as part of creating the
database connection object:
- For
mongosh, use theMongo()constructor to create a database connection. Specify the automatic encryption rules to theschemaMapkey of the <> parameter. See Connect to a MongoDB Cluster with Automatic Client-Side Encryption Enabled for a complete example. - For the official MongoDB 4.2+ compatible drivers, use the
driver-specific database connection constructor (
MongoClient) to create the database connection with the automatic encryption rules included as part of the configuration object. Defer to the driver API reference for more complete documentation and tutorials.
For all clients, the keyVault and kmsProviders specified
to the parameter must grant
access to both the s specified in the automatic
encryption rules and the used to encrypt the
s.
Encryption Schema - Multiple Fields With Inheritance¶
Consider a collection MedCo.patients where each document has
the following structure:
The following fields contain private data that may be queried:
passportIdbloodTypeinsurance.policyNumberinsurance.provider
The deterministic encryption algorithm guarantees that the encrypted output of a value remains static. This allows queries for a specific value to return meaningful results at the cost of increased susceptibility to frequency analysis recovery. The deterministic encryption algorithm therefore meets both the encryption and queryability requirements of the data.
The following fields contain private data that may never be queried:
medicalRecords
The randomized encryption algorithm guarantees that the encrypted output of a value is always unique. This prevents queries for a specific field value from returning meaningful results while supporting the highest possible protection of the field contents. The randomized encryption algorithm therefore meets both the encryption and queryability requirements of the data.
The following schema specifies automatic encryption rules which meet the
encryption requirements for the MedCo.patients collection:
The above automatic encryption rules mark the passportId,
bloodType, insurance.policyNumber, insurance.provider,
and medicalRecords fields for encryption.
- The
passportId,bloodType,insurance.policyNumber, andproviderfields inherit their encryption settings from the parentencryptMetadatafield. Specifically, these fields inherit thealgorithmandkeyIdvalues specifying deterministic encryption with the specified . - The
medicalRecordsfield requires randomized encryption using the specified key. Theencryptoptions override those specified in the parentencryptMetadatafield.
While does not support encrypting
individual array elements, randomized encryption supports encrypting the
entire array field rather than individual elements in the field. The
example automatic encryption rules specify randomized encryption for the
medicalRecords field to encrypt the entire array. If the automatic
encryption rules specified encrypt or
encryptMetadata within medicalRecords.items or
medicalRecords.additionalItems, automatic field level encryption
fails and returns an errors.
The official MongoDB 4.2+ compatible drivers, mongosh,
and the 4.2 or later legacy mongo shell require
specifying the automatic encryption rules as part of creating the
database connection object:
- For
mongosh, use theMongo()constructor to create a database connection. Specify the automatic encryption rules to theschemaMapkey of the <> parameter. See Connect to a MongoDB Cluster with Automatic Client-Side Encryption Enabled for a complete example. - For the official MongoDB 4.2+ compatible drivers, use the
driver-specific database connection constructor (
MongoClient) to create the database connection with the automatic encryption rules included as part of the configuration object. Defer to the driver API reference for more complete documentation and tutorials.
For all clients, the keyVault and kmsProviders specified
to the parameter must grant
access to both the s specified in the automatic
encryption rules and the used to encrypt the
s.
To learn more about your CMK and , see the key vaults page.
To learn more about encryption algorithms, see the Encryption algorithms page.
To learn more about -specific MongoClient options,
see the mongo client page.
Encryption Schema - Encrypt with Pattern Properties¶
You can use the patternProperties keyword in your encryption schema to
define encryption rules for all fields with names that match a regular expression.
Consider a collection MedCo.patients where each document has
the following structure:
The fields that contain private data are identified by a “_PII<type>” tag appended the end of the field name.
passportId_PIIStringbloodType_PIIStringmedicalRecords_PIIArrayinsurance.policyNumber_PIINumberinsurance.provider_PIIString
You can use the patternProperties keyword to configure these fields for
encryption, without identifying each field individually, and without using the
full field name. Do this by using regular expressions that match all fields that
end with the “_PII<type>” tag.
The following JSON schema uses patternProperties and regular expressions to
specify which fields to encrypt.
The above automatic encryption rules mark the passportId_PIIString,
bloodType_PIIString, medicalRecords_PIIArray, insurance.policyNumber_PIINumber,
insurance.provider_PIIString fields for encryption.
To Learn more about the patternProperties keyword, see
patternProperties Keyword.