Skip to content

Commit 464107c

Browse files
most of Val's comments addressed
1 parent 955cedf commit 464107c

File tree

5 files changed

+35
-30
lines changed

5 files changed

+35
-30
lines changed

.github/workflows/encryption-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,5 @@ jobs:
3131
node-version: latest
3232
- name: Install Dependencies
3333
run: npm install
34-
- name: Install mongodb-client-encryption
35-
run: npm install mongodb-client-encryption
3634
- name: Run Tests
3735
run: npm run test-encryption

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ If you have a question about Mongoose (not a bug report) please post it to eithe
4646
* execute `npm run test-tsd` to run the typescript tests
4747
* execute `npm run ts-benchmark` to run the typescript benchmark "performance test" for a single time.
4848
* execute `npm run ts-benchmark-watch` to run the typescript benchmark "performance test" while watching changes on types folder. Note: Make sure to commit all changes before executing this command.
49-
* in order to run tests that require an cluster with encryption locally, run `npm run test-encryption`. Alternatively, you can start an encrypted cluster using the `scripts/configure-cluster-with-encryption.sh` file.
49+
* in order to run tests that require an cluster with encryption locally, run `npm run test-encryption`. Alternatively, you can start an encrypted cluster using the `scripts/configure-cluster-with-encryption.sh` file.
50+
* These scripts can take a few minutes to run. If a encryption script is exited prematurely, restart the shell and delete the `data/` directory to ensure clean-up.
5051

5152
## Documentation
5253

scripts/configure-cluster-with-encryption.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export CWD=$(pwd);
88
mkdir data
99
cd data
1010

11+
# install encryption dependency
12+
npm install mongodb-client-encryption > /dev/null
13+
1114
# note:
1215
# we're using drivers-evergreen-tools which is a repo used by MongoDB drivers to start clusters for testing.
1316
# if you'd like to make changes to the cluster settings, edit the exported variables below.

scripts/run-encryption-tests.sh

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,20 @@
44

55
export CWD=$(pwd);
66

7+
# install encryption dependency
8+
npm install mongodb-client-encryption > /dev/null
9+
710
# set up mongodb cluster and encryption configuration if the data/ folder does not exist
811
# note: for tooling, cluster set-up and configuration look into the 'scripts/configure-cluster-with-encryption.sh' script
9-
1012
if [ -d "data" ]; then
1113
cd data
1214
else
1315
source $CWD/scripts/configure-cluster-with-encryption.sh
1416
fi
1517

16-
# extracts MONGOOSE_TEST_URI and CRYPT_SHARED_LIB_PATH from .yml file into environment variables for this test run
17-
read -r -d '' SOURCE_SCRIPT << EOM
18-
const fs = require('fs');
19-
const file = fs.readFileSync('mo-expansion.yml', { encoding: 'utf-8' })
20-
.trim().split('\\n');
21-
const regex = /^(?<key>.*): "(?<value>.*)"$/;
22-
const variables = file.map(
23-
(line) => regex.exec(line.trim()).groups
24-
).map(
25-
({key, value}) => \`export \${key}='\${value}'\`
26-
).join('\n');
27-
28-
process.stdout.write(variables);
29-
process.stdout.write('\n');
30-
EOM
31-
32-
node --eval "$SOURCE_SCRIPT" | tee expansions.sh
33-
source expansions.sh
34-
35-
export MONGOOSE_TEST_URI=$MONGODB_URI
36-
3718
# run encryption tests
3819
cd ..
3920
npx mocha --exit ./test/encryption/*.test.js
21+
22+
# uninstall encryption dependency
23+
npm uninstall mongodb-client-encryption > /dev/null

test/encryption/encryption.test.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
'use strict';
22

33
const assert = require('assert');
4-
const mdb = require('mongodb');
4+
const mongodb = require('mongodb');
5+
const fs = require('fs');
56
const isBsonType = require('../../lib/helpers/isBsonType');
67

78
const LOCAL_KEY = Buffer.from('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk', 'base64');
89

910
describe('ci', () => {
11+
12+
const cachedUri = process.env.MONGOOSE_TEST_URI;
13+
const cachedLib = process.env.CRYPT_SHARED_LIB_PATH;
14+
15+
before(function() {
16+
const cwd = process.cwd();
17+
const file = fs.readFileSync(cwd + '/data/mo-expansion.yml', { encoding: 'utf-8' }).trim().split('\n');
18+
const regex = /^(?<key>.*): "(?<value>.*)"$/;
19+
const variables = file.map((line) => regex.exec(line.trim()).groups).reduce((acc, { key, value }) => ({ ...acc, [key]: value }), {});
20+
process.env.CRYPT_SHARED_LIB_PATH = variables.CRYPT_SHARED_LIB_PATH;
21+
process.env.MONGOOSE_TEST_URI = variables.MONGODB_URI;
22+
});
23+
24+
after(function() {
25+
process.env.CRYPT_SHARED_LIB_PATH = cachedLib;
26+
process.env.MONGOOSE_TEST_URI = cachedUri;
27+
});
28+
1029
describe('environmental variables', () => {
1130
it('MONGOOSE_TEST_URI is set', async function() {
1231
const uri = process.env.MONGOOSE_TEST_URI;
@@ -26,16 +45,16 @@ describe('ci', () => {
2645
let unencryptedClient;
2746

2847
beforeEach(async function() {
29-
keyVaultClient = new mdb.MongoClient(process.env.MONGOOSE_TEST_URI);
48+
keyVaultClient = new mongodb.MongoClient(process.env.MONGOOSE_TEST_URI);
3049
await keyVaultClient.connect();
3150
await keyVaultClient.db('keyvault').collection('datakeys');
32-
const clientEncryption = new mdb.ClientEncryption(keyVaultClient, {
51+
const clientEncryption = new mongodb.ClientEncryption(keyVaultClient, {
3352
keyVaultNamespace: 'keyvault.datakeys',
3453
kmsProviders: { local: { key: LOCAL_KEY } }
3554
});
3655
dataKey = await clientEncryption.createDataKey('local');
3756

38-
encryptedClient = new mdb.MongoClient(
57+
encryptedClient = new mongodb.MongoClient(
3958
process.env.MONGOOSE_TEST_URI,
4059
{
4160
autoEncryption: {
@@ -66,7 +85,7 @@ describe('ci', () => {
6685
}
6786
);
6887

69-
unencryptedClient = new mdb.MongoClient(process.env.MONGOOSE_TEST_URI);
88+
unencryptedClient = new mongodb.MongoClient(process.env.MONGOOSE_TEST_URI);
7089
});
7190

7291
afterEach(async function() {

0 commit comments

Comments
 (0)