Manual Setup for MongoDB Client-Side Field Level Encryption (CSFLE) with KMIP
This guide walks through the manual setup process for enabling Client-Side Field Level Encryption in MongoDB using a KMIP-compatible Key Management System (KMS).
Overview
| Item | Details |
|---|---|
| Protocol | KMIP 1.0 over TCP/TLS with mutual certificate authentication |
| Port | 5696 (IANA-registered KMIP port) |
| Key type | AES-256 symmetric key |
| MongoDB version | Explicit encryption: MongoDB Community or Enterprise 6.0 and above. Automatic encryption: MongoDB Enterprise 6.0 and above, or Atlas |
| Eviden KMS feature | Requires non-FIPS build |
Explicit encryption on MongoDB Community
Explicit encryption (ClientEncryption) needs neither the crypt shared library nor MongoDB Enterprise: the application encrypts and decrypts each field itself, and the key vault is an ordinary collection. This path is tested against a MongoDB Community container and the KMS.
How the key is held
create_data_key("kmip") makes libmongocrypt register the key material as a KMIP Secret Data object on the KMS (KMIP Register, then Activate) and stores a data encryption key (DEK) document in encryption.__keyVault whose masterKey is {provider: "kmip", keyId: "<KMS unique identifier>"}. Every encrypt or decrypt with a new ClientEncryption sends a KMIP Get for that identifier over mutual TLS. Destroy the Secret Data object on the KMS and the encrypted fields can no longer be decrypted.
Requirements
- The KMS socket server is enabled (
--socket-server-start, KMIP port5696by default) with--clients-ca-cert-file, so that every KMIP connection presents a client certificate. - The KMS server certificate carries a subject alternative name equal to the host in the
endpoint. PyMongo verifies the host name with Python'ssslmodule, which ignores the certificate common name, and refusestlsAllowInvalidHostnamesfor KMS connections. pymongo[encryption](it installspymongocryptand the libmongocrypt library).
Create a key and encrypt a field
from bson.codec_options import CodecOptions
from pymongo import MongoClient
from pymongo.encryption import Algorithm, ClientEncryption
kms_providers = {"kmip": {"endpoint": "your-cosmian_kms-server:5696"}}
tls_options = {"kmip": {
"tlsCAFile": "/opt/kms-certs/ca.pem",
"tlsCertificateKeyFile": "/opt/kms-certs/client.pem", # client certificate followed by its private key
}}
client = MongoClient("mongodb://localhost:27017")
client_encryption = ClientEncryption(
kms_providers, "encryption.__keyVault", client, CodecOptions(), kms_tls_options=tls_options
)
dek_id = client_encryption.create_data_key("kmip", key_alt_names=["yourSecretKeyAlias"])
ciphertext = client_encryption.encrypt(
"123-45-6789",
Algorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic,
key_alt_name="yourSecretKeyAlias",
)
client.medical.patients.insert_one({"name": "Doe", "ssn": ciphertext})
stored = client.medical.patients.find_one({"name": "Doe"})
print(client_encryption.decrypt(stored["ssn"])) # 123-45-6789
The deterministic algorithm makes find_one({"ssn": ciphertext}) work by equality; use AEAD_AES_256_CBC_HMAC_SHA_512_Random when equality queries are not needed.
Verify that the key is on the KMS
The keyId of the DEK's masterKey is the unique identifier of an object of the KMS:
ckms attributes get -i <keyId> # "ObjectType": "SecretData"
Automated test
mise run test:interop:mongodb (non-FIPS only, needs Docker and Python 3.12) builds the KMS, starts it with the complete test_data/configs/server/auth/cert.toml fixture, starts mongo:7.0 and runs .mise/scripts/test/interop/mongodb_csfle_helper.py with the hash-pinned requirements-mongodb-csfle.txt. It checks that:
- the DEK document references a KMIP key, and that key exists on the KMS as Secret Data;
- a field is encrypted, is opaque in MongoDB, matches by equality when deterministic, and decrypts again;
- a KMIP client without a TLS client certificate is refused by the KMS;
- a new client decrypts through the KMS, and cannot once the Secret Data is revoked and destroyed on the KMS.
Prerequisites
Packages
- MongoDB Enterprise ≥
6.0 - Python ≥
3.8 - Python packages:
pymongodnspython
- MongoDB Crypt Shared Library:
- Download from the MongoDB Enterprise downloads section (e.g.,
mongo_crypt_v1.so) - Install to:
/opt/mongo_crypt_shared/mongo_crypt_v1.so
- Download from the MongoDB Enterprise downloads section (e.g.,
- Eviden KMS:
- Eviden KMS endpoint:
your-cosmian_kms-server:port - TLS certs:
- Client cert:
/opt/kms-certs/client.pem - CA cert:
/opt/kms-certs/ca.pem
- Client cert:
- Eviden KMS endpoint:
- Key Vault Namespace:
- Database:
encryption - Collection:
__keyVault
- Database:
- Schema Target Collection (example):
- Database:
medical - Collection:
patients
- Database:
- Alternative Key Name (for the DEK):
yourSecretKeyAlias
Step-by-Step Guide
⚠️ This tutorial was done in a DB test environment with an external Eviden KMS.
1. Define environment variable
Ensure that the path to the crypt shared library is defined:
export CRYPT_SHARED_LIB_PATH=/opt/mongo_crypt_shared/mongo_crypt_v1.so
2. Clean up existing DEKs (optional)
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017")
client["encryption"]["__keyVault"].delete_many({"keyAltNames": ["yourSecretKeyAlias"]})
3. Generate a new DEK manually
from pymongo.encryption import ClientEncryption
from pymongo import MongoClient
from bson.codec_options import CodecOptions
kms_providers = {
"kmip": {
"endpoint": "your-cosmian_kms-server:port"
}
}
tls_options = {
"kmip": {
"tlsCertificateKeyFile": "/opt/kms-certs/client.pem",
"tlsCAFile": "/opt/kms-certs/ca.pem"
}
}
client = MongoClient("mongodb://localhost:27017")
client_encryption = ClientEncryption(
kms_providers=kms_providers,
key_vault_namespace="encryption.__keyVault",
key_vault_client=client,
codec_options=CodecOptions(),
kms_tls_options=tls_options
)
data_key_id = client_encryption.create_data_key("kmip", key_alt_names=["yourSecretKeyAlias"])
print(f"Created DEK with ID: {data_key_id}")
4. Define your encryption schema
schema_map = {
"medical.patients": {
"bsonType": "object",
"properties": {
"ssn": {
"encrypt": {
"keyId": [data_key_id],
"bsonType": "string",
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
}
}
}
}
}
5. Create the encrypted MongoClient
from pymongo.encryption_options import AutoEncryptionOpts
auto_encryption_opts = AutoEncryptionOpts(
kms_providers=kms_providers,
key_vault_namespace="encryption.__keyVault",
schema_map=schema_map,
kms_tls_options=tls_options,
crypt_shared_lib_path=CRYPT_SHARED_LIB_PATH
)
secure_client = MongoClient("mongodb://localhost:27017", auto_encryption_opts=auto_encryption_opts)
6. Insert encrypted data
db = secure_client["medical"]
collection = db["patients"]
collection.drop()
collection.insert_one({
"nom": "Doe",
"ssn": "123-45-6789"
})
print("Encrypted document inserted.")
Notes
- Replace
your-cosmian_kms-server:portwith your KMIP endpoint (e.g.cosmian-kms.example.com:5696). - Replace certificate paths if different.
- The
ssnfield will be transparently encrypted on insert, and decrypted on read. - Automatic encryption (this guide's
AutoEncryptionOptssteps) requires MongoDB Enterprise or Atlas. On MongoDB Community, use explicit encryption.
Reading Encrypted Data with MongoDB CSFLE (Client-Side Field Level Encryption)
Step-by-Step Guide
This guide walks you through reading encrypted documents stored in MongoDB using automatic decryption with the CSFLE feature.
1. Set the Shared Crypt Library Path
Make sure the environment variable is set to the location of your shared crypt library:
export CRYPT_SHARED_LIB_PATH=/opt/mongo_crypt_shared/mongo_crypt_v1.so
2. (Optional) Read Raw Encrypted Documents
You can view the encrypted form of the documents using a regular (non-CSFLE-enabled) MongoDB client:
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017")
print("🔒 Raw encrypted documents:")
for doc in client.medical.patients.find():
print(doc)
3. Define KMS Provider Configuration
kms_providers = {
"kmip": {
"endpoint": "your-cosmian_kms-server:port"
}
}
tls_options = {
"kmip": {
"tlsCertificateKeyFile": "/opt/kms-certs/client.pem",
"tlsCAFile": "/opt/kms-certs/ca.pem"
}
}
4. Configure AutoEncryptionOpts
This will enable automatic decryption using the shared library:
import os
from pymongo.encryption_options import AutoEncryptionOpts
CRYPT_SHARED_LIB_PATH = os.getenv("CRYPT_SHARED_LIB_PATH", "/opt/mongo_crypt_shared/mongo_crypt_v1.so")
auto_encryption_opts = AutoEncryptionOpts(
kms_providers=kms_providers,
key_vault_namespace="encryption.__keyVault",
kms_tls_options=tls_options,
crypt_shared_lib_path=CRYPT_SHARED_LIB_PATH
)
5. Create a Secure Client with CSFLE Enabled
from pymongo import MongoClient
secure_client = MongoClient(
"mongodb://localhost:27017",
auto_encryption_opts=auto_encryption_opts
)
6. Read Decrypted Documents
secure_db = secure_client["medical"]
secure_coll = secure_db["patients"]
print("🔓 Automatically decrypted documents:")
for doc in secure_coll.find():
print(doc)
Result
Encrypted fields will be automatically decrypted in the returned documents, provided the proper key is stored in your key vault and accessible via the configured Eviden KMS.
Additional Notes
- Make sure the key vault namespace (
encryption.__keyVault) matches the namespace used during encryption. - Automatic encryption works only with MongoDB Enterprise or MongoDB Atlas. Explicit encryption works on every edition.