Client-Side Field Level Encryption

    With field level encryption, applications can encrypt fields in documents prior to transmitting data over the wire to the server. Client-side field level encryption supports workloads where applications must guarantee that unauthorized parties, including server administrators, cannot read the encrypted data.

    See also

    The MongoDB documentation on

    To get started using client-side field level encryption in your project, you will need to install the pymongocrypt library as well as the driver itself. Install both the driver and a compatible version of pymongocrypt like this:

    Note that installing on Linux requires pip 19 or later for manylinux2010 wheel support. For more information about installing pymongocrypt see .

    mongocryptd

    The binary is required for automatic client-side encryption and is included as a component in the MongoDB Enterprise Server package. For detailed installation instructions see .

    • Parses the automatic encryption rules specified to the database connection. If the JSON schema contains invalid automatic encryption syntax or any document validation syntax, mongocryptd returns an error.
    • Uses the specified automatic encryption rules to mark fields in read and write operations for encryption.
    • Rejects read/write operations that may return unexpected or incorrect results when applied to an encrypted field. For supported and unsupported operations, see Read/Write Support with Automatic Field Level Encryption.

    A MongoClient configured with auto encryption will automatically spawn the mongocryptd process from the application’s PATH. Applications can control the spawning behavior as part of the automatic encryption options. For example to set the path to the mongocryptd process:

    1. auto_encryption_opts = AutoEncryptionOpts(
    2. ...,
    3. mongocryptd_spawn_path='/path/to/mongocryptd')

    To control the logging output of mongocryptd pass options using mongocryptd_spawn_args:

    If your application wishes to manage the mongocryptd process manually, it is possible to disable spawning mongocryptd:

    1. auto_encryption_opts = AutoEncryptionOpts(
    2. ...,
    3. mongocryptd_bypass_spawn=True,
    4. # URI of the local ``mongocryptd`` process.
    5. mongocryptd_uri='mongodb://localhost:27020')

    mongocryptd is only responsible for supporting automatic client-side field level encryption and does not itself perform any encryption or decryption.

    Automatic client-side field level encryption is enabled by creating a with the auto_encryption_opts option set to an instance of AutoEncryptionOpts. The following examples show how to setup automatic client-side field level encryption using to create a new encryption data key.

    Note

    Automatic client-side field level encryption requires MongoDB 4.2 enterprise or a MongoDB 4.2 Atlas cluster. The community version of the server supports automatic decryption as well as Explicit Encryption.

    Providing Local Automatic Encryption Rules

    Supplying a 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.

    JSON Schemas supplied in the schema_map only apply to configuring automatic client-side field level encryption. Other validation rules in the JSON schema will not be enforced by the driver and will result in an error.:

    Server-Side Field Level Encryption Enforcement

    The MongoDB 4.2 server supports using schema validation to enforce encryption of specific fields in a collection. This schema validation will prevent an application from inserting unencrypted values for any fields marked with the "encrypt" JSON schema keyword.

    The following example shows how to setup automatic client-side field level encryption using to create a new encryption data key and create a collection with the Automatic Encryption JSON Schema Syntax:

    1. import os
    2. from bson.codec_options import CodecOptions
    3. from bson.binary import STANDARD
    4. from pymongo import MongoClient
    5. from pymongo.encryption import (Algorithm,
    6. ClientEncryption)
    7. from pymongo.encryption_options import AutoEncryptionOpts
    8. from pymongo.errors import OperationFailure
    9. from pymongo.write_concern import WriteConcern
    10. def main():
    11. # The MongoDB namespace (db.collection) used to store the
    12. # encrypted documents in this example.
    13. encrypted_namespace = "test.coll"
    14. # This must be the same master key that was used to create
    15. # the encryption key.
    16. local_master_key = os.urandom(96)
    17. kms_providers = {"local": {"key": local_master_key}}
    18. # The MongoDB namespace (db.collection) used to store
    19. # the encryption data keys.
    20. key_vault_namespace = "encryption.__pymongoTestKeyVault"
    21. key_vault_db_name, key_vault_coll_name = key_vault_namespace.split(".", 1)
    22. # The MongoClient used to access the key vault (key_vault_namespace).
    23. key_vault_client = MongoClient()
    24. key_vault = key_vault_client[key_vault_db_name][key_vault_coll_name]
    25. # Ensure that two data keys cannot share the same keyAltName.
    26. key_vault.drop()
    27. key_vault.create_index(
    28. "keyAltNames",
    29. unique=True,
    30. partialFilterExpression={"keyAltNames": {"$exists": True}})
    31. client_encryption = ClientEncryption(
    32. key_vault_namespace,
    33. key_vault_client,
    34. # The CodecOptions class used for encrypting and decrypting.
    35. # This should be the same CodecOptions instance you have configured
    36. # encrypt() or decrypt() in this example so we can use any
    37. # CodecOptions.
    38. CodecOptions())
    39. # Create a new data key and json schema for the encryptedField.
    40. data_key_id = client_encryption.create_data_key(
    41. 'local', key_alt_names=['pymongo_encryption_example_2'])
    42. json_schema = {
    43. "properties": {
    44. "encryptedField": {
    45. "encrypt": {
    46. "keyId": [data_key_id],
    47. "bsonType": "string",
    48. "algorithm":
    49. Algorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic
    50. }
    51. }
    52. },
    53. "bsonType": "object"
    54. }
    55. auto_encryption_opts = AutoEncryptionOpts(
    56. kms_providers, key_vault_namespace)
    57. client = MongoClient(auto_encryption_opts=auto_encryption_opts)
    58. db_name, coll_name = encrypted_namespace.split(".", 1)
    59. db = client[db_name]
    60. # Clear old data
    61. db.drop_collection(coll_name)
    62. # Create the collection with the encryption JSON Schema.
    63. db.create_collection(
    64. coll_name,
    65. # uuid_representation=STANDARD is required to ensure that any
    66. # UUIDs in the $jsonSchema document are encoded to BSON Binary
    67. # with the standard UUID subtype 4. This is only needed when
    68. # running the "create" collection command with an encryption
    69. # JSON Schema.
    70. codec_options=CodecOptions(uuid_representation=STANDARD),
    71. write_concern=WriteConcern(w="majority"),
    72. validator={"$jsonSchema": json_schema})
    73. coll = client[db_name][coll_name]
    74. coll.insert_one({"encryptedField": "123456789"})
    75. print('Decrypted document: %s' % (coll.find_one(),))
    76. unencrypted_coll = MongoClient()[db_name][coll_name]
    77. print('Encrypted document: %s' % (unencrypted_coll.find_one(),))
    78. try:
    79. unencrypted_coll.insert_one({"encryptedField": "123456789"})
    80. except OperationFailure as exc:
    81. print('Unencrypted insert failed: %s' % (exc.details,))
    82. if __name__ == "__main__":
    83. main()

    Explicit Encryption

    Explicit encryption is a MongoDB community feature and does not use the mongocryptd process. Explicit encryption is provided by the class, for example:

    Although automatic encryption requires MongoDB 4.2 enterprise or a MongoDB 4.2 Atlas cluster, automatic decryption is supported for all users. To configure automatic decryption without automatic encryption set bypass_auto_encryption=True in AutoEncryptionOpts:

    1. import os
    2. from pymongo import MongoClient
    3. from pymongo.encryption import (Algorithm,
    4. from pymongo.encryption_options import AutoEncryptionOpts
    5. def main():
    6. # This must be the same master key that was used to create
    7. # the encryption key.
    8. local_master_key = os.urandom(96)
    9. kms_providers = {"local": {"key": local_master_key}}
    10. # the encryption data keys.
    11. key_vault_namespace = "encryption.__pymongoTestKeyVault"
    12. key_vault_db_name, key_vault_coll_name = key_vault_namespace.split(".", 1)
    13. # bypass_auto_encryption=True disable automatic encryption but keeps
    14. # the automatic _decryption_ behavior. bypass_auto_encryption will
    15. # also disable spawning mongocryptd.
    16. auto_encryption_opts = AutoEncryptionOpts(
    17. kms_providers, key_vault_namespace, bypass_auto_encryption=True)
    18. client = MongoClient(auto_encryption_opts=auto_encryption_opts)
    19. coll = client.test.coll
    20. # Clear old data
    21. coll.drop()
    22. # Set up the key vault (key_vault_namespace) for this example.
    23. key_vault = client[key_vault_db_name][key_vault_coll_name]
    24. # Ensure that two data keys cannot share the same keyAltName.
    25. key_vault.drop()
    26. key_vault.create_index(
    27. "keyAltNames",
    28. unique=True,
    29. partialFilterExpression={"keyAltNames": {"$exists": True}})
    30. client_encryption = ClientEncryption(
    31. kms_providers,
    32. key_vault_namespace,
    33. # The MongoClient to use for reading/writing to the key vault.
    34. # This can be the same MongoClient used by the main application.
    35. client,
    36. # The CodecOptions class used for encrypting and decrypting.
    37. # This should be the same CodecOptions instance you have configured
    38. # on MongoClient, Database, or Collection.
    39. coll.codec_options)
    40. # Create a new data key for the encryptedField.
    41. data_key_id = client_encryption.create_data_key(
    42. 'local', key_alt_names=['pymongo_encryption_example_4'])
    43. # Explicitly encrypt a field:
    44. encrypted_field = client_encryption.encrypt(
    45. "123456789",
    46. Algorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic,
    47. key_alt_name='pymongo_encryption_example_4')
    48. coll.insert_one({"encryptedField": encrypted_field})
    49. # Automatically decrypts any encrypted fields.
    50. doc = coll.find_one()
    51. print('Decrypted document: %s' % (doc,))
    52. unencrypted_coll = MongoClient().test.coll
    53. print('Encrypted document: %s' % (unencrypted_coll.find_one(),))
    54. # Cleanup resources.
    55. client_encryption.close()
    56. client.close()
    57. main()

    Previous topic

    TLS/SSL and PyMongo

    Next topic

    Quick search