Skip to content

Commit 95295ad

Browse files
committed
Handle .pgpass in the js client
1 parent bbea5d6 commit 95295ad

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

lib/client.js

+21-4
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;

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"main": "./lib",
2020
"dependencies": {
2121
"generic-pool": "2.0.3",
22-
"buffer-writer": "1.0.0"
22+
"buffer-writer": "1.0.0",
23+
"pgpass": "git://github.com/hoegaarden/pgpass"
2324
},
2425
"devDependencies": {
2526
"jshint": "1.1.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var helper = require(__dirname + '/../test-helper');
2+
3+
if (helper.args.native) {
4+
// do not run testwith native bindings,
5+
// see issue #475 (https://github.com/brianc/node-postgres/issues/475)
6+
process.exit();
7+
}
8+
9+
// Path to the password file
10+
var passfile = __dirname + '/heroku.pgpass';
11+
12+
// Export the path to the password file
13+
process.env.PGPASSFILE = passfile;
14+
15+
// Do a chmod 660, because git doesn't track thosepermissions
16+
require('fs').chmodSync(passfile, 384);
17+
18+
var pg = helper.pg;
19+
20+
var host = 'ec2-107-20-224-218.compute-1.amazonaws.com';
21+
var database = 'db6kfntl5qhp2';
22+
var user = 'kwdzdnqpdiilfs';
23+
24+
var config = {
25+
host: host,
26+
database: database,
27+
user: user,
28+
ssl: true
29+
};
30+
31+
// connect & disconnect from heroku
32+
pg.connect(config, assert.success(function(client, done) {
33+
client.query('SELECT NOW() as time', assert.success(function(res) {
34+
assert(res.rows[0].time.getTime());
35+
36+
// cleanup ... remove the env variable
37+
delete process.env.PGPASSFILE;
38+
39+
done();
40+
pg.end();
41+
}))
42+
}));

test/integration/client/heroku.pgpass

+1
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)