From 2af3ad1ce5059d4d7b27e1795e73194c54b4ce63 Mon Sep 17 00:00:00 2001 From: Ania <31079643+AniaMakes@users.noreply.github.com> Date: Wed, 7 Jul 2021 09:13:17 +0100 Subject: [PATCH] ACC-1106 SSL fine tune for DB connection (#309) * bump ecma version to use spread operator * added ssl object to connection set up * run make verify on precommit rather than on prepush to avoid typo commits * amended the tests to expect the new object keys * fix issue with object assign not liking ... being not iterable * bumped package semver for release Co-authored-by: AniaMakes --- .eslintrc.js | 2 +- db/pg/index.js | 19 ++++++++++++------- package.json | 4 ++-- test/db/pg/index.spec.js | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 03dd8b4d..ecd72bc1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,7 +8,7 @@ const config = { 'node': true }, 'parserOptions': { - 'ecmaVersion': 2017, + 'ecmaVersion': 2018, 'sourceType': 'module' }, 'rules': { diff --git a/db/pg/index.js b/db/pg/index.js index 3e8ef003..47356157 100644 --- a/db/pg/index.js +++ b/db/pg/index.js @@ -12,6 +12,15 @@ const { DB } = require('config'); const MODULE_ID = path.relative(process.cwd(), module.id) || require(path.resolve('./package.json')).name; const log = new Logger({source: MODULE_ID}); +const sslSetUp = { + ssl: true, + extra: { + ssl: { + rejectUnauthorized: false, + }, + } +} + let db; module.exports = exports = async (options = DB) => { @@ -19,8 +28,7 @@ module.exports = exports = async (options = DB) => { log.info(`${MODULE_ID} creating new DB instance with options => `, options); if (options.uri) { - const conn = Object.assign({ ssl: { rejectUnauthorized : false } }, pgConn.parse(options.uri)); - + const conn = Object.assign({ ...sslSetUp}, pgConn.parse(options.uri)); log.info(`${MODULE_ID} creating new DB instance with URI String => `, conn); db = await massive(conn); @@ -31,13 +39,10 @@ module.exports = exports = async (options = DB) => { host: options.host, password: options.password, port: options.port, - user: options.user_name + user: options.user_name, + ...sslSetUp, }; - if (options.ssl === true) { - conn.ssl = { rejectUnauthorized : false }; - } - db = await massive(conn); } } diff --git a/package.json b/package.json index e2232c28..92cc54b3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ft-next-syndication-api", "description": "Next Syndication API", - "version": "0.38.1", + "version": "0.38.2", "private": true, "dependencies": { "@financial-times/n-es-client": "3.0.0", @@ -107,7 +107,7 @@ "husky": { "hooks": { "commit-msg": "node_modules/.bin/secret-squirrel-commitmsg", - "pre-commit": "node_modules/.bin/secret-squirrel", + "pre-commit": "node_modules/.bin/secret-squirrel && make verify -j3", "pre-push": "make verify -j3" } } diff --git a/test/db/pg/index.spec.js b/test/db/pg/index.spec.js index 198622f0..4783f178 100644 --- a/test/db/pg/index.spec.js +++ b/test/db/pg/index.spec.js @@ -57,9 +57,42 @@ describe(MODULE_ID, function () { port: 'DB_PORT', database: 'DB_NAME', user: 'DB_USER', - password: 'DB_PASSWORD' + password: 'DB_PASSWORD', + ssl: true, + extra: { + ssl: { + rejectUnauthorized: false, + }, + } }); expect(DB).to.equal(db); }); + + it('creates a new database instance if options do not match', async function() { + beforeEach(() => { + db = null; + }) + + const DB = await underTest({ + uri: 'DATABASE_URL' + }); + + expect(massiveStub).to.have.been.calledWith({ + database: 'DATABASE_URL', + user: '', + password: '', + port: null, + host: null, + ssl: true, + extra: { + ssl: { + rejectUnauthorized: false, + }, + } + }); + + expect(DB).to.equal(db); + }) + });