|
| 1 | +.. _server-keyvault-rewrap-manydatakey-method: |
| 2 | + |
| 3 | +============================ |
| 4 | +KeyVault.rewrapManyDataKey() |
| 5 | +============================ |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +.. method:: KeyVault.rewrapManyDataKey(filter, options) |
| 16 | + |
| 17 | + ``KeyVault.rewrapManyDataKey`` decrypts multiple data |
| 18 | + keys and re-encrypts them with a new ``masterKey``. If a new |
| 19 | + ``masterKey`` is not given, the current ``masterKey`` is used. |
| 20 | + |
| 21 | + ``KeyVault.rewrapManyDataKey`` has the following syntax: |
| 22 | + |
| 23 | + .. code-block:: javascript |
| 24 | + |
| 25 | + let keyVault = db.getMongo().getKeyVault() |
| 26 | + |
| 27 | + keyVault.rewrapManyDataKey( |
| 28 | + <filter>, |
| 29 | + <options> |
| 30 | + ) |
| 31 | + |
| 32 | + .. list-table:: |
| 33 | + :header-rows: 1 |
| 34 | + :widths: 20 20 80 |
| 35 | + |
| 36 | + * - Parameter |
| 37 | + |
| 38 | + - Type |
| 39 | + |
| 40 | + - Description |
| 41 | + |
| 42 | + * - ``filter`` |
| 43 | + |
| 44 | + - :ref:`query filter document <document-query-filter>` |
| 45 | + |
| 46 | + - The query filter for the keyvault collection. |
| 47 | + |
| 48 | + * - ``options`` |
| 49 | + |
| 50 | + - document |
| 51 | + |
| 52 | + - |
| 53 | + This document has two fields: |
| 54 | + |
| 55 | + - ``provider``: A :ref:`KMS provider |
| 56 | + <qe-fundamentals-kms-providers>` (AWS KMS, Azure Key Vault, |
| 57 | + GCP KMS, the local provider, or KMIP) |
| 58 | + - ``masterKey``: A KMS-specific key used to encrypt the new |
| 59 | + data key. |
| 60 | + |
| 61 | + :returns: |
| 62 | + |
| 63 | + A :ref:`BulkWriteResult <server-bulkwriteresult-method>` object |
| 64 | + that reports how many data keys were affected. |
| 65 | + |
| 66 | +Behavior |
| 67 | +-------- |
| 68 | + |
| 69 | +This operation is not atomic and should not be run in parallel with |
| 70 | +other key management operations. |
| 71 | + |
| 72 | +Requires Configuring Client-Side Field Level Encryption on Database Connection |
| 73 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 | + |
| 75 | +.. include:: /includes/extracts/csfle-requires-enabling-encryption.rst |
| 76 | + |
| 77 | +Example |
| 78 | +------- |
| 79 | + |
| 80 | +These examples allow you to rapidly evaluate client-side field level |
| 81 | +encryption. For specific examples using each supported |
| 82 | +:abbr:`KMS (Key Management Service)` provider, see |
| 83 | +:ref:`field-level-encryption-data-key-manage`. |
| 84 | + |
| 85 | +.. include:: /includes/extracts/csfle-connection-boilerplate.rst |
| 86 | + |
| 87 | +Retrieve the :method:`KeyVault <getKeyVault()>` object and use the |
| 88 | +:method:`KeyVault.rewrapManyDataKey` method to re-wrap the existing |
| 89 | +keys in a new ``masterKey``. If no new ``masterKey`` is given, each |
| 90 | +data key retains its respective current ``masterKey``. |
| 91 | + |
| 92 | +Re-wrap Data Keys with Current``masterKey`` |
| 93 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | + |
| 95 | +The following example show how you can re-wrap each data key with its |
| 96 | +respective current ``masterKey``: |
| 97 | + |
| 98 | +.. code-block:: javascript |
| 99 | + |
| 100 | + let keyVault = mongo.getKeyVault() |
| 101 | + |
| 102 | + keyVault.rewrapManyDataKey() |
| 103 | + |
| 104 | +Migrate to a New ``masterKey`` |
| 105 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 106 | + |
| 107 | +The following example shows how you can use |
| 108 | +:method:KeyVault.rewrapManyDataKey()` to migrate to a new ``masterKey``: |
| 109 | + |
| 110 | +.. code-block:: javascript |
| 111 | + |
| 112 | + let keyVault = mongo.getKeyVault() |
| 113 | + |
| 114 | + keyVault.rewrapManyDataKey({}, { |
| 115 | + provider: 'aws', |
| 116 | + masterKey: { |
| 117 | + region: 'us-east-2', |
| 118 | + key: 'arn:aws:kms:us-east-2:...' |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | +Re-wrap Data Keys that have not been Re-wrapped Recently |
| 123 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 124 | + |
| 125 | +The following example shows how to re-wrap data keys that have not |
| 126 | +been re-wrapped in the previous thirty days. |
| 127 | + |
| 128 | +.. code-block:: javascript |
| 129 | + |
| 130 | + let keyVault = mongo.getKeyVault() |
| 131 | + |
| 132 | + const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); |
| 133 | + |
| 134 | + keyVault.rewrapManyDataKey({ updateDate: { $lt: thirtyDaysAgo } }); |
| 135 | + |
| 136 | +Output |
| 137 | +~~~~~~ |
| 138 | + |
| 139 | +:method:`KeyVault.rewrapManyDataKey()` returns a ``BulkWriteResult`` |
| 140 | +object detailing how many data keys were affected: |
| 141 | + |
| 142 | +.. code-block:: json |
| 143 | + :copyable: false |
| 144 | + |
| 145 | + { |
| 146 | + bulkWriteResult: BulkWriteResult { |
| 147 | + result: { |
| 148 | + ok: 1, |
| 149 | + writeErrors: [], |
| 150 | + writeConcernErrors: [], |
| 151 | + insertedIds: [], |
| 152 | + nInserted: 0, |
| 153 | + nUpserted: 0, |
| 154 | + nMatched: 3, |
| 155 | + nModified: 3, |
| 156 | + nRemoved: 0, |
| 157 | + upserted: [], |
| 158 | + opTime: { ts: Timestamp({ t: 1655840760, i: 3 }), t: 23 } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
0 commit comments