|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | + |
| 4 | +import { LEGACY_HELLO_COMMAND, MongoClient, type MongoClientOptions } from '../mongodb'; |
| 5 | + |
| 6 | +const REQUIRED_ENV = ['MONGODB_URI', 'SSL_KEY_FILE', 'SSL_CA_FILE']; |
| 7 | + |
| 8 | +describe('TLS Support', function () { |
| 9 | + for (const key of REQUIRED_ENV) { |
| 10 | + if (process.env[key] == null) { |
| 11 | + throw new Error(`skipping SSL tests, ${key} environment variable is not defined`); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + const CONNECTION_STRING = process.env.MONGODB_URI as string; |
| 16 | + const TLS_CERT_KEY_FILE = process.env.SSL_KEY_FILE as string; |
| 17 | + const TLS_CA_FILE = process.env.SSL_CA_FILE as string; |
| 18 | + const tlsSettings = { |
| 19 | + tls: true, |
| 20 | + tlsCertificateKeyFile: TLS_CERT_KEY_FILE, |
| 21 | + tlsCAFile: TLS_CA_FILE |
| 22 | + }; |
| 23 | + |
| 24 | + it( |
| 25 | + 'should connect with tls via client options', |
| 26 | + makeConnectionTest(CONNECTION_STRING, tlsSettings) |
| 27 | + ); |
| 28 | + |
| 29 | + it( |
| 30 | + 'should connect with tls via url options', |
| 31 | + makeConnectionTest( |
| 32 | + `${CONNECTION_STRING}?${Object.keys(tlsSettings) |
| 33 | + .map(key => `${key}=${tlsSettings[key]}`) |
| 34 | + .join('&')}` |
| 35 | + ) |
| 36 | + ); |
| 37 | + |
| 38 | + context('when tls filepaths are provided', () => { |
| 39 | + let client: MongoClient; |
| 40 | + afterEach(async () => { |
| 41 | + if (client) await client.close(); |
| 42 | + }); |
| 43 | + |
| 44 | + context('when tls filepaths have length > 0', () => { |
| 45 | + beforeEach(async () => { |
| 46 | + client = new MongoClient(CONNECTION_STRING, tlsSettings); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should read in files async at connect time', async () => { |
| 50 | + expect(client.options).property('tlsCAFile', TLS_CA_FILE); |
| 51 | + expect(client.options).property('tlsCertificateKeyFile', TLS_CERT_KEY_FILE); |
| 52 | + expect(client.options).not.have.property('ca'); |
| 53 | + expect(client.options).not.have.property('key'); |
| 54 | + |
| 55 | + await client.connect(); |
| 56 | + |
| 57 | + expect(client.options).property('ca').to.exist; |
| 58 | + expect(client.options).property('key').to.exist; |
| 59 | + }); |
| 60 | + |
| 61 | + context('when client has been opened and closed more than once', function () { |
| 62 | + it('should only read files once', async () => { |
| 63 | + await client.connect(); |
| 64 | + await client.close(); |
| 65 | + |
| 66 | + const caFileAccessTime = (await fs.stat(TLS_CA_FILE)).atime; |
| 67 | + const certKeyFileAccessTime = (await fs.stat(TLS_CERT_KEY_FILE)).atime; |
| 68 | + |
| 69 | + await client.connect(); |
| 70 | + |
| 71 | + expect((await fs.stat(TLS_CA_FILE)).atime).to.deep.equal(caFileAccessTime); |
| 72 | + expect((await fs.stat(TLS_CERT_KEY_FILE)).atime).to.deep.equal(certKeyFileAccessTime); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + context('when tlsCAFile has length === 0', () => { |
| 78 | + beforeEach(() => { |
| 79 | + client = new MongoClient(CONNECTION_STRING, { |
| 80 | + tls: true, |
| 81 | + tlsCAFile: '', |
| 82 | + tlsCertificateKeyFile: TLS_CERT_KEY_FILE |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should throw an error at connect time', async () => { |
| 87 | + const err = await client.connect().catch(e => e); |
| 88 | + |
| 89 | + expect(err).to.be.instanceof(Error); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + context('when tlsCertificateKeyFile has length === 0', () => { |
| 94 | + beforeEach(() => { |
| 95 | + client = new MongoClient(CONNECTION_STRING, { |
| 96 | + tls: true, |
| 97 | + tlsCAFile: TLS_CA_FILE, |
| 98 | + tlsCertificateKeyFile: '' |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should throw an error at connect time', async () => { |
| 103 | + const err = await client.connect().catch(e => e); |
| 104 | + |
| 105 | + expect(err).to.be.instanceof(Error); |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +function makeConnectionTest(connectionString: string, clientOptions?: MongoClientOptions) { |
| 112 | + return async function () { |
| 113 | + const client = new MongoClient(connectionString, clientOptions); |
| 114 | + |
| 115 | + await client.connect(); |
| 116 | + await client.db('admin').command({ [LEGACY_HELLO_COMMAND]: 1 }); |
| 117 | + await client.db('test').collection('test').findOne({}); |
| 118 | + return await client.close(); |
| 119 | + }; |
| 120 | +} |
0 commit comments