KeyVault.createKey(keyManagementService, customerMasterKey, ["keyAltName"])将数据加密密钥添加到与数据库连接关联的密钥保管库。 客户端字段级加密使用数据加密密钥来支持字段值的加密和解密。
返回: 创建的数据加密密钥的
UUID唯一标识符。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
createKey() 通过以下语法实现:
keyVault = db.getMongo().getKeyVault() keyVault.createKey( keyManagementService, customerMasterKey, [ "keyAltName" ] )
Parameter | 类型 | 说明 |
|---|---|---|
| 字符串 | 必需 用于检索客户主密钥 (CMK) 的密钥管理服务 (KMS )。 接受以下参数:
如果未为 |
| 字符串或文档 | 用于加密数据加密密钥的客户主密钥 (CMK)。 如果 根据您的 KMS 提供商,按如下方式提供客户主密钥:
如果 |
| 字符串数组 | Optional 数据加密密钥的替代名称。 使用
|
| 文档 | Optional 指定新密钥选项的文档。
|
行为
需要在数据库连接上配置客户端字段级加密
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:
Use the
mongoshcommand line options to establish a connection with the required options. The command line options only support the Amazon Web Services KMS provider for CMK management.
例子
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.
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:
使用
getKey()检索UUID创建的密钥。-或-
使用
getKeyByAltName()按备用名称检索密钥。