Skip to content

Commit 3c838c4

Browse files
feat: support SSL for Postgres connection in @codefresh-io/eventbus (#165)
* ci: bump required Node.js to 20 * feat: add `POSTGRES_SSL_ENABLE` env variable for `@codefresh-io/eventbus` * ci: remove redundant `eslint` command * build: bump `mongodb` package * refactor: replace `node-uuid` with `node:crypto` * ci: delete obsolete CI spec * build: optimize package size
1 parent d47cce5 commit 3c838c4

19 files changed

+1321
-866
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__tests__/**/*
2+
**/*spec.js

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v18.0.0
1+
v20

codefresh.yaml

Lines changed: 0 additions & 28 deletions
This file was deleted.

infra/__tests__/smoke_test.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { splitUriBySlash, getDbNameFromUri } = require('../helper');
21
const { MongoMemoryServer } = require('mongodb-memory-server');
2+
const { splitUriBySlash, getDbNameFromUri } = require('../helper');
33
const mongoClient = require('../mongo');
44

55
/**

infra/config.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
const _ = require('lodash');
42
const path = require('path');
53
const fs = require('fs');
@@ -15,8 +13,8 @@ function findAppRoot(dir = path.dirname(require.main.filename)) {
1513
}
1614

1715
function getApproot() {
18-
if ((process.env.NODE_ENV === 'test') &&
19-
(_.includes(__dirname, 'node_modules'))) {
16+
if ((process.env.NODE_ENV === 'test')
17+
&& (_.includes(__dirname, 'node_modules'))) {
2018
return path.resolve(__dirname).split('/node_modules')[0];
2119
}
2220
return findAppRoot();
@@ -52,12 +50,14 @@ base.eventbus = {
5250
serviceName: name,
5351
};
5452

53+
/** @type {import('pg').PoolConfig} */
5554
base.postgres = {
5655
host: process.env.POSTGRES_HOST || APPLICATION_DOMAIN,
57-
port: process.env.POSTGRES_PORT || 5432,
56+
port: Number.parseInt(process.env.POSTGRES_PORT || '5432', 10),
5857
database: process.env.POSTGRES_DATABASE || 'postgres',
5958
user: process.env.POSTGRES_USER || 'postgres',
6059
password: process.env.POSTGRES_PASSWORD || 'postgres',
60+
ssl: process.env.POSTGRES_SSL_ENABLE === 'true',
6161
};
6262

6363
base.mongo = {
@@ -93,9 +93,9 @@ base.logger = {
9393
});
9494
}
9595
// human readable format
96-
return `${options.timestamp()} ${options.level.toUpperCase()} >> ` +
97-
`${options.message || ''}` +
98-
`${options.meta && Object.keys(options.meta).length ? ` << ${JSON.stringify(options.meta)}` : ''}`;
96+
return `${options.timestamp()} ${options.level.toUpperCase()} >> `
97+
+ `${options.message || ''}`
98+
+ `${options.meta && Object.keys(options.meta).length ? ` << ${JSON.stringify(options.meta)}` : ''}`;
9999
},
100100
},
101101
basePath: null,

infra/encryption/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ function _encryptDecryptObjectValues(safeId, obj, keysToEncrypt = [], encrypt =
1111
return Promise.resolve(obj);
1212
}
1313
const pairs = _.chain(keysToEncrypt)
14-
.map(k => _.has(obj, k) && _.assign({ key: k, value: _.get(obj, k) }))
14+
.map((k) => _.has(obj, k) && _.assign({ key: k, value: _.get(obj, k) }))
1515
.compact()
1616
.value();
1717

1818
const resObj = _.clone(obj);
1919

2020
return safe.getOrCreateSafe(safeId)
2121
.then((safeObj) => {
22-
const Promises = pairs.map(kv => _encryptDecryptValue(safeObj, kv.value, encrypt)
23-
.then(res => _.set(resObj, kv.key, res)));
22+
const Promises = pairs.map((kv) => _encryptDecryptValue(safeObj, kv.value, encrypt)
23+
.then((res) => _.set(resObj, kv.key, res)));
2424
return Promise.all(Promises);
2525
})
2626
.then(() => resObj);
@@ -35,13 +35,13 @@ function decryptObjectValues(safeId, obj, keysToEncrypt) {
3535
}
3636

3737
function replaceEncryptedValues(encryptedObject = {}, keys = [], replaceWith = '*****') {
38-
return Promise.map(keys, k => _.has(encryptedObject, k) && _.set(encryptedObject, k, replaceWith))
38+
return Promise.map(keys, (k) => _.has(encryptedObject, k) && _.set(encryptedObject, k, replaceWith))
3939
.then(() => encryptedObject);
4040
}
4141

4242
module.exports = {
4343
encryptObjectValues,
4444
decryptObjectValues,
45-
getSafe: safeId => safe.getOrCreateSafe(safeId),
45+
getSafe: (safeId) => safe.getOrCreateSafe(safeId),
4646
replaceEncryptedValues,
4747
};

infra/encryption/safe.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
2-
1+
const { randomUUID } = require('node:crypto');
32
const _ = require('lodash');
43
const Promise = require('bluebird');
5-
const uuid = require('node-uuid');
64
const crypto = require('crypto');
75

86
const config = require('../config');
@@ -45,7 +43,7 @@ function getOrCreateSafe(safeId) {
4543

4644
const newSafe = {
4745
_id: safeId,
48-
key: (new Buffer(uuid.v4())).toString('base64'), // eslint-disable-line
46+
key: (Buffer.from(randomUUID())).toString('base64'), // eslint-disable-line
4947
};
5048
return collection.insertOne(newSafe)
5149
.then(() => new Safe(newSafe))
@@ -113,7 +111,6 @@ Safe.prototype.write_crypto = function (plaintext) { // eslint-disable-line
113111
return deferred.promise;
114112
};
115113

116-
117114
Safe.prototype.read = Safe.prototype.read_crypto;
118115
Safe.prototype.write = Safe.prototype.write_crypto;
119116

infra/eventbus.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
const Promise = require('bluebird');
42
const eventBus = require('@codefresh-io/eventbus');
53
const monitor = require('@codefresh-io/cf-monitor');
@@ -39,6 +37,7 @@ class Eventbus {
3937
database: this.config.postgres.database,
4038
user: this.config.postgres.user,
4139
password: this.config.postgres.password,
40+
ssl: this.config.postgres.ssl,
4241
},
4342
microServiceName: this.config.eventbus.serviceName,
4443
});
@@ -59,7 +58,6 @@ class Eventbus {
5958
});
6059
}
6160

62-
6361
/**
6462
* stops the connection to eventbus
6563
* @returns {*}
@@ -119,5 +117,4 @@ class Eventbus {
119117
}
120118
}
121119

122-
123120
module.exports = new Eventbus();

infra/express.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const Promise = require('bluebird');
32
const express = require('express');
43
const compression = require('compression');
@@ -122,9 +121,9 @@ class Express {
122121
const statusCode = err.statusCode || 500;
123122
// check if err object has overridden toString method
124123
// before sending toString() response to prevent [object Object] responses
125-
const message = err.toString === Object.prototype.toString ?
126-
(err.message || 'Internal server error') :
127-
err.toString();
124+
const message = err.toString === Object.prototype.toString
125+
? (err.message || 'Internal server error')
126+
: err.toString();
128127
res.status(statusCode).send({ message });
129128
});
130129
});
@@ -154,10 +153,9 @@ class Express {
154153
.then((ret) => {
155154
res.send(ret);
156155
})
157-
.catch(err => next(err));
156+
.catch((err) => next(err));
158157
};
159158
}
160159
}
161160

162-
163161
module.exports = new Express();

infra/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ const splitUriBySlash = (uri) => {
88
};
99
};
1010

11-
const getDbNameFromUri = uri => splitUriBySlash(uri).dbName;
11+
const getDbNameFromUri = (uri) => splitUriBySlash(uri).dbName;
1212
module.exports = { splitUriBySlash, getDbNameFromUri };

infra/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
2-
31
const monitor = require('@codefresh-io/cf-monitor');
42

53
monitor.init();
64
const Promise = require('bluebird'); // jshint ignore:line
5+
const cflogs = require('cf-logs');
6+
const { openapi } = require('@codefresh-io/cf-openapi');
77
const config = require('./config');
88
const eventbus = require('./eventbus');
99
const mongo = require('./mongo');
1010
const processEvents = require('./process-events');
1111
const express = require('./express');
1212
const logging = require('./logging');
1313
const redis = require('./redis');
14-
const cflogs = require('cf-logs');
15-
const { openapi } = require('@codefresh-io/cf-openapi');
1614
const { publishInterface, subscribeInterface } = require('./openapi-events');
1715

1816
let logger;
@@ -87,7 +85,7 @@ class Microservice {
8785
openapi.events().setSubscribeInterface(subscribeInterface);
8886
}
8987
})
90-
.then(() => express.init(config, app => initFn(app, eventbus), opt))
88+
.then(() => express.init(config, (app) => initFn(app, eventbus), opt))
9189
.then(() => {
9290
logger.info('Initialization completed');
9391
this.markAsReady();

infra/logging.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
const Promise = require('bluebird');
42
const cflogs = require('cf-logs');
53
const { name: serviceName } = require('./config');
@@ -31,5 +29,4 @@ class Logging {
3129
}
3230
}
3331

34-
3532
module.exports = new Logging();

infra/mongo.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class Mongo {
3333
logger.info('Mongo db initialized');
3434
}
3535

36-
3736
/**
3837
* stops the connection to mongo
3938
*/

infra/openapi-events.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
const eventbus = require('./eventbus');
22

3-
const publishInterface = (serviceName) => eventbus.publish('openapi.push', { // eslint-disable-line
4-
aggregateId: serviceName,
5-
}, true, true);
3+
const publishInterface = (aggregateId) => {
4+
eventbus.publish(
5+
'openapi.push',
6+
{ aggregateId },
7+
true,
8+
true,
9+
);
10+
};
611

712
const subscribeInterface = (handler) => {
8-
eventbus.subscribe('openapi.push', (data) => {
9-
const serviceName = data.aggregateId;
10-
return Promise.resolve()
11-
.then(() => handler(serviceName));
12-
});
13+
eventbus.subscribe(
14+
'openapi.push',
15+
async (data) => handler(data.aggregateId),
16+
);
1317
};
1418

1519
module.exports = {

infra/process-events.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
const EventEmitter = require('events');
42
const Promise = require('bluebird');
53

@@ -36,5 +34,4 @@ class ProcessEvents extends EventEmitter {
3634
}
3735
}
3836

39-
4037
module.exports = new ProcessEvents();

infra/redis.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
const Promise = require('bluebird');
42
const monitor = require('@codefresh-io/cf-monitor');
53
const redis = require('redis');
@@ -24,14 +22,13 @@ class Redis {
2422
deferred.resolve();
2523
}, 30000);
2624

27-
this.client =
28-
redis.createClient({
29-
host: config.redis.url,
30-
port: config.redis.port,
31-
password: config.redis.password,
32-
db: config.redis.db,
33-
tls: config.redis.tls,
34-
});
25+
this.client = redis.createClient({
26+
host: config.redis.url,
27+
port: config.redis.port,
28+
password: config.redis.password,
29+
db: config.redis.db,
30+
tls: config.redis.tls,
31+
});
3532

3633
this.client.on('ready', () => {
3734
logger.info('Redis client ready');
@@ -55,7 +52,6 @@ class Redis {
5552
return deferred.promise;
5653
}
5754

58-
5955
/**
6056
* stops the connection to redis
6157
* @returns {*}

infra/validation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const customJsonSchemaStringJoi = Joi.extend((joi) => ({ // eslint-disable-line
3636
}));
3737
Joi.jsonSchemaString = customJsonSchemaStringJoi.jsonSchemaString;
3838

39-
4039
function validateField(field, val, options = { optional: false }) {
4140
if (val === undefined && _.get(options, 'optional')) {
4241
return Promise.resolve();
@@ -50,8 +49,8 @@ function validateFields(partialObject, options) {
5049
}
5150

5251
function validateWithContext(obj, context) {
53-
return this.validate(Object.assign({}, obj, context))
54-
.then(normalized => _.omit(normalized, Object.keys(context)));
52+
return this.validate({ ...obj, ...context })
53+
.then((normalized) => _.omit(normalized, Object.keys(context)));
5554
}
5655

5756
module.exports = {

0 commit comments

Comments
 (0)