KeyVault.rewrapManyDataKey(filter, options)解密多个数据加密密钥 (DEK),并使用新的客户主密钥 (CMK) 重新加密。 使用此方法轮换加密 DEK 的 CMK。 要了解有关 CMK 和 DEK 的更多信息,请参阅数据加密密钥和客户主密钥。
您可以通过
masterKey参数指定客户主密钥。如果您不包含masterKey参数,该方法将使用 DEK 元数据中引用的 CMK 解密和加密每个 DEK。 要了解有关 DEK 元数据的更多信息,请参阅用于解密的元数据。返回: 一个BulkWriteResult对象,报告受影响的数据键数量。
警告
备份你的密钥保管库集合
在轮换数据加密密钥之前,请确保为密钥保管库集合创建备份。 如果您无法访问数据加密密钥,您将丢失所有加密数据。
要了解如何创建collection的备份,请参阅使用 MongoDB 工具进行备份和恢复。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
KeyVault.rewrapManyDataKey 通过以下语法实现:
let keyVault = db.getMongo().getKeyVault() keyVault.rewrapManyDataKey( <filter>, <options> )
Parameter | 类型 | 说明 |
|---|---|---|
| keyvault collection的查询筛选器 | |
| 文档 | 该文档有两个字段:
|
行为
此操作不是原子操作,不应与其他密钥管理操作并行运行。
需要在数据库连接上配置客户端字段级加密
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.
例子
这些示例可让您快速评估客户端字段级加密。 有关使用每个受支持的KMS提供商的具体示例,请参阅加密密钥管理。
Retrieve the KeyVault object and use the KeyVault.rewrapManyDataKey() method to rewrap the existing keys in a new masterKey. If no new masterKey is given, each data key retains its respective current masterKey.
使用当前主密钥重新包装数据密钥
以下示例展示了如何使用各自的当前masterKey重新包装每个数据键:
let keyVault = mongo.getKeyVault() keyVault.rewrapManyDataKey()
使用新的 masterKey 重新包装数据密钥
以下示例展示了如何使用新的masterKey重新包装每个数据键:
let keyVault = mongo.getKeyVault() keyVault.rewrapManyDataKey({}, { provider: 'aws', masterKey: { region: 'us-east-2', key: 'arn:aws:kms:us-east-2:...' } })
重新包装最近未重新包装的数据密钥
以下示例展示了如何重新包装过去 30 天内未重新包装的数据密钥。
let keyVault = mongo.getKeyVault() const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); keyVault.rewrapManyDataKey({ updateDate: { $lt: thirtyDaysAgo } });
输出
KeyVault.rewrapManyDataKey() returns a BulkWriteResult object detailing how many data keys were affected:
{ bulkWriteResult: BulkWriteResult { result: { ok: 1, writeErrors: [], writeConcernErrors: [], insertedIds: [], nInserted: 0, nUpserted: 0, nMatched: 3, nModified: 3, nRemoved: 0, upserted: [], opTime: { ts: Timestamp({ t: 1655840760, i: 3 }), t: 23 } } } }