From ec13449c087a076da0cd773ed1ceaebe6b6feef9 Mon Sep 17 00:00:00 2001 From: "m.tarrade@sap.com" Date: Tue, 9 Apr 2024 14:08:28 +0200 Subject: [PATCH] add handling for default and production config file --- configmanager/server.js | 74 ++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/configmanager/server.js b/configmanager/server.js index ff88bd9..76cadde 100644 --- a/configmanager/server.js +++ b/configmanager/server.js @@ -14,12 +14,10 @@ app.get('/:namespace/:application', (req, res) => { res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains"); res.setHeader("Content-Security-Policy", "script-src 'self'"); const { namespace, application } = req.params; - const filePath = path.resolve(path.normalize(`${__dirname}/data/cad-${namespace}-${application}.json`).replace(/^(\.\.(\/|\\|$))+/, '')); + const filePath = path.resolve(path.normalize(`/data/cad-${namespace}-${application}.json`).replace(/^(\.\.(\/|\\|$))+/, '')); const defaultFilePath = `/data/cad-default.json`; - const configFilePath = `/data/config-default.json`; - if(!filePath.startsWith(__dirname)){ - return res.end() - } + const configFilePath = path.resolve(path.normalize(`/data/config-${namespace}-${application}.json`).replace(/^(\.\.(\/|\\|$))+/, '')); + const defaultConfigFilePath = `/data/config-default.json`; // Check if the file exists fs.access(filePath, fs.constants.F_OK, (err) => { @@ -40,17 +38,28 @@ app.get('/:namespace/:application', (req, res) => { const decoysJson = JSON.parse(decoys); // Check if the file exists fs.access(configFilePath, fs.constants.F_OK, err => { - if(err) return res.json({ decoy: decoysJson }); - - // If the file exists, read its contents and return as JSON object - fs.readFile(configFilePath, 'utf8', (err, config) => { - if(err) return res.json({ decoy: decoysJson }); - if (config) { - const configJson = JSON.parse(config); - return res.json({ decoy: decoysJson, config: configJson }); - } - return res.json({ decoy: decoysJson }) - }) + if(err) { + fs.access(defaultConfigFilePath, fs.constants.F_OK, err => { + if (err) { return res.json({ decoy: decoysJson }) } + fs.readFile(defaultConfigFilePath, 'utf8', (err, config) => { + if(err) return res.json({ decoy: decoysJson }); + if (config) { + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } + return res.json({ decoy: decoysJson }) + }) + }) + } else { + fs.readFile(configFilePath, 'utf8', (err, config) => { + if(err) return res.json({ decoy: decoysJson }); + if (config) { + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } + return res.json({ decoy: decoysJson }) + }) + } }) }); } @@ -66,17 +75,28 @@ app.get('/:namespace/:application', (req, res) => { const decoysJson = JSON.parse(decoys); // Check if the file exists fs.access(configFilePath, fs.constants.F_OK, err => { - if(err) return res.json({ decoys: decoysJson }); - - // If the file exists, read its contents and return as JSON object - fs.readFile(configFilePath, 'utf8', (err, config) => { - if(err) return res.json({ decoys: decoysJson }); - if (config) { - const configJson = JSON.parse(config); - return res.json({ decoy: decoysJson, config: configJson }); - } - return res.json({ decoy: decoysJson }) - }) + if(err) { + fs.access(defaultConfigFilePath, fs.constants.F_OK, err => { + if (err) { return res.json({ decoy: decoysJson }) } + fs.readFile(defaultConfigFilePath, 'utf8', (err, config) => { + if(err) return res.json({ decoy: decoysJson }); + if (config) { + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } + return res.json({ decoy: decoysJson }) + }) + }) + } else { + fs.readFile(configFilePath, 'utf8', (err, config) => { + if(err) return res.json({ decoy: decoysJson }); + if (config) { + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } + return res.json({ decoy: decoysJson }) + }) + } }) }); }