Skip to content

Commit 1c441d2

Browse files
authored
Merge pull request brianc#2208 from sehrope/add-scram-tests
Add some SCRAM tests and enable them on travis
2 parents c55758f + c25e889 commit 1c441d2

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ matrix:
2727
# Run tests/paths that require password authentication
2828
- node_js: lts/erbium
2929
env:
30-
- CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres PGPASSWORD=test-password
30+
- CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres PGPASSWORD=test-password SCRAM_TEST_PGUSER=scram_test SCRAM_TEST_PGPASSWORD=test4scram
3131
before_script: |
3232
sudo -u postgres sed -i \
3333
-e '/^local/ s/trust$/peer/' \
@@ -36,6 +36,9 @@ matrix:
3636
sudo -u postgres psql -c "ALTER ROLE postgres PASSWORD 'test-password'; SELECT pg_reload_conf()"
3737
yarn build
3838
node packages/pg/script/create-test-tables.js postgresql:///
39+
sudo -u postgres -- psql \
40+
-c "SET password_encryption = 'scram-sha-256'" \
41+
-c "CREATE ROLE scram_test login password 'test4scram'"
3942
4043
- node_js: lts/carbon
4144
addons:

packages/pg/test/integration/client/sasl-scram-tests.js

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,75 @@
11
'use strict'
2-
var helper = require(__dirname + '/../test-helper')
3-
var pg = helper.pg
2+
const helper = require('./../test-helper')
3+
const pg = helper.pg
4+
const suite = new helper.Suite()
5+
const { native } = helper.args
46

5-
var suite = new helper.Suite()
7+
/**
8+
* This test only executes if the env variables SCRAM_TEST_PGUSER and
9+
* SCRAM_TEST_PGPASSWORD are defined. You can override additional values
10+
* for the host, port and database with other SCRAM_TEST_ prefixed vars.
11+
* If the variables are not defined the test will be skipped.
12+
*
13+
* SQL to create test role:
14+
*
15+
* SET password_encryption = 'scram-sha-256';
16+
* CREATE ROLE scram_test login password 'test4scram';
17+
*
18+
* Add the following entries to pg_hba.conf:
19+
*
20+
* host all scram_test ::1/128 scram-sha-256
21+
* host all scram_test 0.0.0.0/0 scram-sha-256
22+
*
23+
* Then run this file with after exporting:
24+
*
25+
* SCRAM_TEST_PGUSER=scram_test
26+
* SCRAM_TEST_PGPASSWORD=test4scram
27+
*/
628

7-
/*
8-
SQL to create test role:
29+
// Base config for SCRAM tests
30+
const config = {
31+
user: process.env.SCRAM_TEST_PGUSER,
32+
password: process.env.SCRAM_TEST_PGPASSWORD,
33+
host: process.env.SCRAM_TEST_PGHOST, // optional
34+
port: process.env.SCRAM_TEST_PGPORT, // optional
35+
database: process.env.SCRAM_TEST_PGDATABASE, // optional
36+
}
937

10-
set password_encryption = 'scram-sha-256';
11-
create role npgtest login password 'test';
38+
if (native) {
39+
suite.testAsync('skipping SCRAM tests (on native)', () => {})
40+
return
41+
}
42+
if (!config.user || !config.password) {
43+
suite.testAsync('skipping SCRAM tests (missing env)', () => {})
44+
return
45+
}
1246

13-
pg_hba:
14-
host all npgtest ::1/128 scram-sha-256
15-
host all npgtest 0.0.0.0/0 scram-sha-256
16-
17-
18-
*/
19-
/*
20-
suite.test('can connect using sasl/scram', function () {
21-
var connectionString = 'pg://npgtest:test@localhost/postgres'
22-
const pool = new pg.Pool({ connectionString: connectionString })
23-
pool.connect(
24-
assert.calls(function (err, client, done) {
25-
assert.ifError(err, 'should have connected')
26-
done()
27-
})
28-
)
47+
suite.testAsync('can connect using sasl/scram', async () => {
48+
const client = new pg.Client(config)
49+
let usingSasl = false
50+
client.connection.once('authenticationSASL', () => {
51+
usingSasl = true
52+
})
53+
await client.connect()
54+
assert.ok(usingSasl, 'Should be using SASL for authentication')
55+
await client.end()
2956
})
3057

31-
suite.test('sasl/scram fails when password is wrong', function () {
32-
var connectionString = 'pg://npgtest:bad@localhost/postgres'
33-
const pool = new pg.Pool({ connectionString: connectionString })
34-
pool.connect(
35-
assert.calls(function (err, client, done) {
36-
assert.ok(err, 'should have a connection error')
37-
done()
38-
})
39-
)
58+
suite.testAsync('sasl/scram fails when password is wrong', async () => {
59+
const client = new pg.Client({
60+
...config,
61+
password: config.password + 'append-something-to-make-it-bad',
62+
})
63+
let usingSasl = false
64+
client.connection.once('authenticationSASL', () => {
65+
usingSasl = true
66+
})
67+
await assert.rejects(
68+
() => client.connect(),
69+
{
70+
code: '28P01',
71+
},
72+
'Error code should be for a password error'
73+
)
74+
assert.ok(usingSasl, 'Should be using SASL for authentication')
4075
})
41-
*/

0 commit comments

Comments
 (0)