Skip to content

feat: monitor MongoDB client #163

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 0 additions & 32 deletions .eslintrc.js

This file was deleted.

8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ typings/

# webstorm files
.idea/


# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
57 changes: 57 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// @ts-check
import { FlatCompat } from '@eslint/eslintrc';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

// mimic CommonJS variables -- not needed if using CommonJS
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const compat = new FlatCompat({
baseDirectory: __dirname, // optional; default: process.cwd()
resolvePluginsRelativeTo: __dirname, // optional
});

export default [
// mimic environments
...compat.env({
es2020: true,
node: true,
mocha: true,
jest: true,
}),

// mimic plugins
...compat.plugins('mocha', 'jest'),

// translate an entire config
...compat.config({
plugins: ['mocha', 'jest'],
extends: 'airbnb-base',
env: {
es2020: true,
node: true,
jest: true,
mocha: true,
},
rules: {
indent: ['error', 4, { SwitchCase: 1 }],
'no-underscore-dangle': [0],
'max-len': ['error', {
code: 140,
ignoreComments: true,
}],
'no-console': 0,
'object-curly-newline': ['error', {
ObjectPattern: { multiline: true },
}],
'newline-per-chained-call': ['error', {
ignoreChainWithDepth: 10,
}],
'object-property-newline': ['error', {
allowAllPropertiesOnSameLine: true,
}],
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
},
}),
];
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


/* eslint-disable global-require */
/* eslint-disable import/order */
const config = require('./infra/config');
const service = require('./infra');
const {
Expand Down Expand Up @@ -50,6 +50,7 @@ enabledComponents.forEach((key) => {
throw new Error(`Component '${key}'' is dependent on component '${dependency}' which is missing.`);
}
});
// eslint-disable-next-line import/no-dynamic-require
exportedComponents[component.name || key] = require(`./infra/${key}`);
});

Expand Down
2 changes: 1 addition & 1 deletion infra/__tests__/smoke_test.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { splitUriBySlash, getDbNameFromUri } = require('../helper');
const { MongoMemoryServer } = require('mongodb-memory-server');
const { splitUriBySlash, getDbNameFromUri } = require('../helper');
const mongoClient = require('../mongo');

/**
Expand Down
16 changes: 7 additions & 9 deletions infra/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


const _ = require('lodash');
const path = require('path');
const fs = require('fs');
Expand All @@ -15,8 +13,8 @@ function findAppRoot(dir = path.dirname(require.main.filename)) {
}

function getApproot() {
if ((process.env.NODE_ENV === 'test') &&
(_.includes(__dirname, 'node_modules'))) {
if ((process.env.NODE_ENV === 'test')
&& (_.includes(__dirname, 'node_modules'))) {
return path.resolve(__dirname).split('/node_modules')[0];
}
return findAppRoot();
Expand Down Expand Up @@ -93,9 +91,9 @@ base.logger = {
});
}
// human readable format
return `${options.timestamp()} ${options.level.toUpperCase()} >> ` +
`${options.message || ''}` +
`${options.meta && Object.keys(options.meta).length ? ` << ${JSON.stringify(options.meta)}` : ''}`;
return `${options.timestamp()} ${options.level.toUpperCase()} >> `
+ `${options.message || ''}`
+ `${options.meta && Object.keys(options.meta).length ? ` << ${JSON.stringify(options.meta)}` : ''}`;
},
},
basePath: null,
Expand All @@ -106,7 +104,7 @@ base.logger = {
correlationId: () => {
try {
return getRequestId();
} catch (err) {
} catch {
return {};
}
},
Expand All @@ -117,7 +115,7 @@ base.logger = {
authEntity.activeAccount = _.omit(authEntity.activeAccount, 'features');
}
return authEntity;
} catch (err) {
} catch {
return {};
}
},
Expand Down
10 changes: 5 additions & 5 deletions infra/encryption/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ function _encryptDecryptObjectValues(safeId, obj, keysToEncrypt = [], encrypt =
return Promise.resolve(obj);
}
const pairs = _.chain(keysToEncrypt)
.map(k => _.has(obj, k) && _.assign({ key: k, value: _.get(obj, k) }))
.map((k) => _.has(obj, k) && _.assign({ key: k, value: _.get(obj, k) }))
.compact()
.value();

const resObj = _.clone(obj);

return safe.getOrCreateSafe(safeId)
.then((safeObj) => {
const Promises = pairs.map(kv => _encryptDecryptValue(safeObj, kv.value, encrypt)
.then(res => _.set(resObj, kv.key, res)));
const Promises = pairs.map((kv) => _encryptDecryptValue(safeObj, kv.value, encrypt)
.then((res) => _.set(resObj, kv.key, res)));
return Promise.all(Promises);
})
.then(() => resObj);
Expand All @@ -35,13 +35,13 @@ function decryptObjectValues(safeId, obj, keysToEncrypt) {
}

function replaceEncryptedValues(encryptedObject = {}, keys = [], replaceWith = '*****') {
return Promise.map(keys, k => _.has(encryptedObject, k) && _.set(encryptedObject, k, replaceWith))
return Promise.map(keys, (k) => _.has(encryptedObject, k) && _.set(encryptedObject, k, replaceWith))
.then(() => encryptedObject);
}

module.exports = {
encryptObjectValues,
decryptObjectValues,
getSafe: safeId => safe.getOrCreateSafe(safeId),
getSafe: (safeId) => safe.getOrCreateSafe(safeId),
replaceEncryptedValues,
};
8 changes: 2 additions & 6 deletions infra/encryption/safe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@


const _ = require('lodash');
const Promise = require('bluebird');
const uuid = require('node-uuid');
const crypto = require('crypto');
const crypto = require('node:crypto');

const config = require('../config');
const mongoClient = require('../mongo');
Expand Down Expand Up @@ -45,7 +42,7 @@ function getOrCreateSafe(safeId) {

const newSafe = {
_id: safeId,
key: (new Buffer(uuid.v4())).toString('base64'), // eslint-disable-line
key: (Buffer.from(crypto.randomUUID())).toString('base64'),
};
return collection.insertOne(newSafe)
.then(() => new Safe(newSafe))
Expand Down Expand Up @@ -113,7 +110,6 @@ Safe.prototype.write_crypto = function (plaintext) { // eslint-disable-line
return deferred.promise;
};


Safe.prototype.read = Safe.prototype.read_crypto;
Safe.prototype.write = Safe.prototype.write_crypto;

Expand Down
4 changes: 0 additions & 4 deletions infra/eventbus.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


const Promise = require('bluebird');
const eventBus = require('@codefresh-io/eventbus');
const monitor = require('@codefresh-io/cf-monitor');
Expand Down Expand Up @@ -59,7 +57,6 @@ class Eventbus {
});
}


/**
* stops the connection to eventbus
* @returns {*}
Expand Down Expand Up @@ -119,5 +116,4 @@ class Eventbus {
}
}


module.exports = new Eventbus();
10 changes: 4 additions & 6 deletions infra/express.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const Promise = require('bluebird');
const express = require('express');
const compression = require('compression');
Expand Down Expand Up @@ -122,9 +121,9 @@ class Express {
const statusCode = err.statusCode || 500;
// check if err object has overridden toString method
// before sending toString() response to prevent [object Object] responses
const message = err.toString === Object.prototype.toString ?
(err.message || 'Internal server error') :
err.toString();
const message = err.toString === Object.prototype.toString
? (err.message || 'Internal server error')
: err.toString();
res.status(statusCode).send({ message });
});
});
Expand Down Expand Up @@ -154,10 +153,9 @@ class Express {
.then((ret) => {
res.send(ret);
})
.catch(err => next(err));
.catch((err) => next(err));
};
}
}


module.exports = new Express();
2 changes: 1 addition & 1 deletion infra/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ const splitUriBySlash = (uri) => {
};
};

const getDbNameFromUri = uri => splitUriBySlash(uri).dbName;
const getDbNameFromUri = (uri) => splitUriBySlash(uri).dbName;
module.exports = { splitUriBySlash, getDbNameFromUri };
10 changes: 4 additions & 6 deletions infra/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@


const monitor = require('@codefresh-io/cf-monitor');

monitor.init();
const Promise = require('bluebird'); // jshint ignore:line
const cflogs = require('cf-logs');
const { openapi } = require('@codefresh-io/cf-openapi');
const config = require('./config');
const eventbus = require('./eventbus');
const mongo = require('./mongo');
const processEvents = require('./process-events');
const express = require('./express');
const logging = require('./logging');
const redis = require('./redis');
const cflogs = require('cf-logs');
const { openapi } = require('@codefresh-io/cf-openapi');
const { publishInterface, subscribeInterface } = require('./openapi-events');

let logger;
Expand Down Expand Up @@ -87,7 +85,7 @@ class Microservice {
openapi.events().setSubscribeInterface(subscribeInterface);
}
})
.then(() => express.init(config, app => initFn(app, eventbus), opt))
.then(() => express.init(config, (app) => initFn(app, eventbus), opt))
.then(() => {
logger.info('Initialization completed');
this.markAsReady();
Expand All @@ -103,7 +101,7 @@ class Microservice {
// - first phase need to make sure to not accept any new requests/events
// - then a decent amount of time will be given to clear all on-going contexts
// - second phase will close all dependencies connections like mongo, postgres etc
stop() { // eslint-disable-line
stop() {
const enabledComponents = config.getConfigArray('enabledComponents');
const gracePeriod = config.gracePeriodTimers.totalPeriod;

Expand Down
3 changes: 0 additions & 3 deletions infra/logging.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


const Promise = require('bluebird');
const cflogs = require('cf-logs');
const { name: serviceName } = require('./config');
Expand Down Expand Up @@ -31,5 +29,4 @@ class Logging {
}
}


module.exports = new Logging();
Loading