Skip to content

Commit

Permalink
Merge pull request #1438 from telefonicaid/task/use_apikey_remove_device
Browse files Browse the repository at this point in the history
Task/use apikey to find, update and remove device
  • Loading branch information
fgalan committed Sep 11, 2023
2 parents d90c0d7 + 6b26373 commit d220ac0
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 80 deletions.
7 changes: 4 additions & 3 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Fix: ensure device apikey in already provisioned device (#1430)
- Upgrade mongodb dev dep from 4.7.0 to 4.17.0
- Upgrade mongoose dep from 5.13.14 to 5.13.20
- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426, #1435)
- Fix: ensure device apikey in already provisioned device (#1430)
- Upgrade mongodb dev dep from 4.7.0 to 4.17.0
- Upgrade mongoose dep from 5.13.14 to 5.13.20
1 change: 1 addition & 0 deletions lib/fiware-iotagent-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ exports.update = ngsi.update;
exports.setCommandResult = ngsi.setCommandResult;
exports.listDevices = deviceService.listDevices;
exports.getDevice = deviceService.getDevice;
exports.updateDevice = deviceService.updateDevice;
exports.getDeviceSilently = deviceService.getDeviceSilently;
exports.getDeviceByName = deviceService.getDeviceByName;
exports.getDeviceByNameAndType = deviceService.getDeviceByNameAndType;
Expand Down
2 changes: 1 addition & 1 deletion lib/services/commands/commandService.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function markAsExpired(command) {

async.waterfall(
[
apply(deviceService.getDevice, command.deviceId, command.service, command.subservice),
apply(deviceService.getDevice, command.deviceId, null, command.service, command.subservice),
getGroup,
calculateTypeInformation,
updateExpiredCommand
Expand Down
2 changes: 1 addition & 1 deletion lib/services/common/genericMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function handleError(error, req, res, next) {
* Express middleware for tracing the complete request arriving to the IoTA in debug mode.
*/
function traceRequest(req, res, next) {
logger.debug(context, 'Request for path [%s] from [%s]', req.path, req.get('host'));
logger.debug(context, '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(context, 'Body:\n\n%s\n\n', JSON.stringify(req.body, null, 4));
Expand Down
4 changes: 2 additions & 2 deletions lib/services/devices/deviceRegistryMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function storeDevice(newDevice, callback) {
* @param {String} service Service of the device to remove.
* @param {String} subservice Subservice inside the service for the removed device.
*/
function removeDevice(id, service, subservice, callback) {
function removeDevice(id, apikey, service, subservice, callback) {
const services = Object.keys(registeredDevices);

for (let i = 0; i < services.length; i++) {
Expand Down Expand Up @@ -141,7 +141,7 @@ function listDevices(type, service, subservice, limit, offset, callback) {
});
}

function getDevice(id, service, subservice, callback) {
function getDevice(id, apikey, service, subservice, callback) {
if (registeredDevices[service] && registeredDevices[service][id]) {
callback(null, registeredDevices[service][id]);
} else {
Expand Down
31 changes: 22 additions & 9 deletions lib/services/devices/deviceRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ function storeDevice(newDevice, callback) {
* @param {String} service Service of the device to remove.
* @param {String} subservice Subservice inside the service for the removed device.
*/
function removeDevice(id, service, subservice, callback) {
function removeDevice(id, apikey, service, subservice, callback) {
const condition = {
id,
service,
subservice
};

if (apikey) {
condition.apikey = apikey;
}
logger.debug(context, 'Removing device with id [%s]', id);

Device.model.deleteOne(condition, function (error) {
Expand Down Expand Up @@ -211,17 +213,21 @@ function findOneInMongoDB(queryParams, id, callback) {
* Internal function used to find a device in the DB.
*
* @param {String} id ID of the Device to find.
* @param {String} Apikey Apikey of the Device to find.
* @param {String} service Service the device belongs to (optional).
* @param {String} subservice Division inside the service (optional).
*/
function getDeviceById(id, service, subservice, callback) {
const queryParams = {
function getDeviceById(id, apikey, service, subservice, callback) {
let queryParams = {
id,
service,
subservice
};
if (apikey) {
queryParams.apikey = apikey;
}
context = fillService(context, queryParams);
logger.debug(context, 'Looking for device with id [%s].', id);
logger.debug(context, 'Looking for device with id [%s] and queryParams [%j].', id, queryParams);
findOneInMongoDB(queryParams, id, callback);
}

Expand All @@ -232,10 +238,17 @@ function getDeviceById(id, service, subservice, callback) {
* @param {String} service Service the device belongs to.
* @param {String} subservice Division inside the service.
*/
function getDevice(id, service, subservice, callback) {
getDeviceById(id, service, subservice, function (error, data) {
function getDevice(id, apikey, service, subservice, callback) {
getDeviceById(id, apikey, service, subservice, function (error, data) {
if (error) {
callback(error);
// Try without apikey: apikey will be added to device later
getDeviceById(id, null, service, subservice, function (error, data) {
if (error) {
callback(error);
} else {
callback(null, data);
}
});
} else {
callback(null, data);
}
Expand Down Expand Up @@ -284,7 +297,7 @@ function getByName(name, service, servicepath, callback) {
*/
function update(device, callback) {
logger.debug(context, 'Storing updated values for device [%s]:\n%s', device.id, JSON.stringify(device, null, 4));
getDeviceById(device.id, device.service, device.subservice, function (error, data) {
getDevice(device.id, device.apikey, device.service, device.subservice, function (error, data) {
if (error) {
callback(error);
} else {
Expand Down
38 changes: 25 additions & 13 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ function registerDevice(deviceObj, callback) {
function checkDuplicates(deviceObj, innerCb) {
config.getRegistry().getSilently(
deviceObj.id,
deviceObj.apikey,
deviceObj.service,
deviceObj.subservice,
/* eslint-disable-next-line no-unused-vars */
Expand Down Expand Up @@ -419,7 +420,7 @@ function removeAllSubscriptions(device, callback) {
* @param {String} service Service of the device to unregister.
* @param {String} subservice Subservice inside the service for the unregisterd device.
*/
function unregisterDevice(id, service, subservice, callback) {
function unregisterDevice(id, apikey, service, subservice, callback) {
function processContextUnregister(body, innerCallback) {
innerCallback(null);
}
Expand All @@ -428,9 +429,9 @@ function unregisterDevice(id, service, subservice, callback) {
innerCallback(null);
}

logger.debug(context, 'Removing device register in Device Service');
logger.debug(context, 'Removing device %j %j %j %j register in Device Service', id, apikey, service, subservice);

config.getRegistry().get(id, service, subservice, function (error, device) {
config.getRegistry().get(id, apikey, service, subservice, function (error, device) {
if (error) {
callback(error);
} else {
Expand All @@ -454,7 +455,7 @@ function unregisterDevice(id, service, subservice, callback) {
processUnsubscribes,
apply(registrationUtils.sendRegistrations, true, mergedDevice),
processContextUnregister,
apply(config.getRegistry().remove, id, service, subservice)
apply(config.getRegistry().remove, id, apikey, service, subservice)
],
callback
);
Expand Down Expand Up @@ -529,8 +530,17 @@ function listDevices(service, subservice, limit, offset, callback) {
* @param {String} service Service for which the requested device.
* @param {String} subservice Subservice inside the service for which the device is requested.
*/
function getDevice(deviceId, service, subservice, callback) {
config.getRegistry().get(deviceId, service, subservice, callback);
function getDevice(deviceId, apikey, service, subservice, callback) {
config.getRegistry().get(deviceId, apikey, service, subservice, callback);
}

/**
* Update a device from the device registry.
*
* @param {String} device JSON object contain the device to update.
*/
function updateDevice(device, callback) {
config.getRegistry().update(device, callback);
}

/**
Expand All @@ -540,8 +550,8 @@ function getDevice(deviceId, service, subservice, callback) {
* @param {String} service Service for which the requested device.
* @param {String} subservice Subservice inside the service for which the device is requested.
*/
function getDeviceSilently(deviceId, service, subservice, callback) {
config.getRegistry().getSilently(deviceId, service, subservice, callback);
function getDeviceSilently(deviceId, apikey, service, subservice, callback) {
config.getRegistry().getSilently(deviceId, apikey, service, subservice, callback);
}

/**
Expand Down Expand Up @@ -608,8 +618,8 @@ function checkRegistry(fn) {
};
}

function findOrCreate(deviceId, group, callback) {
getDeviceSilently(deviceId, group.service, group.subservice, function (error, device) {
function findOrCreate(deviceId, apikey, group, callback) {
getDeviceSilently(deviceId, apikey, group.service, group.subservice, function (error, device) {
if (!error && device) {
if (
(!('apikey' in device) || device.apikey === undefined) &&
Expand All @@ -618,11 +628,12 @@ function findOrCreate(deviceId, group, callback) {
) {
logger.info(context, 'Update provisioned device %j with measure/group apikey %j', device, group.apikey);
device.apikey = group.apikey; // group apikey is the same of current measure apikey
updateRegisterDevice(device, function (error, device) {
updateDevice(device, function (error) {
callback(error, device, group);
});
} else {
callback(null, device, group);
}
callback(null, device, group);
} else if (error.name === 'DEVICE_NOT_FOUND') {
const newDevice = {
id: deviceId,
Expand Down Expand Up @@ -689,7 +700,7 @@ function retrieveDevice(deviceId, apiKey, callback) {
async.waterfall(
[
apply(groupService.get, config.getConfig().defaultResource || '', apiKey),
apply(findOrCreate, deviceId),
apply(findOrCreate, deviceId, apiKey),
apply(
mergeDeviceWithConfiguration,
['lazy', 'active', 'staticAttributes', 'commands', 'subscriptions'],
Expand All @@ -704,6 +715,7 @@ function retrieveDevice(deviceId, apiKey, callback) {
exports.listDevices = intoTrans(context, checkRegistry)(listDevices);
exports.listDevicesWithType = intoTrans(context, checkRegistry)(listDevicesWithType);
exports.getDevice = intoTrans(context, checkRegistry)(getDevice);
exports.updateDevice = intoTrans(context, checkRegistry)(updateDevice);
exports.getDeviceSilently = intoTrans(context, checkRegistry)(getDeviceSilently);
exports.getDevicesByAttribute = intoTrans(context, checkRegistry)(getDevicesByAttribute);
exports.getDeviceByName = intoTrans(context, checkRegistry)(getDeviceByName);
Expand Down
16 changes: 14 additions & 2 deletions lib/services/devices/devices-NGSI-LD.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,13 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
if (entityInfoUpdated) {
async.waterfall(
[
apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice),
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
deviceObj.service,
deviceObj.subservice
),
apply(extractDeviceDifference, deviceObj),
createInitialEntityNgsiLD,
apply(combineWithNewDevice, deviceObj),
Expand All @@ -382,7 +388,13 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
} else {
async.waterfall(
[
apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice),
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
deviceObj.service,
deviceObj.subservice
),
apply(extractDeviceDifference, deviceObj),
updateEntityNgsiLD,
apply(combineWithNewDevice, deviceObj),
Expand Down
16 changes: 14 additions & 2 deletions lib/services/devices/devices-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,13 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
if (entityInfoUpdated) {
async.waterfall(
[
apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice),
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
deviceObj.service,
deviceObj.subservice
),
apply(extractDeviceDifference, deviceObj),
createInitialEntityNgsi2,
apply(combineWithNewDevice, deviceObj),
Expand All @@ -437,7 +443,13 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
} else {
async.waterfall(
[
apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice),
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
deviceObj.service,
deviceObj.subservice
),
apply(extractDeviceDifference, deviceObj),
updateEntityNgsi2,
apply(combineWithNewDevice, deviceObj),
Expand Down
2 changes: 1 addition & 1 deletion lib/services/groups/groupService.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function remove(service, subservice, resource, apikey, device, callback) {
}

function unregisterDevice(device, cb) {
deviceService.unregister(device.id, service, subservice, function (error) {
deviceService.unregister(device.id, device.apikey, service, subservice, function (error) {
if (error) {
cb(error);
}
Expand Down
19 changes: 14 additions & 5 deletions lib/services/northBound/deviceProvisioningServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ function handleListDevices(req, res, next) {
function handleGetDevice(req, res, next) {
deviceService.getDevice(
req.params.deviceId,
req.query.apikey,
req.headers['fiware-service'],
req.headers['fiware-servicepath'],
function (error, device) {
Expand All @@ -269,8 +270,8 @@ function handleGetDevice(req, res, next) {
* This middleware handles the removal of a particular device specified with the deviceId.
*/
function handleRemoveDevice(req, res, next) {
function getDevice(deviceId, service, subservice, callback) {
deviceService.getDevice(deviceId, service, subservice, function (error, device) {
function getDevice(deviceId, apikey, service, subservice, callback) {
deviceService.getDevice(deviceId, apikey, service, subservice, function (error, device) {
if (error) {
callback(error);
} else if (device) {
Expand All @@ -289,18 +290,25 @@ function handleRemoveDevice(req, res, next) {
}
}

function unregisterDevice(deviceId, service, subservice, device, callback) {
return deviceService.unregister(deviceId, service, subservice, callback);
function unregisterDevice(deviceId, apikey, service, subservice, device, callback) {
return deviceService.unregister(deviceId, apikey, service, subservice, callback);
}

async.waterfall(
[
apply(statsRegistry.add, 'deviceRemovalRequests', 1),
apply(getDevice, req.params.deviceId, req.headers['fiware-service'], req.headers['fiware-servicepath']),
apply(
getDevice,
req.params.deviceId,
req.query.apikey,
req.headers['fiware-service'],
req.headers['fiware-servicepath']
),
applyRemoveDeviceHandler,
apply(
unregisterDevice,
req.params.deviceId,
req.query.apikey,
req.headers['fiware-service'],
req.headers['fiware-servicepath']
)
Expand Down Expand Up @@ -334,6 +342,7 @@ function handleUpdateDevice(req, res, next) {
} else {
deviceService.getDevice(
req.params.deviceId,
req.query.apikey,
req.headers['fiware-service'],
req.headers['fiware-servicepath'],
function (error, device) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/general/contextBrokerKeystoneSecurityAccess-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with Keystone', functio
});

it('subscribe requests use auth header', function (done) {
iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) {
iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) {
iotAgentLib.subscribe(device, ['dimming'], null, function (error) {
should.not.exist(error);

Expand All @@ -314,7 +314,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with Keystone', functio
'X-Subject-Token': '12345679ABCDEF'
});

iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) {
iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) {
iotAgentLib.subscribe(device, ['dimming'], null, function (error) {
iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) {
contextBrokerMock.done();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mongodb/mongodb-registry-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe('NGSI-v2 - MongoDB Device Registry', function () {
});

it('should be removed from MongoDB', function (done) {
iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) {
iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) {
iotAgentDb
.db()
.collection('devices')
Expand Down
Loading

0 comments on commit d220ac0

Please sign in to comment.