From ba163adadc98fcbcaa4f7c9e3a0c17ec185e15a8 Mon Sep 17 00:00:00 2001 From: bmastahac Date: Fri, 22 Jan 2021 18:24:48 +0200 Subject: [PATCH] Removed domain fallback and fixed callback error handling --- components/anchoring/controllers.js | 11 ++++++-- components/anchoring/index.js | 16 ----------- components/anchoring/utils/index.js | 15 ++-------- components/anchoring/versions/index.js | 11 ++++++-- components/bricking/controllers.js | 38 +++++++++++++++++--------- components/bricking/utils/index.js | 17 ++---------- 6 files changed, 47 insertions(+), 61 deletions(-) diff --git a/components/anchoring/controllers.js b/components/anchoring/controllers.js index 5c44d85..6d9740c 100644 --- a/components/anchoring/controllers.js +++ b/components/anchoring/controllers.js @@ -5,11 +5,16 @@ function createHandler(server){ return function addAnchor(request, response, next) { - // get the domain configuration based on the domain extracted from anchorId. if no domain found fallback on default - const domainConfig = require("./utils").getAnchoringDomainConfig(request.params.anchorId); + // get the domain configuration based on the domain extracted from anchorId. + const receivedDomain = require('./utils').getDomainFromKeySSI(request.params.anchorId); + const domainConfig = require("./utils").getAnchoringDomainConfig(receivedDomain); + if (!domainConfig) + { + console.log('Anchoring Domain not found : ', receivedDomain); + return response.send(500); + } //init will receive all the available context information : the whole strategy, body, anchorId from the query and the protocol let flow = $$.flow.start(domainConfig.type); - //let flow = $$.flow.start('ETH'); flow.init(domainConfig, request.params.anchorId, request.body, server.rootFolder); // all the available information was passed on init. diff --git a/components/anchoring/index.js b/components/anchoring/index.js index 8228e8c..e4a0342 100644 --- a/components/anchoring/index.js +++ b/components/anchoring/index.js @@ -17,22 +17,6 @@ function Anchoring(server) { AnchorVersions(server); AnchorSubscribe(server); - - //todo : check for body & param - // de verificat daca ce primim in body si parameters este corect si nu e malformat - Pentru entry points apelate din extern - // ancorare - validat body structure - // bricks - nu e nimic de validat. Ce se primeste in stream se pune in brick - // bricksFabric - e folosit intern. Nu validam - // BricksLedger - e folosit intern. Nu validam - // todo : de revizuit operatiile I/O de read/write file codate cu Sync, exceptie bootup. Recodare folosind callback - // discutat locatie fisiere. Momentan e folosit server.rootFolder. De revizuit codul ca toate sa fie relativ la server.rootFolder - // pentru deployment locatia va fi mounted. Se poate folosi similar cu implementarea de la bricks : - //if (typeof process.env.EDFS_BRICK_STORAGE_FOLDER !== 'undefined') { - // storageFolder = process.env.EDFS_BRICK_STORAGE_FOLDER; - //} - // Intrebare : Ar fi ok sa folosesc aceiasi abordare pentru tot, folosind intrari diferite pentru fiecare entrypoint? - // validare date primite din exterior - // revizuire : https://privatesky.xyz/?API/api-hub/overview } module.exports = Anchoring; diff --git a/components/anchoring/utils/index.js b/components/anchoring/utils/index.js index c3006c6..c28fc83 100644 --- a/components/anchoring/utils/index.js +++ b/components/anchoring/utils/index.js @@ -1,22 +1,13 @@ -const getAnchoringDomainConfig = (ssiString) => { +const getAnchoringDomainConfig = (domain) => { const config = require("../../../config"); - const domain = getDomainFromKeySSI(ssiString); - let domainConfig = config.getConfig('endpointsConfig', 'anchoring', 'domainStrategies', domain); - - if (!domainConfig) { - domainConfig = config.getConfig('endpointsConfig', 'anchoring', 'domainStrategies', 'default'); - } - - return domainConfig; + return config.getConfig('endpointsConfig', 'anchoring', 'domainStrategies', domain); }; const getDomainFromKeySSI = function (ssiString) { const openDSU = require("opendsu"); const keySSISpace = openDSU.loadApi("keyssi"); - const keySSI = keySSISpace.parse(ssiString); - const domain = keySSI.getDLDomain(); - return domain; + return keySSI.getDLDomain(); } module.exports = {getAnchoringDomainConfig, getDomainFromKeySSI} \ No newline at end of file diff --git a/components/anchoring/versions/index.js b/components/anchoring/versions/index.js index c91bcce..2684304 100644 --- a/components/anchoring/versions/index.js +++ b/components/anchoring/versions/index.js @@ -1,9 +1,16 @@ function AnchorVersions(server) { server.get(`/anchor/:domain/versions/:keyssi`, (request, response, next) => { - const domainConfig = require("../utils").getAnchoringDomainConfig(request.params.keyssi); + + // get the domain configuration based on the domain extracted from anchorId. + const receivedDomain = require('../utils').getDomainFromKeySSI(request.params.keyssi); + const domainConfig = require("../utils").getAnchoringDomainConfig(receivedDomain); + if (!domainConfig) + { + console.log('Anchoring Domain not found : ', receivedDomain); + return response.send(500); + } const flow = $$.flow.start(domainConfig.type); - //const flow = $$.flow.start('ETH'); flow.init(domainConfig,request.params.keyssi, request.body, server.rootFolder); flow.readVersions(request.params.keyssi,server, (err, fileHashes) => { if (err) { diff --git a/components/bricking/controllers.js b/components/bricking/controllers.js index 684cefd..f164e0f 100644 --- a/components/bricking/controllers.js +++ b/components/bricking/controllers.js @@ -2,12 +2,16 @@ function createHandlerUploadBrick(server) { return function uploadBrick(request, response, next) { - const flow = $$.flow.start('BricksManager'); - //ensure we support both signatures for put-brick. With and without domain. Fallback to 'default'. - const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(request.params.domain); - const domain = require('./utils/index').getSafeDomain(request.params.domain); + const brickDomain = request.params.domain; + const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(brickDomain); + if (!domainConfig) + { + console.log('Brick Domain not found : ', request.params.domain); + return response.send(500); + } - flow.init(domainConfig, domain, server.rootFolder); + const flow = $$.flow.start('BricksManager'); + flow.init(domainConfig, brickDomain, server.rootFolder); flow.write(request, (err, result) => { if (err) { @@ -25,12 +29,16 @@ function createHandlerDownloadBrick(server) return function downloadBrick(request, response, next) { response.setHeader('content-type', 'application/octet-stream'); response.setHeader('Cache-control', 'max-age=31536000'); // set brick cache expiry to 1 year - //ensure we support both signatures for put-brick. With and without domain. Fallback to 'default'. - const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(request.params.domain); - const domain = require('./utils/index').getSafeDomain(request.params.domain); + const brickDomain = request.params.domain; + const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(brickDomain); + if (!domainConfig) + { + console.log('Brick Domain not found : ', request.params.domain); + return response.send(500); + } const flow = $$.flow.start('BricksManager'); - flow.init(domainConfig,domain,server.rootFolder); + flow.init(domainConfig,brickDomain,server.rootFolder); flow.read(request.params.hashLink, response, (err, result) => { if (err) { @@ -48,12 +56,16 @@ function createHandlerDownloadMultipleBricks(server) return function downloadMultipleBricks(request, response, next) { response.setHeader('content-type', 'application/octet-stream'); response.setHeader('Cache-control', 'max-age=31536000'); // set brick cache expiry to 1 year - //ensure we support both signatures for put-brick. With and without domain. Fallback to 'default'. - const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(request.params.domain); - const domain = require('./utils/index').getSafeDomain(request.params.domain); + const brickDomain = request.params.domain; + const domainConfig = require('./utils/index').getBricksDomainConfigByDomain(brickDomain); + if (!domainConfig) + { + console.log('Brick Domain not found : ', request.params.domain); + return response.send(500); + } const flow = $$.flow.start('BricksManager'); - flow.init(domainConfig,domain,server.rootFolder); + flow.init(domainConfig,brickDomain,server.rootFolder); flow.readMultipleBricks(request.query.hashes, response, (err, result) => { if (err) { return response.send(404, 'Brick not found'); diff --git a/components/bricking/utils/index.js b/components/bricking/utils/index.js index 930e639..cb97e71 100644 --- a/components/bricking/utils/index.js +++ b/components/bricking/utils/index.js @@ -4,30 +4,17 @@ const getBricksDomainConfigFromKeySSI = (ssiString) => { }; const getBricksDomainConfigByDomain = (domain) => { - domain = getSafeDomain(domain); return __getDomainConfig(domain); }; function __getDomainConfig(domain) { const config = require("../../../config"); let domainConfig = config.getConfig('endpointsConfig', 'bricking', 'domains', domain); - if (!domainConfig) { - domainConfig = config.getConfig('endpointsConfig', 'bricking', 'domains', 'default'); - } + return domainConfig; } -const getSafeDomain = (domain) => { - if (typeof domain === 'undefined') { - return "default"; - } - return domain; -}; - const getDomainFromKeySSI = function (ssiString) { - if (typeof ssiString === 'undefined') { - return "default"; - } const openDSU = require("opendsu"); const keySSISpace = openDSU.loadApi("keyssi"); @@ -36,4 +23,4 @@ const getDomainFromKeySSI = function (ssiString) { return domain; }; -module.exports = {getBricksDomainConfigFromKeySSI, getDomainFromKeySSI, getBricksDomainConfigByDomain, getSafeDomain}; \ No newline at end of file +module.exports = {getBricksDomainConfigFromKeySSI, getDomainFromKeySSI, getBricksDomainConfigByDomain}; \ No newline at end of file