Skip to content

Commit f5130fe

Browse files
committed
fix(NODE-5548): ensure that tlsCertificateKeyFile maps to cert and key options
1 parent bd031fc commit f5130fe

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/mongo_client.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,11 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
438438
options.ca ??= await fs.readFile(options.tlsCAFile, { encoding: 'utf8' });
439439
}
440440
if (typeof options.tlsCertificateKeyFile === 'string') {
441-
options.key ??= await fs.readFile(options.tlsCertificateKeyFile, { encoding: 'utf8' });
441+
if (!options.key || !options.cert) {
442+
const contents = await fs.readFile(options.tlsCertificateKeyFile, { encoding: 'utf8' });
443+
options.key ??= contents;
444+
options.cert ??= contents;
445+
}
442446
}
443447
}
444448
if (typeof options.srvHost === 'string') {
@@ -787,6 +791,7 @@ export interface MongoOptions
787791
* |:----------------------|:----------------------------------------------|:-------------------|
788792
* | `ca` | `tlsCAFile` | `string` |
789793
* | `crl` | N/A | `string` |
794+
* | `cert` | `tlsCertificateKeyFile` | `string` |
790795
* | `key` | `tlsCertificateKeyFile` | `string` |
791796
* | `passphrase` | `tlsCertificateKeyFilePassword` | `string` |
792797
* | `rejectUnauthorized` | `tlsAllowInvalidCertificates` | `boolean` |
@@ -804,9 +809,9 @@ export interface MongoOptions
804809
*
805810
* The files specified by the paths passed in to the `tlsCAFile` and `tlsCertificateKeyFile` fields
806811
* are read lazily on the first call to `MongoClient.connect`. Once these files have been read and
807-
* the `ca` and `key` fields are populated, they will not be read again on subsequent calls to
808-
* `MongoClient.connect`. As a result, until the first call to `MongoClient.connect`, the `ca`
809-
* and `key` fields will be undefined.
812+
* the `ca`, `cert` and `key` fields are populated, they will not be read again on subsequent calls to
813+
* `MongoClient.connect`. As a result, until the first call to `MongoClient.connect`, the `ca`,
814+
* `cert` and `key` fields will be undefined.
810815
*/
811816
tls: boolean;
812817

test/manual/tls_support.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ describe('TLS Support', function () {
5151
expect(client.options).property('tlsCertificateKeyFile', TLS_CERT_KEY_FILE);
5252
expect(client.options).not.have.property('ca');
5353
expect(client.options).not.have.property('key');
54+
expect(client.options).not.have.property('cert');
5455

5556
await client.connect();
5657

5758
expect(client.options).property('ca').to.exist;
5859
expect(client.options).property('key').to.exist;
60+
expect(client.options).property('cert').to.exist;
5961
});
6062

6163
context('when client has been opened and closed more than once', function () {

test/unit/mongo_client.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ describe('MongoOptions', function () {
5858
expect(options).to.not.have.property('tlsCertificateKeyFilePassword');
5959
expect(options).to.not.have.property('key');
6060
expect(options).to.not.have.property('ca');
61+
expect(options).to.not.have.property('cert');
6162
expect(options).to.have.property('tlsCertificateKeyFile', filename);
6263
expect(options).to.have.property('tlsCAFile', filename);
6364
expect(options).has.property('passphrase', 'tlsCertificateKeyFilePassword');

0 commit comments

Comments
 (0)