encryption – Client-Side Field Level Encryption (original) (raw)

View this page

Toggle table of contents sidebar

Support for explicit client-side field level encryption.

class pymongo.encryption.Algorithm(value)

An enum that defines the supported encryption algorithms.

AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic = 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'

AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic.

AEAD_AES_256_CBC_HMAC_SHA_512_Random = 'AEAD_AES_256_CBC_HMAC_SHA_512-Random'

AEAD_AES_256_CBC_HMAC_SHA_512_Random.

INDEXED = 'Indexed'

Indexed.

Added in version 4.2.

RANGE = 'Range'

Range.

Added in version 4.9.

RANGEPREVIEW = 'RangePreview'

DEPRECATED - RangePreview.

Note

Support for RangePreview is deprecated. Use Algorithm.RANGE instead.

Added in version 4.4.

UNINDEXED = 'Unindexed'

Unindexed.

Added in version 4.2.

class pymongo.encryption.ClientEncryption(kms_providers, key_vault_namespace, key_vault_client, codec_options, kms_tls_options=None, key_expiration_ms=None)

Explicit client-side field level encryption.

The ClientEncryption class encapsulates explicit operations on a key vault collection that cannot be done directly on a MongoClient. Similar to configuring auto encryption on a MongoClient, it is constructed with a MongoClient (to a MongoDB cluster containing the key vault collection), KMS provider configuration, and keyVaultNamespace. It provides an API for explicitly encrypting and decrypting values, and creating data keys. It does not provide an API to query keys from the key vault collection, as this can be done directly on the MongoClient.

See Explicit Encryption for an example.

Parameters:

Changed in version 4.12: Added the key_expiration_ms parameter.

Changed in version 4.0: Added the kms_tls_options parameter and the “kmip” KMS provider.

Added in version 3.9.

add_key_alt_name(id, key_alt_name)

Add key_alt_name to the set of alternate names in the key document with UUID key_id.

Parameters:

Returns:

The previous version of the key document.

Return type:

Any

Added in version 4.2.

close()

Release resources.

Note that using this class in a with-statement will automatically callclose():

with ClientEncryption(...) as client_encryption: encrypted = client_encryption.encrypt(value, ...) decrypted = client_encryption.decrypt(encrypted)

Return type:

None

create_data_key(kms_provider, master_key=None, key_alt_names=None, key_material=None)

Create and insert a new data key into the key vault collection.

Parameters:

reference the key with the alternate name

client_encryption.encrypt("457-55-5462", key_alt_name="name1",
algorithm=Algorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)

Returns:

The _id of the created data key document as aBinary with subtypeUUID_SUBTYPE.

Return type:

Binary

Changed in version 4.2: Added the key_material parameter.

create_encrypted_collection(database, name, encrypted_fields, kms_provider=None, master_key=None, **kwargs)

Create a collection with encryptedFields.

Warning

This function does not update the encryptedFieldsMap in the client’s AutoEncryptionOpts, thus the user must create a new client after calling this function with the encryptedFields returned.

Normally collection creation is automatic. This method should only be used to specify options on creation. EncryptionError will be raised if the collection already exists.

Parameters:

Return type:

tuple[Collection[__DocumentTypeArg_], Mapping[str, Any]]

All optional create collection command parameters should be passed as keyword arguments to this method. See the documentation for create_collection() for all valid options.

Raises:

Parameters:

Return type:

tuple[Collection[__DocumentTypeArg_], Mapping[str, Any]]

Added in version 4.4.

decrypt(value)

Decrypt an encrypted value.

Parameters:

Returns:

The decrypted BSON value.

Return type:

Any

delete_key(id)

Delete a key document in the key vault collection that has the given key_id.

Parameters:

Returns:

The delete result.

Return type:

DeleteResult

Added in version 4.2.

encrypt(value, algorithm, key_id=None, key_alt_name=None, query_type=None, contention_factor=None, range_opts=None)

Encrypt a BSON value with a given key and algorithm.

Note that exactly one of key_id or key_alt_name must be provided.

Parameters:

Returns:

The encrypted value, a Binary with subtype 6.

Return type:

Binary

Changed in version 4.9: Added the range_opts parameter.

Changed in version 4.7: key_id can now be passed in as a uuid.UUID.

Changed in version 4.2: Added the query_type and contention_factor parameters.

encrypt_expression(expression, algorithm, key_id=None, key_alt_name=None, query_type=None, contention_factor=None, range_opts=None)

Encrypt a BSON expression with a given key and algorithm.

Note that exactly one of key_id or key_alt_name must be provided.

Parameters:

Returns:

The encrypted expression, a RawBSONDocument.

Return type:

RawBSONDocument

Changed in version 4.9: Added the range_opts parameter.

Changed in version 4.7: key_id can now be passed in as a uuid.UUID.

Added in version 4.4.

get_key(id)

Get a data key by id.

Parameters:

Returns:

The key document.

Return type:

RawBSONDocument | None

Added in version 4.2.

get_key_by_alt_name(key_alt_name)

Get a key document in the key vault collection that has the given key_alt_name.

Parameters:

key_alt_name (str) – (str): The key alternate name of the key to get.

Returns:

The key document.

Return type:

RawBSONDocument | None

Added in version 4.2.

get_keys()

Get all of the data keys.

Returns:

An instance of Cursor over the data key documents.

Return type:

Cursor[RawBSONDocument]

Added in version 4.2.

remove_key_alt_name(id, key_alt_name)

Remove key_alt_name from the set of keyAltNames in the key document with UUID id.

Also removes the keyAltNames field from the key document if it would otherwise be empty.

Parameters:

Returns:

Returns the previous version of the key document.

Return type:

RawBSONDocument | None

Added in version 4.2.

rewrap_many_data_key(filter, provider=None, master_key=None)

Decrypts and encrypts all matching data keys in the key vault with a possibly new master_key value.

Parameters:

Returns:

A RewrapManyDataKeyResult.

Return type:

RewrapManyDataKeyResult

This method allows you to re-encrypt all of your data-keys with a new CMK, or master key. Note that this does not require re-encrypting any of the data in your encrypted collections, but rather refreshes the key that protects the keys that encrypt the data:

client_encryption.rewrap_many_data_key( filter={"keyAltNames": "optional filter for which keys you want to update"}, master_key={ "provider": "azure", # replace with your cloud provider "master_key": { # put the rest of your master_key options here "key": "" }, }, )

Added in version 4.2.

class pymongo.encryption.QueryType(value)

An enum that defines the supported values for explicit encryption query_type.

Added in version 4.2.

EQUALITY = 'equality'

Used to encrypt a value for an equality query.

RANGE = 'range'

Used to encrypt a value for a range query.

Added in version 4.9.

RANGEPREVIEW = 'RangePreview'

DEPRECATED - Used to encrypt a value for a rangePreview query.

Note

Support for RangePreview is deprecated. Use QueryType.RANGE instead.

Added in version 4.4.

class pymongo.encryption.RewrapManyDataKeyResult(bulk_write_result=None)

Result object returned by a rewrap_many_data_key() operation.

Added in version 4.2.

Parameters:

bulk_write_result (Optional _[_BulkWriteResult])

property bulk_write_result_: BulkWriteResult | None_

The result of the bulk write operation used to update the key vault collection with one or more rewrapped data keys. Ifrewrap_many_data_key() does not find any matching keys to rewrap, no bulk write operation will be executed and this field will beNone.