From da143b60e764fa939c367d65ace3c81ff194870f Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Thu, 10 Aug 2023 13:39:37 -0600 Subject: [PATCH 1/2] fix deprecation warning for tlsCertificateKeyFile --- src/connection_string.ts | 10 ++-- test/unit/mongo_client.test.js | 96 ++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/connection_string.ts b/src/connection_string.ts index ac8a3315f76..c7ff1849de2 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -347,10 +347,10 @@ export function parseOptions( allProvidedOptions.set(key, values); } - if ( + const didMapTLSCertificateFile = allProvidedOptions.has('tlsCertificateKeyFile') && - !allProvidedOptions.has('tlsCertificateFile') - ) { + !allProvidedOptions.has('tlsCertificateFile'); + if (didMapTLSCertificateFile) { allProvidedOptions.set('tlsCertificateFile', allProvidedOptions.get('tlsCertificateKeyFile')); } @@ -387,7 +387,9 @@ export function parseOptions( } } else { const { deprecated } = descriptor; - if (deprecated) { + const shouldEmitTLSCertificateFileDeprecation = + didMapTLSCertificateFile && key === 'tlsCertificateFile'; + if (deprecated && !shouldEmitTLSCertificateFileDeprecation) { const deprecatedMsg = typeof deprecated === 'string' ? `: ${deprecated}` : ''; emitWarning(`${key} is a deprecated option${deprecatedMsg}`); } diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index 0a2e6130bf1..d102086fbb2 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -12,6 +12,8 @@ const { MongoClient, MongoParseError, ServerApiVersion } = require('../mongodb') const { MongoLogger } = require('../mongodb'); const sinon = require('sinon'); const { Writable } = require('stream'); +const { once } = require('events'); +const { setTimeout } = require('timers/promises'); describe('MongoOptions', function () { it('MongoClient should always freeze public options', function () { @@ -29,46 +31,60 @@ describe('MongoOptions', function () { expect(options.prototype).to.not.exist; }); - it('should rename tls options correctly', function () { - const filename = `${os.tmpdir()}/tmp.pem`; - fs.closeSync(fs.openSync(filename, 'w')); - const options = parseOptions('mongodb://localhost:27017/?ssl=true', { - tlsCertificateKeyFile: filename, - tlsCertificateFile: filename, - tlsCAFile: filename, - sslCRL: filename, - tlsCertificateKeyFilePassword: 'tlsCertificateKeyFilePassword', - sslValidate: false - }); - fs.unlinkSync(filename); - - /* - * If set TLS enabled, equivalent to setting the ssl option. - * - * ### Additional options: - * - * | nodejs option | MongoDB equivalent | type | - * |:---------------------|----------------------------------------------------|:---------------------------------------| - * | `ca` | sslCA, tlsCAFile | `string \| Buffer \| Buffer[]` | - * | `crl` | sslCRL | `string \| Buffer \| Buffer[]` | - * | `cert` | sslCert, tlsCertificateFile | `string \| Buffer \| Buffer[]` | - * | `key` | sslKey, tlsCertificateKeyFile | `string \| Buffer \| KeyObject[]` | - * | `passphrase` | sslPass, tlsCertificateKeyFilePassword | `string` | - * | `rejectUnauthorized` | sslValidate | `boolean` | - * - */ - expect(options).to.not.have.property('tlsCertificateKeyFile'); - expect(options).to.not.have.property('tlsCAFile'); - expect(options).to.not.have.property('sslCRL'); - expect(options).to.not.have.property('tlsCertificateKeyFilePassword'); - expect(options).has.property('ca', ''); - expect(options).has.property('crl', ''); - expect(options).has.property('cert', ''); - expect(options).has.property('key'); - expect(options.key).has.length(0); - expect(options).has.property('passphrase', 'tlsCertificateKeyFilePassword'); - expect(options).has.property('rejectUnauthorized', false); - expect(options).has.property('tls', true); + describe('tls options', () => { + let filename; + before(() => { + filename = `${os.tmpdir()}/tmp.pem`; + fs.closeSync(fs.openSync(filename, 'w')); + }); + after(() => { + fs.unlinkSync(filename); + }); + it('should rename tls options correctly', function () { + const options = parseOptions('mongodb://localhost:27017/?ssl=true', { + tlsCertificateKeyFile: filename, + tlsCertificateFile: filename, + tlsCAFile: filename, + sslCRL: filename, + tlsCertificateKeyFilePassword: 'tlsCertificateKeyFilePassword', + sslValidate: false + }); + + /* + * If set TLS enabled, equivalent to setting the ssl option. + * + * ### Additional options: + * + * | nodejs option | MongoDB equivalent | type | + * |:---------------------|----------------------------------------------------|:---------------------------------------| + * | `ca` | sslCA, tlsCAFile | `string \| Buffer \| Buffer[]` | + * | `crl` | sslCRL | `string \| Buffer \| Buffer[]` | + * | `cert` | sslCert, tlsCertificateFile | `string \| Buffer \| Buffer[]` | + * | `key` | sslKey, tlsCertificateKeyFile | `string \| Buffer \| KeyObject[]` | + * | `passphrase` | sslPass, tlsCertificateKeyFilePassword | `string` | + * | `rejectUnauthorized` | sslValidate | `boolean` | + * + */ + expect(options).to.not.have.property('tlsCertificateKeyFile'); + expect(options).to.not.have.property('tlsCAFile'); + expect(options).to.not.have.property('sslCRL'); + expect(options).to.not.have.property('tlsCertificateKeyFilePassword'); + expect(options).has.property('ca', ''); + expect(options).has.property('crl', ''); + expect(options).has.property('cert', ''); + expect(options).has.property('key'); + expect(options.key).has.length(0); + expect(options).has.property('passphrase', 'tlsCertificateKeyFilePassword'); + expect(options).has.property('rejectUnauthorized', false); + expect(options).has.property('tls', true); + }); + + it('should not emit a deprecation warning for `tlsCertificateKeyFile` when `tlsCaFile` is specified', () => { + const promiseSpy = sinon.spy(process, 'emitWarning'); + new MongoClient(`mongodb://localhost:27017/my_db?tlsCertificateKeyFile=${filename}`); + + expect(promiseSpy).not.to.have.been.called; + }); }); const ALL_OPTIONS = { From 21aabd5f73317b8795e42a2897e0c98e3b454aa0 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Thu, 10 Aug 2023 13:52:03 -0600 Subject: [PATCH 2/2] chore: fix lint --- test/unit/mongo_client.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index d102086fbb2..2791f1d8ed2 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -12,8 +12,6 @@ const { MongoClient, MongoParseError, ServerApiVersion } = require('../mongodb') const { MongoLogger } = require('../mongodb'); const sinon = require('sinon'); const { Writable } = require('stream'); -const { once } = require('events'); -const { setTimeout } = require('timers/promises'); describe('MongoOptions', function () { it('MongoClient should always freeze public options', function () {