Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(NODE-6505): Setup CI #4

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1ccf38e
temp
aditi-khare-mongoDB Nov 19, 2024
aeda36b
temp
aditi-khare-mongoDB Nov 20, 2024
e5d8cad
temp 2
aditi-khare-mongoDB Nov 20, 2024
a041206
test
aditi-khare-mongoDB Nov 21, 2024
0c978a2
edit
aditi-khare-mongoDB Nov 21, 2024
f8cbb9a
removed errors
aditi-khare-mongoDB Nov 21, 2024
2fa3417
on push
aditi-khare-mongoDB Nov 21, 2024
c71d3c2
test
aditi-khare-mongoDB Nov 21, 2024
e6facfe
path
aditi-khare-mongoDB Nov 21, 2024
06f9758
path
aditi-khare-mongoDB Nov 21, 2024
86f7874
path
aditi-khare-mongoDB Nov 21, 2024
7a30734
path
aditi-khare-mongoDB Nov 21, 2024
b39c754
fixed
aditi-khare-mongoDB Nov 22, 2024
ccb726e
typo
aditi-khare-mongoDB Nov 22, 2024
b0a1c3c
install mocha
aditi-khare-mongoDB Nov 22, 2024
6198ade
fixed?
aditi-khare-mongoDB Nov 22, 2024
a135e79
fix tests
aditi-khare-mongoDB Nov 22, 2024
ca25868
run tests
aditi-khare-mongoDB Nov 22, 2024
270d151
ready for rereview
aditi-khare-mongoDB Nov 25, 2024
69dbda6
ready for rereview 2
aditi-khare-mongoDB Nov 25, 2024
3ce14a4
typo
aditi-khare-mongoDB Nov 25, 2024
eac708f
ready for review
aditi-khare-mongoDB Dec 10, 2024
f38366b
change to two files for local testing
aditi-khare-mongoDB Dec 10, 2024
62d18d8
fixed deps
aditi-khare-mongoDB Dec 10, 2024
40858d4
requested changes
aditi-khare-mongoDB Dec 11, 2024
d6044e7
fix
aditi-khare-mongoDB Dec 11, 2024
08f4c23
fix wording
aditi-khare-mongoDB Dec 18, 2024
1098636
change all occurences of encrypted-cluster to data
aditi-khare-mongoDB Dec 26, 2024
955cedf
remove extra gha call - use local script instead
aditi-khare-mongoDB Dec 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/encryption-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Encryption Tests

on:
push:
branches: ['master']
pull_request:
branches: [ 'master' ]
workflow_dispatch: {}

permissions:
contents: write
pull-requests: write
id-token: write

jobs:
run-tests:
permissions:
# required for all workflows
security-events: write
id-token: write
contents: write
runs-on: ubuntu-latest
name: Encryption tests
env:
FORCE_COLOR: true
steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
- name: Setup node
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: latest
- name: Install Dependencies
run: npm install
- name: Install mongodb-client-encryption
run: npm install mongodb-client-encryption
- name: Set up cluster
id: setup-cluster

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have tooling to run encryption tests using drivers-evergreen-tools - can we just use the same tooling in CI instead of using the github action?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

uses: mongodb-labs/drivers-evergreen-tools@master
with:
version: 8.0.0
topology: sharded_cluster
auth: auth
- name: Run Tests
run: npm run test-encryption
env:
MONGOOSE_TEST_URI: ${{ steps.setup-cluster.outputs.cluster-uri }}
CRYPT_SHARED_LIB_PATH: ${{ steps.setup-cluster.outputs.crypt-shared-lib-path }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ examples/ecommerce-netlify-functions/.netlify/state.json

notes.md
list.out

encrypted-cluster
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ If you have a question about Mongoose (not a bug report) please post it to eithe
* execute `npm run test-tsd` to run the typescript tests
* execute `npm run ts-benchmark` to run the typescript benchmark "performance test" for a single time.
* 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.
* in order to run tests that require an encrypted cluster locally, run `npm run test-encryption-local`. Alternatively, you can start an encrypted cluster using the `scripts/start-encrypted-cluster.sh` file.

## Documentation

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
"test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.js",
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
"test-tsd": "node ./test/types/check-types-filename && tsd",
"test-encryption": "mocha --exit ./test/encryption/*.test.js",
"test-encryption-local": "chmod +x scripts/run-encryption-tests-local.sh && scripts/run-encryption-tests-local.sh",
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
"test-coverage": "nyc --reporter=html --reporter=text npm test",
"ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check"
Expand Down
35 changes: 35 additions & 0 deletions scripts/run-encryption-tests-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
baileympearson marked this conversation as resolved.
Show resolved Hide resolved

# sets up an encrypted mongodb cluster

export CWD=$(pwd);

if [ -d "encrypted-cluster" ]; then
cd encrypted-cluster
else
source $CWD/scripts/start-encrypted-cluster.sh
fi

# IMPORTANT: extracts mongodb-uri, and starts the cluster of servers, store the uri for GitHub output
baileympearson marked this conversation as resolved.
Show resolved Hide resolved

read -r -d '' SOURCE_SCRIPT << EOM
const fs = require('fs');
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
const file = fs.readFileSync('mo-expansion.yml', { encoding: 'utf-8' })
.trim().split('\\n');
const regex = /^(?<key>.*): "(?<value>.*)"$/;
const variables = file.map(
(line) => regex.exec(line.trim()).groups
).map(
({key, value}) => \`export \${key}='\${value}'\`
).join('\n');

process.stdout.write(variables);
process.stdout.write('\n');
EOM

node --eval "$SOURCE_SCRIPT" | tee expansions.sh
source expansions.sh

export MONGOOSE_TEST_URI=$MONGODB_URI

npm run test-encryption
27 changes: 27 additions & 0 deletions scripts/start-encrypted-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

export CWD=$(pwd);
mkdir encrypted-cluster
cd encrypted-cluster

if [ ! -d "drivers-evergreen-tools/" ]; then
git clone --depth=1 "https://github.com/mongodb-labs/drivers-evergreen-tools.git"
fi

export DRIVERS_TOOLS=$CWD/encrypted-cluster/drivers-evergreen-tools
export MONGODB_VERSION=8.0
export AUTH=true
export MONGODB_BINARIES=$DRIVERS_TOOLS/mongodb/bin
export NODE_DRIVER=~/dev/node-mongodb-native
aditi-khare-mongoDB marked this conversation as resolved.
Show resolved Hide resolved
export MONGO_ORCHESTRATION_HOME=$DRIVERS_TOOLS/mo
export PROJECT_ORCHESTRATION_HOME=$DRIVERS_TOOLS/.evergreen/orchestration
export TOPOLOGY=sharded_cluster
export SSL=nossl

cd $DRIVERS_TOOLS
rm -rf mongosh mongodb mo
mkdir mo
cd -

rm expansions.sh 2> /dev/null

bash $DRIVERS_TOOLS/.evergreen/run-orchestration.sh
97 changes: 97 additions & 0 deletions test/encryption/encryption.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

const assert = require('assert');
const mdb = require('mongodb');
const isBsonType = require('../../lib/helpers/isBsonType');

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

describe('environmental variables', () => {
it('MONGOOSE_TEST_URI is set', async function() {
const uri = process.env.MONGOOSE_TEST_URI;
assert.ok(uri);
});

it('CRYPT_SHARED_LIB_PATH is set', async function() {
const shared_library_path = process.env.CRYPT_SHARED_LIB_PATH;
assert.ok(shared_library_path);
});
});

describe('basic integration', () => {
let keyVaultClient;
let dataKey;
let encryptedClient;
let dummyClient;

beforeEach(async function() {
keyVaultClient = new mdb.MongoClient(process.env.MONGOOSE_TEST_URI);
await keyVaultClient.connect();
await keyVaultClient.db('keyvault').collection('datakeys');
const clientEncryption = new mdb.ClientEncryption(keyVaultClient, {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { local: { key: LOCAL_KEY } }
});
dataKey = await clientEncryption.createDataKey('local');

encryptedClient = new mdb.MongoClient(
process.env.MONGOOSE_TEST_URI,
{
autoEncryption: {
keyVaultNamespace: 'keyvault.datakeys',
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
kmsProviders: { local: { key: LOCAL_KEY } },
schemaMap: {
'db.coll': {
bsonType: 'object',
encryptMetadata: {
keyId: [dataKey]
},
properties: {
a: {
encrypt: {
bsonType: 'int',
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random',
keyId: [dataKey]
}
}
}
}
},
extraOptions: {
cryptdSharedLibRequired: true,
cryptSharedLibPath: process.env.CRYPT_SHARED_LIB_PATH
}
}
}
);

dummyClient = new mdb.MongoClient(process.env.MONGOOSE_TEST_URI);
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
});

afterEach(async function() {
await keyVaultClient.close();
await encryptedClient.close();
await dummyClient.close();
});

it('supports mongodb csfle auto-encryption integration', async() => {
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
await encryptedClient.connect();
await encryptedClient.db('db').collection('coll').insertOne({ a: 1 });
baileympearson marked this conversation as resolved.
Show resolved Hide resolved

const { insertedId } = await encryptedClient.db('db').collection('coll').insertOne({ a: 1 });

// a dummyClient not configured with autoEncryption, returns a encrypted binary type, meaning that encryption succeeded
const encryptedResult = await dummyClient.db('db').collection('coll').findOne({ _id: insertedId });

assert.ok(encryptedResult);
assert.ok(encryptedResult.a);
assert.ok(isBsonType(encryptedResult.a, 'Binary'));
assert.ok(encryptedResult.a.sub_type === 6);

// when the encryptedClient runs a find, the original unencrypted value is returned
const unencryptedCursor = await encryptedClient.db('db').collection('coll').find();
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
const unencryptedResult = await unencryptedCursor.next();
assert.ok(unencryptedResult);
assert.ok(unencryptedResult.a === 1);
});
});
Loading