From 225ae1b24d3148839b84ee653479e47af022232a Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 12 Dec 2023 12:07:41 +0100 Subject: [PATCH 01/13] remove domain dep --- lib/iotagent-manager.js | 65 +++++++++++++++--------- lib/services/configurationData.js | 22 ++++---- lib/services/configurations.js | 26 +++++----- lib/services/iotaRedirector.js | 71 +++++++++++++------------- lib/services/protocolData.js | 35 +++++++------ lib/services/protocols.js | 23 +++++---- lib/utils/logging.js | 84 +++++++++++++++++++++++++++++++ 7 files changed, 216 insertions(+), 110 deletions(-) create mode 100644 lib/utils/logging.js diff --git a/lib/iotagent-manager.js b/lib/iotagent-manager.js index 06813d9..6cb5317 100644 --- a/lib/iotagent-manager.js +++ b/lib/iotagent-manager.js @@ -36,28 +36,45 @@ const async = require('async'); const apply = async.apply; let northboundServer; const logger = require('logops'); -const context = { - op: 'IoTAManager.NorthboundServer' -}; +const loggingMiddleware = require('./utils/logging').requestLogger('IotAgent-Manager'); +// const context = { +// op: 'IoTAManager.NorthboundServer' +// }; + +/** + * Express middleware for tracing the complete request arriving to the IoTA in debug mode. + */ +function traceRequest(req, res, next) { + const logger = req.logger; + logger.debug('Request for path [%s] from [%s]', req.path, req.get('host')); + logger.debug('Headers:\n%j\n', req.headers); + logger.debug('Request for path [%s] query [%j] from [%s]', req.path, req.query, req.get('host')); + + if (req.is('json') || req.is('application/ld+json')) { + logger.debug('Body:\n\n%s\n\n', JSON.stringify(req.body, null, 4)); + } + + next(); +} function startServer(newConfig, callback) { let baseRoot = '/'; logger.format = logger.formatters.pipe; - logger.getContext = function domainContext() { - var domainObj = require('domain').active || {}; - return { - corr: domainObj.corr, - trans: domainObj.trans, - op: domainObj.op, - from: domainObj.from, - srv: domainObj.service, - subsrv: domainObj.subservice, - msg: domainObj.msg, - comp: config.componentName || 'IoTAgentManager' - }; - }; + // logger.getContext = function domainContext() { + // var domainObj = require('domain').active || {}; + // return { + // corr: domainObj.corr, + // trans: domainObj.trans, + // op: domainObj.op, + // from: domainObj.from, + // srv: domainObj.service, + // subsrv: domainObj.subservice, + // msg: domainObj.msg, + // comp: config.componentName || 'IoTAgentManager' + // }; + // }; northboundServer = { server: null, @@ -65,17 +82,19 @@ function startServer(newConfig, callback) { router: express.Router() }; - logger.info(context, 'Starting IoT Agent Manager listening on port [%s]', newConfig.server.port); - logger.debug(context, 'Using config:\n\n%s\n', JSON.stringify(newConfig, null, 4)); + logger.info('Starting IoT Agent Manager listening on port [%s]', newConfig.server.port); + logger.debug('Using config:\n\n%s\n', JSON.stringify(newConfig, null, 4)); northboundServer.app.set('port', newConfig.server.port); northboundServer.app.set('host', newConfig.server.host || '0.0.0.0'); northboundServer.app.set('etag', false); - northboundServer.app.use(iotaLib.requestDomain); + //northboundServer.app.use(iotaLib.requestDomain); + northboundServer.app.use(loggingMiddleware); northboundServer.app.use(bodyParser.json({ limit: newConfig.bodyParserLimit })); if (newConfig.logLevel && newConfig.logLevel === 'DEBUG') { - northboundServer.app.use(middleware.traceRequest); + //northboundServer.app.use(middleware.traceRequest); + northboundServer.app.use(traceRequest); } if (newConfig.server.baseRoot) { @@ -128,7 +147,7 @@ function start(newConfig, callback) { * */ function stop(callback) { - logger.info(context, 'Stopping IoTA Manager'); + logger.info('Stopping IoTA Manager'); if (northboundServer) { northboundServer.server.close(callback); @@ -142,10 +161,10 @@ function stop(callback) { * */ function handleShutdown(signal) { - logger.info(context, 'Received %s, starting shutdown processs', signal); + logger.info('Received %s, starting shutdown processs', signal); stop((err) => { if (err) { - logger.error(context, err); + logger.error(err); return process.exit(1); } return process.exit(0); diff --git a/lib/services/configurationData.js b/lib/services/configurationData.js index 4daa7b1..1a95793 100644 --- a/lib/services/configurationData.js +++ b/lib/services/configurationData.js @@ -26,11 +26,11 @@ const Configuration = require('../model/Configuration'); const iotagentLib = require('iotagent-node-lib'); const errors = require('../errors'); -const logger = require('logops'); +//const logger = require('logops'); const async = require('async'); -const context = { - op: 'IoTAManager.ConfigurationDB' -}; +// const context = { +// op: 'IoTAManager.ConfigurationDB' +// }; const provisioningAPITranslation = { name: 'id', service: 'service', @@ -44,7 +44,7 @@ const provisioningAPITranslation = { payloadType: 'payloadType' }; -function createGetWithFields(fields) { +function createGetWithFields(logger, fields) { return function () { const queryObj = {}; let i = 0; @@ -68,7 +68,7 @@ function createGetWithFields(fields) { const callback = arguments[i]; - logger.debug(context, 'Looking for configuration with params %j and query %j', fields, queryObj); + logger.debug('Looking for configuration with params %j and query %j', fields, queryObj); const query = Configuration.model.find(queryObj); @@ -98,7 +98,7 @@ function createGetWithFields(fields) { }; } -function save(protocol, description, iotagent, resource, configuration, oldConfiguration, callback) { +function save(logger, protocol, description, iotagent, resource, configuration, oldConfiguration, callback) { /* eslint-disable-next-line new-cap */ const configurationObj = oldConfiguration || new Configuration.model(); const attributeList = [ @@ -125,7 +125,7 @@ function save(protocol, description, iotagent, resource, configuration, oldConfi 'payloadType' ]; - logger.debug(context, 'Saving Configuration [%s][%s][%s]', protocol, iotagent, resource); + logger.debug('Saving Configuration [%s][%s][%s]', protocol, iotagent, resource); configurationObj.protocol = protocol; configurationObj.description = description; @@ -142,18 +142,18 @@ function save(protocol, description, iotagent, resource, configuration, oldConfi configurationObj[provisioningAPITranslation[attributeList[i]] || attributeList[i]] = description; } } - logger.debug(context, 'Saving Configuration %j translated to %j ', configuration, configurationObj); + logger.debug('Saving Configuration %j translated to %j ', configuration, configurationObj); configurationObj.save(callback); } exports.get = iotagentLib.alarms.intercept( iotagentLib.constants.MONGO_ALARM, - createGetWithFields(['apikey', 'resource', 'protocol', 'type']) + createGetWithFields(['logger', 'apikey', 'resource', 'protocol', 'type']) ); exports.save = iotagentLib.alarms.intercept(iotagentLib.constants.MONGO_ALARM, save); exports.list = iotagentLib.alarms.intercept( iotagentLib.constants.MONGO_ALARM, - createGetWithFields(['service', 'subservice', 'protocol', 'apikey', 'type', 'limit', 'offset']) + createGetWithFields(['logger', 'service', 'subservice', 'protocol', 'apikey', 'type', 'limit', 'offset']) ); diff --git a/lib/services/configurations.js b/lib/services/configurations.js index d2822f4..3386de8 100644 --- a/lib/services/configurations.js +++ b/lib/services/configurations.js @@ -23,12 +23,12 @@ const errors = require('../errors'); const configurationData = require('./configurationData'); -const logger = require('logops'); -const domain = require('../utils/domain'); -const fillService = domain.fillService; -var context = { - op: 'IoTAManager.Configurations' -}; +//const logger = require('logops'); +// const domain = require('../utils/domain'); +// const fillService = domain.fillService; +// var context = { +// op: 'IoTAManager.Configurations' +// }; const retrievingAPITranslation = { subservice: 'service_path', type: 'entity_type', @@ -57,7 +57,7 @@ function validateListParameters(req, res, next) { } } -function translateToApi(configurations) { +function translateToApi(logger, configurations) { const services = []; const attributeList = [ '_id', @@ -86,7 +86,7 @@ function translateToApi(configurations) { 'payloadType' ]; - logger.debug(context, 'configurations %j', configurations); + logger.debug('configurations %j', configurations); for (let j = 0; j < configurations.services.length; j++) { const service = {}; @@ -94,7 +94,7 @@ function translateToApi(configurations) { service[retrievingAPITranslation[attributeList[i]] || attributeList[i]] = configurations.services[j][attributeList[i]]; } - logger.debug(context, 'translated to %j', service); + logger.debug('translated to %j', service); services.push(service); } @@ -105,11 +105,9 @@ function translateToApi(configurations) { } function handleListRequest(req, res, next) { - context = fillService(context, { - service: req.headers['fiware-service'], - subservice: req.headers['fiware-servicepath'] - }); + const logger = req.logger; configurationData.list( + logger, req.headers['fiware-service'], req.headers['fiware-servicepath'], req.query.protocol, @@ -121,7 +119,7 @@ function handleListRequest(req, res, next) { if (error) { next(error); } else { - res.status(200).json(translateToApi(configurations)); + res.status(200).json(translateToApi(logger, configurations)); } } ); diff --git a/lib/services/iotaRedirector.js b/lib/services/iotaRedirector.js index 1cb2fbc..85ce067 100644 --- a/lib/services/iotaRedirector.js +++ b/lib/services/iotaRedirector.js @@ -23,15 +23,15 @@ const protocols = require('./protocolData'); const request = require('iotagent-node-lib').request; -const logger = require('logops'); +//const logger = require('logops'); const errors = require('../errors'); const _ = require('underscore'); const async = require('async'); -const domain = require('../utils/domain'); -const fillService = domain.fillService; -var context = { - op: 'IoTAManager.Redirector' -}; +// const domain = require('../utils/domain'); +// const fillService = domain.fillService; +// var context = { +// op: 'IoTAManager.Redirector' +// }; function guessCollection(body) { if (body.services) { @@ -42,11 +42,11 @@ function guessCollection(body) { return null; } -function extractProtocols(body) { +function extractProtocols(logger, body) { const collectionName = guessCollection(body); let protocols; - logger.debug(context, 'Extracting protocols from body for collection [%s]', collectionName); + logger.debug('Extracting protocols from body for collection [%s]', collectionName); function extractProtocol(previous, item) { if (item.protocol) { @@ -63,7 +63,7 @@ function extractProtocols(body) { if (collectionName) { protocols = body[collectionName].reduce(extractProtocol, []); - logger.debug(context, 'Protocols found: %j', protocols); + logger.debug('Protocols found: %j', protocols); return protocols; } @@ -71,18 +71,20 @@ function extractProtocols(body) { } function queryParamExtractor(req, res, next) { + const logger = req.logger; if (req.query.protocol) { - logger.debug(context, 'Protocol found in the query parameters [%s]', req.query.protocol); + logger.debug('Protocol found in the query parameters [%s]', req.query.protocol); req.protocolId = [req.query.protocol]; } else if (req.method !== 'GET') { req.splitAndRedirect = true; - req.protocolId = extractProtocols(req.body); + req.protocolId = extractProtocols(logger, req.body); } next(); } function getProtocols(req, res, next) { + const logger = req.logger; function addProtocolAsync(previous, item, callback) { protocols.get(item, function (error, protocol) { if (error) { @@ -104,7 +106,7 @@ function getProtocols(req, res, next) { } if (req.protocolId) { - logger.debug(context, 'Finding objects for the following protocols: %j', req.protocolId); + logger.debug('Finding objects for the following protocols: %j', req.protocolId); if (req.protocolId.length === 0) { next(new errors.ProtocolNotFound('', '')); @@ -115,7 +117,7 @@ function getProtocols(req, res, next) { next(error); }); } else { - logger.debug(context, 'Finding objects for all the protocols'); + logger.debug('Finding objects for all the protocols'); protocols.list(function (error, protocols) { if (error) { @@ -123,7 +125,7 @@ function getProtocols(req, res, next) { } else { req.protocolId = protocols.map(extractId); req.protocolObj = protocols.reduce(addProtocolSync, {}); - logger.debug(context, 'Finding objects for this protocol: %j', req.protocolId); + logger.debug('Finding objects for this protocol: %j', req.protocolId); next(); } }); @@ -187,6 +189,7 @@ function splitByProtocol(req) { * @return {Object} Request object prepared to be sent. */ function createRequest(req, protocol, body) { + const logger = req.logger; const options = { qs: _.clone(req.query), method: req.method, @@ -195,7 +198,7 @@ function createRequest(req, protocol, body) { const protocolObj = req.protocolObj[protocol]; let protocolAddress = protocolObj.iotagent; - logger.debug(context, 'creating request using protocol:\n\n%j\n', protocolObj); + logger.debug('creating request using protocol:\n\n%j\n', protocolObj); // Save original apikey before be overwrite by apikey from body (probably modified) if (req.query.apikey) { @@ -236,16 +239,17 @@ function createRequest(req, protocol, body) { delete options.headers['content-length']; options.headers.connection = 'close'; - context = fillService(context, { - service: options.headers['fiware-service'], - subservice: options.headers['fiware-servicepath'] - }); - logger.debug(context, 'Forwarding request:\n\n%j\n', options); + // context = fillService(context, { + // service: options.headers['fiware-service'], + // subservice: options.headers['fiware-servicepath'] + // }); + logger.debug('Forwarding request:\n\n%j\n', options); return options; } function createRequests(req, res, next) { + const logger = req.logger; function mapToBody(split, protocol) { return createRequest(req, protocol, split[protocol]); } @@ -253,11 +257,11 @@ function createRequests(req, res, next) { if (req.splitAndRedirect) { const split = splitByProtocol(req); - logger.debug(context, 'Creating request for multiple elements'); + logger.debug('Creating request for multiple elements'); req.requests = req.protocolId.map(mapToBody.bind(null, split)); } else { - logger.debug(context, 'Creating request for redirecting single requests'); + logger.debug('Creating request for redirecting single requests'); req.requests = []; for (let i = 0; i < req.protocolId.length; i++) { req.requests.push(createRequest(req, req.protocolId[i], req.body)); @@ -268,6 +272,7 @@ function createRequests(req, res, next) { } function processRequests(req, res, next) { + const logger = req.logger; function extractStatusCode(result) { if (result && result.length === 2 && result[0].statusCode) { return result[0].statusCode; @@ -276,7 +281,7 @@ function processRequests(req, res, next) { } function sendRequest(options, callback) { - logger.debug(context, 'Sending redirection with following options: %j', options); + logger.debug('Sending redirection with following options: %j', options); // Use original apikey, not apikey from body if (options.qs.apikeyReq) { options.qs.apikey = options.qs.apikeyReq; @@ -287,7 +292,6 @@ function processRequests(req, res, next) { // Parsing is done directly within the Got library. if (error.name === 'ParseError') { logger.error( - context, 'REDIRECTION-003: Error parsing response body from the redirected request: %s to [%s]', error, options.uri @@ -295,17 +299,11 @@ function processRequests(req, res, next) { return callback(new errors.TargetResponseError({ msg: error.name })); } - logger.error( - context, - 'REDIRECTION-001: Error found redirecting requests to [%s]: %j', - options.uri, - error - ); + logger.error('REDIRECTION-001: Error found redirecting requests to [%s]: %j', options.uri, error); return callback(new errors.TargetServerError(error)); } else if (response.statusCode >= 500) { logger.error( - context, 'REDIRECTION-002: Wrong status code detected [%s] and body response [%s] redirecting request to [%s]', response.statusCode, response.body, @@ -327,7 +325,7 @@ function processRequests(req, res, next) { if (response.statusCode === 409) { parseError = new errors.DuplicateError(body); } - logger.debug(context, 'Response body from the redirected request parsed: \n\n%j\n', body); + logger.debug('Response body from the redirected request parsed: \n\n%j\n', body); return callback(parseError, [response, body]); } else { return callback(null, [response, null]); @@ -354,7 +352,7 @@ function processRequests(req, res, next) { } for (let i = 0; i < results.length; i++) { - logger.debug(context, 'results[%s][1] %j ', i, results[i][1]); + logger.debug('results[%s][1] %j ', i, results[i][1]); if (results[i][1].count) { totalCount += results[i][1].count; @@ -389,7 +387,7 @@ function processRequests(req, res, next) { function requestHandler(error, results) { let combinedResult; if (error) { - logger.error(context, 'The redirection ended up in error: ', error); + logger.error('The redirection ended up in error: ', error); next(error); } else { const statusCodes = _.uniq(results.map(extractStatusCode)); @@ -413,7 +411,7 @@ function processRequests(req, res, next) { JSON.stringify(statusCodes) + '] redirecting request'; - logger.error(context, errorMsg); + logger.error(errorMsg); next(new errors.TargetServerError(errorMsg)); } @@ -442,9 +440,10 @@ exports.loadContextRoutes = loadContextRoutes; /* eslint-disable-next-line no-unused-vars */ function handleError(error, req, res, next) { + const logger = req.logger; let code = 500; - logger.warn(context, 'Error [%s] handling request: %s', error.name, error.message); + logger.warn('Error [%s] handling request: %s', error.name, error.message); if (error.code && String(error.code).match(/^[2345]\d\d$/)) { code = error.code; diff --git a/lib/services/protocolData.js b/lib/services/protocolData.js index c1bdd7b..e9a5246 100644 --- a/lib/services/protocolData.js +++ b/lib/services/protocolData.js @@ -28,19 +28,20 @@ const configurations = require('./configurationData'); const async = require('async'); const errors = require('../errors'); const apply = async.apply; -const logger = require('logops'); -const context = { - op: 'IoTAManager.ProtocolAPI' -}; +//const logger = require('logops'); +// const context = { +// op: 'IoTAManager.ProtocolAPI' +// }; -function processConfiguration(protocol, description, iotagent, resource, configuration, callback) { - configurations.get(configuration.apikey, resource, protocol, function (error, oldConfiguration) { +function processConfiguration(logger, protocol, description, iotagent, resource, configuration, callback) { + configurations.get(logger, configuration.apikey, resource, protocol, function (error, oldConfiguration) { if (error) { callback(error); } else if (oldConfiguration.services.length === 0) { - configurations.save(protocol, description, iotagent, resource, configuration, null, callback); + configurations.save(logger, protocol, description, iotagent, resource, configuration, null, callback); } else { configurations.save( + logger, protocol, description, iotagent, @@ -53,7 +54,7 @@ function processConfiguration(protocol, description, iotagent, resource, configu }); } -function cleanConfigurations(protocol, iotagent, resource, callback) { +function cleanConfigurations(logger, protocol, iotagent, resource, callback) { Configuration.model.deleteMany( { protocol, @@ -63,12 +64,11 @@ function cleanConfigurations(protocol, iotagent, resource, callback) { /* eslint-disable-next-line no-unused-vars */ function (error, commandResult) { if (error) { - logger.error(context, 'MONGODB-003: Internal MongoDB Error removing services from protocol: %s', error); + logger.error('MONGODB-003: Internal MongoDB Error removing services from protocol: %s', error); callback(new errors.InternalDbError(error)); } else { logger.debug( - context, 'Configurations for Protocol [%s][%s][%s] successfully removed.', protocol, iotagent, @@ -117,7 +117,7 @@ function listProtocol(callback) { }); } -function save(newProtocol, callback) { +function save(logger, newProtocol, callback) { getProtocol(newProtocol.protocol, function (error, protocol) { if (error && error.name !== 'PROTOCOL_NOT_FOUND') { callback(error); @@ -131,7 +131,9 @@ function save(newProtocol, callback) { protocolObj[attributeList[i]] = newProtocol[attributeList[i]]; } - actions.push(apply(cleanConfigurations, newProtocol.protocol, newProtocol.iotagent, newProtocol.resource)); + actions.push( + apply(cleanConfigurations, logger, newProtocol.protocol, newProtocol.iotagent, newProtocol.resource) + ); if (newProtocol.services) { actions.push( @@ -140,6 +142,7 @@ function save(newProtocol, callback) { newProtocol.services, apply( processConfiguration, + logger, newProtocol.protocol, newProtocol.description, newProtocol.iotagent, @@ -156,12 +159,12 @@ function save(newProtocol, callback) { }); } -function removeProtocol(id, callback) { +function removeProtocol(logger, id, callback) { const condition = { protocol: id }; - logger.debug(context, 'Removing protocol with id [%s]', id); + logger.debug('Removing protocol with id [%s]', id); /* eslint-disable-next-line no-unused-vars */ getProtocol(condition.protocol, function (error, protocol) { if (error) { @@ -169,10 +172,10 @@ function removeProtocol(id, callback) { } else { Protocol.model.deleteOne(condition, function (error, results) { if (error) { - logger.debug(context, 'Internal MongoDB Error getting device: %s', error); + logger.debug('Internal MongoDB Error getting device: %s', error); callback(new errors.InternalDbError(error)); } else { - logger.debug(context, 'Protocol [%s] successfully removed with results [%j]', id, results); + logger.debug('Protocol [%s] successfully removed with results [%j]', id, results); callback(null); } }); diff --git a/lib/services/protocols.js b/lib/services/protocols.js index 9244037..18f963f 100644 --- a/lib/services/protocols.js +++ b/lib/services/protocols.js @@ -25,16 +25,17 @@ const protocolTemplate = require('../templates/protocol.json'); const Protocol = require('../model/Protocol'); const protocols = require('./protocolData'); const async = require('async'); -const logger = require('logops'); +//const logger = require('logops'); const middleware = require('iotagent-node-lib').middlewares; -const context = { - op: 'IoTAManager.ProtocolAPI' -}; +// const context = { +// op: 'IoTAManager.ProtocolAPI' +// }; function readProtocolList(req, res, next) { + const logger = req.logger; const condition = {}; - logger.debug(context, 'List protocols request with args %j', JSON.stringify(req.query)); + logger.debug('List protocols request with args %j', JSON.stringify(req.query)); const query = Protocol.model.find(condition).sort(); @@ -51,10 +52,10 @@ function readProtocolList(req, res, next) { results ) { if (error) { - logger.error(context, 'error: %j', error); + logger.error('error: %j', error); next(error); } else { - logger.debug(context, 'results of query: %j', results); + logger.debug('results of query: %j', results); req.protocolList = results[0]; req.protocolCount = results[1]; next(); @@ -63,9 +64,10 @@ function readProtocolList(req, res, next) { } function saveProtocol(req, res, next) { - logger.debug(context, 'Update/create protocol request: %j', req.body); + const logger = req.logger; + logger.debug('Update/create protocol request: %j', req.body); - protocols.save(req.body, next); + protocols.save(logger, req.body, next); } /* eslint-disable-next-line no-unused-vars */ function handleProtocolList(req, res, next) { @@ -80,7 +82,8 @@ function returnProtocolCreationResponse(req, res, next) { } /* eslint-disable-next-line no-unused-vars */ function deleteProtocol(req, res, next) { - protocols.remove(req.params.id, function (error) { + const logger = req.logger; + protocols.remove(logger, req.params.id, function (error) { if (error) { res.status(error.code).json(error); } else { diff --git a/lib/utils/logging.js b/lib/utils/logging.js new file mode 100644 index 0000000..d9e9d62 --- /dev/null +++ b/lib/utils/logging.js @@ -0,0 +1,84 @@ +/* + * Copyright 2014 Telefonica Investigaci�n y Desarrollo, S.A.U + * + * This file is part of iotagent-manager + * + * iotagent-manager is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * iotagent-manager is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with iotagent-manager. + * If not, seehttp://www.gnu.org/licenses/. + * + * For those usages not covered by the GNU Affero General Public License + * please contact with::[iot_support@tid.es] + */ + +'use strict'; + +var uuid = require('uuid'), + constants = require('../constants'); + +var logger = require('logops'); + +/** + * Express middleWare that creates a domain per request + * It also generates a unique request id that can be used to track requests in logs. + * + * @return {Function} Express middleWare. + */ + +function requestLogger(componentName) { + return function (req, res, next) { + let contextSrv; + if (req.headers && req.headers[constants.ORGANIZATION_HEADER]) { + contextSrv = req.headers[constants.ORGANIZATION_HEADER]; + } + let contextSubsrv; + if (req.headers && req.headers[constants.PATH_HEADER]) { + contextSubsrv = req.headers[constants.PATH_HEADER]; + } + let contextFrom; + // x-forwarded-for/forwarded overwrites x-real-ip + if (req.headers[constants.X_REAL_IP_HEADER]) { + contextFrom = req.headers[constants.X_REAL_IP_HEADER]; + } + if (req.headers[constants.X_FORWARDED_FOR_HEADER]) { + contextFrom = req.headers[constants.X_FORWARDED_FOR_HEADER]; + } + if (req.headers[constants.FORWARDED_HEADER]) { + contextFrom = req.headers[constants.FORWARDED_HEADER]; + } + let contextTrans = (req.requestId = uuid.v4()); + let contextCorr = req.get(constants.CORRELATOR_HEADER); + if (!contextCorr) { + contextCorr = contextTrans; + } + req.corr = contextCorr; // for propagate in FWD request + res.set(constants.CORRELATOR_HEADER, contextCorr); // for response + const contextStart = Date.now(); + req.logger = logger.child({ + corr: contextCorr, + trans: contextTrans, + op: req.url, + from: contextFrom, + srv: contextSrv, + subsrv: contextSubsrv, + comp: componentName + }); + res.once('finish', function () { + const responseTime = Date.now() - contextStart; + req.logger.debug('response-time: ' + responseTime + ' statusCode: ' + res.statusCode); + }); + next(); + }; +} + +exports.requestLogger = requestLogger; From 351a66acd41e517b70efe276517b8c4f67d746d4 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 12 Dec 2023 12:25:08 +0100 Subject: [PATCH 02/13] apply to dbConn --- lib/iotagent-manager.js | 2 +- lib/model/dbConn.js | 44 ++++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/iotagent-manager.js b/lib/iotagent-manager.js index 6cb5317..a0ee76b 100644 --- a/lib/iotagent-manager.js +++ b/lib/iotagent-manager.js @@ -139,7 +139,7 @@ function startServer(newConfig, callback) { function start(newConfig, callback) { config.setConfig(newConfig); - async.series([apply(startServer, newConfig), dbConn.configureDb], callback); + async.series([apply(startServer, newConfig), apply(dbConn.configureDb, logger)], callback); } /** diff --git a/lib/model/dbConn.js b/lib/model/dbConn.js index b4b56f1..9ee7b40 100644 --- a/lib/model/dbConn.js +++ b/lib/model/dbConn.js @@ -29,14 +29,14 @@ const mongoose = require('mongoose'); const config = require('../utils/commonConfig'); const constants = require('../utils/constants'); -const logger = require('logops'); +//const logger = require('logops'); const async = require('async'); const errors = require('../errors'); let defaultDb; const DEFAULT_DB_NAME = 'iotagent-manager'; -const context = { - op: 'IoTAManager.DbConn' -}; +// const context = { +// op: 'IoTAManager.DbConn' +// }; mongoose.Promise = global.Promise; // not including this causes DeprecationWarning @@ -50,7 +50,7 @@ function loadModels() { * * @this Reference to the dbConn module itself. */ -function init(host, db, port, username, password, options, callback) { +function init(logger, host, db, port, username, password, options, callback) { let url; let credentials = ''; let retries = 0; @@ -87,11 +87,11 @@ function init(host, db, port, username, password, options, callback) { /* eslint-disable-next-line no-unused-vars */ function createConnectionHandler(error, results) { if (defaultDb) { - logger.info(context, 'Successfully connected to MongoDB.'); + logger.info('Successfully connected to MongoDB.'); module.exports.db = defaultDb; loadModels(); } else { - logger.error(context, 'MONGODB-002: Error found after [%d] attempts: %s', retries, error || lastError); + logger.error('MONGODB-002: Error found after [%d] attempts: %s', retries, error || lastError); } callback(error); @@ -103,7 +103,6 @@ function init(host, db, port, username, password, options, callback) { function connectionAttempt(url, options, callback) { logger.info( - context, 'Attempting to connect to MongoDB instance with url %j and options %j. Attempt %d', url, options, @@ -116,39 +115,39 @@ function init(host, db, port, username, password, options, callback) { /* eslint-disable-next-line no-unused-vars */ const candidateDb = mongoose.createConnection(url, options, function (error, result) { if (error) { - logger.error(context, 'MONGODB-001: Error trying to connect to MongoDB: %s', error); + logger.error('MONGODB-001: Error trying to connect to MongoDB: %s', error); lastError = error; } else { defaultDb = candidateDb; defaultDb.on('error', function (error) { - logger.error(context, 'Mongo Driver error: %j', error); + logger.error('Mongo Driver error: %j', error); }); /* eslint-disable-next-line no-unused-vars */ defaultDb.on('connecting', function (error) { - logger.debug(context, 'Mongo Driver connecting'); + logger.debug('Mongo Driver connecting'); }); defaultDb.on('connected', function () { - logger.debug(context, 'Mongo Driver connected'); + logger.debug('Mongo Driver connected'); }); defaultDb.on('reconnected', function () { - logger.debug(context, 'Mongo Driver reconnected'); + logger.debug('Mongo Driver reconnected'); }); defaultDb.on('disconnected', function () { - logger.debug(context, 'Mongo Driver disconnected'); + logger.debug('Mongo Driver disconnected'); }); defaultDb.on('reconnectFailed', function () { - logger.error(context, 'MONGODB-004: MongoDB connection was lost'); + logger.error('MONGODB-004: MongoDB connection was lost'); process.exit(1); }); defaultDb.on('disconnecting', function () { - logger.debug(context, 'Mongo Driver disconnecting'); + logger.debug('Mongo Driver disconnecting'); }); defaultDb.on('open', function () { - logger.debug(context, 'Mongo Driver open'); + logger.debug('Mongo Driver open'); }); defaultDb.on('close', function () { - logger.debug(context, 'Mongo Driver close'); + logger.debug('Mongo Driver close'); }); } @@ -165,10 +164,10 @@ function init(host, db, port, username, password, options, callback) { retries++; if (retries === 1) { - logger.info(context, 'First connection attempt'); + logger.info('First connection attempt'); attempt(); } else { - logger.info(context, 'Waiting %d seconds before attempting again.', seconds); + logger.info('Waiting %d seconds before attempting again.', seconds); setTimeout(attempt, seconds * 1000); } } @@ -177,11 +176,11 @@ function init(host, db, port, username, password, options, callback) { async.whilst(retryCheck, tryCreateConnection, createConnectionHandler); } -function configureDb(callback) { +function configureDb(logger, callback) { const currentConfig = config.getConfig(); if (!currentConfig.mongodb || !currentConfig.mongodb.host) { - logger.fatal(context, 'No host found for MongoDB driver.'); + logger.fatal('No host found for MongoDB driver.'); callback(new errors.BadConfiguration('No host found for MongoDB driver')); } else { let dbName = currentConfig.mongodb.db; @@ -197,6 +196,7 @@ function configureDb(callback) { } init( + logger, config.getConfig().mongodb.host, dbName, port, From ac15cf542520aee6ad566c69488d587726c8a038 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 12 Dec 2023 16:08:15 +0100 Subject: [PATCH 03/13] fix require constants --- lib/utils/logging.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/logging.js b/lib/utils/logging.js index d9e9d62..4570c1e 100644 --- a/lib/utils/logging.js +++ b/lib/utils/logging.js @@ -24,7 +24,7 @@ 'use strict'; var uuid = require('uuid'), - constants = require('../constants'); + constants = require('./constants'); var logger = require('logops'); From f2d558319f0e69682ebe09d131923ce41b366353 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 13 Dec 2023 09:37:16 +0100 Subject: [PATCH 04/13] fix lint --- lib/utils/logging.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/utils/logging.js b/lib/utils/logging.js index 4570c1e..1aeebb9 100644 --- a/lib/utils/logging.js +++ b/lib/utils/logging.js @@ -21,12 +21,10 @@ * please contact with::[iot_support@tid.es] */ -'use strict'; +const { v4: uuidv4 } = require('uuid'); +const constants = require('./constants'); -var uuid = require('uuid'), - constants = require('./constants'); - -var logger = require('logops'); +const logger = require('logops'); /** * Express middleWare that creates a domain per request @@ -56,7 +54,8 @@ function requestLogger(componentName) { if (req.headers[constants.FORWARDED_HEADER]) { contextFrom = req.headers[constants.FORWARDED_HEADER]; } - let contextTrans = (req.requestId = uuid.v4()); + req.requestId = uuidv4(); + const contextTrans = req.requestId; let contextCorr = req.get(constants.CORRELATOR_HEADER); if (!contextCorr) { contextCorr = contextTrans; From a85e44eab00c7cdf376aef5a66d38fe6e2ba51de Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 18 Dec 2023 11:10:09 +0100 Subject: [PATCH 05/13] remove import --- lib/iotagent-manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iotagent-manager.js b/lib/iotagent-manager.js index a0ee76b..853920b 100644 --- a/lib/iotagent-manager.js +++ b/lib/iotagent-manager.js @@ -31,7 +31,7 @@ const protocols = require('./services/protocols'); const configurations = require('./services/configurations'); const iotaRedirector = require('./services/iotaRedirector'); const dbConn = require('./model/dbConn'); -const iotaLib = require('iotagent-node-lib'); +//const iotaLib = require('iotagent-node-lib'); const async = require('async'); const apply = async.apply; let northboundServer; From b46d312719caf1c37946d4ab77926cf66fde66ea Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 18 Dec 2023 11:13:54 +0100 Subject: [PATCH 06/13] do not log in handleError --- lib/services/iotaRedirector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/iotaRedirector.js b/lib/services/iotaRedirector.js index 85ce067..c0684eb 100644 --- a/lib/services/iotaRedirector.js +++ b/lib/services/iotaRedirector.js @@ -443,7 +443,7 @@ function handleError(error, req, res, next) { const logger = req.logger; let code = 500; - logger.warn('Error [%s] handling request: %s', error.name, error.message); + //TBD: logger.warn('Error [%s] handling request: %s', error.name, error.message); if (error.code && String(error.code).match(/^[2345]\d\d$/)) { code = error.code; From c8c104ea0cb0e7b7091cd889bd5c42b29abaa131 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 26 Feb 2024 10:22:06 +0100 Subject: [PATCH 07/13] fix access to logger --- lib/iotagent-manager.js | 9 ++++++++- lib/model/dbConn.js | 2 +- lib/services/configurationData.js | 14 +++++++------- lib/services/configurations.js | 1 - lib/services/protocolData.js | 2 +- lib/utils/constants.js | 6 +++++- test/unit/configuration-retrieval-test.js | 6 +++--- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/iotagent-manager.js b/lib/iotagent-manager.js index 853920b..fec1dfe 100644 --- a/lib/iotagent-manager.js +++ b/lib/iotagent-manager.js @@ -139,7 +139,14 @@ function startServer(newConfig, callback) { function start(newConfig, callback) { config.setConfig(newConfig); - async.series([apply(startServer, newConfig), apply(dbConn.configureDb, logger)], callback); + async.series( + [ + apply(startServer, newConfig), + //dbConn.configureDb], + apply(dbConn.configureDb, logger) + ], + callback + ); } /** diff --git a/lib/model/dbConn.js b/lib/model/dbConn.js index 9ee7b40..84713cf 100644 --- a/lib/model/dbConn.js +++ b/lib/model/dbConn.js @@ -29,7 +29,7 @@ const mongoose = require('mongoose'); const config = require('../utils/commonConfig'); const constants = require('../utils/constants'); -//const logger = require('logops'); +const logger = require('logops'); const async = require('async'); const errors = require('../errors'); let defaultDb; diff --git a/lib/services/configurationData.js b/lib/services/configurationData.js index 1a95793..0b899b0 100644 --- a/lib/services/configurationData.js +++ b/lib/services/configurationData.js @@ -26,7 +26,7 @@ const Configuration = require('../model/Configuration'); const iotagentLib = require('iotagent-node-lib'); const errors = require('../errors'); -//const logger = require('logops'); +const logger = require('logops'); const async = require('async'); // const context = { // op: 'IoTAManager.ConfigurationDB' @@ -44,7 +44,7 @@ const provisioningAPITranslation = { payloadType: 'payloadType' }; -function createGetWithFields(logger, fields) { +function createGetWithFields(fields) { return function () { const queryObj = {}; let i = 0; @@ -98,7 +98,7 @@ function createGetWithFields(logger, fields) { }; } -function save(logger, protocol, description, iotagent, resource, configuration, oldConfiguration, callback) { +function save(theLogger, protocol, description, iotagent, resource, configuration, oldConfiguration, callback) { /* eslint-disable-next-line new-cap */ const configurationObj = oldConfiguration || new Configuration.model(); const attributeList = [ @@ -125,7 +125,7 @@ function save(logger, protocol, description, iotagent, resource, configuration, 'payloadType' ]; - logger.debug('Saving Configuration [%s][%s][%s]', protocol, iotagent, resource); + theLogger.debug('Saving Configuration [%s][%s][%s]', protocol, iotagent, resource); configurationObj.protocol = protocol; configurationObj.description = description; @@ -142,18 +142,18 @@ function save(logger, protocol, description, iotagent, resource, configuration, configurationObj[provisioningAPITranslation[attributeList[i]] || attributeList[i]] = description; } } - logger.debug('Saving Configuration %j translated to %j ', configuration, configurationObj); + theLogger.debug('Saving Configuration %j translated to %j ', configuration, configurationObj); configurationObj.save(callback); } exports.get = iotagentLib.alarms.intercept( iotagentLib.constants.MONGO_ALARM, - createGetWithFields(['logger', 'apikey', 'resource', 'protocol', 'type']) + createGetWithFields(['apikey', 'resource', 'protocol', 'type']) ); exports.save = iotagentLib.alarms.intercept(iotagentLib.constants.MONGO_ALARM, save); exports.list = iotagentLib.alarms.intercept( iotagentLib.constants.MONGO_ALARM, - createGetWithFields(['logger', 'service', 'subservice', 'protocol', 'apikey', 'type', 'limit', 'offset']) + createGetWithFields(['service', 'subservice', 'protocol', 'apikey', 'type', 'limit', 'offset']) ); diff --git a/lib/services/configurations.js b/lib/services/configurations.js index a4003cc..aaee8f2 100644 --- a/lib/services/configurations.js +++ b/lib/services/configurations.js @@ -111,7 +111,6 @@ function translateToApi(logger, configurations) { function handleListRequest(req, res, next) { const logger = req.logger; configurationData.list( - logger, req.headers['fiware-service'], req.headers['fiware-servicepath'], req.query.protocol, diff --git a/lib/services/protocolData.js b/lib/services/protocolData.js index e9a5246..edbf04d 100644 --- a/lib/services/protocolData.js +++ b/lib/services/protocolData.js @@ -34,7 +34,7 @@ const apply = async.apply; // }; function processConfiguration(logger, protocol, description, iotagent, resource, configuration, callback) { - configurations.get(logger, configuration.apikey, resource, protocol, function (error, oldConfiguration) { + configurations.get(configuration.apikey, resource, protocol, function (error, oldConfiguration) { if (error) { callback(error); } else if (oldConfiguration.services.length === 0) { diff --git a/lib/utils/constants.js b/lib/utils/constants.js index a2b2c54..9ced005 100644 --- a/lib/utils/constants.js +++ b/lib/utils/constants.js @@ -35,5 +35,9 @@ module.exports = { DEFAULT_RESOURCE: '/iot/d', DEFAULT_MONGODB_RETRIES: 5, - DEFAULT_MONGODB_RETRY_TIME: 5 + DEFAULT_MONGODB_RETRY_TIME: 5, + + FORWARDED_HEADER: 'forwarded', + X_REAL_IP_HEADER: 'x-real-ip', + CORRELATOR_HEADER: 'fiware-correlator' }; diff --git a/test/unit/configuration-retrieval-test.js b/test/unit/configuration-retrieval-test.js index 2ee1b48..da97485 100644 --- a/test/unit/configuration-retrieval-test.js +++ b/test/unit/configuration-retrieval-test.js @@ -160,7 +160,7 @@ describe('Configuration list', function () { }); }); - describe('When a configuration list request with a limit 3 arrives to the IoTAM', function () { + describe('When a configuration list request with a limit 2 arrives to the IoTAM', function () { const options = { url: 'http://localhost:' + iotConfig.server.port + '/iot/services', headers: { @@ -168,14 +168,14 @@ describe('Configuration list', function () { 'fiware-servicepath': '/gardens' }, qs: { - limit: 3 + limit: 2 }, method: 'GET' }; it('should return just 3 results', function (done) { request(options, function (error, response, body) { - body.services.length.should.equal(3); + body.services.length.should.equal(2); done(); }); }); From de5cedca7000562b597f4d7b728df27ec03a93a9 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 26 Feb 2024 10:27:50 +0100 Subject: [PATCH 08/13] clean code --- lib/iotagent-manager.js | 29 +------------------ lib/model/dbConn.js | 3 -- lib/services/configurationData.js | 4 +-- lib/services/configurations.js | 6 ---- lib/services/iotaRedirector.js | 10 ------- lib/services/protocolData.js | 4 --- lib/services/protocols.js | 4 --- lib/utils/domain.js | 46 ------------------------------- lib/utils/logging.js | 8 +++--- 9 files changed, 6 insertions(+), 108 deletions(-) delete mode 100644 lib/utils/domain.js diff --git a/lib/iotagent-manager.js b/lib/iotagent-manager.js index fec1dfe..3431d82 100644 --- a/lib/iotagent-manager.js +++ b/lib/iotagent-manager.js @@ -31,15 +31,11 @@ const protocols = require('./services/protocols'); const configurations = require('./services/configurations'); const iotaRedirector = require('./services/iotaRedirector'); const dbConn = require('./model/dbConn'); -//const iotaLib = require('iotagent-node-lib'); const async = require('async'); const apply = async.apply; let northboundServer; const logger = require('logops'); const loggingMiddleware = require('./utils/logging').requestLogger('IotAgent-Manager'); -// const context = { -// op: 'IoTAManager.NorthboundServer' -// }; /** * Express middleware for tracing the complete request arriving to the IoTA in debug mode. @@ -62,20 +58,6 @@ function startServer(newConfig, callback) { logger.format = logger.formatters.pipe; - // logger.getContext = function domainContext() { - // var domainObj = require('domain').active || {}; - // return { - // corr: domainObj.corr, - // trans: domainObj.trans, - // op: domainObj.op, - // from: domainObj.from, - // srv: domainObj.service, - // subsrv: domainObj.subservice, - // msg: domainObj.msg, - // comp: config.componentName || 'IoTAgentManager' - // }; - // }; - northboundServer = { server: null, app: express(), @@ -88,12 +70,10 @@ function startServer(newConfig, callback) { northboundServer.app.set('port', newConfig.server.port); northboundServer.app.set('host', newConfig.server.host || '0.0.0.0'); northboundServer.app.set('etag', false); - //northboundServer.app.use(iotaLib.requestDomain); northboundServer.app.use(loggingMiddleware); northboundServer.app.use(bodyParser.json({ limit: newConfig.bodyParserLimit })); if (newConfig.logLevel && newConfig.logLevel === 'DEBUG') { - //northboundServer.app.use(middleware.traceRequest); northboundServer.app.use(traceRequest); } @@ -139,14 +119,7 @@ function startServer(newConfig, callback) { function start(newConfig, callback) { config.setConfig(newConfig); - async.series( - [ - apply(startServer, newConfig), - //dbConn.configureDb], - apply(dbConn.configureDb, logger) - ], - callback - ); + async.series([apply(startServer, newConfig), apply(dbConn.configureDb, logger)], callback); } /** diff --git a/lib/model/dbConn.js b/lib/model/dbConn.js index 84713cf..4b30f16 100644 --- a/lib/model/dbConn.js +++ b/lib/model/dbConn.js @@ -34,9 +34,6 @@ const async = require('async'); const errors = require('../errors'); let defaultDb; const DEFAULT_DB_NAME = 'iotagent-manager'; -// const context = { -// op: 'IoTAManager.DbConn' -// }; mongoose.Promise = global.Promise; // not including this causes DeprecationWarning diff --git a/lib/services/configurationData.js b/lib/services/configurationData.js index 0b899b0..b0996b8 100644 --- a/lib/services/configurationData.js +++ b/lib/services/configurationData.js @@ -28,9 +28,7 @@ const iotagentLib = require('iotagent-node-lib'); const errors = require('../errors'); const logger = require('logops'); const async = require('async'); -// const context = { -// op: 'IoTAManager.ConfigurationDB' -// }; + const provisioningAPITranslation = { name: 'id', service: 'service', diff --git a/lib/services/configurations.js b/lib/services/configurations.js index aaee8f2..42e1b37 100644 --- a/lib/services/configurations.js +++ b/lib/services/configurations.js @@ -23,12 +23,6 @@ const errors = require('../errors'); const configurationData = require('./configurationData'); -//const logger = require('logops'); -// const domain = require('../utils/domain'); -// const fillService = domain.fillService; -// var context = { -// op: 'IoTAManager.Configurations' -// }; const retrievingAPITranslation = { subservice: 'service_path', type: 'entity_type', diff --git a/lib/services/iotaRedirector.js b/lib/services/iotaRedirector.js index b6a8683..828e18e 100644 --- a/lib/services/iotaRedirector.js +++ b/lib/services/iotaRedirector.js @@ -23,15 +23,9 @@ const protocols = require('./protocolData'); const request = require('iotagent-node-lib').request; -//const logger = require('logops'); const errors = require('../errors'); const _ = require('underscore'); const async = require('async'); -// const domain = require('../utils/domain'); -// const fillService = domain.fillService; -// var context = { -// op: 'IoTAManager.Redirector' -// }; function guessCollection(body) { if (body.services) { @@ -239,10 +233,6 @@ function createRequest(req, protocol, body) { delete options.headers['content-length']; options.headers.connection = 'close'; - // context = fillService(context, { - // service: options.headers['fiware-service'], - // subservice: options.headers['fiware-servicepath'] - // }); logger.debug('Forwarding request:\n\n%j\n', options); return options; diff --git a/lib/services/protocolData.js b/lib/services/protocolData.js index edbf04d..0c69742 100644 --- a/lib/services/protocolData.js +++ b/lib/services/protocolData.js @@ -28,10 +28,6 @@ const configurations = require('./configurationData'); const async = require('async'); const errors = require('../errors'); const apply = async.apply; -//const logger = require('logops'); -// const context = { -// op: 'IoTAManager.ProtocolAPI' -// }; function processConfiguration(logger, protocol, description, iotagent, resource, configuration, callback) { configurations.get(configuration.apikey, resource, protocol, function (error, oldConfiguration) { diff --git a/lib/services/protocols.js b/lib/services/protocols.js index 18f963f..b4c9ae7 100644 --- a/lib/services/protocols.js +++ b/lib/services/protocols.js @@ -25,11 +25,7 @@ const protocolTemplate = require('../templates/protocol.json'); const Protocol = require('../model/Protocol'); const protocols = require('./protocolData'); const async = require('async'); -//const logger = require('logops'); const middleware = require('iotagent-node-lib').middlewares; -// const context = { -// op: 'IoTAManager.ProtocolAPI' -// }; function readProtocolList(req, res, next) { const logger = req.logger; diff --git a/lib/utils/domain.js b/lib/utils/domain.js deleted file mode 100644 index 9b6d371..0000000 --- a/lib/utils/domain.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U - * - * This file is part of fiware-pep-steelskin - * - * fiware-pep-steelskin is free software: you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the License, - * or (at your option) any later version. - * - * fiware-pep-steelskin is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with fiware-pep-steelskin. - * If not, see http://www.gnu.org/licenses/. - * - * For those usages not covered by the GNU Affero General Public License - * please contact with::[iot_support@tid.es] - */ - -'use strict'; - - -/** - * Fills service and subservice information in a context object for logging matters. - * - * @param {Object} context Context object that will be used to add the service and subservice information. - * @param {Object} data Data object (configuration or device) containing service information. - * @return {Object} New context containing service information. - */ -function fillService(context, data) { - if (data.service) { - context.srv = data.service; - } - - if (data.subservice) { - context.subsrv = data.subservice; - } - - return context; -} - -exports.fillService = fillService; diff --git a/lib/utils/logging.js b/lib/utils/logging.js index 1aeebb9..0350bd3 100644 --- a/lib/utils/logging.js +++ b/lib/utils/logging.js @@ -36,12 +36,12 @@ const logger = require('logops'); function requestLogger(componentName) { return function (req, res, next) { let contextSrv; - if (req.headers && req.headers[constants.ORGANIZATION_HEADER]) { - contextSrv = req.headers[constants.ORGANIZATION_HEADER]; + if (req.headers && req.headers[constants.SERVICE_HEADER]) { + contextSrv = req.headers[constants.SERVICE_HEADER]; } let contextSubsrv; - if (req.headers && req.headers[constants.PATH_HEADER]) { - contextSubsrv = req.headers[constants.PATH_HEADER]; + if (req.headers && req.headers[constants.SUBSERVICE_HEADER]) { + contextSubsrv = req.headers[constants.SUBSERVICE_HEADER]; } let contextFrom; // x-forwarded-for/forwarded overwrites x-real-ip From 5c47f967556f5af919b28468dd8d16081a253d63 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 26 Feb 2024 10:30:30 +0100 Subject: [PATCH 09/13] Update CNR --- Changelog | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog b/Changelog index 1e345bc..4c1af95 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +- Remove: dependency on deprecated domain node module + 3.1.0 (January 8th, 2024) - Add: transport and endpoint to Group model (iotagent-node-lib#1542) From 543ccd00f947cae1701e5bb633994011a45bf670 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 26 Feb 2024 10:39:28 +0100 Subject: [PATCH 10/13] fix logger usage --- lib/model/dbConn.js | 1 - lib/services/iotaRedirector.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/model/dbConn.js b/lib/model/dbConn.js index 4b30f16..08e2963 100644 --- a/lib/model/dbConn.js +++ b/lib/model/dbConn.js @@ -29,7 +29,6 @@ const mongoose = require('mongoose'); const config = require('../utils/commonConfig'); const constants = require('../utils/constants'); -const logger = require('logops'); const async = require('async'); const errors = require('../errors'); let defaultDb; diff --git a/lib/services/iotaRedirector.js b/lib/services/iotaRedirector.js index 828e18e..af3b003 100644 --- a/lib/services/iotaRedirector.js +++ b/lib/services/iotaRedirector.js @@ -434,7 +434,7 @@ function handleError(error, req, res, next) { const logger = req.logger; let code = 500; - //TBD: logger.warn('Error [%s] handling request: %s', error.name, error.message); + logger.warn('Error [%s] handling request: %s', error.name, error.message); if (error.code && String(error.code).match(/^[2345]\d\d$/)) { code = error.code; From 4c5056f3781fcc47ed731af496d0035d758f6a0e Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 26 Feb 2024 12:07:21 +0100 Subject: [PATCH 11/13] restore tests values --- test/unit/configuration-retrieval-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/configuration-retrieval-test.js b/test/unit/configuration-retrieval-test.js index da97485..2ee1b48 100644 --- a/test/unit/configuration-retrieval-test.js +++ b/test/unit/configuration-retrieval-test.js @@ -160,7 +160,7 @@ describe('Configuration list', function () { }); }); - describe('When a configuration list request with a limit 2 arrives to the IoTAM', function () { + describe('When a configuration list request with a limit 3 arrives to the IoTAM', function () { const options = { url: 'http://localhost:' + iotConfig.server.port + '/iot/services', headers: { @@ -168,14 +168,14 @@ describe('Configuration list', function () { 'fiware-servicepath': '/gardens' }, qs: { - limit: 2 + limit: 3 }, method: 'GET' }; it('should return just 3 results', function (done) { request(options, function (error, response, body) { - body.services.length.should.equal(2); + body.services.length.should.equal(3); done(); }); }); From b0a255d69e1153673ab10be29249c36d0f2e9c4f Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 26 Feb 2024 12:52:35 +0100 Subject: [PATCH 12/13] add missed constant --- lib/utils/constants.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/utils/constants.js b/lib/utils/constants.js index 9ced005..66a0184 100644 --- a/lib/utils/constants.js +++ b/lib/utils/constants.js @@ -37,6 +37,7 @@ module.exports = { DEFAULT_MONGODB_RETRIES: 5, DEFAULT_MONGODB_RETRY_TIME: 5, + X_FORWARDED_FOR_HEADER: 'x-forwarded-for', FORWARDED_HEADER: 'forwarded', X_REAL_IP_HEADER: 'x-real-ip', CORRELATOR_HEADER: 'fiware-correlator' From 886a9944791fc6c730f76f40020206f349cd935d Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 12 Mar 2024 09:12:25 +0100 Subject: [PATCH 13/13] Update Changelog --- Changelog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Changelog b/Changelog index 43a54c9..76d3673 100644 --- a/Changelog +++ b/Changelog @@ -1,4 +1,4 @@ -- Remove: dependency on deprecated domain node module +- Remove: dependency on deprecated domain node module (#268) 3.2.0 (February 27th, 2024)