Docs Menu

KeyVault.rewrapManyDataKey()

On this page

  • Behavior
  • Example
KeyVault.rewrapManyDataKey(filter, options)

KeyVault.rewrapManyDataKey decrypts multiple data keys and re-encrypts them with a new masterKey. If a new masterKey is not given, the current masterKey is used.

KeyVault.rewrapManyDataKey has the following syntax:

let keyVault = db.getMongo().getKeyVault()
keyVault.rewrapManyDataKey(
<filter>,
<options>
)
Parameter
Type
Description
filter
The query filter for the keyvault collection.
options
document

This document has two fields:

  • provider: A KMS provider (AWS KMS, Azure Key Vault, GCP KMS, the local provider, or KMIP)
  • masterKey: A KMS-specific key used to encrypt the new data key.
Returns:A BulkWriteResult object that reports how many data keys were affected.
Important
Back-Up Your Key Vault collection

Before you rotate your Data Encryption Keys, ensure you create a backup of your Key Vault collection. If you lose access to your Data Encryption Keys, you will lose all your encrypted data.

To learn how to create a backup of a collection, see Back Up and Restore with MongoDB Tools.

Important
Key Rotation Support

To view your driver's dependencies for the key rotation API, see Compatibility.

This operation is not atomic and should not be run in parallel with other key management operations.

The mongo 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:

These examples allow you to rapidly evaluate client-side field level encryption. For specific examples using each supported KMS provider, see Encryption Key Management.

Configuring client-side field level encryption for a locally managed key requires specifying a base64-encoded 96-byte string with no line breaks. The following operation generates a key that meets the stated requirements and loads it into the mongo shell:

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

Create the client-side field level encryption object using the generated local key string:

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

Use the Mongo() constructor to create a database connection with the client-side field level encryption options. Replace the mongodb://myMongo.example.net URI with the connection string URI of the target cluster.

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

Retrieve the KeyVault object and use the KeyVault.rewrapManyDataKey() method to re-wrap the existing keys in a new masterKey. If no new masterKey is given, each data key retains its respective current masterKey.

The following example show how you can re-wrap each data key with its respective current masterKey:

let keyVault = mongo.getKeyVault()
keyVault.rewrapManyDataKey()

The following example shows how you can use :method:KeyVault.rewrapManyDataKey()` to migrate to a new masterKey:

let keyVault = mongo.getKeyVault()
keyVault.rewrapManyDataKey({}, {
provider: 'aws',
masterKey: {
region: 'us-east-2',
key: 'arn:aws:kms:us-east-2:...'
}
})

The following example shows how to re-wrap data keys that have not been re-wrapped in the previous thirty days.

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 }
}
}
}
What is MongoDB? →

On this page

Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.