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

feat: support CA_CERT env var override #194

Merged
merged 2 commits into from
Sep 19, 2024
Merged

feat: support CA_CERT env var override #194

merged 2 commits into from
Sep 19, 2024

Conversation

conorsch
Copy link
Contributor

When connecting to remote databases that are SSL-required, the app will sometimes report cert errors. First, I tried to resolve this by pulling in ca-certificates in the container environment, but that wasn't sufficient. Turns out node-postgres in v8.x updated the ssl logic and that broke setups for many managed databases. As a workaround, one can now:

  1. set the CA_CERT env var with the string contents of the db's CA
  2. remove the sslmode=require from the connection auth starting

The fact that 2 is necessary is a bit scary, but it's caused by the fact that the connectionString setting clobbers any manual ssl opts in the db config.

Refs #193.

When connecting to remote databases that are SSL-required, the app will
sometimes report cert errors. First, I tried to resolve this by pulling
in `ca-certificates` in the container environment, but that wasn't
sufficient. Turns out `node-postgres` in v8.x updated the ssl logic and
that broke setups for many managed databases. As a workaround, one can
now:

  1. set the CA_CERT env var with the string contents of the db's CA
  2. remove the `sslmode=require` from the connection auth starting

The fact that 2 is necessary is a bit scary, but it's caused by the fact
that the connectionString setting clobbers any manual `ssl` opts in the
db config.
@conorsch
Copy link
Contributor Author

Built a container from this branch, and was able to connect to a DO pg database, provided both were true:

  1. the CA_CERT env var was declared with the correct cert info
  2. the ?sslmode=require param was removed from the DATABASE_URL env var

My intent in the diff was to add support for this setup, but it's important that the existing behavior not change. @ejmg I'd appreciate if you could take this branch for a spin locally, and ensure it still works just fine for you in your normal workflows. Otherwise, we should edit it.

Also open to constructive criticism on the overall approach here.

connectionString: process.env.DATABASE_URL,
});
ssl: {},

Choose a reason for hiding this comment

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

In js/ts you can do in-place conditionals in objects like this:

Suggested change
ssl: {},
...(process.env.CA_CERT && {
ssl: {
rejectUnauthorized: true,
ca: process.env.CA_CERT,
},
}),

Choose a reason for hiding this comment

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

Just a style preference, your code works perfectly fine as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL! Great suggestion, added. (I also tested with a new ad-hoc container build to confirm I didn't break anything with my clumsy TS.)

@conorsch
Copy link
Contributor Author

Thanks for review!

@conorsch conorsch merged commit a71e42e into main Sep 19, 2024
1 check passed
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.

2 participants