对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

KeyVault.createKey()(mongosh方法)

KeyVault.createKey(keyManagementService, customerMasterKey, ["keyAltName"])

将数据加密密钥添加到与数据库连接关联的密钥保管库。 客户端字段级加密使用数据加密密钥来支持字段值的加密和解密。

返回:

创建的数据加密密钥的UUID唯一标识符。

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

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

createKey() 通过以下语法实现:

keyVault = db.getMongo().getKeyVault()
keyVault.createKey(
keyManagementService,
customerMasterKey,
[ "keyAltName" ]
)
Parameter
类型
说明

keyManagementService

字符串

必需

用于检索客户主密钥 (CMK) 的密钥管理服务 (KMS )。 接受以下参数:

如果未为database connection配置指定的 KMS,则数据加密密钥创建将失败。

customerMasterKey

字符串或文档

用于加密数据加密密钥的客户主密钥 (CMK)。 如果keyManagementServiceawsazuregcp ,则为必填项。

根据您的 KMS 提供商,按如下方式提供客户主密钥:

  • 对于Amazon Web Services KMS,请将主密钥的完整Amazon资源名称 (ARN)指定为单个字符串。

  • 对于Azure Key Vault KMS,请指定包含以下键值对的文档:

    • keyVaultEndpoint - 要使用的 Azure Key Vault 的 DNS 名称

    • keyVersion — 可选。 keyName中指定的密钥版本(如果适用)

    版本 5.0 中的新增功能。

  • 对于Google Cloud Platform KMS ,请指定包含以下键值对的文档:

    • projectId - GCP 项目名称

    • location - KMS 密钥环的位置

    • keyRing - KMS 密钥环的名称(通常为“全局”)

    • keyName - 要使用的密钥的名称

    • keyVersion — 可选。 keyName中指定的密钥版本(如果适用)

    版本 5.0 中的新增功能。

createKey() requests that the KMS encrypt the data encryption key material using the specified CMK. If the CMK does not exist or if the AutoEncryptionOpts configuration does not have sufficient privileges to use the CMK, createKey() returns an error.

如果keyManagementServicelocal ,则此参数无效,可以安全地省略。

keyAltName

字符串数组

Optional

数据加密密钥的替代名称。 使用keyAltName提高特定数据加密密钥的可查找性,或作为注释的模拟。

getKeyVault()方法自动在keyAltNames字段上创建唯一索引,并使用仅针对存在keyAltNames的文档的部分索引筛选器。

options

文档

Optional

指定新密钥选项的文档。 options具有以下字段:

  • masterKey:用于加密数据的新主密钥。

  • keyAltNames:备用名称数组,每个主密钥一个。

  • keyMaterial:用于创建密钥的 bindata。

The mongosh client-side field level encryption methods require a database connection with client-side field level encryption enabled. If the current database connection was not initiated with client-side field level encryption enabled, either:

The following example is intended for rapid evaluation of client-side field level encryption. For specific examples of using KeyVault.createKey() with each supported KMS provider, see Create a Data Key.

1

启动mongosh客户端。

mongosh --nodb
2

要为本地托管的密钥配置客户端字段级加密,请生成一个不带换行符的 base64 编码的 96 字节字符串。

const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
3

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

let autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, TEST_LOCAL_KEY)
}
}
}
4

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

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

Retrieve the keyVault object and use the KeyVault.createKey() method to create a new data encryption key using the locally managed key:

keyVault = encryptedClient.getKeyVault()
keyVault.createKey("local", ["data-encryption-key"])

If successful, createKey() returns the UUID of the new data encryption key. To retrieve the new data encryption key document from the key vault, either:

给本页内容打分