@@ -8,18 +8,19 @@ import {
8
8
MongoServerSelectionError
9
9
} from '../mongodb' ;
10
10
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 '] ;
12
12
13
13
describe ( 'TLS Support' , function ( ) {
14
14
for ( const key of REQUIRED_ENV ) {
15
15
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` ) ;
17
17
}
18
18
}
19
19
20
20
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 ;
23
24
const tlsSettings = {
24
25
tls : true ,
25
26
tlsCertificateKeyFile : TLS_CERT_KEY_FILE ,
@@ -42,41 +43,79 @@ describe('TLS Support', function () {
42
43
43
44
context ( 'when tls filepaths are provided' , ( ) => {
44
45
let client : MongoClient ;
46
+
45
47
afterEach ( async ( ) => {
46
- if ( client ) await client . close ( ) ;
48
+ await client ? .close ( ) ;
47
49
} ) ;
48
50
49
51
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 ( ) ;
53
75
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 ;
60
78
61
- await client . connect ( ) ;
79
+ await client . connect ( ) ;
62
80
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
+ } ) ;
66
85
} ) ;
67
86
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
+ } ) ;
72
96
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' ) ;
75
100
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
+ } ) ;
77
106
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
+ } ) ;
80
119
} ) ;
81
120
} ) ;
82
121
} ) ;
@@ -114,6 +153,29 @@ describe('TLS Support', function () {
114
153
} ) ;
115
154
} ) ;
116
155
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
+
117
179
context ( 'when tlsCertificateKeyFile is provided, but tlsCAFile is missing' , ( ) => {
118
180
let client : MongoClient ;
119
181
beforeEach ( ( ) => {
0 commit comments