diff --git a/src/connection_string.ts b/src/connection_string.ts index 4f12722bf1f..a6a1677412f 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -1096,30 +1096,10 @@ export const OPTIONS = { } }, tlsCAFile: { - transform({ name, values: [value] }) { - if (typeof value !== 'string') { - throw new MongoParseError(`${name} must be of type string`); - } - - if (value.length === 0) { - throw new MongoParseError(`${name} must be have non-zero length`); - } - - return value; - } + type: 'string' }, tlsCertificateKeyFile: { - transform({ name, values: [value] }) { - if (typeof value !== 'string') { - throw new MongoParseError(`${name} must be of type string`); - } - - if (value.length === 0) { - throw new MongoParseError(`${name} must be have non-zero length`); - } - - return value; - } + type: 'string' }, tlsCertificateKeyFilePassword: { target: 'passphrase', diff --git a/src/mongo_client.ts b/src/mongo_client.ts index d2cc471593e..b4203b07d75 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -435,10 +435,10 @@ export class MongoClient extends TypedEventEmitter { const options = this[kOptions]; if (options.tls) { - if (options.tlsCAFile) { + if (typeof options.tlsCAFile === 'string') { options.ca ??= await fs.readFile(options.tlsCAFile, { encoding: 'utf8' }); } - if (options.tlsCertificateKeyFile) { + if (typeof options.tlsCertificateKeyFile === 'string') { options.key ??= await fs.readFile(options.tlsCertificateKeyFile, { encoding: 'utf8' }); } } diff --git a/test/manual/tls_support.test.ts b/test/manual/tls_support.test.ts index a3ee4b78a40..179527b1c4b 100644 --- a/test/manual/tls_support.test.ts +++ b/test/manual/tls_support.test.ts @@ -37,15 +37,15 @@ describe('TLS Support', function () { context('when tls filepaths are provided', () => { let client: MongoClient; + afterEach(async () => { + if (client) await client.close(); + }); + context('when tls filepaths have length > 0', () => { beforeEach(async () => { client = new MongoClient(CONNECTION_STRING, tlsSettings); }); - afterEach(async () => { - if (client) await client.close(); - }); - it('should read in files async at connect time', async () => { expect(client.options).property('tlsCAFile', TLS_CA_FILE); expect(client.options).property('tlsCertificateKeyFile', TLS_CERT_KEY_FILE); @@ -73,6 +73,38 @@ describe('TLS Support', function () { }); }); }); + + context('when tlsCAFile has length === 0', () => { + beforeEach(() => { + client = new MongoClient(CONNECTION_STRING, { + tls: true, + tlsCAFile: '', + tlsCertificateKeyFile: TLS_CERT_KEY_FILE + }); + }); + + it('should throw an error at connect time', async () => { + const err = await client.connect().catch(e => e); + + expect(err).to.be.instanceof(Error); + }); + }); + + context('when tlsCertificateKeyFile has length === 0', () => { + beforeEach(() => { + client = new MongoClient(CONNECTION_STRING, { + tls: true, + tlsCAFile: TLS_CA_FILE, + tlsCertificateKeyFile: '' + }); + }); + + it('should throw an error at connect time', async () => { + const err = await client.connect().catch(e => e); + + expect(err).to.be.instanceof(Error); + }); + }); }); }); diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index 4a676e15c34..2869a7d270e 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -455,26 +455,6 @@ describe('MongoOptions', function () { expect(optionsUndefined.checkServerIdentity).to.equal(undefined); }); - context('when tlsCAPath is provided as an empty string', function () { - it('throws MongoParseError', function () { - expect(() => { - parseOptions('mongodb://localhost:27017/?tls=true', { - tlsCAFile: '' - }); - }).to.throw(MongoParseError); - }); - }); - - context('when tlsCertificateKeyFile is provided as an empty string', function () { - it('throws MongoParseError', function () { - expect(() => { - parseOptions('mongodb://localhost:27017/?tls=true', { - tlsCertificateKeyFile: '' - }); - }).to.throw(MongoParseError); - }); - }); - describe('compressors', function () { it('can be set when passed in as an array in the options object', function () { const clientViaOpt = new MongoClient('mongodb://localhost', {