Skip to content

Allow users to pass certs when PG environment variable PGSSLMODE=require/verify-ca/verify-full - #2517

Open
RichardJCai wants to merge 1 commit into
brianc:masterfrom
RichardJCai:allowing_passing_certs_through_pg_env
Open

Allow users to pass certs when PG environment variable PGSSLMODE=require/verify-ca/verify-full#2517
RichardJCai wants to merge 1 commit into
brianc:masterfrom
RichardJCai:allowing_passing_certs_through_pg_env

Conversation

@RichardJCai

Copy link
Copy Markdown

Previously if readSSLConfigFromEnvironment was used to generate the SSL config, the ssl object would not be created to create certs.

I ran into this issue when trying to execute yarn tests to a secure cluster (SSLMODE=require), certs would never be read even when I specified PGSSLCERT, PGSSLKEY, PGSSLROOTCERT.

This fix allows the user to pass in certs through PG env variables.

I'm not great with JS so apologies in advance

Why this is necessary.

To highlight this issue in a simple case

Running the script I attached below through PGSSLMODE=require PGSSLCERT=/home/ubuntu/certs/client.testuser.crt PGSSLROOTCERT=/home/ubuntu/certs/ca.crt PGSSLKEY=/home/ubuntu/certs/client.testuser.key PGHOST=localhost PGPORT=26257 PGUSER=root node test.js results in the following error:

const {Client, Pool} = require("./packages/pg")

const client = new Client()
client.connect(err => {
if (err) {
    console.error('error connecting', err.stack)
} else {
    console.log('connected')
    client.end()
}
})

const pool = new Pool()
pool
.connect()
.then(client => {
    console.log('connected')
    client.release()
})
.catch(err => console.error('error connecting', err.stack))
.then(() => pool.end())
error connecting Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket._finishInit (_tls_wrap.js:932:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)
error connecting Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket._finishInit (_tls_wrap.js:932:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)

This is because the ssl field in ConnectionParameters is simply set to true and the certs fields are not populated

ConnectionParameters {
  user: 'root',
  database: 'root',
  port: 26257,
  host: 'localhost',
  binary: false,
  options: undefined,
  ssl: true,
  client_encoding: '',
  replication: undefined,
  isDomainSocket: false,
  application_name: undefined,
  fallback_application_name: undefined,
  statement_timeout: false,
  idle_in_transaction_session_timeout: false,
  query_timeout: false,
  connect_timeout: 0
}

@charmander charmander left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They shouldn’t be required, just supported.

@RichardJCai

Copy link
Copy Markdown
Author

They shouldn’t be required, just supported.

Gotcha, thought erroring would be better since if certs aren't passed in with the SSLMODEs then they won't be able to connect and getting a somewhat vague

error connecting Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket._finishInit (_tls_wrap.js:932:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)
error connecting Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket._finishInit (_tls_wrap.js:932:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)

is confusing since theres actually no certs to update. As far as I can tell, certs won't be populated anywhere when readSSLConfigFromEnvironment is called.

Do you think logging a warning is appropriate? It certainly would've helped me out.

@RichardJCai
RichardJCai requested a review from charmander April 12, 2021 15:24
@RichardJCai
RichardJCai force-pushed the allowing_passing_certs_through_pg_env branch from 29e0be5 to e0af654 Compare April 19, 2021 00:48
@RichardJCai

Copy link
Copy Markdown
Author

Updated so that it'll return an object with the cert properties.

Please let me know if this is reasonable, mostly the part about returning undefined if the environment variable is not present for the certs.

@RichardJCai RichardJCai changed the title Require users to pass certs when PG environment variable PGSSLMODE=require/verify-ca/verify-full Allow users to pass certs when PG environment variable PGSSLMODE=require/verify-ca/verify-full Apr 19, 2021
@dyllandry

Copy link
Copy Markdown

I just tried passing a cert via env var PGSSLROOTCERT and it doesn't work. From reading the node-postgres docs it seems like it should.

node-postgres uses the same environment variables as libpq to connect to a PostgreSQL server.

@rafiss
rafiss force-pushed the allowing_passing_certs_through_pg_env branch 3 times, most recently from 6b736e5 to 7b1c4b1 Compare March 25, 2022 20:50
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.
@rafiss
rafiss force-pushed the allowing_passing_certs_through_pg_env branch from 7b1c4b1 to 4fd1c6e Compare January 5, 2024 18:20
@rafiss

rafiss commented Jan 5, 2024

Copy link
Copy Markdown
Contributor

The CI here is failing because #3110 needs to be merged (cc @brianc)

But this PR is ready for review.

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

Successfully merging this pull request may close these issues.

4 participants