@@ -2046,4 +2046,126 @@ describe('Client Side Encryption Prose Tests', metadata, function () {
20462046 } ) ;
20472047 } ) ;
20482048 } ) ;
2049+
2050+ context ( '16. Rewrap' , function ( ) {
2051+ const masterKeys = {
2052+ aws : {
2053+ region : 'us-east-1' ,
2054+ key : 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0'
2055+ } ,
2056+ azure : {
2057+ keyVaultEndpoint : 'key-vault-csfle.vault.azure.net' ,
2058+ keyName : 'key-name-csfle'
2059+ } ,
2060+ gcp : {
2061+ projectId : 'devprod-drivers' ,
2062+ location : 'global' ,
2063+ keyRing : 'key-ring-csfle' ,
2064+ keyName : 'key-name-csfle'
2065+ } ,
2066+ kmip : { } ,
2067+ local : undefined
2068+ } ;
2069+ let client1 , client2 ;
2070+
2071+ /**
2072+ * Run the following test case for each pair of KMS providers (referred to as ``srcProvider`` and ``dstProvider``).
2073+ * Include pairs where ``srcProvider`` equals ``dstProvider``.
2074+ */
2075+ function * generateTestCombinations ( ) {
2076+ const providers = Object . keys ( masterKeys ) ;
2077+ for ( const srcProvider of providers ) {
2078+ for ( const dstProvider of providers ) {
2079+ yield { srcProvider, dstProvider } ;
2080+ }
2081+ }
2082+ }
2083+
2084+ beforeEach ( function ( ) {
2085+ client1 = this . configuration . newClient ( ) ;
2086+ client2 = this . configuration . newClient ( ) ;
2087+ } ) ;
2088+
2089+ afterEach ( async function ( ) {
2090+ await client1 . close ( ) ;
2091+ await client2 . close ( ) ;
2092+ } ) ;
2093+
2094+ for ( const { srcProvider, dstProvider } of generateTestCombinations ( ) ) {
2095+ it (
2096+ `should rewrap data key from ${ srcProvider } to ${ dstProvider } ` ,
2097+ metadata ,
2098+ async function ( ) {
2099+ // Step 1. Drop the collection ``keyvault.datakeys``
2100+ await client1
2101+ . db ( 'keyvault' )
2102+ . dropCollection ( 'datakeys' )
2103+ . catch ( ( ) => null ) ;
2104+
2105+ // Step 2. Create a ``ClientEncryption`` object named ``clientEncryption1``
2106+ const clientEncryption1 = new this . configuration . mongodbClientEncryption . ClientEncryption (
2107+ client1 ,
2108+ {
2109+ keyVaultNamespace : 'keyvault.datakeys' ,
2110+ kmsProviders : getKmsProviders ( ) ,
2111+ tlsOptions : {
2112+ kmip : {
2113+ tlsCAFile : process . env . KMIP_TLS_CA_FILE ,
2114+ tlsCertificateKeyFile : process . env . KMIP_TLS_CERT_FILE
2115+ }
2116+ } ,
2117+ bson : BSON
2118+ }
2119+ ) ;
2120+
2121+ // Step 3. Call ``clientEncryption1.createDataKey`` with ``srcProvider``
2122+ const keyId = await clientEncryption1 . createDataKey ( srcProvider , {
2123+ masterKey : masterKeys [ srcProvider ]
2124+ } ) ;
2125+
2126+ // Step 4. Call ``clientEncryption1.encrypt`` with the value "test"
2127+ const cipherText = await clientEncryption1 . encrypt ( 'test' , {
2128+ keyId,
2129+ algorithm : 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
2130+ } ) ;
2131+
2132+ // Step 5. Create a ``ClientEncryption`` object named ``clientEncryption2``
2133+ const clientEncryption2 = new this . configuration . mongodbClientEncryption . ClientEncryption (
2134+ client2 ,
2135+ {
2136+ keyVaultNamespace : 'keyvault.datakeys' ,
2137+ kmsProviders : getKmsProviders ( ) ,
2138+ tlsOptions : {
2139+ kmip : {
2140+ tlsCAFile : process . env . KMIP_TLS_CA_FILE ,
2141+ tlsCertificateKeyFile : process . env . KMIP_TLS_CERT_FILE
2142+ }
2143+ } ,
2144+ bson : BSON
2145+ }
2146+ ) ;
2147+
2148+ // Step 6. Call ``clientEncryption2.rewrapManyDataKey`` with an empty ``filter``
2149+ const rewrapManyDataKeyResult = await clientEncryption2 . rewrapManyDataKey (
2150+ { } ,
2151+ {
2152+ provider : dstProvider ,
2153+ masterKey : masterKeys [ dstProvider ]
2154+ }
2155+ ) ;
2156+
2157+ expect ( rewrapManyDataKeyResult ) . to . have . property ( 'bulkWriteResult' ) ;
2158+ expect ( rewrapManyDataKeyResult . bulkWriteResult ) . to . have . property ( 'nModified' , 1 ) ;
2159+
2160+ // 7. Call ``clientEncryption1.decrypt`` with the ``ciphertext``. Assert the return value is "test".
2161+ const decryptResult1 = await clientEncryption1 . decrypt ( cipherText ) ;
2162+ expect ( decryptResult1 ) . to . equal ( 'test' ) ;
2163+
2164+ // 8. Call ``clientEncryption2.decrypt`` with the ``ciphertext``. Assert the return value is "test".
2165+ const decryptResult2 = await clientEncryption2 . decrypt ( cipherText ) ;
2166+ expect ( decryptResult2 ) . to . equal ( 'test' ) ;
2167+ }
2168+ ) ;
2169+ }
2170+ } ) ;
20492171} ) ;
0 commit comments