@@ -2127,106 +2127,143 @@ TODO(NODE-5283): The error thrown in this test fails an instanceof check with Mo
2127
2127
} ;
2128
2128
let client1 , client2 ;
2129
2129
2130
- /**
2131
- * Run the following test case for each pair of KMS providers (referred to as ``srcProvider`` and ``dstProvider``).
2132
- * Include pairs where ``srcProvider`` equals ``dstProvider``.
2133
- */
2134
- function * generateTestCombinations ( ) {
2135
- const providers = Object . keys ( masterKeys ) ;
2136
- for ( const srcProvider of providers ) {
2137
- for ( const dstProvider of providers ) {
2138
- yield { srcProvider, dstProvider } ;
2130
+ describe ( 'Case 1: Rewrap with separate ClientEncryption' , function ( ) {
2131
+ /**
2132
+ * Run the following test case for each pair of KMS providers (referred to as ``srcProvider`` and ``dstProvider``).
2133
+ * Include pairs where ``srcProvider`` equals ``dstProvider``.
2134
+ */
2135
+ function * generateTestCombinations ( ) {
2136
+ const providers = Object . keys ( masterKeys ) ;
2137
+ for ( const srcProvider of providers ) {
2138
+ for ( const dstProvider of providers ) {
2139
+ yield { srcProvider, dstProvider } ;
2140
+ }
2139
2141
}
2140
2142
}
2141
- }
2142
2143
2143
- beforeEach ( function ( ) {
2144
- client1 = this . configuration . newClient ( ) ;
2145
- client2 = this . configuration . newClient ( ) ;
2146
- } ) ;
2144
+ beforeEach ( function ( ) {
2145
+ client1 = this . configuration . newClient ( ) ;
2146
+ client2 = this . configuration . newClient ( ) ;
2147
+ } ) ;
2147
2148
2148
- afterEach ( async function ( ) {
2149
- await client1 . close ( ) ;
2150
- await client2 . close ( ) ;
2151
- } ) ;
2149
+ afterEach ( async function ( ) {
2150
+ await client1 . close ( ) ;
2151
+ await client2 . close ( ) ;
2152
+ } ) ;
2152
2153
2153
- for ( const { srcProvider, dstProvider } of generateTestCombinations ( ) ) {
2154
- it (
2155
- `should rewrap data key from ${ srcProvider } to ${ dstProvider } ` ,
2156
- metadata ,
2157
- async function ( ) {
2158
- // Step 1. Drop the collection ``keyvault.datakeys``
2159
- await client1
2160
- . db ( 'keyvault' )
2161
- . dropCollection ( 'datakeys' )
2162
- . catch ( ( ) => null ) ;
2163
-
2164
- // Step 2. Create a ``ClientEncryption`` object named ``clientEncryption1``
2165
- const clientEncryption1 = new this . configuration . mongodbClientEncryption . ClientEncryption (
2166
- client1 ,
2167
- {
2168
- keyVaultNamespace : 'keyvault.datakeys' ,
2169
- kmsProviders : getKmsProviders ( ) ,
2170
- tlsOptions : {
2171
- kmip : {
2172
- tlsCAFile : process . env . KMIP_TLS_CA_FILE ,
2173
- tlsCertificateKeyFile : process . env . KMIP_TLS_CERT_FILE
2174
- }
2175
- } ,
2176
- extraOptions : getEncryptExtraOptions ( ) ,
2177
- bson : BSON
2178
- }
2179
- ) ;
2154
+ for ( const { srcProvider, dstProvider } of generateTestCombinations ( ) ) {
2155
+ it (
2156
+ `should rewrap data key from ${ srcProvider } to ${ dstProvider } ` ,
2157
+ metadata ,
2158
+ async function ( ) {
2159
+ // Step 1. Drop the collection ``keyvault.datakeys``
2160
+ await client1
2161
+ . db ( 'keyvault' )
2162
+ . dropCollection ( 'datakeys' )
2163
+ . catch ( ( ) => null ) ;
2164
+
2165
+ // Step 2. Create a ``ClientEncryption`` object named ``clientEncryption1``
2166
+ const clientEncryption1 =
2167
+ new this . configuration . mongodbClientEncryption . ClientEncryption ( client1 , {
2168
+ keyVaultNamespace : 'keyvault.datakeys' ,
2169
+ kmsProviders : getKmsProviders ( ) ,
2170
+ tlsOptions : {
2171
+ kmip : {
2172
+ tlsCAFile : process . env . KMIP_TLS_CA_FILE ,
2173
+ tlsCertificateKeyFile : process . env . KMIP_TLS_CERT_FILE
2174
+ }
2175
+ } ,
2176
+ extraOptions : getEncryptExtraOptions ( ) ,
2177
+ bson : BSON
2178
+ } ) ;
2179
+
2180
+ // Step 3. Call ``clientEncryption1.createDataKey`` with ``srcProvider``
2181
+ const keyId = await clientEncryption1 . createDataKey ( srcProvider , {
2182
+ masterKey : masterKeys [ srcProvider ]
2183
+ } ) ;
2180
2184
2181
- // Step 3. Call ``clientEncryption1.createDataKey`` with ``srcProvider``
2182
- const keyId = await clientEncryption1 . createDataKey ( srcProvider , {
2183
- masterKey : masterKeys [ srcProvider ]
2184
- } ) ;
2185
+ // Step 4. Call ``clientEncryption1.encrypt`` with the value "test"
2186
+ const cipherText = await clientEncryption1 . encrypt ( 'test' , {
2187
+ keyId,
2188
+ algorithm : 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
2189
+ } ) ;
2185
2190
2186
- // Step 4. Call ``clientEncryption1.encrypt`` with the value "test"
2187
- const cipherText = await clientEncryption1 . encrypt ( 'test' , {
2188
- keyId,
2189
- algorithm : 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
2190
- } ) ;
2191
+ // Step 5. Create a ``ClientEncryption`` object named ``clientEncryption2``
2192
+ const clientEncryption2 =
2193
+ new this . configuration . mongodbClientEncryption . ClientEncryption ( client2 , {
2194
+ keyVaultNamespace : 'keyvault.datakeys' ,
2195
+ kmsProviders : getKmsProviders ( ) ,
2196
+ tlsOptions : {
2197
+ kmip : {
2198
+ tlsCAFile : process . env . KMIP_TLS_CA_FILE ,
2199
+ tlsCertificateKeyFile : process . env . KMIP_TLS_CERT_FILE
2200
+ }
2201
+ } ,
2202
+ extraOptions : getEncryptExtraOptions ( ) ,
2203
+ bson : BSON
2204
+ } ) ;
2191
2205
2192
- // Step 5. Create a ``ClientEncryption`` object named ``clientEncryption2``
2193
- const clientEncryption2 = new this . configuration . mongodbClientEncryption . ClientEncryption (
2194
- client2 ,
2195
- {
2196
- keyVaultNamespace : 'keyvault.datakeys' ,
2197
- kmsProviders : getKmsProviders ( ) ,
2198
- tlsOptions : {
2199
- kmip : {
2200
- tlsCAFile : process . env . KMIP_TLS_CA_FILE ,
2201
- tlsCertificateKeyFile : process . env . KMIP_TLS_CERT_FILE
2202
- }
2203
- } ,
2204
- extraOptions : getEncryptExtraOptions ( ) ,
2205
- bson : BSON
2206
- }
2207
- ) ;
2206
+ // Step 6. Call ``clientEncryption2.rewrapManyDataKey`` with an empty ``filter``
2207
+ const rewrapManyDataKeyResult = await clientEncryption2 . rewrapManyDataKey (
2208
+ { } ,
2209
+ {
2210
+ provider : dstProvider ,
2211
+ masterKey : masterKeys [ dstProvider ]
2212
+ }
2213
+ ) ;
2208
2214
2209
- // Step 6. Call ``clientEncryption2.rewrapManyDataKey`` with an empty ``filter``
2210
- const rewrapManyDataKeyResult = await clientEncryption2 . rewrapManyDataKey (
2211
- { } ,
2212
- {
2213
- provider : dstProvider ,
2214
- masterKey : masterKeys [ dstProvider ]
2215
- }
2216
- ) ;
2215
+ expect ( rewrapManyDataKeyResult ) . to . have . property ( 'bulkWriteResult' ) ;
2216
+ expect ( rewrapManyDataKeyResult . bulkWriteResult ) . to . have . property ( 'modifiedCount' , 1 ) ;
2217
2217
2218
- expect ( rewrapManyDataKeyResult ) . to . have . property ( 'bulkWriteResult' ) ;
2219
- expect ( rewrapManyDataKeyResult . bulkWriteResult ) . to . have . property ( 'modifiedCount' , 1 ) ;
2218
+ // 7. Call ``clientEncryption1.decrypt`` with the ``ciphertext``. Assert the return value is "test".
2219
+ const decryptResult1 = await clientEncryption1 . decrypt ( cipherText ) ;
2220
+ expect ( decryptResult1 ) . to . equal ( 'test' ) ;
2220
2221
2221
- // 7. Call ``clientEncryption1.decrypt`` with the ``ciphertext``. Assert the return value is "test".
2222
- const decryptResult1 = await clientEncryption1 . decrypt ( cipherText ) ;
2223
- expect ( decryptResult1 ) . to . equal ( 'test' ) ;
2222
+ // 8. Call ``clientEncryption2.decrypt`` with the ``ciphertext``. Assert the return value is "test".
2223
+ const decryptResult2 = await clientEncryption2 . decrypt ( cipherText ) ;
2224
+ expect ( decryptResult2 ) . to . equal ( 'test' ) ;
2225
+ }
2226
+ ) ;
2227
+ }
2228
+ } ) ;
2224
2229
2225
- // 8. Call ``clientEncryption2.decrypt`` with the ``ciphertext``. Assert the return value is "test".
2226
- const decryptResult2 = await clientEncryption2 . decrypt ( cipherText ) ;
2227
- expect ( decryptResult2 ) . to . equal ( 'test' ) ;
2228
- }
2229
- ) ;
2230
- }
2230
+ describe ( 'Case 2: RewrapManyDataKeyOpts.provider is not optional' , function ( ) {
2231
+ let client ;
2232
+ let clientEncryption ;
2233
+
2234
+ // 1. Create a ``ClientEncryption`` object named ``clientEncryption`` with these options:
2235
+ // class ClientEncryptionOpts {
2236
+ // keyVaultClient: <new MongoClient>,
2237
+ // keyVaultNamespace: "keyvault.datakeys",
2238
+ // kmsProviders: <all KMS providers>,
2239
+ before ( function ( ) {
2240
+ client = this . configuration . newClient ( ) ;
2241
+ clientEncryption = new this . configuration . mongodbClientEncryption . ClientEncryption ( client , {
2242
+ keyVaultNamespace : 'keyvault.datakeys' ,
2243
+ kmsProviders : getKmsProviders ( ) ,
2244
+ extraOptions : getEncryptExtraOptions ( ) ,
2245
+ bson : BSON
2246
+ } ) ;
2247
+ } ) ;
2248
+
2249
+ after ( async function ( ) {
2250
+ await client ?. close ( ) ;
2251
+ } ) ;
2252
+
2253
+ // 2. Call ``clientEncryption.rewrapManyDataKey`` with an empty ``filter`` and these options:
2254
+ // class RewrapManyDataKeyOpts {
2255
+ // masterKey: {}
2256
+ // }
2257
+ // Assert that `clientEncryption.rewrapManyDataKey` raises a client error indicating that the
2258
+ // required ``RewrapManyDataKeyOpts.provider`` field is missing.
2259
+ context ( 'when provider field is missing' , function ( ) {
2260
+ it ( 'raises an error' , async function ( ) {
2261
+ const error = await clientEncryption
2262
+ . rewrapManyDataKey ( { } , { masterKey : { } } )
2263
+ . catch ( error => error ) ;
2264
+ expect ( error . message ) . to . include ( 'expected UTF-8 provider' ) ;
2265
+ } ) ;
2266
+ } ) ;
2267
+ } ) ;
2231
2268
} ) ;
2232
2269
} ) ;
0 commit comments