22
33const assert = require ( 'assert' ) ;
44const mdb = require ( 'mongodb' ) ;
5+ const isBsonType = require ( '../../lib/helpers/isBsonType' ) ;
6+
7+ const LOCAL_KEY = Buffer . from ( 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk' , 'base64' ) ;
58
69describe ( 'environmental variables' , ( ) => {
7- it ( 'MONGODB_TEST_URI is set' , async function ( ) {
10+ it ( 'MONGOOSE_TEST_URI is set' , async function ( ) {
811 const uri = process . env . MONGOOSE_TEST_URI ;
912 assert . ok ( uri ) ;
1013 } ) ;
@@ -16,30 +19,77 @@ describe('environmental variables', () => {
1619} ) ;
1720
1821describe ( 'basic integration' , ( ) => {
19- it ( 'supports mongodb csfle auto-encryption integration' , async ( ) => {
20- // 1. Create a MongoClient configured with auto encryption (referred to as `client_encrypted`)
21- const client = new mdb . MongoClient (
22+ let keyVaultClient ;
23+ let dataKey ;
24+ let encryptedClient ;
25+ let dummyClient ;
26+
27+ beforeEach ( async function ( ) {
28+ keyVaultClient = new mdb . MongoClient ( process . env . MONGOOSE_TEST_URI ) ;
29+ await keyVaultClient . connect ( ) ;
30+ await keyVaultClient . db ( 'keyvault' ) . collection ( 'datakeys' ) ;
31+ const clientEncryption = new mdb . ClientEncryption ( keyVaultClient , {
32+ keyVaultNamespace : 'keyvault.datakeys' ,
33+ kmsProviders : { local : { key : LOCAL_KEY } }
34+ } ) ;
35+ dataKey = await clientEncryption . createDataKey ( 'local' ) ;
36+
37+ encryptedClient = new mdb . MongoClient (
2238 process . env . MONGOOSE_TEST_URI ,
2339 {
2440 autoEncryption : {
2541 keyVaultNamespace : 'keyvault.datakeys' ,
26- kmsProviders : { local : { key : Buffer . from (
27- 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk' ,
28- 'base64'
29- )
30- } } ,
42+ kmsProviders : { local : { key : LOCAL_KEY } } ,
43+ schemaMap : {
44+ 'db.coll' : {
45+ bsonType : 'object' ,
46+ encryptMetadata : {
47+ keyId : [ new mdb . UUID ( dataKey ) ]
48+ } ,
49+ properties : {
50+ a : {
51+ encrypt : {
52+ bsonType : 'int' ,
53+ algorithm : 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' ,
54+ keyId : [ new mdb . UUID ( dataKey ) ]
55+ }
56+ }
57+ }
58+ }
59+ } ,
3160 extraOptions : {
3261 cryptdSharedLibRequired : true ,
3362 cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
3463 }
3564 }
3665 }
3766 ) ;
38- await client . connect ( ) ;
39- const insertResult = await client
40- . db ( 'db' )
41- . collection ( 'coll' )
42- . insertOne ( { unencrypted : 'test' } ) ;
43- assert . ok ( insertResult . insertedId ) ;
67+
68+ dummyClient = new mdb . MongoClient ( process . env . MONGOOSE_TEST_URI ) ;
69+ } ) ;
70+
71+ afterEach ( async function ( ) {
72+ await keyVaultClient . close ( ) ;
73+ await encryptedClient . close ( ) ;
74+ await dummyClient . close ( ) ;
75+ } ) ;
76+
77+ it ( 'supports mongodb csfle auto-encryption integration' , async ( ) => {
78+ await encryptedClient . connect ( ) ;
79+ await encryptedClient . db ( 'db' ) . collection ( 'coll' ) . insertOne ( { a : 1 } ) ;
80+
81+ // a dummyClient not configured with autoEncryption, returns a encrypted binary type, meaning that encryption succeeded
82+ const encryptedCursor = await dummyClient . db ( 'db' ) . collection ( 'coll' ) . find ( ) ;
83+ const encryptedResult = await encryptedCursor . next ( ) ;
84+ assert . ok ( encryptedResult ) ;
85+ assert . ok ( encryptedResult . a ) ;
86+ assert . ok ( isBsonType ( encryptedResult . a , 'Binary' ) ) ;
87+ assert . ok ( encryptedResult . a . sub_type === 6 ) ;
88+
89+ // when the encryptedClient runs a find, the original unencrypted value is returned
90+ const unencryptedCursor = await encryptedClient . db ( 'db' ) . collection ( 'coll' ) . find ( ) ;
91+ const unencryptedResult = await unencryptedCursor . next ( ) ;
92+ assert . ok ( unencryptedResult ) ;
93+ assert . ok ( unencryptedResult . a === 1 ) ;
4494 } ) ;
4595} ) ;
0 commit comments