Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of ?sslmode=require and ssl property in pg.Pool #3355

Open
albertpurnama opened this issue Dec 27, 2024 · 2 comments
Open

Usage of ?sslmode=require and ssl property in pg.Pool #3355

albertpurnama opened this issue Dec 27, 2024 · 2 comments

Comments

@albertpurnama
Copy link

I have the following code snippet

    const pool = new pg.Pool({
      connectionString: env.DATABASE_URL,
      ssl: env.DATABASE_CA_CERT
        ? {
            ca: env.DATABASE_CA_CERT,
            rejectUnauthorized: true,
          }
        : undefined,
    });

seems like a pretty standard setup. I have downloaded CA_CERT from DigitalOcean managed database product.

I copied the connection string which includes the ?sslmode=require query parameter at the end.

I experience the error:

Error: self-signed certificate in certificate chain

It seems like the query param sslmode=require completely overrides the ssl property setting. I confirmed this by removing the sslmode=require and the pool got connected normally.

Can anyone explain a little bit deeper on why this is the case? I might miss some stuff, but if sslmode=require is set on the connection string, how should one set the CA cert for the connection pool?

@hjr3
Copy link
Contributor

hjr3 commented Jan 2, 2025

Use the sslcert query parameter.

* `sslcert=<filename>` - reads data from the given file and includes the result as `ssl.cert`

Example:

it('configuration parameter sslcert=/path/to/cert', function () {
var connectionString = 'pg:///?sslcert=' + __dirname + '/example.cert'
var subject = parse(connectionString)
subject.ssl.should.eql({
cert: 'example cert\n',
})
})

@charmander
Copy link
Collaborator

You can also go directly to pg-connection-string (which is how pg parses connectionString) and merge the resulting configuration exactly the way you want:

import parseConnectionString from 'pg-connection-string';

const pgConfig = parseConnectionString(env.DATABASE_URL);

if (env.DATABASE_CA_CERT) {
  pgConfig.ssl.ca = env.DATABASE_CA_CERT;
}

const pool = new pg.Pool(pgConfig);

Whether you choose to use sslcert in the query string or a separate environment variable, I would recommend using sslmode=verify-full instead of starting with sslmode=require and setting rejectUnauthorized: true afterwards, for consistency in behavior with any other place that query string might be used, and for clarity. (The current version of pg-connection-string actually treats all of prefer, require, verify-ca, and verify-full as verify-full, but that will probably change in a future major version.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants