Skip to content

Commit

Permalink
fix(NODE-3924): Change to throw at connect time
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed Jul 28, 2023
1 parent c454e37 commit 5352581
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
24 changes: 2 additions & 22 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
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' });
}
}
Expand Down
40 changes: 36 additions & 4 deletions test/manual/tls_support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
});
});

Expand Down
20 changes: 0 additions & 20 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit 5352581

Please sign in to comment.