Skip to content

Commit

Permalink
fix configTrust
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Nov 30, 2023
1 parent 115bffd commit 966c155
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
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;
23 changes: 15 additions & 8 deletions lib/models/keystone.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +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 filePath = '/opt/perseo-fe/configTrust.js';
const configTrustFilePath = './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);
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);
}
});
}

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 @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion 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

0 comments on commit 966c155

Please sign in to comment.