On this page本页内容
New in version 4.2.在版本4.2中新增。
The official MongoDB 4.2+ compatible drivers provide a client-side field level encryption framework. 官方的MongoDB 4.2+兼容驱动程序提供了客户端字段级加密框架。Applications can encrypt fields in documents prior to transmitting data over the wire to the server. 应用程序可以在通过网络将数据传输到服务器之前对文档中的字段进行加密。Only applications with access to the correct encryption keys can decrypt and read the protected data. 只有能够访问正确加密密钥的应用程序才能解密和读取受保护的数据。Deleting an encryption key renders all data encrypted using that key as permanently unreadable.删除加密密钥会使使用该密钥加密的所有数据永久不可读。
For example, a MongoDB cluster enforcing authentication uses TLS encryption to protect data in transit. 例如,实施身份验证的MongoDB集群使用TLS加密来保护传输中的数据。The cluster also uses the MongoDB encrypted storage engine to secure data on disk. 集群还使用MongoDB加密存储引擎来保护磁盘上的数据。Consider the following scenarios:考虑以下场景:
With each scenario, a user with privileged access to either the MongoDB cluster or a host machine can bypass encryption and read data that is private, privileged, or confidential. 在每个场景中,对MongoDB集群或主机具有权限访问权限的用户都可以绕过加密,读取私有、权限或机密数据。Using client-side field level encryption to protect data prior to being written to the server mitigates the risk of exposing that data in the event network or disk encryption is bypassed.在将数据写入服务器之前,使用客户端字段级加密来保护数据,可以降低数据在绕过网络或磁盘加密的事件中暴露的风险。
Consider the following document:考虑以下文件:
{ "name" : "John Doe", "address" : { "street" : "1234 Main Street", "city" : "MongoDBVille", "zip" : 99999 }, "phone" : "949-555-1212", "ssn" : "123-45-6789" }
With client-side field level encryption, the application can specifically encrypt sensitive information like the 通过客户端字段级加密,应用程序可以专门加密ssn
and phone
. ssn
和phone
等敏感信息。Encrypted fields are stored as 加密字段存储为子类型6的二进制数据:binary data
with subtype 6:
{ "name" : "John Doe", "address" : { "street" : "1234 Main Street", "city" : "MongoDBVille", "zip" : 99999 }, "phone" : BinData(6,"U2FsdGVkX1+CGIDGUnGgtS46+c7R5u17SwPDEmzyCbA="), "ssn" : BinData(6,"AaloEw285E3AnfjP+r8ph2YCvMI1+rWzpZK97tV6iz0jx") }
For a complete list of official 4.2+ compatible drivers with support for client-side field level encryption, see Driver Compatibility Table.有关支持客户端字段级加密的官方4.2+兼容驱动程序的完整列表,请参阅驱动程序兼容性表。
For an end-to-end procedure for configuring field level encryption using select MongoDB 4.2+ compatible drivers, see the Client Side Field Level Encryption Guide.有关使用select MongoDB 4.2+兼容驱动程序配置字段级加密的端到端过程,请参阅客户端字段级加密指南。
MongoDB supports two methods of client-side field level encryption using the official MongoDB 4.2+ compatible drivers:MongoDB支持两种使用官方MongoDB 4.2+兼容驱动程序的客户端字段级加密方法:
Official MongoDB 4.2+ compatible drivers, 官方MongoDB 4.2+兼容驱动程序、mongosh
, and the MongoDB 4.2 or later legacy mongo
shell support explicitly encrypting or decrypting fields with a specific data encryption key and encryption algorithm.mongosh
和MongoDB 4.2或更高版本的旧mongo
shell支持使用特定数据加密密钥和加密算法显式加密或解密字段。
Applications must modify any code associated with constructing read and write operations to include encryption/decryption logic via the driver encryption library. 应用程序必须修改与构建读写操作相关的任何代码,以通过驱动程序加密库包含加密/解密逻辑。Applications are responsible for selecting the appropriate data encryption key for encryption/decryption on a per-operation basis.应用程序负责在每次操作的基础上选择适当的数据加密密钥进行加密/解密。
For more information, see Explicit (Manual) Client-Side Field Level Encryption.有关更多信息,请参阅显式(手动)客户端字段级加密。
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.现场级加密的自动功能仅在MongoDB Enterprise 4.2或更高版本以及MongoDB Atlas 4.2或更晚版本集群中可用。
Official MongoDB 4.2+ compatible drivers, 官方MongoDB 4.2+兼容驱动程序、mongosh
, and the MongoDB 4.2 or later legacy mongo
shell support automatically encrypting fields in read and write operations.mongosh
和MongoDB 4.2或更高版本的旧mongo
shell支持在读写操作中自动加密字段。
Applications must create a database connection object (e.g. 应用程序必须使用自动加密配置设置创建数据库连接对象(例如MongoClient
) with the automatic encryption configuration settings. MongoClient
)。The configuration settings must include automatic encryption rules using a strict subset of the JSON Schema Draft 4 standard syntax and encryption-specific schema keywords. 配置设置必须包括使用JSON模式草稿4标准语法的严格子集和加密特定模式关键字的自动加密规则。Applications do not have to modify code associated with the read/write operation. 应用程序不必修改与读/写操作相关的代码。See Automatic Encryption Rules for complete documentation on automatic encryption rules.有关自动加密规则的完整文档,请参阅自动加密规则。
For more information, see Automatic Client-Side Field Level Encryption.有关更多信息,请参阅自动客户端字段级加密。
MongoDB 4.2+ compatible drivers, MongoDB 4.2+兼容的驱动程序、mongosh
, and the MongoDB 4.2 or later legacy mongo
shell automatically decrypt Binary
subtype 6 objects created using client-side field level encryption. mongosh
和MongoDB 4.2或更高版本的旧mongo
shell会自动解密使用客户端字段级加密创建的Binary
子类型6对象。For more information on automatic decryption, see Automatic Field Decryption.有关自动解密的详细信息,请参阅自动字段解密。
MongoDB client-side field level encryption only supports encrypting single fields in a document. MongoDB客户端字段级加密仅支持对文档中的单个字段进行加密。To encrypt an entire document, you must encrypt each individual field in the document.要加密整个文档,必须加密文档中的每个字段。
The following diagram illustrates the relationships between the driver and each encryption component:下图说明了驱动程序和每个加密组件之间的关系:
libmongocrypt
is the Apache-licensed open-source core cryptography library used by the official MongoDB 4.2+ compatible drivers, mongosh
, and the MongoDB 4.2 or later legacy mongo
shell for powering client-side field level encryption. libmongocrypt
是Apache授权的开源核心加密库,由官方MongoDB 4.2+兼容驱动程序、mongosh
和MongoDB 4.2或更高版本的遗留mongo
shell使用,用于支持客户端字段级加密。mongocryptd
supports Automatic Client-Side Field Level Encryption and is only available with MongoDB Enterprise. mongocryptd
支持自动客户端字段级加密,仅适用于MongoDB Enterprise。mongocryptd
does not perform cryptographic functions.mongocrypted
不执行加密功能。The Key Management Service (KMS) stores the Customer Master Key (CMK) used to encrypt data encryption keys. MongoDB supports the following KMS providers:密钥管理服务(KMS)存储用于加密数据加密密钥的客户主密钥(CMK)。MongoDB支持以下KMS提供商:
MongoDB client-side field level encryption uses the encrypt-then-MAC approach combined with either a deterministic or random initialization vector to encrypt field values. MongoDB客户端字段级加密使用encrypt-then-MAC方法结合确定性或随机初始化向量来加密字段值。MongoDB only supports the AEAD AES-256-CBC encryption algorithm with HMAC-SHA-512 MAC.MongoDB仅支持带有HMAC-SHA-512 MAC的AEAD AES-256-CBC加密算法。
The deterministic encryption algorithm ensures a given input value always encrypts to the same output value each time the algorithm is executed. 确定性加密算法确保每次执行算法时,给定的输入值总是加密到相同的输出值。While deterministic encryption provides greater support for read operations, encrypted data with low cardinality is susceptible to frequency analysis recovery.虽然确定性加密为读取操作提供了更大的支持,但基数较低的加密数据易于进行频率分析恢复。
For sensitive fields that are not used in read operations, applications may use Randomized Encryption for improved protection from frequency analysis recovery.对于在读取操作中未使用的敏感字段,应用程序可以使用随机化加密来提高对频率分析恢复的保护。
The randomized encryption algorithm ensures that a given input value always encrypts to a different output value each time the algorithm is executed. 随机加密算法确保每次执行算法时,给定的输入值总是加密到不同的输出值。While randomized encryption provides the strongest guarantees of data confidentiality, it also prevents support for any read operations which must operate on the encrypted field to evaluate the query.尽管随机化加密提供了数据机密性的最强保证,但它也阻止了对必须对加密字段进行操作才能评估查询的任何读取操作的支持。
Randomized encryption also supports encrypting entire objects or arrays. 随机加密还支持加密整个对象或数组。For example, consider the following document:例如,考虑以下文档:
{ "personal_information" : { "ssn" : "123-45-6789", "credit_score" : 750, "credit_cards" : [ "1234-5678-9012-3456", "9876-5432-1098-7654"] }, "phone_numbers" : [ "(212) 555-0153" ] }
Encrypting the 使用随机化加密算法对personal_information
and phone_numbers
fields using the randomized encryption algorithm encrypts the entire object. personal_information
和phone_number
字段进行加密将加密整个对象。While this protects all fields nested under those fields, it also prevents querying against those nested fields.虽然这可以保护嵌套在这些字段下的所有字段,但也可以防止对这些嵌套字段进行查询。
For sensitive fields that are used in read operations, applications must use Deterministic Encryption for improved read support on encrypted fields.对于在读取操作中使用的敏感字段,应用程序必须使用确定性加密来改进对加密字段的读取支持。
The BinData
blob metadata includes the data encryption key _id
and encryption algorithm used to encrypt the binary data. BinData
blob元数据包括用于加密二进制数据的数据加密密钥_id
和加密算法。The 4.2+ compatible drivers, 4.2+兼容驱动程序mongosh
, and the MongoDB 4.2 or later legacy mongo
shell use this metadata to attempt automatic decryption of BinData
subtype 6 objects. mongosh
和MongoDB 4.2或更高版本的mongo
shell使用此元数据尝试自动解密BinData子类型6对象。The automatic decryption process works as follows:自动解密过程的工作原理如下:
BinData
blob metadata for the data encryption key and encryption algorithm used to encrypt the value.BinData
blob元数据中用于加密值的数据加密密钥和加密算法。BinData
blob.BinData
blob。For the Amazon Web Services KMS, Azure Key Vault, or Google Cloud Platform KMS, send the data encryption key to the KMS provider for decryption. 对于Amazon Web Services KMS、Azure Key Vault或Google Cloud Platform KMS,请将数据加密密钥发送给KMS提供商进行解密。If the CMK does not exist or if the connection configuration does not grant access to the CMK, decryption fails and the driver returns the 如果CMK不存在或连接配置未授予对CMK的访问权限,则解密失败,驱动程序返回BinData
blob.BinData
blob。
For the Locally Managed Key, retrieve the local key and decrypt the data encryption key. 对于本地管理密钥,检索本地密钥并解密数据加密密钥。If the local key specified in the database configuration was not used to encrypt the data encryption key, decryption fails and the driver returns the 如果数据库配置中指定的本地密钥未用于加密数据加密密钥,则解密失败,驱动程序将返回BinData
blob.BinData
blob。
BinData
value using the decrypted data encryption key and appropriate algorithm.BinData
值。Applications with access to the MongoDB server that do not also have access to the required master key and data encryption keys cannot decrypt the 具有MongoDB服务器访问权限的应用程序,如果无法访问所需的主密钥和数据加密密钥,则无法解密BinData
values.BinData
值。
For more information on configuring the database connection for client-side field level encryption, see the 有关为客户端字段级加密配置数据库连接的更多信息,请参阅Mongo()
constructor or defer to the documentation for your preferred driver's client construction method.Mongo()
构造函数,或参考首选驱动程序客户端构造方法的文档。
Starting with MongoDB 4.2, the server supports using schema validation to enforce encryption of specific fields in a collection. 从MongoDB 4.2开始,服务器支持使用模式验证来强制加密集合中的特定字段。Use the automatic encryption rule keywords with the 在$jsonSchema
validation object to indicate which fields require encryption. $jsonSchema
验证对象中使用自动加密规则关键字来指示哪些字段需要加密。The server rejects any write operations to that collection where the specified fields are not 如果指定的字段不是Binary (BinData)
subtype 6 objects.Binary (BinData)
子类型6对象,服务器将拒绝对该集合的任何写入操作。
For example, the following 例如,以下collMod
command modifies the hr.employees
collection to include a validator
. collMod
命令修改hr.employees
集合以包含validator
。The $jsonSchema
validation object includes client-side field level encryption keywords to indicate that:$jsonSchema
验证对象包括客户端字段级加密关键字,以指示:
taxid
field must be encrypted. taxid
字段必须加密。taxid-short
field must be encrypted. Clients should use the specified data encryption key and the deterministic encryption algorithm when encrypting the field.taxid-short
字段必须加密。客户端在加密字段时应使用指定的数据加密密钥和确定性加密算法。db.getSiblingDB("hr").runCommand( { "collMod" : "employees", "validator" : { "$jsonSchema" : { "bsonType" : "object", "properties" : { "taxid" : { "encrypt" : { "keyId" : [UUID("e114f7ad-ad7a-4a68-81a7-ebcb9ea0953a")], "algorithm" : "AEAD_AES_256_CBC_HMAC_SHA_512-Random", } }, "taxid-short" : { "encrypt" : { "keyId" : [UUID("33408ee9-e499-43f9-89fe-5f8533870617")], "algorithm" : "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic", "bsonType" : "string" } } } } } } )
Clients performing explicit (manual) field level encryption must执行显式(手动)字段级加密的客户端在发出写入操作之前,必须使用与远程encrypt
at minimum the taxid
and taxid-short
fields using the same settings as the remote $jsonSchema
prior to issuing the write operation.$jsonSchema
相同的设置至少加密taxid
和taxid-short
字段。
Clients performing automatic client-side field level encryption have specific behavior depending on the database connection configuration:执行自动客户端字段级加密的客户端具有特定的行为,具体取决于数据库连接配置:
Automatic client-side field level encryption is available with MongoDB Enterprise 4.2 or later only.只有MongoDB Enterprise 4.2或更高版本才提供自动客户端字段级加密。
ClientSideFieldLevelEncryptionOptions
schemaMap
object contains a key for the specified collection, the client uses that object to perform automatic field level encryption and ignores the remote schema. ClientSideFieldLevelEncryptionOptions
schemaMap
对象包含指定集合的密钥,则客户端将使用该对象执行自动字段级加密,并忽略远程模式。taxid
and taxid-short
fields.taxid
和taxid-short
字段。If the connection 如果连接ClientSideFieldLevelEncryptionOptions
schemaMap
object does not contain a key for the specified collection, the client downloads the server-side remote schema for the collection and uses it to perform automatic field level encryption.ClientSideFieldLevelEncryptionOptions
schemaMap
对象不包含指定集合的密钥,则客户端将下载该集合的服务器端远程模式,并使用它执行自动字段级加密。
This configuration requires the client to trust the server has a valid schema with respect to automatic field level encryption. 此配置要求客户端信任服务器具有关于自动字段级加密的有效架构。The client only uses the remote schema to perform automatic field level encryption and does not enforce any other validation rules specified in the schema.客户端仅使用远程模式执行自动字段级加密,不强制执行模式中指定的任何其他验证规则。
Since the MongoDB server cannot decrypt nor introspect the contents of the encrypted field, it cannot validate that clients used the specified encryption options to encrypt a given field. 由于MongoDB服务器无法解密或检查加密字段的内容,因此无法验证客户端是否使用指定的加密选项对给定字段进行加密。This allows two clients to insert encrypted data using different keyIDs or encryption algorithms for a specific field. 这允许两个客户端使用不同的密钥ID或特定字段的加密算法插入加密数据。While some workloads may require independent field level encryption implementations, inconsistent implementation of encryption options for a field across clients may result in incorrect or unexpected behavior of queries against the encrypted field.虽然某些工作负载可能需要独立的字段级加密实现,但跨客户端的字段加密选项实现不一致可能会导致针对加密字段的查询行为不正确或意外。
For example, client 例如,客户机A使用随机加密对A
encrypts the PII
field using random encryption while client B
encrypts the PII
field using deterministic encryption. PII
字段进行加密,而客户机B
使用确定性加密对PII
字段进行加密。The randomized encryption algorithm always returns a different unique value while the deterministic algorithm always returns the same value. 随机加密算法总是返回不同的唯一值,而确定性算法总是返回相同的值。Queries expecting deterministically encrypted data for that field return inconsistent results, as the server cannot match any of the randomly encrypted fields.由于服务器无法匹配任何随机加密的字段,因此需要该字段的确定加密数据的查询返回不一致的结果。
MongoDB 4.2 client-side field level encryption is only available with the following official 4.2+ compatible driver versions:MongoDB 4.2客户端字段级加密仅适用于以下官方4.2+兼容驱动程序版本:
Driver | Supported Versions | |
---|---|---|
Node | 3.4.0+ | |
Java | 3.12.0+ | |
Java Reactive Streams | 1.13.0+ | Java RS Documentation |
Python (PyMongo) | 3.10.0+ | |
C#/.NET | 2.10.0+ | .NET Driver Quickstart |
C | 1.17.5 | C Driver Client-Side Field Level Encryption |
Go | 1.2+ | Go Driver Quickstart |
Scala | 2.8.0+ | Scala Documentation |
PHP | 1.6.0+ | PHP Driver Quickstart |
Ruby | 2.12.1+ | Ruby Driver Quickstart |
Please refer to the driver reference documentation for syntax and implementation examples.有关语法和实现示例,请参阅驱动程序参考文档。