Skip to content

Commit b308a5d

Browse files
committed
PostgreSQL clients: respect $PGSSLROOTCERT env var
This is a shim for brianc/node-postgres#2723.
1 parent fd593d0 commit b308a5d

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

lib/db.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
1+
import {ok} from 'node:assert'
2+
import {readFileSync} from 'node:fs'
13
import _pg from 'pg'
24
const {Pool} = _pg
35

6+
// pg doesn't support $PGSSLROOTCERT yet, so we pass it in ourselves if SSL is not disabled.
7+
// see https://github.com/brianc/node-postgres/issues/2723
8+
9+
// https://github.com/brianc/node-postgres/blob/pg%408.12.0/packages/pg/lib/defaults.js#L43
10+
let ssl = false
11+
// > PGSSLMODE behaves the same as the sslmode connection parameter.
12+
// https://www.postgresql.org/docs/14/libpq-envars.html
13+
// > sslmode – This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. There are six modes:
14+
// > - disable – only try a non-SSL connection
15+
// > […]
16+
// https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNECT-SSLMODE
17+
if (process.env.PGSSLMODE && process.env.PGSSLMODE !== 'disable') {
18+
ssl = {}
19+
if ('PGSSLROOTCERT' in process.env) {
20+
ok(process.env.PGSSLROOTCERT, '$PGSSLROOTCERT must not be empty')
21+
ssl.ca = readFileSync(process.env.PGSSLROOTCERT, {encoding: 'utf8'})
22+
}
23+
}
24+
25+
const getPgOpts = (opt = {}) => {
26+
const pgOpts = {
27+
...opt,
28+
// todo: let this depend on the configured matching parallelism
29+
max: parseInt(process.env.PG_POOL_SIZE || '30'),
30+
}
31+
if (ssl || opt.ssl) {
32+
pgOpts.ssl = {
33+
...(ssl || {}),
34+
...(opt.ssl || {}),
35+
}
36+
}
37+
return pgOpts
38+
}
39+
440
const connectToPostgres = (opt = {}) => {
541
// todo?
642
// > Do not use pool.query if you need transactional integrity: the pool will dispatch every query passed to pool.query on the first available idle client. Transactions within PostgreSQL are scoped to a single client and so dispatching individual queries within a single transaction across multiple, random clients will cause big problems in your app and not work. For more info please read transactions.
743
// https://node-postgres.com/api/pool
8-
const db = new Pool({
9-
...opt,
10-
// todo: let this depend on the configured matching parallelism
11-
max: parseInt(process.env.PG_POOL_SIZE || '30'),
12-
})
44+
const db = new Pool(getPgOpts(opt))
1345

1446
// todo: don't parse timestamptz into JS Date, keep ISO 8601 strings
1547
// todo: don't parse date into JS Date, keep ISO 8601 strings
@@ -19,5 +51,6 @@ const connectToPostgres = (opt = {}) => {
1951
}
2052

2153
export {
54+
getPgOpts,
2255
connectToPostgres,
2356
}

lib/refresh-schedule-feeds.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {ok} from 'node:assert'
77
import {Summary, Gauge} from 'prom-client'
88
import sortBy from 'lodash/sortBy.js';
99
import {createLogger} from './logger.js'
10+
import {getPgOpts} from './db.js'
1011
import {readImportedDatabases} from '../postgis-gtfs-importer/index.js'
1112
import {importGtfsAtomically} from '../postgis-gtfs-importer/import.js'
1213
import {
@@ -48,6 +49,7 @@ const queryImportedScheduleFeedVersions = async (cfg) => {
4849
const databaseNamePrefix = `${DB_NAME_PREFIX}${scheduleFeedName}_`
4950
const res = await readImportedDatabases({
5051
databaseNamePrefix,
52+
pgOpts: getPgOpts(),
5153
})
5254

5355
return res.oldDbs
@@ -72,6 +74,7 @@ const fetchAndImportScheduleFeed = async (cfg) => {
7274
connectDownloadScriptToStdout: verboseLogging,
7375
importScriptVerbose: verboseLogging,
7476
connectImportScriptToStdout: verboseLogging,
77+
pgOpts: getPgOpts(),
7578
databaseNamePrefix,
7679
gtfsDownloadUrl,
7780
gtfsDownloadUserAgent: `${pkg.name} v${pkg.version}`, // todo: allow customising via env var

0 commit comments

Comments
 (0)