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

fix(NODE-5262, NODE-5311): batch backport #3683

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2448,7 +2448,7 @@ tasks:
- func: bootstrap kms servers
- func: run custom csfle tests
vars:
CSFLE_GIT_REF: 1524eac203e4145e9f4835e519f1e4663ff15953
CSFLE_GIT_REF: c56c70340093070b1ef5c8a28190187eea21a6e9
- name: run-custom-csfle-tests-5.0-master
tags:
- run-custom-dependency-tests
Expand Down Expand Up @@ -2478,7 +2478,7 @@ tasks:
- func: bootstrap kms servers
- func: run custom csfle tests
vars:
CSFLE_GIT_REF: 1524eac203e4145e9f4835e519f1e4663ff15953
CSFLE_GIT_REF: c56c70340093070b1ef5c8a28190187eea21a6e9
- name: run-custom-csfle-tests-rapid-master
tags:
- run-custom-dependency-tests
Expand Down Expand Up @@ -2508,7 +2508,7 @@ tasks:
- func: bootstrap kms servers
- func: run custom csfle tests
vars:
CSFLE_GIT_REF: 1524eac203e4145e9f4835e519f1e4663ff15953
CSFLE_GIT_REF: c56c70340093070b1ef5c8a28190187eea21a6e9
- name: run-custom-csfle-tests-latest-master
tags:
- run-custom-dependency-tests
Expand Down
2 changes: 1 addition & 1 deletion .evergreen/generate_evergreen_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ const oneOffFuncAsTasks = oneOffFuncs.map(oneOffFunc => ({
]
}));

const FLE_PINNED_COMMIT = '1524eac203e4145e9f4835e519f1e4663ff15953'
const FLE_PINNED_COMMIT = 'c56c70340093070b1ef5c8a28190187eea21a6e9';

for (const version of ['5.0', 'rapid', 'latest']) {
for (const ref of [FLE_PINNED_COMMIT, 'master']) {
Expand Down
6 changes: 2 additions & 4 deletions .evergreen/run-custom-csfle-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ popd # mongo-c-driver

pushd libmongocrypt/bindings/node

# TODO(NODE-5180): remove --force option
npm install --force --production --ignore-scripts
npm install --production --ignore-scripts
bash ./etc/build-static.sh

popd # libmongocrypt/bindings/node
Expand Down Expand Up @@ -82,8 +81,7 @@ pushd ../csfle-deps-tmp/libmongocrypt/bindings/node
killall mongocryptd || true

# only prod deps were installed earlier, install devDependencies here (except for mongodb!)
# TODO(NODE-5180): remove --force option
npm install --force --ignore-scripts
npm install --ignore-scripts

# copy mongodb into CSFLE's node_modules
rm -rf node_modules/mongodb
Expand Down
3 changes: 1 addition & 2 deletions .evergreen/run-serverless-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ if [ -z ${MONGODB_URI+omitted} ]; then echo "MONGODB_URI is unset" && exit 1; fi
if [ -z ${SERVERLESS_ATLAS_USER+omitted} ]; then echo "SERVERLESS_ATLAS_USER is unset" && exit 1; fi
if [ -z ${SERVERLESS_ATLAS_PASSWORD+omitted} ]; then echo "SERVERLESS_ATLAS_PASSWORD is unset" && exit 1; fi

# TODO(NODE-5180): remove --force option
npm install --force 'mongodb-client-encryption@alpha'
npm install mongodb-client-encryption

npx mocha \
--config test/mocha_mongodb.json \
Expand Down
7 changes: 3 additions & 4 deletions .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ else
source "$DRIVERS_TOOLS"/.evergreen/csfle/set-temp-creds.sh
fi

# TODO(NODE-5180): remove --force option
npm install --force 'mongodb-client-encryption@alpha'
npm install --force @mongodb-js/zstd
npm install --force snappy
npm install mongodb-client-encryption
npm install @mongodb-js/zstd
npm install snappy

export AUTH=$AUTH
export SINGLE_MONGOS_LB_URI=${SINGLE_MONGOS_LB_URI}
Expand Down
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare global {
clientSideEncryption?: boolean;
serverless?: 'forbid' | 'allow' | 'require';
auth?: 'enabled' | 'disabled';
nodejs?: string;
};

sessions?: {
Expand Down
3 changes: 2 additions & 1 deletion src/cmap/handshake/client_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ export function getFAASEnv(): Map<string, string | Int32> | null {
VERCEL_REGION = ''
} = process.env;

const isAWSFaaS = AWS_EXECUTION_ENV.length > 0 || AWS_LAMBDA_RUNTIME_API.length > 0;
const isAWSFaaS =
AWS_EXECUTION_ENV.startsWith('AWS_Lambda_') || AWS_LAMBDA_RUNTIME_API.length > 0;
const isAzureFaaS = FUNCTIONS_WORKER_RUNTIME.length > 0;
const isGCPFaaS = K_SERVICE.length > 0 || FUNCTION_NAME.length > 0;
const isVercelFaaS = VERCEL.length > 0;
Expand Down
23 changes: 20 additions & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export interface ErrorDescription extends Document {
errInfo?: Document;
}

function isAggregateError(e: Error): e is Error & { errors: Error[] } {
return 'errors' in e && Array.isArray(e.errors);
}

/**
* @public
* @category Error
Expand All @@ -130,15 +134,28 @@ export class MongoError extends Error {
cause?: Error; // depending on the node version, this may or may not exist on the base

constructor(message: string | Error) {
super(MongoError.buildErrorMessage(message));
if (message instanceof Error) {
super(message.message);
this.cause = message;
} else {
super(message);
}

this[kErrorLabels] = new Set();
}

/** @internal */
private static buildErrorMessage(e: Error | string): string {
if (typeof e === 'string') {
return e;
}
if (isAggregateError(e) && e.message.length === 0) {
return e.errors.length === 0
? 'AggregateError has an empty errors array. Please check the `cause` property for more information.'
: e.errors.map(({ message }) => message).join(', ');
}

return e.message;
}

override get name(): string {
return 'MongoError';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ describe('Handshake Prose Tests', function () {
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'big']
]
},
{
expectedProvider: undefined,
context: '8. Invalid - AWS_EXECUTION_ENV does not start with "AWS_Lambda_"',
env: [['AWS_EXECUTION_ENV', 'EC2']]
}
];

Expand Down
54 changes: 54 additions & 0 deletions test/integration/node-specific/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from 'chai';

import { MongoClient, MongoError, MongoServerSelectionError } from '../../../src';

describe('Error (Integration)', function () {
describe('AggregateErrors', function () {
for (const { errors, message } of [
{
errors: [],
message:
'AggregateError has an empty errors array. Please check the `cause` property for more information.'
},
{ errors: [new Error('message 1')], message: 'message 1' },
{
errors: [new Error('message 1'), new Error('message 2')],
message: 'message 1, message 2'
}
]) {
it(
`constructs the message properly with an array of ${errors.length} errors`,
{ requires: { nodejs: '>=16' } },
() => {
const error = new AggregateError(errors);
const mongoError = new MongoError(error);

expect(mongoError.message).to.equal(message);
}
);
}

context('when the message on the AggregateError is non-empty', () => {
it(`uses the AggregateError's message`, { requires: { nodejs: '>=16' } }, () => {
const error = new AggregateError([new Error('non-empty')]);
error.message = 'custom error message';
const mongoError = new MongoError(error);
expect(mongoError.message).to.equal('custom error message');
});
});

it('sets the AggregateError to the cause property', { requires: { nodejs: '>=16' } }, () => {
const error = new AggregateError([new Error('error 1')]);
const mongoError = new MongoError(error);
expect(mongoError.cause).to.equal(error);
});
});

it('NODE-5296: handles aggregate errors from dns lookup', async function () {
const error = await MongoClient.connect('mongodb://localhost:27222', {
serverSelectionTimeoutMS: 1000
}).catch(e => e);
expect(error).to.be.instanceOf(MongoServerSelectionError);
expect(error.message).not.to.be.empty;
});
});
25 changes: 25 additions & 0 deletions test/tools/runner/filters/node_version_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const { satisfies } = require('semver');

/**
* Filter for specific nodejs versions
*
* example:
* metadata: {
* requires: {
* nodejs: '>=14'
* }
* }
*/
class NodeVersionFilter {
filter(test) {
if (!test.metadata) return true;
if (!test.metadata.requires) return true;
if (!test.metadata.requires.nodejs) return true;

return satisfies(process.version, test.metadata.requires.nodejs);
}
}

module.exports = NodeVersionFilter;
65 changes: 50 additions & 15 deletions test/unit/cmap/handshake/client_metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,63 @@ describe('client metadata module', () => {
});

describe('getFAASEnv()', function () {
const tests: Array<[string, string]> = [
['AWS_EXECUTION_ENV', 'aws.lambda'],
const tests: Array<[envVariable: string, provider: string]> = [
['AWS_LAMBDA_RUNTIME_API', 'aws.lambda'],
['FUNCTIONS_WORKER_RUNTIME', 'azure.func'],
['K_SERVICE', 'gcp.func'],
['FUNCTION_NAME', 'gcp.func'],
['VERCEL', 'vercel']
];
for (const [envVariable, provider] of tests) {
context(`when ${envVariable} is in the environment`, () => {
context(`when ${envVariable} is set to a non-empty string`, () => {
before(() => {
process.env[envVariable] = 'non empty string';
process.env[envVariable] = 'non_empty_string';
});
after(() => {
delete process.env[envVariable];
});
it('determines the correct provider', () => {
expect(getFAASEnv()?.get('name')).to.equal(provider);
});

context(`when ${envVariable} is set to an empty string`, () => {
before(() => {
process.env[envVariable] = '';
});
after(() => {
delete process.env[envVariable];
});
it('returns null', () => {
expect(getFAASEnv()).to.be.null;
});
});
});
}

context('when AWS_EXECUTION_ENV starts with "AWS_Lambda_"', () => {
before(() => {
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_correctStartString';
});
after(() => {
delete process.env.AWS_EXECUTION_ENV;
});
it('indicates the runtime is aws lambda', () => {
expect(getFAASEnv()?.get('name')).to.equal('aws.lambda');
});
});

context('when AWS_EXECUTION_ENV does not start with "AWS_Lambda_"', () => {
before(() => {
process.env.AWS_EXECUTION_ENV = 'AWS_LambdaIncorrectStartString';
});
after(() => {
delete process.env.AWS_EXECUTION_ENV;
});
it('returns null', () => {
expect(getFAASEnv()).to.be.null;
});
});

context('when there is no FAAS provider data in the env', () => {
it('returns null', () => {
expect(getFAASEnv()).to.be.null;
Expand All @@ -69,9 +104,9 @@ describe('client metadata module', () => {
context('unrelated environments', () => {
before(() => {
// aws
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_non_empty_string';
// azure
process.env.FUNCTIONS_WORKER_RUNTIME = 'non-empty-string';
process.env.FUNCTIONS_WORKER_RUNTIME = 'non_empty_string';
});
after(() => {
delete process.env.AWS_EXECUTION_ENV;
Expand All @@ -85,10 +120,10 @@ describe('client metadata module', () => {
context('vercel and aws which share env variables', () => {
before(() => {
// vercel
process.env.VERCEL = 'non-empty-string';
process.env.VERCEL = 'non_empty_string';
// aws
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
process.env.AWS_LAMBDA_RUNTIME_API = 'non-empty-string';
process.env.AWS_EXECUTION_ENV = 'non_empty_string';
process.env.AWS_LAMBDA_RUNTIME_API = 'non_empty_string';
});
after(() => {
delete process.env.VERCEL;
Expand Down Expand Up @@ -383,15 +418,15 @@ describe('client metadata module', () => {
aws: [
{
context: 'no additional metadata',
env: [['AWS_EXECUTION_ENV', 'non-empty string']],
env: [['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string']],
outcome: {
name: 'aws.lambda'
}
},
{
context: 'AWS_REGION provided',
env: [
['AWS_EXECUTION_ENV', 'non-empty string'],
['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string'],
['AWS_REGION', 'non-null']
],
outcome: {
Expand All @@ -402,7 +437,7 @@ describe('client metadata module', () => {
{
context: 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE provided',
env: [
['AWS_EXECUTION_ENV', 'non-empty string'],
['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string'],
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '3']
],
outcome: {
Expand Down Expand Up @@ -506,7 +541,7 @@ describe('client metadata module', () => {

context('when a numeric FAAS env variable is not numerically parsable', () => {
before(() => {
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_non_empty_string';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '123not numeric';
});

Expand All @@ -525,7 +560,7 @@ describe('client metadata module', () => {
context('when faas region is too large', () => {
beforeEach('1. Omit fields from `env` except `env.name`.', () => {
sinon.stub(process, 'env').get(() => ({
AWS_EXECUTION_ENV: 'iLoveJavaScript',
AWS_EXECUTION_ENV: 'AWS_Lambda_iLoveJavaScript',
AWS_REGION: 'a'.repeat(512)
}));
});
Expand All @@ -542,7 +577,7 @@ describe('client metadata module', () => {
context('release too large', () => {
beforeEach('2. Omit fields from `os` except `os.type`.', () => {
sinon.stub(process, 'env').get(() => ({
AWS_EXECUTION_ENV: 'iLoveJavaScript',
AWS_EXECUTION_ENV: 'AWS_Lambda_iLoveJavaScript',
AWS_REGION: 'abc'
}));
sinon.stub(os, 'release').returns('a'.repeat(512));
Expand Down