From 6cfdb035726680ecacf01916c7be235fcd94438d Mon Sep 17 00:00:00 2001 From: m4n3dw0lf Date: Sat, 24 Mar 2018 14:12:45 -0300 Subject: [PATCH 1/4] Support for ContextBroker in HTTPS context via config.contextBroker.protocol parameter --- config-blank.js | 3 ++- lib/services/common/iotManagerService.js | 5 ++--- lib/services/devices/deviceService.js | 9 ++++----- lib/services/devices/registrationUtils.js | 5 ++--- lib/services/ngsi/subscriptionService.js | 15 ++++++--------- 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/config-blank.js b/config-blank.js index 27601db32..0e6d446c4 100644 --- a/config-blank.js +++ b/config-blank.js @@ -25,7 +25,8 @@ var config = { logLevel: 'DEBUG', contextBroker: { host: '192.168.56.101', - port: '1026' + port: '1026', + protocol: 'http' }, server: { port: 4041 diff --git a/lib/services/common/iotManagerService.js b/lib/services/common/iotManagerService.js index 792a234be..ebcfaa559 100644 --- a/lib/services/common/iotManagerService.js +++ b/lib/services/common/iotManagerService.js @@ -19,8 +19,6 @@ * * For those usages not covered by the GNU Affero General Public License * please contact with::daniel.moranjimenez@telefonica.com - * - * Modified by: Federico M. Facca - Martel Innovate */ 'use strict'; @@ -68,7 +66,8 @@ function register(callback) { function sendRegistration(services, callback) { var options = { - url: config.getConfig().iotManager.url + config.getConfig().iotManager.path, + url: config.getConfig().contextBroker.protocol + '://' + config.getConfig().iotManager.host + ':' + + config.getConfig().iotManager.port + config.getConfig().iotManager.path, method: 'POST', json: { protocol: config.getConfig().iotManager.protocol, diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 91248d3cb..b3d1e8b51 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -19,8 +19,6 @@ * * For those usages not covered by the GNU Affero General Public License * please contact with::daniel.moranjimenez@telefonica.com - * - * Modified by: Federico M. Facca - Martel Innovate */ 'use strict'; @@ -124,7 +122,8 @@ function getInitialValueForType(type) { */ function createInitialEntity(deviceData, newDevice, callback) { var options = { - url: config.getConfig().contextBroker.url + '/v1/updateContext', + url: config.getConfig().contextBroker.protocol + '://' + config.getConfig().contextBroker.host + ':' + config.getConfig().contextBroker.port + + '/v1/updateContext', method: 'POST', json: { contextElements: [ @@ -235,7 +234,7 @@ function mergeArrays(original, newArray) { newKeys = _.pluck(newArray, 'object_id'), addedKeys = _.difference(newKeys, originalKeys), differenceArray = newArray.filter(function(item) { - return item.object_id && addedKeys.indexOf(item.object_id) >= 0; + return addedKeys.indexOf(item.object_id) >= 0; }), originalNames = _.pluck(original, 'name'), newNames = _.pluck(newArray, 'name'), @@ -313,7 +312,7 @@ function findConfigurationGroup(deviceObj, callback) { deviceObj.subservice, handlerGroupFind); } else { - config.getGroupRegistry().findType( + config.getGroupRegistry().findBy(['service', 'subservice', 'type'])( deviceObj.service, deviceObj.subservice, deviceObj.type, diff --git a/lib/services/devices/registrationUtils.js b/lib/services/devices/registrationUtils.js index 871d1770e..f25e57599 100644 --- a/lib/services/devices/registrationUtils.js +++ b/lib/services/devices/registrationUtils.js @@ -19,8 +19,6 @@ * * For those usages not covered by the GNU Affero General Public License * please contact with::daniel.moranjimenez@telefonica.com - * - * Modified by: Federico M. Facca - Martel Innovate */ 'use strict'; @@ -88,7 +86,8 @@ function createRegistrationHandler(unregister, deviceData, callback) { */ function sendRegistrations(unregister, deviceData, callback) { var options = { - url: config.getConfig().contextBroker.url + '/NGSI9/registerContext', + url: config.getConfig().contextBroker.protocol + '://' + config.getConfig().contextBroker.host + ':' + + config.getConfig().contextBroker.port + '/NGSI9/registerContext', method: 'POST', json: { contextRegistrations: [ diff --git a/lib/services/ngsi/subscriptionService.js b/lib/services/ngsi/subscriptionService.js index 8ed6a0162..3306bc8e0 100644 --- a/lib/services/ngsi/subscriptionService.js +++ b/lib/services/ngsi/subscriptionService.js @@ -19,8 +19,6 @@ * * For those usages not covered by the GNU Affero General Public License * please contact with::daniel.moranjimenez@telefonica.com - * - * Modified by: Federico M. Facca - Martel Innovate */ 'use strict'; @@ -127,9 +125,9 @@ function subscribe(device, triggers, content, callback) { } if (device.cbHost) { - options.uri = 'http://' + device.cbHost + '/v1/subscribeContext'; + options.uri = config.getConfig().contextBroker.protocol + '://' + device.cbHost + '/v1/subscribeContext'; } else { - options.uri = 'http://' + config.getConfig().contextBroker.host + ':' + + options.uri = config.getConfig().contextBroker.protocol + '://' + config.getConfig().contextBroker.host + ':' + config.getConfig().contextBroker.port + '/v1/subscribeContext'; } @@ -196,12 +194,11 @@ function unsubscribe(device, id, callback) { }; - if (device.cbHost && device.cbHost.indexOf("://") !== -1) { - options.uri = device.cbHost + '/v1/unsubscribeContext'; - } else if (device.cbHost && device.cbHost.indexOf("://") == -1) { - options.uri = "http://" + device.cbHost + '/v1/unsubscribeContext'; + if (device.cbHost) { + options.uri = config.getConfig().contextBroker.protocol + '://' + device.cbHost + '/v1/unsubscribeContext'; } else { - options.uri = config.getConfig().contextBroker.url + '/v1/unsubscribeContext'; + options.uri = config.getConfig().contextBroker.protocol + '://' + config.getConfig().contextBroker.host + ':' + config.getConfig().contextBroker.port + + '/v1/unsubscribeContext'; } request(options, createUnsuscribeHandler(device, id, callback)); From 91547a1c6353199933de3942ff4f9ba3ee4bdb18 Mon Sep 17 00:00:00 2001 From: m4n3dw0lf Date: Sat, 24 Mar 2018 14:30:07 -0300 Subject: [PATCH 2/4] IOTA_CB_PROTOCOL for http or https contextbrokers --- lib/commonConfig.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/commonConfig.js b/lib/commonConfig.js index 211b220b0..6c39840be 100644 --- a/lib/commonConfig.js +++ b/lib/commonConfig.js @@ -52,6 +52,7 @@ function processEnvironmentVariables() { 'IOTA_CB_URL', 'IOTA_CB_HOST', 'IOTA_CB_PORT', + 'IOTA_CB_PROTOCOL', 'IOTA_NORTH_HOST', 'IOTA_NORTH_PORT', 'IOTA_PROVIDER_URL', @@ -95,7 +96,12 @@ function processEnvironmentVariables() { if (process.env.IOTA_CB_URL) { config.contextBroker.url = process.env.IOTA_CB_URL; } else if (process.env.IOTA_CB_HOST) { - config.contextBroker.url = "http://" + process.env.IOTA_CB_HOST; + if (process.env.IOTA_CB_PROTOCOL) { + config.contextBroker.url = process.env.IOTA_CB_PROTOCOL + :// + process.env.IOTA_CB_HOST; + } + else { + config.contextBroker.url = "http://" + process.env.IOTA_CB_HOST; + } if (process.env.IOTA_CB_PORT) { config.contextBroker.url += ":" + process.env.IOTA_CB_PORT; } From d97fb3f7d61918a2ff257a397bef8aeef044dc12 Mon Sep 17 00:00:00 2001 From: m4n3dw0lf Date: Sat, 24 Mar 2018 16:36:28 -0300 Subject: [PATCH 3/4] Improving commit. --- lib/commonConfig.js | 10 +--------- lib/fiware-iotagent-lib.js | 7 +++---- lib/services/northBound/northboundServer.js | 12 +++++++++++- package.json | 8 +++++--- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/commonConfig.js b/lib/commonConfig.js index 6c39840be..2edd143b4 100644 --- a/lib/commonConfig.js +++ b/lib/commonConfig.js @@ -96,15 +96,7 @@ function processEnvironmentVariables() { if (process.env.IOTA_CB_URL) { config.contextBroker.url = process.env.IOTA_CB_URL; } else if (process.env.IOTA_CB_HOST) { - if (process.env.IOTA_CB_PROTOCOL) { - config.contextBroker.url = process.env.IOTA_CB_PROTOCOL + :// + process.env.IOTA_CB_HOST; - } - else { - config.contextBroker.url = "http://" + process.env.IOTA_CB_HOST; - } - if (process.env.IOTA_CB_PORT) { - config.contextBroker.url += ":" + process.env.IOTA_CB_PORT; - } + config.contextBroker.url = (process.env.IOTA_CB_PROTOCOL || "http") + "://" + process.env.IOTA_CB_HOST + ( ":" + process.env.IOTA_CB_PORT || "") } if (process.env.IOTA_NORTH_HOST) { diff --git a/lib/fiware-iotagent-lib.js b/lib/fiware-iotagent-lib.js index e51d095e1..7d56b4d53 100644 --- a/lib/fiware-iotagent-lib.js +++ b/lib/fiware-iotagent-lib.js @@ -83,10 +83,9 @@ function doActivate(newConfig, callback) { logger.format = logger.formatters.pipe; if (newConfig.contextBroker) { - if (! newConfig.contextBroker.url && newConfig.contextBroker.host && newConfig.contextBroker.port) { - newConfig.contextBroker.url = "http://" + newConfig.contextBroker.host + ":" + newConfig.contextBroker.port; - } else if (! newConfig.contextBroker.url && newConfig.contextBroker.host && !newConfig.contextBroker.port) { - newConfig.contextBroker.url = "http://" + newConfig.contextBroker.host; + if (! newConfig.contextBroker.url) { + newConfig.contextBroker.url = ( newConfig.contextBroker.protocol + "://" || "http://" ) + + newConfig.contextBroker.host + ":" + ( newConfig.contextBroker.port || "1026"); } } diff --git a/lib/services/northBound/northboundServer.js b/lib/services/northBound/northboundServer.js index 6bbc1a59c..6f3585232 100644 --- a/lib/services/northBound/northboundServer.js +++ b/lib/services/northBound/northboundServer.js @@ -23,6 +23,8 @@ 'use strict'; var http = require('http'), + https = require('https'), + fs = require('fs'), async = require('async'), express = require('express'), packageInformation = require('../../../package.json'), @@ -91,7 +93,15 @@ function start(config, callback) { northboundServer.app.use(middlewares.handleError); - northboundServer.server = http.createServer(northboundServer.app); + if ( typeof(config.server.tls) != "undefined" ){ + var privateKey = fs.readFileSync(config.server.tls.key,'utf-8'); + var certificate = fs.readFileSync(config.server.tls.crt,'utf-8'); + var keypair = { key : privateKey, cert : certificate }; + northboundServer.server = https.createServer(keypair,northboundServer.app); + } + else { + northboundServer.server = http.createServer(northboundServer.app); + } northboundServer.server.listen(northboundServer.app.get('port'), northboundServer.app.get('host'), callback); } diff --git a/package.json b/package.json index ae70ee527..ef045d914 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,12 @@ "async": "1.5.2", "body-parser": "^1.11.0", "command-shell-lib": "1.0.0", - "jison": "0.4.17", "express": "^4.11.2", + "fs": "0.0.2", + "https": "^1.0.0", + "jison": "0.4.17", "logops": "1.0.0", + "mongodb": "2.2.10", "mongoose": "4.13.3", "mu2": "^0.5.20", "mustache": "2.2.1", @@ -46,8 +49,7 @@ "revalidator": "^0.3.1", "sax": "^0.6.0", "underscore": "^1.7.0", - "xmldom": "0.1.19", - "mongodb": "2.2.10" + "xmldom": "0.1.19" }, "devDependencies": { "grunt": "0.4.5", From 8ffc1e7975c9c35baf2f961108de6a326bb99cfa Mon Sep 17 00:00:00 2001 From: m4n3dw0lf Date: Sat, 24 Mar 2018 18:19:35 -0300 Subject: [PATCH 4/4] Verify ContextBroker certificate option. --- lib/commonConfig.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/commonConfig.js b/lib/commonConfig.js index 2edd143b4..f9b022fec 100644 --- a/lib/commonConfig.js +++ b/lib/commonConfig.js @@ -53,6 +53,7 @@ function processEnvironmentVariables() { 'IOTA_CB_HOST', 'IOTA_CB_PORT', 'IOTA_CB_PROTOCOL', + 'IOTA_CB_VERIFY', 'IOTA_NORTH_HOST', 'IOTA_NORTH_PORT', 'IOTA_PROVIDER_URL', @@ -93,6 +94,10 @@ function processEnvironmentVariables() { } } + if (process.env.IOTA_CB_VERIFY) { + config.contextBroker.verify = process.env.IOTA_CB_VERIFY; + } + if (process.env.IOTA_CB_URL) { config.contextBroker.url = process.env.IOTA_CB_URL; } else if (process.env.IOTA_CB_HOST) {