From e81d4a2fc0ffc1d32dc3e124ceadd887e0a78ec4 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Tue, 15 Aug 2023 12:44:27 -0600 Subject: [PATCH] fix(NODE-5495): do not emit deprecation warning when tlsCertificateKeyFile is specified and tlsCertificateFile is not (#3810) --- src/connection_string.ts | 10 ++-- test/unit/mongo_client.test.js | 94 +++++++++++++++++++--------------- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/connection_string.ts b/src/connection_string.ts index ac8a3315f7..c7ff1849de 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 0a2e6130bf..2791f1d8ed 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -29,46 +29,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 = {