Skip to content

Commit

Permalink
Merge pull request #758 from telefonicaid/task/watch_and_reload_confi…
Browse files Browse the repository at this point in the history
…gTrust_file_changes

Task/watch and reload config trust file changes
  • Loading branch information
fgalan authored Nov 30, 2023
2 parents cee3e9e + 8c35be2 commit 57ba8ab
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add: watch changes and reload configTrust file (#757)
15 changes: 8 additions & 7 deletions configTrust.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ var configTrust = {};

configTrust.trusts = [
{
host: 'keystone',
host: 'iot-keystone',
port: '5001',
id: 'trust1',
user: 'user1',
user: 'adm1',
password: 'password',
service: 'domain1'
service: 'smartcity'
},
{
host: 'keystone',
host: 'iot-keystone',
port: '5001',
id: 'trust2',
user: 'user2',
password: 'password2',
service: 'domain2'
user: 'foo',
password: 'password',
service: 'smartcity'
}
];
exports.configTrust = configTrust;
35 changes: 31 additions & 4 deletions lib/models/keystone.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,41 @@
var util = require('util'),
request = require('request'),
logger = require('logops'),
configTrust = require('../../configTrust'),
configTrust = require('../../configTrust.js').configTrust,
alarm = require('../alarm'),
errors = {};

const fs = require('fs');
const configTrustFilePath = './configTrust.js';

function requireUncached(module) {
delete require.cache[require.resolve(module)];
return require(module);
}

function watchConfigTrustFile() {
fs.watch(configTrustFilePath, (event, filename) => {
logger.info('watchConfigTrustFile changed by %s detected in file %s', event, filename);
try {
configTrust = requireUncached('../../configTrust.js').configTrust;
logger.debug('reloaded configTrust %j', configTrust);
} catch (err) {
logger.error('Error %s reloading module: %s ', err, filename);
}
});
}

function getToken(trust, callback) {
var trustConf = configTrust.trusts.find((item) => item.id === trust);
logger.debug('getToken by trust %s from configTrust %j', trust, configTrust);
var trustConf = null;
if (configTrust && configTrust.trusts && configTrust.trusts.length > 0) {
trustConf = configTrust.trusts.find((item) => item.id === trust);
logger.info('getToken with trustConf %j', trustConf);
}

// check trust was found or log it
if (!trustConf) {
logger.error('Trust [%s] not found in configTrust file', trust);
logger.error('Trust %s not found in configTrust file with content %s', trust, configTrust);
callback(new errors.TokenRetrievalError(trust, 'trust not found' + trust));
}
var options = {
Expand Down Expand Up @@ -62,7 +88,7 @@ function getToken(trust, callback) {
}
};

logger.debug('retrieving token with trust [%s]', trust);
logger.debug('retrieving token with request options %j', options);

request(options, function handleResponse(error, response /*, body*/) {
if (error) {
Expand All @@ -88,6 +114,7 @@ function getToken(trust, callback) {
}

exports.getToken = getToken;
exports.watchConfigTrustFile = watchConfigTrustFile;

(function() {
errors.TokenRetrievalError = function(trust, msg) {
Expand Down
4 changes: 2 additions & 2 deletions lib/models/updateAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function getCachedToken(service, subservice, name) {

function generateToken(trust, cached) {
cached.generating = true;
logger.info('start generating token');
logger.info('start generating token with trust %s', trust);
// Here we call KeyStone to generate a token, we'll entry into the event loop
keystone.getToken(trust, function(error, token) {
if (!error) {
Expand Down Expand Up @@ -473,7 +473,7 @@ function doRequestV2(action, event, token, callback) {

function makeTokenListenerFunc(action, event, version, callback) {
return function tokenHandlerFunc(error, token) {
logger.debug('tokenHandlerFunc %s %s', error, token);
logger.debug('tokenHandlerFunc error:%s token:%s', error, token);
if (error || !token) {
return callback(error);
} else {
Expand Down
2 changes: 2 additions & 0 deletions lib/perseo.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var domain = require('domain'),
versionRoutes = require('./routes/versionController'),
metricsRoutes = require('./routes/metricsController'),
rules = require('./models/rules'),
keystone = require('./models/keystone'),
metrics = require('./models/metrics'),
app = express(),
server,
Expand Down Expand Up @@ -169,6 +170,7 @@ function start(callbackStart) {
],
function(err) {
require('./refreshCore'); // Start periodical update of rules at core
keystone.watchConfigTrustFile();
if (err && err.httpCode >= 500) {
// just propagate to perseo.bin 500 error
myutils.logErrorIf(err, null, context);
Expand Down
2 changes: 1 addition & 1 deletion test/utils/utilsT.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var fs = require('fs'),
path = require('path'),
MongoClient = require('mongodb').MongoClient,
config = require('../../config'),
configTrust = require('../../configTrust'),
configTrust = require('../../configTrust.js').configTrust,
fakeServerPort = 9753,
fakeServerCode = 200,
fakeServerMessage = 'All right',
Expand Down

0 comments on commit 57ba8ab

Please sign in to comment.