Docs 菜单

getClientEncryption()

getClientEncryption()

Returns the ClientEncryption object for the current database collection. The ClientEncryption object supports explicit (manual) encryption and decryption of field values for Client-Side field level encryption.

返回:当前数据库连接的ClientEncryption对象。

此命令可用于以下环境中托管的部署:

getClientEncryption()的语法如下:

db.getMongo().getClientEncryption();

Use the ClientEncryption object to access the following explicit encryption methods:

mongosh 客户端字段级加密方法需要启用客户端字段级加密的数据库连接。如果当前数据库连接在启动时未启用客户端字段级加密,则任选以下其一:

The getKeyVault() method automatically creates a 唯一索引 on the keyAltNames field with a partial index filter for only documents where keyAltNames exists. getKeyVault() creates this index in the key vault collection. This prevents any two data encryption keys in the same key vault from having the same key alternative name and therefore avoids ambiguity around which data encryption key is appropriate for encryption/decryption.

警告

Do not drop the unique index created by getKeyVault(). Client-Side Field Level Encryption operations depend on server-enforced uniqueness of keyAltNames. Removing the index may lead to unexpected or unpredictable behavior.

以下示例使用本地托管的 KMS 进行客户端字段级加密配置。

为本地管理的密钥配置客户端字段级加密:

  • 生成不带换行符的 base64 编码的 96 字节字符串

  • 使用 mongosh 加载密钥

export TEST_LOCAL_KEY=$(echo "$(head -c 96 /dev/urandom | base64 | tr -d '\n')")
mongosh --nodb

使用生成的本地密钥字符串创建客户端字段级加密对象:

var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, process.env["TEST_LOCAL_KEY"])
}
}
}

使用配置了客户端字段级加密选项的Mongo()构造函数来创建数据库连接。 将mongodb://myMongo.example.net URI 替换为目标集群的连接字符串 URI

encryptedClient = Mongo(
"mongodb://myMongo.example.net:27017/?replSetName=myMongo",
autoEncryptionOpts
)

Use the getClientEncryption() method to retrieve the client encryption object:

clientEncryption = encryptedClient.getClientEncryption()

有关在启用客户端字段级加密的情况下启动 MongoDB 连接的完整文档,请参阅Mongo()