From d965ac8bb00e443c6c9ce72674b72edcaa3e9ecc Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 12 Jul 2018 15:23:25 +0300 Subject: Implement secrets --- cli.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 17 ++++++++++++++--- models.js | 6 +++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index c14a8b8..04fca98 100755 --- a/cli.js +++ b/cli.js @@ -225,6 +225,59 @@ vorpal callback(); }); +vorpal + .command('secret set ', 'Set a secret key value pair.') + .action(async function (args, callback) { + try { + await checkConnection(mongoURL, this); + } catch (err) { + showMongoNotSetError(vorpal); + return callback(); + } + + if (!args.key || !args.value) { + this.log('Please provide both "key" and "value" arguments.'); + return callback(); + } + + try { + await models.secrets.update( + {key: args.key}, + {key: args.key, value: args.value}, + {upsert: true}); + this.log(chalk.green(`Secret "${args.key}" set successfully.`)); + } catch (err) { + this.log(chalk.red(`Failed to set secret ${args.key}!`)); + this.log(chalk.red(String(err))); + } + callback(); + }); + +vorpal + .command('secret remove ', 'Remove a secret key value pair.') + .action(async function (args, callback) { + try { + await checkConnection(mongoURL, this); + } catch (err) { + showMongoNotSetError(vorpal); + return callback(); + } + + if (!args.key) { + this.log('Please provide the "key" argument.'); + return callback(); + } + + try { + await models.secrets.remove({key: args.key}); + this.log(chalk.green(`Secret "${args.key}" removed successfully.`)); + } catch (err) { + this.log(chalk.red(`Failed to remove secret ${args.key}!`)); + this.log(chalk.red(String(err))); + } + callback(); + }); + vorpal .command('info', 'Show information about the session') .action(async function (args, callback) { diff --git a/index.js b/index.js index 2c7a373..558d7d0 100644 --- a/index.js +++ b/index.js @@ -21,20 +21,31 @@ app.all('/:path*', async (req, res) => { const path = req.params.path.split('/')[0]; if (!path || path.length === 0) { res.status(400); - res.send('Empty endpoint.'); + return res.send('Empty endpoint.'); } const endpoint = await models.endpoints.findOne({name: path}); if (!endpoint) { res.status(400); - res.send('No such endpoint.'); + return res.send('No such endpoint.'); } + const secretsList = await models.endpoints.find(); + if (!secretsList) { + res.status(500); + return res.send('Failed to fetch secrets from the database.'); + } + + const secrets = secretsList.reduce((prev, cur) => ({ + ...prev, + [cur.key]: cur.value + }), {}); + console.info(`${chalk.green(req.path)}, running endpoint "${chalk.yellow(endpoint.name)}".`); const {code} = endpoint; try { const func = requireFromString(code); - func(req, res); + func(req, res, secrets); } catch (err) { res.status(500); console.error(chalk.red(`Error in endpoint ${endpoint.name}! Details below.`)); diff --git a/models.js b/models.js index cf5137d..50ee3b8 100644 --- a/models.js +++ b/models.js @@ -2,7 +2,11 @@ module.exports = db => { const endpoints = db.get('endpoints'); endpoints.createIndex({name: 1}, {unique: true}); + const secrets = db.get('secrets'); + secrets.createIndex({key: 1}, {unique: true}); + return { - endpoints + endpoints, + secrets }; }; -- cgit v1.3