Skip to content

Commit e81d4a2

Browse files
fix(NODE-5495): do not emit deprecation warning when tlsCertificateKeyFile is specified and tlsCertificateFile is not (#3810)
1 parent c3b35b3 commit e81d4a2

File tree

2 files changed

+60
-44
lines changed

2 files changed

+60
-44
lines changed

src/connection_string.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ export function parseOptions(
347347
allProvidedOptions.set(key, values);
348348
}
349349

350-
if (
350+
const didMapTLSCertificateFile =
351351
allProvidedOptions.has('tlsCertificateKeyFile') &&
352-
!allProvidedOptions.has('tlsCertificateFile')
353-
) {
352+
!allProvidedOptions.has('tlsCertificateFile');
353+
if (didMapTLSCertificateFile) {
354354
allProvidedOptions.set('tlsCertificateFile', allProvidedOptions.get('tlsCertificateKeyFile'));
355355
}
356356

@@ -387,7 +387,9 @@ export function parseOptions(
387387
}
388388
} else {
389389
const { deprecated } = descriptor;
390-
if (deprecated) {
390+
const shouldEmitTLSCertificateFileDeprecation =
391+
didMapTLSCertificateFile && key === 'tlsCertificateFile';
392+
if (deprecated && !shouldEmitTLSCertificateFileDeprecation) {
391393
const deprecatedMsg = typeof deprecated === 'string' ? `: ${deprecated}` : '';
392394
emitWarning(`${key} is a deprecated option${deprecatedMsg}`);
393395
}

test/unit/mongo_client.test.js

+54-40
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,60 @@ describe('MongoOptions', function () {
2929
expect(options.prototype).to.not.exist;
3030
});
3131

32-
it('should rename tls options correctly', function () {
33-
const filename = `${os.tmpdir()}/tmp.pem`;
34-
fs.closeSync(fs.openSync(filename, 'w'));
35-
const options = parseOptions('mongodb://localhost:27017/?ssl=true', {
36-
tlsCertificateKeyFile: filename,
37-
tlsCertificateFile: filename,
38-
tlsCAFile: filename,
39-
sslCRL: filename,
40-
tlsCertificateKeyFilePassword: 'tlsCertificateKeyFilePassword',
41-
sslValidate: false
42-
});
43-
fs.unlinkSync(filename);
44-
45-
/*
46-
* If set TLS enabled, equivalent to setting the ssl option.
47-
*
48-
* ### Additional options:
49-
*
50-
* | nodejs option | MongoDB equivalent | type |
51-
* |:---------------------|----------------------------------------------------|:---------------------------------------|
52-
* | `ca` | sslCA, tlsCAFile | `string \| Buffer \| Buffer[]` |
53-
* | `crl` | sslCRL | `string \| Buffer \| Buffer[]` |
54-
* | `cert` | sslCert, tlsCertificateFile | `string \| Buffer \| Buffer[]` |
55-
* | `key` | sslKey, tlsCertificateKeyFile | `string \| Buffer \| KeyObject[]` |
56-
* | `passphrase` | sslPass, tlsCertificateKeyFilePassword | `string` |
57-
* | `rejectUnauthorized` | sslValidate | `boolean` |
58-
*
59-
*/
60-
expect(options).to.not.have.property('tlsCertificateKeyFile');
61-
expect(options).to.not.have.property('tlsCAFile');
62-
expect(options).to.not.have.property('sslCRL');
63-
expect(options).to.not.have.property('tlsCertificateKeyFilePassword');
64-
expect(options).has.property('ca', '');
65-
expect(options).has.property('crl', '');
66-
expect(options).has.property('cert', '');
67-
expect(options).has.property('key');
68-
expect(options.key).has.length(0);
69-
expect(options).has.property('passphrase', 'tlsCertificateKeyFilePassword');
70-
expect(options).has.property('rejectUnauthorized', false);
71-
expect(options).has.property('tls', true);
32+
describe('tls options', () => {
33+
let filename;
34+
before(() => {
35+
filename = `${os.tmpdir()}/tmp.pem`;
36+
fs.closeSync(fs.openSync(filename, 'w'));
37+
});
38+
after(() => {
39+
fs.unlinkSync(filename);
40+
});
41+
it('should rename tls options correctly', function () {
42+
const options = parseOptions('mongodb://localhost:27017/?ssl=true', {
43+
tlsCertificateKeyFile: filename,
44+
tlsCertificateFile: filename,
45+
tlsCAFile: filename,
46+
sslCRL: filename,
47+
tlsCertificateKeyFilePassword: 'tlsCertificateKeyFilePassword',
48+
sslValidate: false
49+
});
50+
51+
/*
52+
* If set TLS enabled, equivalent to setting the ssl option.
53+
*
54+
* ### Additional options:
55+
*
56+
* | nodejs option | MongoDB equivalent | type |
57+
* |:---------------------|----------------------------------------------------|:---------------------------------------|
58+
* | `ca` | sslCA, tlsCAFile | `string \| Buffer \| Buffer[]` |
59+
* | `crl` | sslCRL | `string \| Buffer \| Buffer[]` |
60+
* | `cert` | sslCert, tlsCertificateFile | `string \| Buffer \| Buffer[]` |
61+
* | `key` | sslKey, tlsCertificateKeyFile | `string \| Buffer \| KeyObject[]` |
62+
* | `passphrase` | sslPass, tlsCertificateKeyFilePassword | `string` |
63+
* | `rejectUnauthorized` | sslValidate | `boolean` |
64+
*
65+
*/
66+
expect(options).to.not.have.property('tlsCertificateKeyFile');
67+
expect(options).to.not.have.property('tlsCAFile');
68+
expect(options).to.not.have.property('sslCRL');
69+
expect(options).to.not.have.property('tlsCertificateKeyFilePassword');
70+
expect(options).has.property('ca', '');
71+
expect(options).has.property('crl', '');
72+
expect(options).has.property('cert', '');
73+
expect(options).has.property('key');
74+
expect(options.key).has.length(0);
75+
expect(options).has.property('passphrase', 'tlsCertificateKeyFilePassword');
76+
expect(options).has.property('rejectUnauthorized', false);
77+
expect(options).has.property('tls', true);
78+
});
79+
80+
it('should not emit a deprecation warning for `tlsCertificateKeyFile` when `tlsCaFile` is specified', () => {
81+
const promiseSpy = sinon.spy(process, 'emitWarning');
82+
new MongoClient(`mongodb://localhost:27017/my_db?tlsCertificateKeyFile=${filename}`);
83+
84+
expect(promiseSpy).not.to.have.been.called;
85+
});
7286
});
7387

7488
const ALL_OPTIONS = {

0 commit comments

Comments
 (0)