From baae52f1248ebbc195c814b5195d26fb8f19946e Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 22 May 2024 16:24:45 +0200 Subject: [PATCH 01/11] uses previous device in device update handler --- lib/bindings/AMQPBinding.js | 2 +- lib/bindings/ARGOBinding.js | 2 +- lib/bindings/HTTPBinding.js | 10 ++++++---- lib/bindings/MQTTBinding.js | 2 +- lib/iotagent-json.js | 12 +++++++++--- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/bindings/AMQPBinding.js b/lib/bindings/AMQPBinding.js index 1d82c0753..7dd38a850 100644 --- a/lib/bindings/AMQPBinding.js +++ b/lib/bindings/AMQPBinding.js @@ -246,7 +246,7 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device object containing all the information about the provisioned device. */ -function deviceUpdatingHandler(device, callback) { +function deviceUpdatingHandler(device, oldDevice, callback) { callback(null, device); } diff --git a/lib/bindings/ARGOBinding.js b/lib/bindings/ARGOBinding.js index 46fd11db8..ad81fd596 100644 --- a/lib/bindings/ARGOBinding.js +++ b/lib/bindings/ARGOBinding.js @@ -522,7 +522,7 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device object containing all the information about the updated device. */ -function deviceUpdatingHandler(device, callback) { +function deviceUpdatingHandler(device, oldDevice, callback) { return callback(null, device); } diff --git a/lib/bindings/HTTPBinding.js b/lib/bindings/HTTPBinding.js index 7055db0ae..06ac8a3de 100644 --- a/lib/bindings/HTTPBinding.js +++ b/lib/bindings/HTTPBinding.js @@ -546,10 +546,12 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device object containing all the information about the updated device. */ -function deviceUpdatingHandler(device, callback) { - config.getLogger().debug(context, 'httpbinding.deviceUpdatingHandler device %j', device); +function deviceUpdatingHandler(newDevice, oldDevice, callback) { + config + .getLogger() + .debug(context, 'httpbinding.deviceUpdatingHandler newDevice %j oldDevice %j', newDevice, oldDevice); let group = {}; - iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', device.apikey, function ( + iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', oldDevice.apikey, function ( error, foundGroup ) { @@ -557,7 +559,7 @@ function deviceUpdatingHandler(device, callback) { group = foundGroup; } config.getLogger().debug(context, 'httpbinding.deviceUpdatingHandler group %j', group); - setPollingAndDefaultTransport(device, group, callback); + setPollingAndDefaultTransport(newDevice, group, callback); }); } diff --git a/lib/bindings/MQTTBinding.js b/lib/bindings/MQTTBinding.js index d0361ba6b..e20b2f502 100644 --- a/lib/bindings/MQTTBinding.js +++ b/lib/bindings/MQTTBinding.js @@ -345,7 +345,7 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device object containing all the information about the provisioned device. */ -function deviceUpdatingHandler(device, callback) { +function deviceUpdatingHandler(device, oldDevice, callback) { callback(null, device); } diff --git a/lib/iotagent-json.js b/lib/iotagent-json.js index 3d66dcab9..ad54da970 100644 --- a/lib/iotagent-json.js +++ b/lib/iotagent-json.js @@ -101,6 +101,7 @@ function updateHandler(id, type, attributes, service, subservice, callback) { * @param {Object} device Device provisioning information. */ function deviceProvisioningHandler(device, callback) { + config.getLogger().debug(context, 'deviceProvisioningHandler for device %j', device); transportSelector.applyFunctionFromBinding([device], 'deviceProvisioningHandler', null, function (error, devices) { if (error) { callback(error); @@ -111,13 +112,18 @@ function deviceProvisioningHandler(device, callback) { } /** - * Calls all the device updating handlers for each transport protocol binding whenever a new device is updated + * Calls all the device updating handlers for each transport protocol binding whenever a device is updated * in the Agent. * * @param {Object} device Device updating information. */ -function deviceUpdatingHandler(device, callback) { - transportSelector.applyFunctionFromBinding([device], 'deviceUpdatingHandler', null, function (error, devices) { +function deviceUpdatingHandler(newDevice, oldDevice, callback) { + config.getLogger().debug(context, 'deviceUpdatingHandler for newDevice %j oldDevice %j', newDevice, oldDevice); + // Maybe get apikey from group ? + transportSelector.applyFunctionFromBinding([newDevice, oldDevice], 'deviceUpdatingHandler', null, function ( + error, + devices + ) { if (error) { callback(error); } else { From 91b231a8edbf346c04e8c0e43fdbd104b0248f47 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 23 May 2024 14:33:05 +0200 Subject: [PATCH 02/11] update CNR --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 67b4cc840..e7c220d2f 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1,2 @@ +- Fix: update device using previous device apikey to avoid error when apikey is updated /(iota-json#833) - Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (#827) From aa5f004120cb76f865e43c0001d3f186fb7eec2a Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 23 May 2024 14:34:10 +0200 Subject: [PATCH 03/11] Update CNR --- lib/iotagent-json.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/iotagent-json.js b/lib/iotagent-json.js index ad54da970..134d9321e 100644 --- a/lib/iotagent-json.js +++ b/lib/iotagent-json.js @@ -119,7 +119,6 @@ function deviceProvisioningHandler(device, callback) { */ function deviceUpdatingHandler(newDevice, oldDevice, callback) { config.getLogger().debug(context, 'deviceUpdatingHandler for newDevice %j oldDevice %j', newDevice, oldDevice); - // Maybe get apikey from group ? transportSelector.applyFunctionFromBinding([newDevice, oldDevice], 'deviceUpdatingHandler', null, function ( error, devices From 721633235ddd32760984c42ab7d020d681403f40 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 31 May 2024 10:06:53 +0200 Subject: [PATCH 04/11] add tests about modify provisioned device apikey --- test/unit/ngsiv2/HTTP_update_device_test.js | 127 ++++++++++++++++++ .../provisionDeviceHTTP4.json | 47 +++++++ .../updateProvisionDevice4.json | 3 + 3 files changed, 177 insertions(+) create mode 100644 test/unit/ngsiv2/HTTP_update_device_test.js create mode 100644 test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json create mode 100644 test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json diff --git a/test/unit/ngsiv2/HTTP_update_device_test.js b/test/unit/ngsiv2/HTTP_update_device_test.js new file mode 100644 index 000000000..76fd3967a --- /dev/null +++ b/test/unit/ngsiv2/HTTP_update_device_test.js @@ -0,0 +1,127 @@ +/* + * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U + * + * This file is part of iotagent-json + * + * iotagent-json 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-json 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-json. + * If not, seehttp://www.gnu.org/licenses/. + * + * For those usages not covered by the GNU Affero General Public License + * please contact with::[contacto@tid.es] + * + * Modified by: Daniel Calvo - ATOS Research & Innovation + */ + +/* eslint-disable no-unused-vars */ + +const iotagentMqtt = require('../../../'); +const config = require('./config-test.js'); +const nock = require('nock'); +const should = require('should'); +const iotAgentLib = require('iotagent-node-lib'); +const async = require('async'); +const request = require('request'); +const utils = require('../../utils'); +let mockedClientServer; +let contextBrokerMock; + +describe('HTTP binding - Update provisioned devices with a new apikey', function () { + const provisionOptions = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices', + method: 'POST', + json: utils.readExampleFile('./test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json'), + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + } + }; + + beforeEach(function (done) { + config.logLevel = 'DEBUG'; + nock.cleanAll(); + contextBrokerMock = nock('http://192.168.1.1:1026') + .matchHeader('fiware-service', 'smartgondor') + .matchHeader('fiware-servicepath', '/gardens') + .post('/v2/entities?options=upsert') + .reply(204); + + iotagentMqtt.start(config, function () { + request(provisionOptions, function (error, response, body) { + done(); + }); + }); + }); + + it('should have provisioned with APIKEY1', function (done) { + const options = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_2', + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + }, + method: 'GET' + }; + request(options, function (error, response, body) { + /* jshint camelcase:false */ + const parsedBody = JSON.parse(body); + parsedBody.apikey.should.equal('APIKEY1'); + done(); + }); + }); + + afterEach(function (done) { + nock.cleanAll(); + async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); + }); + + describe('When a request to update a provision device arrives', function () { + const optionsUpdate = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_2', + method: 'PUT', + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + }, + json: utils.readExampleFile('./test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json') + }; + + it('should return a 200 OK and no errors', function (done) { + request(optionsUpdate, function (error, response, body) { + should.not.exist(error); + response.statusCode.should.equal(204); + done(); + }); + }); + + it('should have updated device apikey', function (done) { + request(optionsUpdate, function (error, response, body) { + const options = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_2', + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + }, + method: 'GET' + }; + + request(options, function (error, response, body) { + /* jshint camelcase:false */ + const parsedBody = JSON.parse(body); + parsedBody.apikey.should.equal('APIKEY2'); + done(); + }); + }); + }); + }); +}); diff --git a/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json b/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json new file mode 100644 index 000000000..d7a8d036b --- /dev/null +++ b/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json @@ -0,0 +1,47 @@ +{ + "devices": [ + { + "device_id": "MQTT_2", + "protocol": "GENERIC_PROTO", + "apikey": "APIKEY1", + "entity_name": "Second MQTT Device", + "timezone": "America/Santiago", + "entity_type": "AnMQTTDevice", + "attributes": [ + { + "name": "temperature", + "type": "celsius" + }, + { + "name": "humidity", + "type": "degrees" + }, + { + "name": "luminosity", + "type": "Integer" + + }, + { + "name": "pollution", + "type": "Float" + }, + { + "name": "configuration", + "type": "Object" + }, + { + "name": "tags", + "type": "Array" + }, + { + "name": "enabled", + "type": "Boolean" + }, + { + "name": "alive", + "type": "Null" + } + ] + } + ] +} diff --git a/test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json b/test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json new file mode 100644 index 000000000..3b2648cb8 --- /dev/null +++ b/test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json @@ -0,0 +1,3 @@ +{ + "apikey": "APIKEY2" +} From 292113ae59576bf0beab2bb966a01bec2f3fd46c Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 31 May 2024 10:11:36 +0200 Subject: [PATCH 05/11] fix debug level --- test/unit/ngsiv2/HTTP_update_device_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/ngsiv2/HTTP_update_device_test.js b/test/unit/ngsiv2/HTTP_update_device_test.js index 76fd3967a..8b3a8e3aa 100644 --- a/test/unit/ngsiv2/HTTP_update_device_test.js +++ b/test/unit/ngsiv2/HTTP_update_device_test.js @@ -48,7 +48,7 @@ describe('HTTP binding - Update provisioned devices with a new apikey', function }; beforeEach(function (done) { - config.logLevel = 'DEBUG'; + config.logLevel = 'FATAL'; nock.cleanAll(); contextBrokerMock = nock('http://192.168.1.1:1026') .matchHeader('fiware-service', 'smartgondor') From 6c47366fe142aa02218b90c68317a9a822c007a3 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 31 May 2024 10:20:05 +0200 Subject: [PATCH 06/11] remove unnecessary mock --- test/unit/ngsiv2/HTTP_update_device_test.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/unit/ngsiv2/HTTP_update_device_test.js b/test/unit/ngsiv2/HTTP_update_device_test.js index 8b3a8e3aa..7579b9df5 100644 --- a/test/unit/ngsiv2/HTTP_update_device_test.js +++ b/test/unit/ngsiv2/HTTP_update_device_test.js @@ -50,11 +50,6 @@ describe('HTTP binding - Update provisioned devices with a new apikey', function beforeEach(function (done) { config.logLevel = 'FATAL'; nock.cleanAll(); - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartgondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v2/entities?options=upsert') - .reply(204); iotagentMqtt.start(config, function () { request(provisionOptions, function (error, response, body) { From 0bf5228566eb986ca38728acb0f82d80faa0d333 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 31 May 2024 11:03:33 +0200 Subject: [PATCH 07/11] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1585c6699..1e59cd1dd 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "body-parser-xml": "2.0.5", "dateformat": "3.0.3", "express": "4.19.2", - "iotagent-node-lib": "https://github.com/telefonicaid/iotagent-node-lib.git#master", + "iotagent-node-lib": "https://github.com/telefonicaid/iotagent-node-lib.git#task/device_update_handler_with_old_device_value", "logops": "2.1.2", "mqtt": "4.3.7", "sinon": "~6.1.0", From bd6d043bb21c45fd5083319d3e5949ae8c605758 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 31 May 2024 11:07:48 +0200 Subject: [PATCH 08/11] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1e59cd1dd..1585c6699 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "body-parser-xml": "2.0.5", "dateformat": "3.0.3", "express": "4.19.2", - "iotagent-node-lib": "https://github.com/telefonicaid/iotagent-node-lib.git#task/device_update_handler_with_old_device_value", + "iotagent-node-lib": "https://github.com/telefonicaid/iotagent-node-lib.git#master", "logops": "2.1.2", "mqtt": "4.3.7", "sinon": "~6.1.0", From 3363d785f423b9e885383902e2e04be8d872b961 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 4 Jun 2024 13:41:42 +0200 Subject: [PATCH 09/11] update test --- test/unit/ngsiv2/HTTP_update_device_test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/ngsiv2/HTTP_update_device_test.js b/test/unit/ngsiv2/HTTP_update_device_test.js index 7579b9df5..3e6329f09 100644 --- a/test/unit/ngsiv2/HTTP_update_device_test.js +++ b/test/unit/ngsiv2/HTTP_update_device_test.js @@ -60,7 +60,7 @@ describe('HTTP binding - Update provisioned devices with a new apikey', function it('should have provisioned with APIKEY1', function (done) { const options = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_2', + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4', headers: { 'fiware-service': 'smartgondor', 'fiware-servicepath': '/gardens' @@ -82,7 +82,7 @@ describe('HTTP binding - Update provisioned devices with a new apikey', function describe('When a request to update a provision device arrives', function () { const optionsUpdate = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_2', + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4', method: 'PUT', headers: { 'fiware-service': 'smartgondor', @@ -102,7 +102,7 @@ describe('HTTP binding - Update provisioned devices with a new apikey', function it('should have updated device apikey', function (done) { request(optionsUpdate, function (error, response, body) { const options = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_2', + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4', headers: { 'fiware-service': 'smartgondor', 'fiware-servicepath': '/gardens' From ec6562f0802c37ca06fdc65b846746eee3eeac1a Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 4 Jun 2024 14:59:31 +0200 Subject: [PATCH 10/11] update test --- test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json b/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json index d7a8d036b..d0ef4750e 100644 --- a/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json +++ b/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json @@ -1,7 +1,7 @@ { "devices": [ { - "device_id": "MQTT_2", + "device_id": "MQTT_4", "protocol": "GENERIC_PROTO", "apikey": "APIKEY1", "entity_name": "Second MQTT Device", From e040dfe53d7dd09a55094ce68f242b601a000fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Wed, 5 Jun 2024 09:37:01 +0200 Subject: [PATCH 11/11] Update CHANGES_NEXT_RELEASE --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index dd9b25ad0..be725ecc2 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,3 +1,3 @@ -- Fix: update device using previous device apikey to avoid error when apikey is updated /(iota-json#833) +- Fix: update device using previous device apikey to avoid error when apikey is updated (#833) - Fix: allow send multiple measures to CB in a batch (POST /v2/op/update) and sorted by TimeInstant when possible, instead of using multiples single request (#825, iotagent-node-lib#1612) - Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (#827) \ No newline at end of file