Skip to content

Commit

Permalink
fix(NODE-5495): do not emit deprecation warning when tlsCertificateKe…
Browse files Browse the repository at this point in the history
…yFile is specified and tlsCertificateFile is not (#3810)
  • Loading branch information
baileympearson authored Aug 15, 2023
1 parent c3b35b3 commit e81d4a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
10 changes: 6 additions & 4 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down Expand Up @@ -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}`);
}
Expand Down
94 changes: 54 additions & 40 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit e81d4a2

Please sign in to comment.