Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] northBound server in HTTPS and Verify ContextBroker Certificate #601

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var config = {
logLevel: 'DEBUG',
contextBroker: {
host: '192.168.56.101',
port: '1026'
port: '1026',
protocol: 'http'
},
server: {
port: 4041
Expand Down
11 changes: 7 additions & 4 deletions lib/commonConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ function processEnvironmentVariables() {
'IOTA_CB_URL',
'IOTA_CB_HOST',
'IOTA_CB_PORT',
'IOTA_CB_PROTOCOL',
'IOTA_CB_VERIFY',
'IOTA_NORTH_HOST',
'IOTA_NORTH_PORT',
'IOTA_PROVIDER_URL',
Expand Down Expand Up @@ -92,13 +94,14 @@ 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) {
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) {
Expand Down
7 changes: 3 additions & 4 deletions lib/fiware-iotagent-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
5 changes: 2 additions & 3 deletions lib/services/common/iotManagerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/services/devices/registrationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: [
Expand Down
15 changes: 6 additions & 9 deletions lib/services/ngsi/subscriptionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
}

Expand Down Expand Up @@ -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));
Expand Down
12 changes: 11 additions & 1 deletion lib/services/northBound/northboundServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down