Skip to content

Commit

Permalink
add handling for default and production config file
Browse files Browse the repository at this point in the history
  • Loading branch information
TarradeMarc committed Apr 9, 2024
1 parent a8086e9 commit ec13449
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions configmanager/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 })
})
}
})
});
}
Expand All @@ -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 })
})
}
})
});
}
Expand Down

0 comments on commit ec13449

Please sign in to comment.