Skip to content

Commit

Permalink
Merge 5538b30 into 6693524
Browse files Browse the repository at this point in the history
  • Loading branch information
shriramshankar committed Sep 13, 2017
2 parents 6693524 + 5538b30 commit 6a55d15
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const configModule = require('../config/config');
const repeater = require('../repeater/repeater');
const flush = require('../sampleQueue/sampleQueueOps').flush;
const sendHeartbeat = require('../heartbeat/heartbeat').sendHeartbeat;
const fs = require('fs');
const registryFile = require('../constants').registryLocation;
const registryFileUtils = require('../utils/registryFileUtils');

Expand Down
4 changes: 1 addition & 3 deletions src/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
const debug = require('debug')('refocus-collector:config');
const common = require('../utils/commonUtils');
const errors = require('../errors');
const fs = require('fs');
const validator = require('validator');
const registryFileUtils = require('../utils/registryFileUtils');
const registrySchema = require('../utils/schema').registry;
const refocusInstanceSchema = require('../utils/schema').refocusInstance;
Expand Down Expand Up @@ -95,5 +93,5 @@ function init(reg) {

module.exports = {
init,
validateRegistry, // export for testing only
validateRegistry, // exported for testing only
};
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports = {
// exported for the purpose of testing
mockRegistryLocation: './test/config/testRegistry.json',

// TODO: use the encryptionAlgorithm sent by refocus in the heartbeat response
encryptionAlgorithm: 'aes-256-cbc',

// generators directory location
// TODO - remove once we're getting generators from Refocus
generatorsDir: './generators',
Expand Down
8 changes: 4 additions & 4 deletions src/heartbeat/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ function handleHeartbeatResponse(err, res) {
}

if (res) {
utils.updateCollectorConfig(res.collectorConfig);
utils.addGenerator(res.generatorsAdded);
utils.deleteGenerator(res.generatorsDeleted);
utils.updateGenerator(res.generatorsUpdated);
utils.updateCollectorConfig(res);
utils.addGenerator(res);
utils.deleteGenerator(res);
utils.updateGenerator(res);
}

const config = configModule.getConfig();
Expand Down
59 changes: 41 additions & 18 deletions src/heartbeat/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ const debug = require('debug')('refocus-collector:heartbeat');
const configModule = require('../config/config');
const repeater = require('../repeater/repeater');
const logger = require('winston');
const commonUtils = require('../utils/commonUtils');

// TODO: use the encryptionAlgorithm sent by refocus in the heartbeat response
const encryptionAlgorithm = require('../constants').encryptionAlgorithm;

/**
* Update the "collectorConfig" attribute of the config.
*
* @param {Object} collectorConfig - Heartbeat Response's "collectorConfig"
* attribute
* @param {Object} res - The Heartbeat Response object
*/
function updateCollectorConfig(collectorConfig) {
function updateCollectorConfig(res) {
const collectorConfig = res.collectorConfig;

// get a fresh copy of collector config
const config = configModule.getConfig();
if (collectorConfig) {
Expand All @@ -39,9 +44,13 @@ function updateCollectorConfig(collectorConfig) {
*
* @param {Object} ctx - The context from the generator
* @param {Object} def - The contextDefinition from the generator template
* @param {Object} refocusInstance - The refocus instance object, contaning the
* instance name, the refocus collector token for this instance and the refocus
* instance url
* @param {Object} res - The heartbeat response object
* @returns {Object} the context object with default values populated
*/
function assignContextDefaults(ctx, def) {
function assignContext(ctx, def, refocusInstance, res) {
if (!ctx) {
ctx = {};
}
Expand All @@ -50,10 +59,19 @@ function assignContextDefaults(ctx, def) {
def = {};
}

const heartbeatTimestamp = res.timestamp;
const collectorToken = refocusInstance.token;
const secret = collectorToken + heartbeatTimestamp;

Object.keys(def).forEach((key) => {
if (!ctx.hasOwnProperty(key) && def[key].hasOwnProperty('default')) {
ctx[key] = def[key].default;
}

if (ctx.hasOwnProperty(key) && def.hasOwnProperty(key) &&
def[key].encrypted) {
ctx[key] = commonUtils.decrypt(ctx[key], secret, encryptionAlgorithm);
}
});

debug('assignContextDefaults returning', ctx);
Expand Down Expand Up @@ -87,19 +105,21 @@ function setupRepeater(generator) {
* Function to setup a generator repeater and add the generator to the
* collector config.
*
* @param {Array} generators - Heartbeat Response's "generatorsAdded"
* attribute, an array of generators
* @param {Object} res - The Heartbeat Response object
*/
function addGenerator(generators) {
function addGenerator(res) {
const generators = res.generatorsAdded;

// Get a fresh copy of collector config
const config = configModule.getConfig();
const refocusInstance = config.refocusInstance;
if (generators) {
if (Array.isArray(generators)) {
// Create a new repeater for each generator and add to config.
generators.forEach((g) => {
if (g.generatorTemplate.contextDefinition) {
g.context = assignContextDefaults(g.context,
g.generatorTemplate.contextDefinition);
g.context = assignContext(g.context,
g.generatorTemplate.contextDefinition, refocusInstance, res);
}

config.generators[g.name] = g;
Expand All @@ -119,10 +139,11 @@ function addGenerator(generators) {
* Function to stop the generator repeater and delete the generator from the
* collector config.
*
* @param {Array} generators - Heartbeat Response's "generatorsDeleted"
* attribute, an array of generators
* @param {Object} res - The Heartbeat Response object
*/
function deleteGenerator(generators) {
function deleteGenerator(res) {
const generators = res.generatorsDeleted;

// Get a fresh copy of collector config
const config = configModule.getConfig();
if (generators) {
Expand All @@ -145,19 +166,21 @@ function deleteGenerator(generators) {
/**
* Function to update the generator repeater and the collector config.
*
* @param {Object} generators - Heartbeat Response's "generatorsUpdated"
* attribute, an array of generators.
* @param {Object} res - The Heartbeat Response object
*/
function updateGenerator(generators) {
function updateGenerator(res) {
const generators = res.generatorsUpdated;

// Get a fresh copy of collector config.
const config = configModule.getConfig();
const refocusInstance = config.refocusInstance;
if (generators) {
if (Array.isArray(generators)) {
// Update the repeater for the generators and update the generator config.
generators.forEach((g) => {
if (g.generatorTemplate.contextDefinition) {
g.context = assignContextDefaults(g.context,
g.generatorTemplate.contextDefinition);
g.context = assignContext(g.context,
g.generatorTemplate.contextDefinition, refocusInstance, res);
}

Object.keys(g).forEach((key) => {
Expand All @@ -183,7 +206,7 @@ function updateGenerator(generators) {

module.exports = {
addGenerator,
assignContextDefaults, // exporting for testing purposes only
assignContext, // exporting for testing purposes only
deleteGenerator,
updateGenerator,
updateCollectorConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/sampleQueue/sampleUpsertUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
/**
* Send the upsert and handle any errors in the response.
*
* @param {Object} refocusInstances contains Refocus url and token,
* @param {Object} refocusInstance contains Refocus url and token,
* @param {Array} arr is the array of samples to upsert;
* @throws {ValidationError} if argument(s) is missing,
* or in a wrong format.
Expand Down
32 changes: 31 additions & 1 deletion src/utils/commonUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
*/

/**
* ./src/utils/commonUtils.js
* src/utils/commonUtils.js
* Common utilities.
*/
const fs = require('fs');
const util = require('util');
const errors = require('../errors');
const debug = require('debug')('refocus-collector:commonUtils');
const path = require('path');
const crypto = require('crypto');

module.exports = {

Expand Down Expand Up @@ -72,4 +73,33 @@ module.exports = {

return fileContents;
},

/**
* Decrypt encrypted data using passed in secretKey and algorithm
* @param {String} data - Data to be decrypted
* @param {String} secretKey - Secret key that was used from encryption
* @param {String} algorithm - Name of the encryption algorithm
* @returns {String} - encrypted data
*/
decrypt(data, secretKey, algorithm) {
const decipher = crypto.createDecipher(algorithm, secretKey);
let decrypted = decipher.update(data, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
}, // decrypt

/**
* Encrypt the given data using passed in secretKey and algorithm
* @param {String} data - Data to be encrypted
* @param {String} secretKey - Secret key for encryption
* @param {String} algorithm - Name of the encryption algorithm
* @returns {String} - encrypted data
*/
encrypt(data, secretKey, algorithm) {
const cipher = crypto.createCipher(algorithm, secretKey);
let crypted = cipher.update(data, 'utf-8', 'hex');
crypted += cipher.final('hex');
return crypted;
}, // encrypt

};

0 comments on commit 6a55d15

Please sign in to comment.