From 45de2ebd826f110408961fa882a74e1195e929f2 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 28 Nov 2023 16:53:24 +0100 Subject: [PATCH 1/6] watch changes and reload configTrust file --- lib/models/keystone.js | 20 ++++++++++++++++++++ lib/perseo.js | 2 ++ 2 files changed, 22 insertions(+) diff --git a/lib/models/keystone.js b/lib/models/keystone.js index 61c93f9a..0014db02 100644 --- a/lib/models/keystone.js +++ b/lib/models/keystone.js @@ -29,6 +29,25 @@ var util = require('util'), alarm = require('../alarm'), errors = {}; +const fs = require('fs'); +const filePath = '/opt/perseo-fe/configTrust.js'; + +function requireUncached(module) { + delete require.cache[require.resolve(module)]; + return require(module); +} + +function watchConfigTrustFile() { + fs.watch(filePath, (event, filename) => { + logger.debug('watchConfigTrustFile changed %s detected in file %s', filename, event); + try { + configTrust = requireUncached(filePath); + } catch (err) { + logger.error('Error %s reloading module: %s ', err, filename); + } + }); +} + function getToken(trust, callback) { var trustConf = configTrust.trusts.find((item) => item.id === trust); // check trust was found or log it @@ -88,6 +107,7 @@ function getToken(trust, callback) { } exports.getToken = getToken; +exports.watchConfigTrustFile = watchConfigTrustFile; (function() { errors.TokenRetrievalError = function(trust, msg) { diff --git a/lib/perseo.js b/lib/perseo.js index 4c3ed0fa..06697be5 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -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, @@ -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); From 115bffddd82dd09e9e8a247f29db6c8b3f6417ff Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 28 Nov 2023 16:54:01 +0100 Subject: [PATCH 2/6] update CNR --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index e69de29b..31ea38e3 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -0,0 +1 @@ +- Add: Watch changes and reload configTrust file From 966c15540ebcf594da1c9dcbecef32c883ff2b00 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 30 Nov 2023 10:16:19 +0100 Subject: [PATCH 3/6] fix configTrust --- configTrust.js | 15 ++++++++------- lib/models/keystone.js | 23 +++++++++++++++-------- lib/models/updateAction.js | 2 +- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/configTrust.js b/configTrust.js index 0b495482..368d9e90 100644 --- a/configTrust.js +++ b/configTrust.js @@ -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; diff --git a/lib/models/keystone.js b/lib/models/keystone.js index 0014db02..5107d03a 100644 --- a/lib/models/keystone.js +++ b/lib/models/keystone.js @@ -25,12 +25,12 @@ 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 filePath = '/opt/perseo-fe/configTrust.js'; +const configTrustFilePath = './configTrust.js'; function requireUncached(module) { delete require.cache[require.resolve(module)]; @@ -38,10 +38,11 @@ function requireUncached(module) { } function watchConfigTrustFile() { - fs.watch(filePath, (event, filename) => { - logger.debug('watchConfigTrustFile changed %s detected in file %s', filename, event); + fs.watch(configTrustFilePathh, (event, filename) => { + logger.info('watchConfigTrustFile changed by %s detected in file %s', event, filename); try { - configTrust = requireUncached(filePath); + configTrust = requireUncached('../../configTrust.js').configTrust; + logger.debug('reloaded configTrust %j', configTrust); } catch (err) { logger.error('Error %s reloading module: %s ', err, filename); } @@ -49,10 +50,16 @@ function watchConfigTrustFile() { } 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 = { @@ -81,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) { diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index bcc5c3ef..cdebd135 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -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) { From 01ee444bc5c8a39fa5e45679b36da9f12213d3e9 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 30 Nov 2023 10:36:45 +0100 Subject: [PATCH 4/6] fix test --- lib/models/keystone.js | 2 +- lib/models/updateAction.js | 2 +- test/utils/utilsT.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/models/keystone.js b/lib/models/keystone.js index 5107d03a..1fd2516b 100644 --- a/lib/models/keystone.js +++ b/lib/models/keystone.js @@ -38,7 +38,7 @@ function requireUncached(module) { } function watchConfigTrustFile() { - fs.watch(configTrustFilePathh, (event, filename) => { + fs.watch(configTrustFilePath, (event, filename) => { logger.info('watchConfigTrustFile changed by %s detected in file %s', event, filename); try { configTrust = requireUncached('../../configTrust.js').configTrust; diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index cdebd135..b9cbf3f4 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -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 { diff --git a/test/utils/utilsT.js b/test/utils/utilsT.js index 1139af3e..d10a9221 100644 --- a/test/utils/utilsT.js +++ b/test/utils/utilsT.js @@ -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', From 6317c05ab7625edef158d8aca2faeaff19f90513 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 30 Nov 2023 11:12:18 +0100 Subject: [PATCH 5/6] update CNR --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 31ea38e3..e4dc63fa 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ -- Add: Watch changes and reload configTrust file +- Add: Watch changes and reload configTrust file (#757) From 8c35be28eb838786731897a0b322083d9bd2f144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Thu, 30 Nov 2023 11:40:13 +0100 Subject: [PATCH 6/6] Update CHANGES_NEXT_RELEASE --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index e4dc63fa..a3b55953 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ -- Add: Watch changes and reload configTrust file (#757) +- Add: watch changes and reload configTrust file (#757)