Skip to content

Commit f5e53c6

Browse files
author
Gregoire Salingue
committed
fix: handle ssl connections on postgresql databases
1 parent 0528d65 commit f5e53c6

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

backend/lib/config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,29 @@ const configure = () => {
6666
const envPostgresHost = process.env.DB_POSTGRES_HOST || null;
6767
const envPostgresUser = process.env.DB_POSTGRES_USER || null;
6868
const envPostgresName = process.env.DB_POSTGRES_NAME || null;
69+
const envPostgresSslMode = process.env.DB_POSTGRES_SSL_MODE || null;
6970
if (envPostgresHost && envPostgresUser && envPostgresName) {
7071
// we have enough postgres creds to go with postgres
7172
logger.info("Using Postgres configuration");
73+
74+
// knex does not handle ssl enablement other than in the connectionString, so let's use it
75+
// this prevents the serivce from starting on databases with self signed certificates
76+
// cf https://knexjs.org/guide/#configuration-options
77+
port = process.env.DB_POSTGRES_PORT || 5432
78+
connectionString = `postgresql://${envPostgresUser}:${process.env.DB_POSTGRES_PASSWORD}@${port}/${envPostgresName}`
79+
if (envPostgresSslMode) {
80+
connectionString = connectionString + `?ssl=true&sslmode=${envPostgresSslMode}`
81+
}
7282
instance = {
7383
database: {
84+
connectionString: connectionString,
7485
engine: postgresEngine,
7586
host: envPostgresHost,
76-
port: process.env.DB_POSTGRES_PORT || 5432,
87+
port: port,
7788
user: envPostgresUser,
7889
password: process.env.DB_POSTGRES_PASSWORD,
7990
name: envPostgresName,
91+
ssl: envPostgresSslMode ? { rejectUnauthorized: false } : false
8092
},
8193
keys: getKeys(),
8294
};

0 commit comments

Comments
 (0)