node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Node-Postgres Pool.ssl.mode: disabled is being ignored.

Open BrokenFlame opened this issue 2 years ago • 1 comments

I'm setting the Node-Postgres Pool parameter ssl.mode to disabled via an environment variable. However I'm get the error:

"name":"Error","message":"The server does not support SSL connections","stack":"Error: The server does not support SSL connections\n

My code is simple enough, and the object stringifying the Pool object confirms the setting:

... "ssl\":{\"rejectUnauthorized\":\"false\",\"ca\":null,\"mode\":\"disable\"} ...

The code is pretty simple and works fine when a Certificate Authority Pem is provided, and I need to connect over SSL. It just doesn't seem to work, when I don't want to use SSL to connect to the server.

import { Pool } from 'pg';
import fs from "fs";
import dotenv from "dotenv";

const dbPoolConfig = {
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  database: process.env.DB_NAME,
  connectionTimeoutMillis: 10 * 1000,
  idleTimeoutMillis: 60 * 60 * 1000,
  max: 2,
  statement_timeout: 10 * 1000,
  query_timeout: 10 * 1000,
  ssl: {
    rejectUnauthorized: process.env.DB_REJECT_UNAUTHORIZED,
    ca: process.env.DB_CA_PATH ? fs.readFileSync(process.env.DB_CA_PATH, 'ascii').toString() : null,
    mode: process.env.DB_SSL_MODE ? process.env.DB_SSL_MODE : 'allow',
  },
};

export const dbPool: Pool | null = ['preprod', 'uat', 'development', 'production'].includes(process.env.ENV)
  ? new Pool(dbPoolConfig)
  : null;

// TODO: remove conditional export
export default dbPool;

I'm not sure if this is a bug or, if I'm doing something wrong. Maybe I should be setting the CA to "undefined"?

BrokenFlame avatar Nov 02 '23 01:11 BrokenFlame

If you don’t want to use SSL, you need to set the entire ssl property to a falsy value. ssl.mode is not an option that exists; you might be thinking of the sslmode connection string parameter.

charmander avatar Nov 02 '23 09:11 charmander