From 95295ad2fb5f85cba94f236dea71fcb89b8b889b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Fri, 6 Dec 2013 00:01:51 +0100 Subject: [PATCH 1/3] Handle .pgpass in the js client --- lib/client.js | 29 ++++++++++--- package.json | 3 +- .../integration/client/heroku-pgpass-tests.js | 42 +++++++++++++++++++ test/integration/client/heroku.pgpass | 1 + 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 test/integration/client/heroku-pgpass-tests.js create mode 100644 test/integration/client/heroku.pgpass diff --git a/lib/client.js b/lib/client.js index b924fdb..dc8edc7 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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; diff --git a/package.json b/package.json index f272426..408631d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/integration/client/heroku-pgpass-tests.js b/test/integration/client/heroku-pgpass-tests.js new file mode 100644 index 0000000..2405854 --- /dev/null +++ b/test/integration/client/heroku-pgpass-tests.js @@ -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(); + })) +})); diff --git a/test/integration/client/heroku.pgpass b/test/integration/client/heroku.pgpass new file mode 100644 index 0000000..39bba52 --- /dev/null +++ b/test/integration/client/heroku.pgpass @@ -0,0 +1 @@ +ec2-107-20-224-218.compute-1.amazonaws.com:5432:db6kfntl5qhp2:kwdzdnqpdiilfs:uaZoSSHgi7mVM7kYaROtusClKu From 61f8f55d4353b48eb0d01fb41bc8d81e311a5a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Wed, 11 Dec 2013 01:24:55 +0100 Subject: [PATCH 2/3] Handle .pgpass in the native client --- lib/connection-parameters.js | 4 +++- test/integration/client/heroku-pgpass-tests.js | 8 +------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index f21da1f..a3a083d 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -77,8 +77,10 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) { if(this.database) { params.push("dbname='" + this.database + "'"); } - if(this.isDomainSocket) { + if(this.host) { params.push("host=" + this.host); + } + if(this.isDomainSocket) { return cb(null, params.join(' ')); } if(this.client_encoding) { diff --git a/test/integration/client/heroku-pgpass-tests.js b/test/integration/client/heroku-pgpass-tests.js index 2405854..c037923 100644 --- a/test/integration/client/heroku-pgpass-tests.js +++ b/test/integration/client/heroku-pgpass-tests.js @@ -1,18 +1,12 @@ 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 +// Do a chmod 660, because git doesn't track those permissions require('fs').chmodSync(passfile, 384); var pg = helper.pg; From 2d1024c46d61f0c90313c1bdbcfa622d2aa534d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Sun, 22 Dec 2013 23:21:12 +0100 Subject: [PATCH 3/3] use 'pgpass' from npm --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 408631d..bcbb2ba 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "dependencies": { "generic-pool": "2.0.3", "buffer-writer": "1.0.0", - "pgpass": "git://github.com/hoegaarden/pgpass" + "pgpass": "0.0.1" }, "devDependencies": { "jshint": "1.1.0",