@@ -3,6 +3,11 @@ var helper = require('../test-helper')
3
3
const Suite = require ( '../../suite' )
4
4
5
5
var assert = require ( 'assert' )
6
+ const fs = require ( 'fs' )
7
+
8
+ const tmp = require ( 'tmp' )
9
+ tmp . setGracefulCleanup ( )
10
+
6
11
var ConnectionParameters = require ( '../../../lib/connection-parameters' )
7
12
var defaults = require ( '../../../lib' ) . defaults
8
13
@@ -36,6 +41,38 @@ suite.test('ConnectionParameters initialized from environment variables', functi
36
41
assert . equal ( subject . port , 7890 , 'env port' )
37
42
assert . equal ( subject . database , 'allyerbase' , 'env database' )
38
43
assert . equal ( subject . password , 'open' , 'env password' )
44
+ assert . equal ( subject . ssl , false , 'ssl' )
45
+ } )
46
+
47
+ suite . test ( 'ConnectionParameters initialized from environment variables - ssl' , function ( ) {
48
+ createTempTlsFilesAndExecute ( function (
49
+ certFilePath , keyFilePath , caFilePath ,
50
+ certFileContents , keyFileContents , caFileContents
51
+ ) {
52
+ clearEnv ( )
53
+ process . env [ 'PGHOST' ] = 'local'
54
+ process . env [ 'PGUSER' ] = 'bmc2'
55
+ process . env [ 'PGPORT' ] = 7890
56
+ process . env [ 'PGDATABASE' ] = 'allyerbase'
57
+ process . env [ 'PGPASSWORD' ] = 'open'
58
+
59
+ process . env [ 'PGSSLMODE' ] = 'verify-full'
60
+ process . env [ 'PGSSLCERT' ] = certFilePath
61
+ process . env [ 'PGSSLKEY' ] = keyFilePath
62
+ process . env [ 'PGSSLROOTCERT' ] = caFilePath
63
+
64
+ var subject = new ConnectionParameters ( )
65
+ assert . equal ( subject . host , 'local' , 'env host' )
66
+ assert . equal ( subject . user , 'bmc2' , 'env user' )
67
+ assert . equal ( subject . port , 7890 , 'env port' )
68
+ assert . equal ( subject . database , 'allyerbase' , 'env database' )
69
+ assert . equal ( subject . password , 'open' , 'env password' )
70
+
71
+ assert . equal ( typeof subject . ssl , 'object' , 'env ssl' )
72
+ assert . equal ( subject . ssl . cert , certFileContents , 'env ssl cert' )
73
+ assert . equal ( subject . ssl . key , keyFileContents , 'env ssl key' )
74
+ assert . equal ( subject . ssl . ca , caFileContents , 'env ssl ca' )
75
+ } )
39
76
} )
40
77
41
78
suite . test ( 'ConnectionParameters initialized from mix' , function ( ) {
@@ -56,6 +93,77 @@ suite.test('ConnectionParameters initialized from mix', function () {
56
93
assert . equal ( subject . port , 7890 , 'env port' )
57
94
assert . equal ( subject . database , 'zugzug' , 'config database' )
58
95
assert . equal ( subject . password , defaults . password , 'defaults password' )
96
+ assert . equal ( subject . ssl , false , 'ssl' )
97
+ } )
98
+
99
+ suite . test ( 'ConnectionParameters initialized from mix - ssl' , function ( ) {
100
+ createTempTlsFilesAndExecute ( function (
101
+ certFilePath , keyFilePath , caFilePath ,
102
+ certFileContents , keyFileContents , caFileContents
103
+ ) {
104
+ clearEnv ( )
105
+ process . env [ 'PGHOST' ] = 'local'
106
+ process . env [ 'PGUSER' ] = 'bmc2'
107
+ process . env [ 'PGPORT' ] = 7890
108
+ process . env [ 'PGDATABASE' ] = 'allyerbase'
109
+ process . env [ 'PGPASSWORD' ] = 'open'
110
+ process . env [ 'PGSSLMODE' ] = 'verify-full'
111
+ process . env [ 'PGSSLCERT' ] = certFilePath
112
+ process . env [ 'PGSSLKEY' ] = keyFilePath
113
+ delete process . env [ 'PGPASSWORD' ]
114
+ delete process . env [ 'PGDATABASE' ]
115
+
116
+ var subject = new ConnectionParameters ( {
117
+ // The connection string will mostly override this config. See ConnectionParameters constructor.
118
+ user : 'testing' ,
119
+ database : 'zugzug' ,
120
+ ssl : {
121
+ ca : caFileContents
122
+ } ,
123
+ connectionString : "postgres://user2:pass2@host2:9999/db2"
124
+ } )
125
+ assert . equal ( subject . host , 'host2' , 'string host' )
126
+ assert . equal ( subject . user , 'user2' , 'string user' )
127
+ assert . equal ( subject . port , 9999 , 'string port' )
128
+ assert . equal ( subject . database , 'db2' , 'string database' )
129
+ assert . equal ( subject . password , 'pass2' , 'string password' )
130
+
131
+ assert . equal ( typeof subject . ssl , 'object' , 'env ssl' )
132
+ assert . equal ( subject . ssl . cert , certFileContents , 'env ssl cert' )
133
+ assert . equal ( subject . ssl . key , keyFileContents , 'env ssl key' )
134
+ assert . equal ( subject . ssl . ca , caFileContents , 'config ssl ca' )
135
+ } )
136
+ } )
137
+
138
+ suite . test ( 'ConnectionParameters initialized from config - ssl' , function ( ) {
139
+ createTempTlsFilesAndExecute ( function (
140
+ certFilePath , keyFilePath , caFilePath ,
141
+ certFileContents , keyFileContents , caFileContents
142
+ ) {
143
+ clearEnv ( )
144
+ var subject = new ConnectionParameters ( {
145
+ host : 'local' ,
146
+ user : 'testing' ,
147
+ password : 'open' ,
148
+ port : 7890 ,
149
+ database : 'zugzug' ,
150
+ ssl : {
151
+ cert : certFileContents ,
152
+ key : keyFileContents ,
153
+ ca : caFileContents
154
+ }
155
+ } )
156
+ assert . equal ( subject . host , 'local' , 'env host' )
157
+ assert . equal ( subject . user , 'testing' , 'config user' )
158
+ assert . equal ( subject . port , 7890 , 'env port' )
159
+ assert . equal ( subject . database , 'zugzug' , 'config database' )
160
+ assert . equal ( subject . password , 'open' , 'defaults password' )
161
+
162
+ assert . equal ( typeof subject . ssl , 'object' , 'config ssl' )
163
+ assert . equal ( subject . ssl . cert , certFileContents , 'config ssl cert' )
164
+ assert . equal ( subject . ssl . key , keyFileContents , 'config ssl key' )
165
+ assert . equal ( subject . ssl . ca , caFileContents , 'config ssl ca' )
166
+ } )
59
167
} )
60
168
61
169
suite . test ( 'connection string parsing' , function ( ) {
@@ -67,6 +175,7 @@ suite.test('connection string parsing', function () {
67
175
assert . equal ( subject . password , 'pw' , 'string password' )
68
176
assert . equal ( subject . port , 381 , 'string port' )
69
177
assert . equal ( subject . database , 'lala' , 'string database' )
178
+ assert . equal ( subject . ssl , false , 'ssl' )
70
179
} )
71
180
72
181
suite . test ( 'connection string parsing - ssl' , function ( ) {
@@ -104,6 +213,33 @@ suite.test('ssl is false by default', function () {
104
213
assert . equal ( subject . ssl , false )
105
214
} )
106
215
216
+ // Create temp TLS certificate-mock files and run test logic inside this context
217
+ function createTempTlsFilesAndExecute ( callback ) {
218
+ tmp . dir ( function _tempDirCreated ( err , tmpdir ) {
219
+ if ( err ) throw err ;
220
+
221
+ const certFilePath = tmpdir + '/client.crt'
222
+ const keyFilePath = tmpdir + '/client.key'
223
+ const caFilePath = tmpdir + '/ca.crt'
224
+
225
+ const certFileContents = 'client cert file'
226
+ const keyFileContents = 'client key file'
227
+ const caFileContents = 'CA cert file'
228
+
229
+ fs . appendFileSync ( certFilePath , certFileContents , function ( err ) {
230
+ if ( err ) throw err ;
231
+ } )
232
+ fs . appendFileSync ( keyFilePath , keyFileContents , function ( err ) {
233
+ if ( err ) throw err ;
234
+ } )
235
+ fs . appendFileSync ( caFilePath , caFileContents , function ( err ) {
236
+ if ( err ) throw err ;
237
+ } )
238
+
239
+ callback ( certFilePath , keyFilePath , caFilePath , certFileContents , keyFileContents , caFileContents )
240
+ } )
241
+ }
242
+
107
243
var testVal = function ( mode , expected ) {
108
244
suite . test ( 'ssl is ' + expected + ' when $PGSSLMODE=' + mode , function ( ) {
109
245
clearEnv ( )
0 commit comments