Skip to content

Commit 32278a5

Browse files
committed
Merge pull request brianc#482 from hoegaarden/master
Handle pgpass
2 parents 30fc4c2 + 9ad0159 commit 32278a5

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

lib/client.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var crypto = require('crypto');
22
var EventEmitter = require('events').EventEmitter;
33
var util = require('util');
4+
var pgPass = require('pgpass');
45

56
var ConnectionParameters = require(__dirname + '/connection-parameters');
67
var Query = require(__dirname + '/query');
@@ -38,6 +39,7 @@ util.inherits(Client, EventEmitter);
3839
Client.prototype.connect = function(callback) {
3940
var self = this;
4041
var con = this.connection;
42+
4143
if(this.host && this.host.indexOf('/') === 0) {
4244
con.connect(this.host + '/.s.PGSQL.' + this.port);
4345
} else {
@@ -64,18 +66,33 @@ Client.prototype.connect = function(callback) {
6466
});
6567
});
6668

69+
function checkPgPass(cb) {
70+
return function(msg) {
71+
if (null !== self.password) {
72+
cb(msg);
73+
} else {
74+
pgPass(self.connectionParameters, function(pass){
75+
if (undefined !== pass) {
76+
self.connectionParameters.password = self.password = pass;
77+
}
78+
cb(msg);
79+
});
80+
}
81+
};
82+
}
83+
6784
//password request handling
68-
con.on('authenticationCleartextPassword', function() {
85+
con.on('authenticationCleartextPassword', checkPgPass(function() {
6986
con.password(self.password);
70-
});
87+
}));
7188

7289
//password request handling
73-
con.on('authenticationMD5Password', function(msg) {
90+
con.on('authenticationMD5Password', checkPgPass(function(msg) {
7491
var inner = Client.md5(self.password + self.user);
7592
var outer = Client.md5(inner + msg.salt.toString('binary'));
7693
var md5password = "md5" + outer;
7794
con.password(md5password);
78-
});
95+
}));
7996

8097
con.once('backendKeyData', function(msg) {
8198
self.processID = msg.processID;

lib/connection-parameters.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
8585
if(this.database) {
8686
params.push("dbname='" + this.database + "'");
8787
}
88-
if(this.isDomainSocket) {
88+
if(this.host) {
8989
params.push("host=" + this.host);
90+
}
91+
if(this.isDomainSocket) {
9092
return cb(null, params.join(' '));
9193
}
9294
if(this.client_encoding) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"generic-pool": "2.0.3",
2222
"buffer-writer": "1.0.0",
23+
"pgpass": "0.0.1",
2324
"nan": "~0.6.0"
2425
},
2526
"devDependencies": {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var helper = require(__dirname + '/../test-helper');
2+
3+
// Path to the password file
4+
var passfile = __dirname + '/heroku.pgpass';
5+
6+
// Export the path to the password file
7+
process.env.PGPASSFILE = passfile;
8+
9+
// Do a chmod 660, because git doesn't track those permissions
10+
require('fs').chmodSync(passfile, 384);
11+
12+
var pg = helper.pg;
13+
14+
var host = 'ec2-107-20-224-218.compute-1.amazonaws.com';
15+
var database = 'db6kfntl5qhp2';
16+
var user = 'kwdzdnqpdiilfs';
17+
18+
var config = {
19+
host: host,
20+
database: database,
21+
user: user,
22+
ssl: true
23+
};
24+
25+
// connect & disconnect from heroku
26+
pg.connect(config, assert.success(function(client, done) {
27+
client.query('SELECT NOW() as time', assert.success(function(res) {
28+
assert(res.rows[0].time.getTime());
29+
30+
// cleanup ... remove the env variable
31+
delete process.env.PGPASSFILE;
32+
33+
done();
34+
pg.end();
35+
}))
36+
}));

test/integration/client/heroku.pgpass

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ec2-107-20-224-218.compute-1.amazonaws.com:5432:db6kfntl5qhp2:kwdzdnqpdiilfs:uaZoSSHgi7mVM7kYaROtusClKu

0 commit comments

Comments
 (0)