Skip to content

Commit e6e3692

Browse files
authored
Pass connection parameters to password callback (#3602)
* Pass connection parameters to password callback * Works on my machine - adding logging for gh actions * More debugging logging in tests * Blank out password in unit test * Try again... * Remove debugging
1 parent d80d883 commit e6e3692

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

packages/pg/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bench:
2727
@find benchmark -name "*-bench.js" | $(node-command)
2828

2929
test-unit:
30+
@chmod 600 test/unit/client/pgpass.file
3031
@find test/unit -name "*-tests.js" | $(node-command)
3132

3233
test-connection:

packages/pg/lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class Client extends EventEmitter {
249249
if (typeof this.password === 'function') {
250250
this._Promise
251251
.resolve()
252-
.then(() => this.password())
252+
.then(() => this.password(this.connectionParameters))
253253
.then((pass) => {
254254
if (pass !== undefined) {
255255
if (typeof pass !== 'string') {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const helper = require('./test-helper')
2+
const assert = require('assert')
3+
const suite = new helper.Suite()
4+
const pgpass = require('pgpass')
5+
6+
class Wait {
7+
constructor() {
8+
this.promise = new Promise((resolve) => {
9+
this.resolve = resolve
10+
})
11+
}
12+
13+
until() {
14+
return this.promise
15+
}
16+
17+
done(time) {
18+
if (time) {
19+
setTimeout(this.resolve.bind(this), time)
20+
} else {
21+
this.resolve()
22+
}
23+
}
24+
}
25+
26+
suite.test('password callback is called with conenction params', async function () {
27+
const wait = new Wait()
28+
const client = helper.client({
29+
user: 'foo',
30+
database: 'bar',
31+
host: 'baz',
32+
password: async (params) => {
33+
assert.equal(params.user, 'foo')
34+
assert.equal(params.database, 'bar')
35+
assert.equal(params.host, 'baz')
36+
wait.done(10)
37+
return 'password'
38+
},
39+
})
40+
client.connection.emit('authenticationCleartextPassword')
41+
await wait.until()
42+
assert.equal(client.user, 'foo')
43+
assert.equal(client.database, 'bar')
44+
assert.equal(client.host, 'baz')
45+
assert.equal(client.connectionParameters.password, 'password')
46+
})
47+
48+
suite.test('cleartext password auth does not crash with null password using pg-pass', async function () {
49+
process.env.PGPASSFILE = `${__dirname}/pgpass.file`
50+
// set this to undefined so pgpass will use the file
51+
delete process.env.PGPASSWORD
52+
const wait = new Wait()
53+
const client = helper.client({
54+
host: 'foo',
55+
port: 5432,
56+
database: 'bar',
57+
user: 'baz',
58+
password: (params) => {
59+
return new Promise((resolve) => {
60+
pgpass(params, (pass) => {
61+
wait.done(10)
62+
resolve(pass)
63+
})
64+
})
65+
},
66+
})
67+
client.connection.emit('authenticationCleartextPassword')
68+
await wait.until()
69+
assert.equal(client.password, 'quz')
70+
})

packages/pg/test/unit/client/test-helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ const helper = require('../test-helper')
33
const Connection = require('../../../lib/connection')
44
const { Client } = helper
55

6-
const makeClient = function () {
6+
const makeClient = function (config) {
77
const connection = new Connection({ stream: 'no' })
88
connection.startup = function () {}
99
connection.connect = function () {}
1010
connection.query = function (text) {
1111
this.queries.push(text)
1212
}
1313
connection.queries = []
14-
const client = new Client({ connection: connection })
14+
const client = new Client({ connection: connection, ...config })
1515
client.connect()
1616
client.connection.emit('connect')
1717
return client

0 commit comments

Comments
 (0)