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

allow postgres connections, that require ssl mode #404

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ services:
- TRUST_PROXY=0
- DATABASE_URL=postgresql://postgres@postgres/planka
- SECRET_KEY=notsecretkey
# related: https://github.com/knex/knex/issues/2354
# As knex does not pass query parameters from the connection string we
# have to use environment variables in order to pass the desired values, e.g.
# note: this is optional
# PGSSLMODE=require
depends_on:
- postgres

Expand Down
5 changes: 5 additions & 0 deletions server/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ SECRET_KEY=notsecretkey
# TRUST_PROXY=0
# TOKEN_EXPIRES_IN=365 # In days

# related: https://github.com/knex/knex/issues/2354
# As knex does not pass query parameters from the connection string we
# have to use environment variables in order to pass the desired values, e.g.
# PGSSLMODE=<value>

## Do not edit this

TZ=UTC
7 changes: 6 additions & 1 deletion server/db/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ dotenv.config({

module.exports = {
client: 'pg',
connection: process.env.DATABASE_URL,
connection: {
connectionString: process.env.DATABASE_URL,
ssl: {
Copy link
Member

Choose a reason for hiding this comment

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

It seems it will always try to connect using SSL, but this will cause an error if the server does not support SSL connections.

Copy link
Contributor Author

@orbatschow orbatschow Feb 28, 2023

Choose a reason for hiding this comment

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

It seems that setting the variable to false is equal to forcing an SSL connection, which explains, why non SSL connections are not working anymore.

Proposed fix:

function buildSSLConfig() {
  if (process.env.REJECT_UNAUTHORIZED_SSL_CERTIFICATE) {
    return {
      rejectUnauthorized: false,
    };
  }

  return false;
}

module.exports = {
  client: 'pg',
  connection: {
    connectionString: process.env.DATABASE_URL,
    ssl: buildSSLConfig(),
  },
  migrations: {
    tableName: 'migration',
    directory: path.join(__dirname, 'migrations'),
  },
  seeds: {
    directory: path.join(__dirname, 'seeds'),
  },
  wrapIdentifier: (value, origImpl) => origImpl(_.snakeCase(value)),
};

I will create a PR and also add documentation for environment variable within the .env.sample file. The default equals to true, so connections that don't require a SSL connection will be functional without any changes.

I also tested the changes locally with both variants, but it would be good if you do a second test on your own.

rejectUnauthorized: false,
},
},
migrations: {
tableName: 'migration',
directory: path.join(__dirname, 'migrations'),
Expand Down