Handle .pgpass in the js client

This commit is contained in:
Hannes Hörl 2013-12-06 00:01:51 +01:00
parent bbea5d6be7
commit 95295ad2fb
4 changed files with 68 additions and 7 deletions

View File

@ -1,6 +1,7 @@
var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var pgPass = require('pgpass');
var ConnectionParameters = require(__dirname + '/connection-parameters');
var Query = require(__dirname + '/query');
@ -38,6 +39,7 @@ util.inherits(Client, EventEmitter);
Client.prototype.connect = function(callback) {
var self = this;
var con = this.connection;
if(this.host && this.host.indexOf('/') === 0) {
con.connect(this.host + '/.s.PGSQL.' + this.port);
} else {
@ -64,18 +66,33 @@ Client.prototype.connect = function(callback) {
});
});
//password request handling
con.on('authenticationCleartextPassword', function() {
con.password(self.password);
});
function checkPgPass(cb) {
return function(msg) {
if (null !== self.password) {
cb(msg);
} else {
pgPass(self.connectionParameters, function(pass){
if (undefined !== pass) {
self.connectionParameters.password = self.password = pass;
}
cb(msg);
});
}
};
}
//password request handling
con.on('authenticationMD5Password', function(msg) {
con.on('authenticationCleartextPassword', checkPgPass(function() {
con.password(self.password);
}));
//password request handling
con.on('authenticationMD5Password', checkPgPass(function(msg) {
var inner = Client.md5(self.password + self.user);
var outer = Client.md5(inner + msg.salt.toString('binary'));
var md5password = "md5" + outer;
con.password(md5password);
});
}));
con.once('backendKeyData', function(msg) {
self.processID = msg.processID;

View File

@ -19,7 +19,8 @@
"main": "./lib",
"dependencies": {
"generic-pool": "2.0.3",
"buffer-writer": "1.0.0"
"buffer-writer": "1.0.0",
"pgpass": "git://github.com/hoegaarden/pgpass"
},
"devDependencies": {
"jshint": "1.1.0",

View File

@ -0,0 +1,42 @@
var helper = require(__dirname + '/../test-helper');
if (helper.args.native) {
// do not run testwith native bindings,
// see issue #475 (https://github.com/brianc/node-postgres/issues/475)
process.exit();
}
// Path to the password file
var passfile = __dirname + '/heroku.pgpass';
// Export the path to the password file
process.env.PGPASSFILE = passfile;
// Do a chmod 660, because git doesn't track thosepermissions
require('fs').chmodSync(passfile, 384);
var pg = helper.pg;
var host = 'ec2-107-20-224-218.compute-1.amazonaws.com';
var database = 'db6kfntl5qhp2';
var user = 'kwdzdnqpdiilfs';
var config = {
host: host,
database: database,
user: user,
ssl: true
};
// connect & disconnect from heroku
pg.connect(config, assert.success(function(client, done) {
client.query('SELECT NOW() as time', assert.success(function(res) {
assert(res.rows[0].time.getTime());
// cleanup ... remove the env variable
delete process.env.PGPASSFILE;
done();
pg.end();
}))
}));

View File

@ -0,0 +1 @@
ec2-107-20-224-218.compute-1.amazonaws.com:5432:db6kfntl5qhp2:kwdzdnqpdiilfs:uaZoSSHgi7mVM7kYaROtusClKu