Skip to content

Commit 7b1c4b1

Browse files
RichardJCairafiss
authored andcommitted
Allow users to pass certs with PG environment variables
If PGSSLMODE is specified and is either require, verify-ca or verify-full, then the PGSSLROOTCERT, PGSSLCERT, and PGSSLKEY environment variables will be checked for certificate paths and used to connect. This also includes a fix to CI to stop getting the following error: ``` yarn install v1.22.17 [1/4] Resolving packages... [2/4] Fetching packages... error Command failed. Exit code: 128 Command: git Arguments: ls-remote --tags --heads git://github.com/BonsaiDen/Fomatto.git Directory: /home/runner/work/node-postgres/node-postgres Output: fatal: remote error: The unauthenticated git protocol on port 9418 is no longer supported. Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information. info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command. ```
1 parent 21ccd4f commit 7b1c4b1

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121
os: [ubuntu-latest, windows-latest, macos-latest]
2222
name: Node.js ${{ matrix.node }} (${{ matrix.os }})
2323
steps:
24+
- name: Fix up git URLs
25+
run: echo -e '[url "https://github.com/"]\n insteadOf = "git://github.com/"' >> ~/.gitconfig
2426
- uses: actions/checkout@v2
2527
- name: Setup node
2628
uses: actions/setup-node@v2

packages/pg/lib/connection-parameters.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
var dns = require('dns')
4+
var fs = require('fs')
45

56
var defaults = require('./defaults')
67

@@ -23,10 +24,15 @@ var readSSLConfigFromEnvironment = function () {
2324
case 'disable':
2425
return false
2526
case 'prefer':
27+
return true
2628
case 'require':
2729
case 'verify-ca':
2830
case 'verify-full':
29-
return true
31+
return {
32+
ca: process.env.PGSSLROOTCERT ? fs.readFileSync(process.env.PGSSLROOTCERT).toString() : undefined,
33+
key: process.env.PGSSLKEY ? fs.readFileSync(process.env.PGSSLKEY).toString() : undefined,
34+
cert: process.env.PGSSLCERT ? fs.readFileSync(process.env.PGSSLCERT).toString() : undefined,
35+
}
3036
case 'no-verify':
3137
return { rejectUnauthorized: false }
3238
}

packages/pg/test/integration/connection-pool/tls-tests.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ const suite = new helper.Suite()
99

1010
if (process.env.PG_CLIENT_CERT_TEST) {
1111
suite.testAsync('client certificate', async () => {
12-
const pool = new pg.Pool({
13-
ssl: {
14-
ca: fs.readFileSync(process.env.PGSSLROOTCERT),
15-
cert: fs.readFileSync(process.env.PGSSLCERT),
16-
key: fs.readFileSync(process.env.PGSSLKEY),
17-
},
18-
})
12+
// PGSSLROOTCERT, PGSSLCERT, and PGSSLKEY are all set as environment
13+
// variables in .travis.yml
14+
const pool = new pg.Pool()
1915

2016
await pool.query('SELECT 1')
2117
await pool.end()

packages/pg/test/unit/connection-parameters/environment-variable-tests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ testVal('', false)
117117
testVal('disable', false)
118118
testVal('allow', false)
119119
testVal('prefer', true)
120-
testVal('require', true)
121-
testVal('verify-ca', true)
122-
testVal('verify-full', true)
120+
testVal('require', { ca: undefined, cert: undefined, key: undefined })
121+
testVal('verify-ca', { ca: undefined, cert: undefined, key: undefined })
122+
testVal('verify-full', { ca: undefined, cert: undefined, key: undefined })
123123
testVal('no-verify', { rejectUnauthorized: false })
124124

125125
// restore process.env

0 commit comments

Comments
 (0)