From 966c15540ebcf594da1c9dcbecef32c883ff2b00 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 30 Nov 2023 10:16:19 +0100 Subject: [PATCH] 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) {