@@ -9,6 +9,7 @@ const fs = require('fs');
99const mongoose = require ( '../../lib' ) ;
1010const { Map } = require ( '../../lib/types' ) ;
1111const { join } = require ( 'path' ) ;
12+ const { readFile } = require ( 'fs/promises' ) ;
1213
1314const LOCAL_KEY = Buffer . from ( 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk' , 'base64' ) ;
1415
@@ -32,38 +33,40 @@ function isEncryptedValue(object, property) {
3233}
3334
3435describe ( 'encryption integration tests' , ( ) => {
36+ /** @type { string } */
37+ let cryptSharedLibPath ;
38+ /** @type { string } */
39+ let clusterUri ;
3540 before ( async function ( ) {
36- const expansionFile = join ( __dirname , '../..' , 'mo-expansion.yml ' ) ;
41+ const expansionFile = join ( __dirname , '../..' , 'fle-cluster-config.json ' ) ;
3742 if ( ! exists ( expansionFile ) ) {
3843 throw new Error ( 'must setup a cluster using `npm run setup-test-encryption`.' ) ;
3944 }
4045
41- const lines = fs . readFileSync ( expansionFile , { encoding : 'utf-8' } ) . trim ( ) . split ( '\n' ) ;
46+ /** @type { { uri: string, cryptShared: string } } */
47+ const configuration = JSON . parse ( await readFile ( expansionFile , { encoding : 'utf-8' } ) ) ;
4248
43- const regex = / ^ (?< key > .* ) : " (?< value > .* ) " $ / ;
44- const variables = Object . fromEntries ( lines . map ( ( line ) => regex . exec ( line . trim ( ) ) . groups ) . map ( ( { key, value } ) => [ key , value ] ) ) ;
45-
46- process . env . CRYPT_SHARED_LIB_PATH = variables . CRYPT_SHARED_LIB_PATH ;
47- process . env . MONGOOSE_TEST_URI = variables . MONGODB_URI ;
49+ cryptSharedLibPath = configuration . cryptShared ;
50+ clusterUri = configuration . uri ;
4851 } ) ;
4952
5053 describe ( 'meta: environmental variables are correctly set up' , ( ) => {
51- it ( 'MONGOOSE_TEST_URI is set' , async function ( ) {
52- const uri = process . env . MONGOOSE_TEST_URI ;
54+ it ( 'clusterUri is set' , async function ( ) {
55+ const uri = clusterUri ;
5356 assert . ok ( uri ) ;
5457 } ) ;
5558
56- it ( 'MONGOOSE_TEST_URI points to running cluster' , async function ( ) {
59+ it ( 'clusterUri points to running cluster' , async function ( ) {
5760 try {
58- const connection = await mongoose . connect ( process . env . MONGOOSE_TEST_URI ) ;
61+ const connection = await mongoose . connect ( clusterUri ) ;
5962 await connection . disconnect ( ) ;
6063 } catch ( error ) {
6164 throw new Error ( 'Unable to connect to running cluster' , { cause : error } ) ;
6265 }
6366 } ) ;
6467
65- it ( 'CRYPT_SHARED_LIB_PATH is set' , async function ( ) {
66- const shared_library_path = process . env . CRYPT_SHARED_LIB_PATH ;
68+ it ( 'cryptSharedLibPath is set' , async function ( ) {
69+ const shared_library_path = cryptSharedLibPath ;
6770 assert . ok ( shared_library_path ) ;
6871 } ) ;
6972 } ) ;
@@ -75,7 +78,7 @@ describe('encryption integration tests', () => {
7578 let utilClient ;
7679
7780 beforeEach ( async function ( ) {
78- const keyVaultClient = new mdb . MongoClient ( process . env . MONGOOSE_TEST_URI ) ;
81+ const keyVaultClient = new mdb . MongoClient ( clusterUri ) ;
7982 await keyVaultClient . connect ( ) ;
8083 await keyVaultClient . db ( 'keyvault' ) . collection ( 'datakeys' ) ;
8184 const clientEncryption = new mdb . ClientEncryption ( keyVaultClient , {
@@ -87,7 +90,7 @@ describe('encryption integration tests', () => {
8790 keyId3 = await clientEncryption . createDataKey ( 'local' ) ;
8891 await keyVaultClient . close ( ) ;
8992
90- utilClient = new mdb . MongoClient ( process . env . MONGOOSE_TEST_URI ) ;
93+ utilClient = new mdb . MongoClient ( clusterUri ) ;
9194 } ) ;
9295
9396 afterEach ( async function ( ) {
@@ -148,13 +151,13 @@ describe('encryption integration tests', () => {
148151
149152 connection = createConnection ( ) ;
150153 model = connection . model ( 'Schema' , schema ) ;
151- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
154+ await connection . openUri ( clusterUri , {
152155 dbName : 'db' , autoEncryption : {
153156 keyVaultNamespace : 'keyvault.datakeys' ,
154157 kmsProviders : { local : { key : LOCAL_KEY } } ,
155158 extraOptions : {
156159 cryptdSharedLibRequired : true ,
157- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
160+ cryptSharedLibPath : cryptSharedLibPath
158161 }
159162 }
160163 } ) ;
@@ -175,13 +178,13 @@ describe('encryption integration tests', () => {
175178
176179 connection = createConnection ( ) ;
177180 model = connection . model ( 'Schema' , schema ) ;
178- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
181+ await connection . openUri ( clusterUri , {
179182 dbName : 'db' , autoEncryption : {
180183 keyVaultNamespace : 'keyvault.datakeys' ,
181184 kmsProviders : { local : { key : LOCAL_KEY } } ,
182185 extraOptions : {
183186 cryptdSharedLibRequired : true ,
184- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
187+ cryptSharedLibPath : cryptSharedLibPath
185188 }
186189 }
187190 } ) ;
@@ -207,13 +210,13 @@ describe('encryption integration tests', () => {
207210
208211 connection = createConnection ( ) ;
209212 const model = connection . model ( 'Schema' , schema ) ;
210- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
213+ await connection . openUri ( clusterUri , {
211214 dbName : 'db' , autoEncryption : {
212215 keyVaultNamespace : 'keyvault.datakeys' ,
213216 kmsProviders : { local : { key : LOCAL_KEY } } ,
214217 extraOptions : {
215218 cryptdSharedLibRequired : true ,
216- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
219+ cryptSharedLibPath : cryptSharedLibPath
217220 }
218221 }
219222 } ) ;
@@ -248,13 +251,13 @@ describe('encryption integration tests', () => {
248251
249252 connection = createConnection ( ) ;
250253 const model = connection . model ( 'Schema' , schema ) ;
251- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
254+ await connection . openUri ( clusterUri , {
252255 dbName : 'db' , autoEncryption : {
253256 keyVaultNamespace : 'keyvault.datakeys' ,
254257 kmsProviders : { local : { key : LOCAL_KEY } } ,
255258 extraOptions : {
256259 cryptdSharedLibRequired : true ,
257- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
260+ cryptSharedLibPath : cryptSharedLibPath
258261 }
259262 }
260263 } ) ;
@@ -375,13 +378,13 @@ describe('encryption integration tests', () => {
375378 it ( 'encrypts and decrypts' , async function ( ) {
376379 const { model } = modelFactory ( ) ;
377380
378- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
381+ await connection . openUri ( clusterUri , {
379382 dbName : 'db' , autoEncryption : {
380383 keyVaultNamespace : 'keyvault.datakeys' ,
381384 kmsProviders : { local : { key : LOCAL_KEY } } ,
382385 extraOptions : {
383386 cryptdSharedLibRequired : true ,
384- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
387+ cryptSharedLibPath : cryptSharedLibPath
385388 }
386389 }
387390 } ) ;
@@ -416,13 +419,13 @@ describe('encryption integration tests', () => {
416419
417420 connection = createConnection ( ) ;
418421 const model = connection . model ( 'Schema' , schema ) ;
419- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
422+ await connection . openUri ( clusterUri , {
420423 dbName : 'db' , autoEncryption : {
421424 keyVaultNamespace : 'keyvault.datakeys' ,
422425 kmsProviders : { local : { key : LOCAL_KEY } } ,
423426 extraOptions : {
424427 cryptdSharedLibRequired : true ,
425- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
428+ cryptSharedLibPath : cryptSharedLibPath
426429 }
427430 }
428431 } ) ;
@@ -455,13 +458,13 @@ describe('encryption integration tests', () => {
455458
456459 connection = createConnection ( ) ;
457460 const model = connection . model ( 'Schema' , schema ) ;
458- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
461+ await connection . openUri ( clusterUri , {
459462 dbName : 'db' , autoEncryption : {
460463 keyVaultNamespace : 'keyvault.datakeys' ,
461464 kmsProviders : { local : { key : LOCAL_KEY } } ,
462465 extraOptions : {
463466 cryptdSharedLibRequired : true ,
464- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
467+ cryptSharedLibPath : cryptSharedLibPath
465468 }
466469 }
467470 } ) ;
@@ -491,13 +494,13 @@ describe('encryption integration tests', () => {
491494 } ) ;
492495
493496 connection = createConnection ( ) ;
494- const model = connection . model ( 'Schema' , schema ) ; await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
497+ const model = connection . model ( 'Schema' , schema ) ; await connection . openUri ( clusterUri , {
495498 dbName : 'db' , autoEncryption : {
496499 keyVaultNamespace : 'keyvault.datakeys' ,
497500 kmsProviders : { local : { key : LOCAL_KEY } } ,
498501 extraOptions : {
499502 cryptdSharedLibRequired : true ,
500- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
503+ cryptSharedLibPath : cryptSharedLibPath
501504 }
502505 }
503506 } ) ;
@@ -529,13 +532,13 @@ describe('encryption integration tests', () => {
529532
530533 connection = createConnection ( ) ;
531534 const model = connection . model ( 'Schema' , schema ) ;
532- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
535+ await connection . openUri ( clusterUri , {
533536 dbName : 'db' , autoEncryption : {
534537 keyVaultNamespace : 'keyvault.datakeys' ,
535538 kmsProviders : { local : { key : LOCAL_KEY } } ,
536539 extraOptions : {
537540 cryptdSharedLibRequired : true ,
538- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
541+ cryptSharedLibPath : cryptSharedLibPath
539542 }
540543 }
541544 } ) ;
@@ -619,13 +622,13 @@ describe('encryption integration tests', () => {
619622 describe ( description , function ( ) {
620623 it ( 'encrypts and decrypts' , async function ( ) {
621624 const { model } = modelFactory ( ) ;
622- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
625+ await connection . openUri ( clusterUri , {
623626 dbName : 'db' , autoEncryption : {
624627 keyVaultNamespace : 'keyvault.datakeys' ,
625628 kmsProviders : { local : { key : LOCAL_KEY } } ,
626629 extraOptions : {
627630 cryptdSharedLibRequired : true ,
628- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
631+ cryptSharedLibPath : cryptSharedLibPath
629632 }
630633 }
631634 } ) ;
@@ -710,13 +713,13 @@ describe('encryption integration tests', () => {
710713 describe ( description , function ( ) {
711714 it ( 'encrypts and decrypts' , async function ( ) {
712715 const { model1, model2 } = modelFactory ( ) ;
713- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
716+ await connection . openUri ( clusterUri , {
714717 dbName : 'db' , autoEncryption : {
715718 keyVaultNamespace : 'keyvault.datakeys' ,
716719 kmsProviders : { local : { key : LOCAL_KEY } } ,
717720 extraOptions : {
718721 cryptdSharedLibRequired : true ,
719- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
722+ cryptSharedLibPath : cryptSharedLibPath
720723 }
721724 }
722725 } ) ;
@@ -771,13 +774,13 @@ describe('encryption integration tests', () => {
771774 } , {
772775 encryptionType : 'csfle'
773776 } ) ) ;
774- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
777+ await connection . openUri ( clusterUri , {
775778 dbName : 'db' , autoEncryption : {
776779 keyVaultNamespace : 'keyvault.datakeys' ,
777780 kmsProviders : { local : { key : LOCAL_KEY } } ,
778781 extraOptions : {
779782 cryptdSharedLibRequired : true ,
780- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
783+ cryptSharedLibPath : cryptSharedLibPath
781784 }
782785 }
783786 } ) ;
@@ -838,13 +841,13 @@ describe('encryption integration tests', () => {
838841 } ) ) ;
839842
840843
841- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
844+ await connection . openUri ( clusterUri , {
842845 dbName : 'db' , autoEncryption : {
843846 keyVaultNamespace : 'keyvault.datakeys' ,
844847 kmsProviders : { local : { key : LOCAL_KEY } } ,
845848 extraOptions : {
846849 cryptdSharedLibRequired : true ,
847- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
850+ cryptSharedLibPath : cryptSharedLibPath
848851 }
849852 }
850853 } ) ;
@@ -919,13 +922,13 @@ describe('encryption integration tests', () => {
919922 encryptionType : 'queryableEncryption'
920923 } ) ) ;
921924
922- await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
925+ await connection . openUri ( clusterUri , {
923926 dbName : 'db' , autoEncryption : {
924927 keyVaultNamespace : 'keyvault.datakeys' ,
925928 kmsProviders : { local : { key : LOCAL_KEY } } ,
926929 extraOptions : {
927930 cryptdSharedLibRequired : true ,
928- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
931+ cryptSharedLibPath : cryptSharedLibPath
929932 }
930933 }
931934 } ) ;
@@ -1001,13 +1004,13 @@ describe('encryption integration tests', () => {
10011004 encryptionType : 'csfle'
10021005 } ) ) ;
10031006
1004- const error = await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
1007+ const error = await connection . openUri ( clusterUri , {
10051008 dbName : 'db' , autoEncryption : {
10061009 keyVaultNamespace : 'keyvault.datakeys' ,
10071010 kmsProviders : { local : { key : LOCAL_KEY } } ,
10081011 extraOptions : {
10091012 cryptdSharedLibRequired : true ,
1010- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
1013+ cryptSharedLibPath : cryptSharedLibPath
10111014 }
10121015 }
10131016 } ) . catch ( e => e ) ;
@@ -1062,13 +1065,13 @@ describe('encryption integration tests', () => {
10621065 encryptionType : 'queryableEncryption'
10631066 } ) ) ;
10641067
1065- const error = await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
1068+ const error = await connection . openUri ( clusterUri , {
10661069 dbName : 'db' , autoEncryption : {
10671070 keyVaultNamespace : 'keyvault.datakeys' ,
10681071 kmsProviders : { local : { key : LOCAL_KEY } } ,
10691072 extraOptions : {
10701073 cryptdSharedLibRequired : true ,
1071- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
1074+ cryptSharedLibPath : cryptSharedLibPath
10721075 }
10731076 }
10741077 } ) . catch ( e => e ) ;
@@ -1251,13 +1254,13 @@ describe('encryption integration tests', () => {
12511254 } ) ;
12521255
12531256 const model = mongoose . model ( 'Schema' , schema ) ;
1254- await mongoose . connect ( process . env . MONGOOSE_TEST_URI , {
1257+ await mongoose . connect ( clusterUri , {
12551258 dbName : 'db' , autoEncryption : {
12561259 keyVaultNamespace : 'keyvault.datakeys' ,
12571260 kmsProviders : { local : { key : LOCAL_KEY } } ,
12581261 extraOptions : {
12591262 cryptdSharedLibRequired : true ,
1260- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
1263+ cryptSharedLibPath : cryptSharedLibPath
12611264 }
12621265 }
12631266 } ) ;
@@ -1291,13 +1294,13 @@ describe('encryption integration tests', () => {
12911294 } ) ;
12921295
12931296 const model = mongoose . model ( 'Schema' , schema ) ;
1294- await mongoose . connect ( process . env . MONGOOSE_TEST_URI , {
1297+ await mongoose . connect ( clusterUri , {
12951298 dbName : 'db' , autoEncryption : {
12961299 keyVaultNamespace : 'keyvault.datakeys' ,
12971300 kmsProviders : { local : { key : LOCAL_KEY } } ,
12981301 extraOptions : {
12991302 cryptdSharedLibRequired : true ,
1300- cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
1303+ cryptSharedLibPath : cryptSharedLibPath
13011304 }
13021305 }
13031306 } ) ;
0 commit comments