From 5004d0c5fd4e66e1ae944a99e3672eaee1696186 Mon Sep 17 00:00:00 2001 From: Railag Date: Thu, 8 Nov 2018 13:57:16 +0300 Subject: [PATCH 1/2] EWC-321 lastActive agent decorator --- src/controllers/agent-controller.js | 33 ++++++++--------- src/decorators/agent-last-active-decorator.js | 35 +++++++++++++++++++ src/sequelize/managers/iofog-manager.js | 28 +++++++++++---- 3 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 src/decorators/agent-last-active-decorator.js diff --git a/src/controllers/agent-controller.js b/src/controllers/agent-controller.js index ab078c5d2..0b5e494cb 100644 --- a/src/controllers/agent-controller.js +++ b/src/controllers/agent-controller.js @@ -15,6 +15,7 @@ const logger = require('../logger'); const AgentService = require('../services/agent-service'); const AuthDecorator = require('../decorators/authorization-decorator'); +const AgentDecorator = require('../decorators/agent-last-active-decorator'); const agentProvisionEndPoint = async function (req) { const provisionData = req.body; @@ -114,20 +115,20 @@ const putImageSnapshotEndPoint = async function (req, fog) { module.exports = { agentProvisionEndPoint: agentProvisionEndPoint, - getAgentConfigEndPoint: AuthDecorator.checkFogToken(getAgentConfigEndPoint), - updateAgentConfigEndPoint: AuthDecorator.checkFogToken(updateAgentConfigEndPoint), - getAgentConfigChangesEndPoint: AuthDecorator.checkFogToken(getAgentConfigChangesEndPoint), - updateAgentStatusEndPoint: AuthDecorator.checkFogToken(updateAgentStatusEndPoint), - getAgentMicroservicesEndPoint: AuthDecorator.checkFogToken(getAgentMicroservicesEndPoint), - getAgentMicroserviceEndPoint: AuthDecorator.checkFogToken(getAgentMicroserviceEndPoint), - getAgentRegistriesEndPoint: AuthDecorator.checkFogToken(getAgentRegistriesEndPoint), - getAgentTunnelEndPoint: AuthDecorator.checkFogToken(getAgentTunnelEndPoint), - getAgentStraceEndPoint: AuthDecorator.checkFogToken(getAgentStraceEndPoint), - updateAgentStraceEndPoint: AuthDecorator.checkFogToken(updateAgentStraceEndPoint), - getAgentChangeVersionCommandEndPoint: AuthDecorator.checkFogToken(getAgentChangeVersionCommandEndPoint), - updateHalHardwareInfoEndPoint: AuthDecorator.checkFogToken(updateHalHardwareInfoEndPoint), - updateHalUsbInfoEndPoint: AuthDecorator.checkFogToken(updateHalUsbInfoEndPoint), - deleteNodeEndPoint: AuthDecorator.checkFogToken(deleteNodeEndPoint), - getImageSnapshotEndPoint: AuthDecorator.checkFogToken(getImageSnapshotEndPoint), - putImageSnapshotEndPoint: AuthDecorator.checkFogToken(putImageSnapshotEndPoint) + getAgentConfigEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentConfigEndPoint)), + updateAgentConfigEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateAgentConfigEndPoint)), + getAgentConfigChangesEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentConfigChangesEndPoint)), + updateAgentStatusEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateAgentStatusEndPoint)), + getAgentMicroservicesEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentMicroservicesEndPoint)), + getAgentMicroserviceEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentMicroserviceEndPoint)), + getAgentRegistriesEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentRegistriesEndPoint)), + getAgentTunnelEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentTunnelEndPoint)), + getAgentStraceEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentStraceEndPoint)), + updateAgentStraceEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateAgentStraceEndPoint)), + getAgentChangeVersionCommandEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentChangeVersionCommandEndPoint)), + updateHalHardwareInfoEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateHalHardwareInfoEndPoint)), + updateHalUsbInfoEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateHalUsbInfoEndPoint)), + deleteNodeEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(deleteNodeEndPoint)), + getImageSnapshotEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getImageSnapshotEndPoint)), + putImageSnapshotEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(putImageSnapshotEndPoint)) }; \ No newline at end of file diff --git a/src/decorators/agent-last-active-decorator.js b/src/decorators/agent-last-active-decorator.js new file mode 100644 index 000000000..9f24fde1d --- /dev/null +++ b/src/decorators/agent-last-active-decorator.js @@ -0,0 +1,35 @@ +/* + * ******************************************************************************* + * * Copyright (c) 2018 Edgeworx, Inc. + * * + * * This program and the accompanying materials are made available under the + * * terms of the Eclipse Public License v. 2.0 which is available at + * * http://www.eclipse.org/legal/epl-2.0 + * * + * * SPDX-License-Identifier: EPL-2.0 + * ******************************************************************************* + * + */ +const logger = require('../logger'); +const FogManager = require('../sequelize/managers/iofog-manager'); + +function updateLastActive(f) { + return async function() { + + const fArgs = Array.prototype.slice.call(arguments); + + const fog = fArgs[fArgs.length - 1]; + + const timestamp = Date.now(); + + logger.info('updating agent lastActive timestamp: ' + timestamp); + + await FogManager.updateLastActive(fog.uuid, timestamp); + + return await f.apply(this, fArgs); + } +} + +module.exports = { + updateLastActive: updateLastActive +}; \ No newline at end of file diff --git a/src/sequelize/managers/iofog-manager.js b/src/sequelize/managers/iofog-manager.js index d380991a7..d4b83c98b 100644 --- a/src/sequelize/managers/iofog-manager.js +++ b/src/sequelize/managers/iofog-manager.js @@ -11,12 +11,12 @@ * */ -const BaseManager = require('../managers/base-manager') +const BaseManager = require('../managers/base-manager'); const models = require('./../models'); -const Fog = models.Fog -const Microservice = models.Microservice -const FogAccessToken = models.FogAccessToken -const Strace = models.StraceDiagnostics +const Fog = models.Fog; +const Microservice = models.Microservice; +const FogAccessToken = models.FogAccessToken; +const Strace = models.StraceDiagnostics; class FogManager extends BaseManager { getEntity() { @@ -36,6 +36,18 @@ class FogManager extends BaseManager { }); } + + // no transaction required here, used by agent-last-active decorator + updateLastActive(uuid, timestamp) { + return Fog.update({ + lastActive: timestamp + }, { + where: { + uuid: uuid + } + }); + } + findFogStraces(where, transaction) { return Fog.findOne({ include: [ @@ -52,7 +64,9 @@ class FogManager extends BaseManager { where: where }, {transaction: transaction}) } + + } -const instance = new FogManager() -module.exports = instance \ No newline at end of file +const instance = new FogManager(); +module.exports = instance; \ No newline at end of file From e52a887f7bfa827d7b1302b33abc90a52bb6186b Mon Sep 17 00:00:00 2001 From: Railag Date: Thu, 8 Nov 2018 15:28:49 +0300 Subject: [PATCH 2/2] cleanup --- src/controllers/agent-controller.js | 35 +++++++++---------- src/decorators/agent-last-active-decorator.js | 35 ------------------- src/decorators/authorization-decorator.js | 6 ++++ 3 files changed, 23 insertions(+), 53 deletions(-) delete mode 100644 src/decorators/agent-last-active-decorator.js diff --git a/src/controllers/agent-controller.js b/src/controllers/agent-controller.js index 0b5e494cb..6fc8c8330 100644 --- a/src/controllers/agent-controller.js +++ b/src/controllers/agent-controller.js @@ -15,7 +15,6 @@ const logger = require('../logger'); const AgentService = require('../services/agent-service'); const AuthDecorator = require('../decorators/authorization-decorator'); -const AgentDecorator = require('../decorators/agent-last-active-decorator'); const agentProvisionEndPoint = async function (req) { const provisionData = req.body; @@ -105,7 +104,7 @@ const deleteNodeEndPoint = async function (req, fog) { return await AgentService.deleteNode(fog); }; -const getImageSnapshotEndPoint = async function(req, fog) { +const getImageSnapshotEndPoint = async function (req, fog) { return await AgentService.getImageSnapshot(fog); }; @@ -115,20 +114,20 @@ const putImageSnapshotEndPoint = async function (req, fog) { module.exports = { agentProvisionEndPoint: agentProvisionEndPoint, - getAgentConfigEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentConfigEndPoint)), - updateAgentConfigEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateAgentConfigEndPoint)), - getAgentConfigChangesEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentConfigChangesEndPoint)), - updateAgentStatusEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateAgentStatusEndPoint)), - getAgentMicroservicesEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentMicroservicesEndPoint)), - getAgentMicroserviceEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentMicroserviceEndPoint)), - getAgentRegistriesEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentRegistriesEndPoint)), - getAgentTunnelEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentTunnelEndPoint)), - getAgentStraceEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentStraceEndPoint)), - updateAgentStraceEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateAgentStraceEndPoint)), - getAgentChangeVersionCommandEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getAgentChangeVersionCommandEndPoint)), - updateHalHardwareInfoEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateHalHardwareInfoEndPoint)), - updateHalUsbInfoEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(updateHalUsbInfoEndPoint)), - deleteNodeEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(deleteNodeEndPoint)), - getImageSnapshotEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(getImageSnapshotEndPoint)), - putImageSnapshotEndPoint: AuthDecorator.checkFogToken(AgentDecorator.updateLastActive(putImageSnapshotEndPoint)) + getAgentConfigEndPoint: AuthDecorator.checkFogToken(getAgentConfigEndPoint), + updateAgentConfigEndPoint: AuthDecorator.checkFogToken(updateAgentConfigEndPoint), + getAgentConfigChangesEndPoint: AuthDecorator.checkFogToken(getAgentConfigChangesEndPoint), + updateAgentStatusEndPoint: AuthDecorator.checkFogToken(updateAgentStatusEndPoint), + getAgentMicroservicesEndPoint: AuthDecorator.checkFogToken(getAgentMicroservicesEndPoint), + getAgentMicroserviceEndPoint: AuthDecorator.checkFogToken(getAgentMicroserviceEndPoint), + getAgentRegistriesEndPoint: AuthDecorator.checkFogToken(getAgentRegistriesEndPoint), + getAgentTunnelEndPoint: AuthDecorator.checkFogToken(getAgentTunnelEndPoint), + getAgentStraceEndPoint: AuthDecorator.checkFogToken(getAgentStraceEndPoint), + updateAgentStraceEndPoint: AuthDecorator.checkFogToken(updateAgentStraceEndPoint), + getAgentChangeVersionCommandEndPoint: AuthDecorator.checkFogToken(getAgentChangeVersionCommandEndPoint), + updateHalHardwareInfoEndPoint: AuthDecorator.checkFogToken(updateHalHardwareInfoEndPoint), + updateHalUsbInfoEndPoint: AuthDecorator.checkFogToken(updateHalUsbInfoEndPoint), + deleteNodeEndPoint: AuthDecorator.checkFogToken(deleteNodeEndPoint), + getImageSnapshotEndPoint: AuthDecorator.checkFogToken(getImageSnapshotEndPoint), + putImageSnapshotEndPoint: AuthDecorator.checkFogToken(putImageSnapshotEndPoint) }; \ No newline at end of file diff --git a/src/decorators/agent-last-active-decorator.js b/src/decorators/agent-last-active-decorator.js deleted file mode 100644 index 9f24fde1d..000000000 --- a/src/decorators/agent-last-active-decorator.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ******************************************************************************* - * * Copyright (c) 2018 Edgeworx, Inc. - * * - * * This program and the accompanying materials are made available under the - * * terms of the Eclipse Public License v. 2.0 which is available at - * * http://www.eclipse.org/legal/epl-2.0 - * * - * * SPDX-License-Identifier: EPL-2.0 - * ******************************************************************************* - * - */ -const logger = require('../logger'); -const FogManager = require('../sequelize/managers/iofog-manager'); - -function updateLastActive(f) { - return async function() { - - const fArgs = Array.prototype.slice.call(arguments); - - const fog = fArgs[fArgs.length - 1]; - - const timestamp = Date.now(); - - logger.info('updating agent lastActive timestamp: ' + timestamp); - - await FogManager.updateLastActive(fog.uuid, timestamp); - - return await f.apply(this, fArgs); - } -} - -module.exports = { - updateLastActive: updateLastActive -}; \ No newline at end of file diff --git a/src/decorators/authorization-decorator.js b/src/decorators/authorization-decorator.js index b4840a59d..b4a36ba75 100644 --- a/src/decorators/authorization-decorator.js +++ b/src/decorators/authorization-decorator.js @@ -65,7 +65,13 @@ function checkFogToken(f) { } fArgs.push(fog); + FogAccessTokenManager.updateExpirationTime(fog.accessToken.id, fog.accessToken.expirationTime + config.get('Settings:FogTokenExpirationIntervalSeconds') * 1000); + + const timestamp = Date.now(); + logger.info('updating agent lastActive timestamp: ' + timestamp); + await FogManager.updateLastActive(fog.uuid, timestamp); + return await f.apply(this, fArgs); } }