Skip to content

Commit 698fe69

Browse files
committed
Merge pull request brianc#436 from brianc/ssl-mode-env
Respect PGSSLMODE for setting SSL connection
2 parents e8f7f38 + bfdea75 commit 698fe69

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

lib/connection-parameters.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ var parse = function(str) {
3535
return config;
3636
};
3737

38+
var useSsl = function() {
39+
switch(process.env.PGSSLMODE) {
40+
case "disable":
41+
return false;
42+
case "prefer":
43+
case "require":
44+
case "verify-ca":
45+
case "verify-full":
46+
return true;
47+
}
48+
return defaults.ssl;
49+
};
50+
3851
var ConnectionParameters = function(config) {
3952
config = typeof config == 'string' ? parse(config) : (config || {});
4053
this.user = val('user', config);
@@ -43,7 +56,7 @@ var ConnectionParameters = function(config) {
4356
this.host = val('host', config);
4457
this.password = val('password', config);
4558
this.binary = val('binary', config);
46-
this.ssl = config.ssl || defaults.ssl;
59+
this.ssl = config.ssl || useSsl();
4760
this.client_encoding = val("client_encoding", config);
4861
//a domain socket begins with '/'
4962
this.isDomainSocket = (!(this.host||'').indexOf('/'));

lib/defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ var defaults = module.exports = {
3636
//pool log function / boolean
3737
poolLog: false,
3838

39-
client_encoding: ""
39+
client_encoding: "",
40+
41+
ssl: false
4042
};
4143

4244
//parse int8 so you can get your count values as actual numbers

test/unit/connection-parameters/creation-tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,14 @@ test('libpq connection string building', function() {
151151
});
152152

153153
test('password contains weird characters', function() {
154+
var defaults = require('../../../lib/defaults');
155+
defaults.ssl = true;
154156
var strang = 'postgres://my first name:is&%awesome!@localhost:9000';
155157
var subject = new ConnectionParameters(strang);
156158
assert.equal(subject.user, 'my first name');
157159
assert.equal(subject.password, 'is&%awesome!');
158160
assert.equal(subject.host, 'localhost');
161+
assert.equal(subject.ssl, true);
159162
});
160163

161164
});

test/unit/connection-parameters/environment-variable-tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,38 @@ test('connection string parsing - ssl', function(t) {
7676
assert.equal(!!subject.ssl, false, 'ssl');
7777
});
7878

79+
//clear process.env
80+
for(var key in process.env) {
81+
delete process.env[key];
82+
}
83+
84+
85+
test('ssl is false by default', function() {
86+
var subject = new ConnectionParameters()
87+
assert.equal(subject.ssl, false)
88+
})
89+
90+
var testVal = function(mode, expected) {
91+
//clear process.env
92+
for(var key in process.env) {
93+
delete process.env[key];
94+
}
95+
process.env.PGSSLMODE = mode;
96+
test('ssl is ' + expected + ' when $PGSSLMODE=' + mode, function() {
97+
var subject = new ConnectionParameters();
98+
assert.equal(subject.ssl, expected);
99+
});
100+
};
101+
102+
testVal('', false);
103+
testVal('disable', false);
104+
testVal('allow', false);
105+
testVal('prefer', true);
106+
testVal('require', true);
107+
testVal('verify-ca', true);
108+
testVal('verify-full', true);
109+
110+
79111
//restore process.env
80112
for(var key in realEnv) {
81113
process.env[key] = realEnv[key];

0 commit comments

Comments
 (0)