Skip to content

Commit

Permalink
ACC-1106 SSL fine tune for DB connection (#309)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
AniaMakes and AniaMakes authored Jul 7, 2021
1 parent c8ee0b0 commit 2af3ad1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const config = {
'node': true
},
'parserOptions': {
'ecmaVersion': 2017,
'ecmaVersion': 2018,
'sourceType': 'module'
},
'rules': {
Expand Down
19 changes: 12 additions & 7 deletions db/pg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ 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) => {
if (!db || 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);
Expand All @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
Expand Down
35 changes: 34 additions & 1 deletion test/db/pg/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})

});

0 comments on commit 2af3ad1

Please sign in to comment.