@@ -8,18 +8,19 @@ import {
88 MongoServerSelectionError
99} from '../mongodb' ;
1010
11- const REQUIRED_ENV = [ 'MONGODB_URI' , 'SSL_KEY_FILE ' , 'SSL_CA_FILE ' ] ;
11+ const REQUIRED_ENV = [ 'MONGODB_URI' , 'TLS_KEY_FILE ' , 'TLS_CA_FILE' , 'TLS_CRL_FILE '] ;
1212
1313describe ( 'TLS Support' , function ( ) {
1414 for ( const key of REQUIRED_ENV ) {
1515 if ( process . env [ key ] == null ) {
16- throw new Error ( `skipping SSL tests, ${ key } environment variable is not defined` ) ;
16+ throw new Error ( `skipping TLS tests, ${ key } environment variable is not defined` ) ;
1717 }
1818 }
1919
2020 const CONNECTION_STRING = process . env . MONGODB_URI as string ;
21- const TLS_CERT_KEY_FILE = process . env . SSL_KEY_FILE as string ;
22- const TLS_CA_FILE = process . env . SSL_CA_FILE as string ;
21+ const TLS_CERT_KEY_FILE = process . env . TLS_KEY_FILE as string ;
22+ const TLS_CA_FILE = process . env . TLS_CA_FILE as string ;
23+ const TLS_CRL_FILE = process . env . TLS_CRL_FILE as string ;
2324 const tlsSettings = {
2425 tls : true ,
2526 tlsCertificateKeyFile : TLS_CERT_KEY_FILE ,
@@ -42,41 +43,79 @@ describe('TLS Support', function () {
4243
4344 context ( 'when tls filepaths are provided' , ( ) => {
4445 let client : MongoClient ;
46+
4547 afterEach ( async ( ) => {
46- if ( client ) await client . close ( ) ;
48+ await client ? .close ( ) ;
4749 } ) ;
4850
4951 context ( 'when tls filepaths have length > 0' , ( ) => {
50- beforeEach ( async ( ) => {
51- client = new MongoClient ( CONNECTION_STRING , tlsSettings ) ;
52- } ) ;
52+ context ( 'when connection will succeed' , ( ) => {
53+ beforeEach ( async ( ) => {
54+ client = new MongoClient ( CONNECTION_STRING , tlsSettings ) ;
55+ } ) ;
56+
57+ it ( 'should read in files async at connect time' , async ( ) => {
58+ expect ( client . options ) . property ( 'tlsCAFile' , TLS_CA_FILE ) ;
59+ expect ( client . options ) . property ( 'tlsCertificateKeyFile' , TLS_CERT_KEY_FILE ) ;
60+ expect ( client . options ) . not . have . property ( 'ca' ) ;
61+ expect ( client . options ) . not . have . property ( 'key' ) ;
62+ expect ( client . options ) . not . have . property ( 'cert' ) ;
63+
64+ await client . connect ( ) ;
65+
66+ expect ( client . options ) . property ( 'ca' ) . to . exist ;
67+ expect ( client . options ) . property ( 'key' ) . to . exist ;
68+ expect ( client . options ) . property ( 'cert' ) . to . exist ;
69+ } ) ;
70+
71+ context ( 'when client has been opened and closed more than once' , function ( ) {
72+ it ( 'should only read files once' , async ( ) => {
73+ await client . connect ( ) ;
74+ await client . close ( ) ;
5375
54- it ( 'should read in files async at connect time' , async ( ) => {
55- expect ( client . options ) . property ( 'tlsCAFile' , TLS_CA_FILE ) ;
56- expect ( client . options ) . property ( 'tlsCertificateKeyFile' , TLS_CERT_KEY_FILE ) ;
57- expect ( client . options ) . not . have . property ( 'ca' ) ;
58- expect ( client . options ) . not . have . property ( 'key' ) ;
59- expect ( client . options ) . not . have . property ( 'cert' ) ;
76+ const caFileAccessTime = ( await fs . stat ( TLS_CA_FILE ) ) . atime ;
77+ const certKeyFileAccessTime = ( await fs . stat ( TLS_CERT_KEY_FILE ) ) . atime ;
6078
61- await client . connect ( ) ;
79+ await client . connect ( ) ;
6280
63- expect ( client . options ) . property ( 'ca' ) . to . exist ;
64- expect ( client . options ) . property ( 'key' ) . to . exist ;
65- expect ( client . options ) . property ( 'cert' ) . to . exist ;
81+ expect ( ( await fs . stat ( TLS_CA_FILE ) ) . atime ) . to . deep . equal ( caFileAccessTime ) ;
82+ expect ( ( await fs . stat ( TLS_CERT_KEY_FILE ) ) . atime ) . to . deep . equal ( certKeyFileAccessTime ) ;
83+ } ) ;
84+ } ) ;
6685 } ) ;
6786
68- context ( 'when client has been opened and closed more than once' , function ( ) {
69- it ( 'should only read files once' , async ( ) => {
70- await client . connect ( ) ;
71- await client . close ( ) ;
87+ context ( 'when the connection will fail' , ( ) => {
88+ beforeEach ( async ( ) => {
89+ client = new MongoClient ( CONNECTION_STRING , {
90+ tls : true ,
91+ tlsCRLFile : TLS_CRL_FILE ,
92+ serverSelectionTimeoutMS : 2000 ,
93+ connectTimeoutMS : 2000
94+ } ) ;
95+ } ) ;
7296
73- const caFileAccessTime = ( await fs . stat ( TLS_CA_FILE ) ) . atime ;
74- const certKeyFileAccessTime = ( await fs . stat ( TLS_CERT_KEY_FILE ) ) . atime ;
97+ it ( 'should read in files async at connect time' , async ( ) => {
98+ expect ( client . options ) . property ( 'tlsCRLFile' , TLS_CRL_FILE ) ;
99+ expect ( client . options ) . not . have . property ( 'crl' ) ;
75100
76- await client . connect ( ) ;
101+ const err = await client . connect ( ) . catch ( e => e ) ;
102+
103+ expect ( err ) . to . be . instanceof ( Error ) ;
104+ expect ( client . options ) . property ( 'crl' ) . to . exist ;
105+ } ) ;
77106
78- expect ( ( await fs . stat ( TLS_CA_FILE ) ) . atime ) . to . deep . equal ( caFileAccessTime ) ;
79- expect ( ( await fs . stat ( TLS_CERT_KEY_FILE ) ) . atime ) . to . deep . equal ( certKeyFileAccessTime ) ;
107+ context ( 'when client has been opened and closed more than once' , function ( ) {
108+ it ( 'should only read files once' , async ( ) => {
109+ await client . connect ( ) . catch ( e => e ) ;
110+ await client . close ( ) ;
111+
112+ const crlFileAccessTime = ( await fs . stat ( TLS_CRL_FILE ) ) . atime ;
113+
114+ const err = await client . connect ( ) . catch ( e => e ) ;
115+
116+ expect ( err ) . to . be . instanceof ( Error ) ;
117+ expect ( ( await fs . stat ( TLS_CRL_FILE ) ) . atime ) . to . deep . equal ( crlFileAccessTime ) ;
118+ } ) ;
80119 } ) ;
81120 } ) ;
82121 } ) ;
@@ -114,6 +153,29 @@ describe('TLS Support', function () {
114153 } ) ;
115154 } ) ;
116155
156+ context ( 'when providing tlsCRLFile' , ( ) => {
157+ context ( 'when the file will revoke the certificate' , ( ) => {
158+ let client : MongoClient ;
159+ beforeEach ( ( ) => {
160+ client = new MongoClient ( CONNECTION_STRING , {
161+ tls : true ,
162+ tlsCAFile : TLS_CA_FILE ,
163+ tlsCRLFile : TLS_CRL_FILE ,
164+ serverSelectionTimeoutMS : 5000 ,
165+ connectTimeoutMS : 5000
166+ } ) ;
167+ } ) ;
168+ afterEach ( async ( ) => {
169+ await client ?. close ( ) ;
170+ } ) ;
171+
172+ it ( 'throws a MongoServerSelectionError' , async ( ) => {
173+ const err = await client . connect ( ) . catch ( e => e ) ;
174+ expect ( err ) . to . be . instanceOf ( MongoServerSelectionError ) ;
175+ } ) ;
176+ } ) ;
177+ } ) ;
178+
117179 context ( 'when tlsCertificateKeyFile is provided, but tlsCAFile is missing' , ( ) => {
118180 let client : MongoClient ;
119181 beforeEach ( ( ) => {
0 commit comments