Skip to content

Commit 270d151

Browse files
ready for rereview
1 parent ca25868 commit 270d151

File tree

4 files changed

+72
-37
lines changed

4 files changed

+72
-37
lines changed

.github/scripts/run-kms-servers.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/workflows/encryption-tests.yml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
name: Encryption Tests
22

33
on:
4-
push
5-
#workflow_dispatch: {}
4+
push:
5+
branches: ['master']
6+
workflow_dispatch: {}
67

78
permissions:
89
contents: write
@@ -16,13 +17,7 @@ jobs:
1617
security-events: write
1718
id-token: write
1819
contents: write
19-
runs-on: ${{ matrix.os }}
20-
strategy:
21-
fail-fast: false
22-
matrix:
23-
node: [20]
24-
os: [ubuntu-latest]
25-
mongodb: [8.0.0]
20+
runs-on: ubuntu-latest
2621
name: Encryption tests
2722
env:
2823
FORCE_COLOR: true
@@ -40,11 +35,11 @@ jobs:
4035
id: setup-cluster
4136
uses: mongodb-labs/drivers-evergreen-tools@master
4237
with:
43-
version: ${{ matrix.mongodb }}
38+
version: 8.0.0
4439
topology: sharded_cluster
4540
auth: auth
4641
- name: Run Tests
47-
run: npx mocha --exit ./test/encryption/*.test.js
42+
run: npm run encryption-test
4843
env:
4944
MONGOOSE_TEST_URI: ${{ steps.setup-cluster.outputs.cluster-uri }}
5045
CRYPT_SHARED_LIB_PATH: ${{ steps.setup-cluster.outputs.crypt-shared-lib-path }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.js",
106106
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
107107
"test-tsd": "node ./test/types/check-types-filename && tsd",
108+
"test-encryption": "mocha --exit ./test/encryption/*.test.js",
108109
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
109110
"test-coverage": "nyc --reporter=html --reporter=text npm test",
110111
"ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check"

test/encryption/encryption.test.js

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
const assert = require('assert');
44
const mdb = require('mongodb');
5+
const isBsonType = require('../../lib/helpers/isBsonType');
6+
7+
const LOCAL_KEY = Buffer.from('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk', 'base64');
58

69
describe('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

1821
describe('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

Comments
 (0)