|
| 1 | +// npm packages |
| 2 | +const got = require('got'); |
| 3 | +const chalk = require('chalk'); |
| 4 | +const inquirer = require('inquirer'); |
| 5 | + |
| 6 | +// our packages |
| 7 | +const {userConfig, isLoggedIn, logout} = require('../config'); |
| 8 | + |
| 9 | +exports.command = ['secret [cmd]']; |
| 10 | +exports.describe = 'create, list or remove deployment secrets'; |
| 11 | +exports.builder = { |
| 12 | + cmd: { |
| 13 | + default: 'new', |
| 14 | + description: 'command to execute [new | ls | rm]', |
| 15 | + }, |
| 16 | +}; |
| 17 | +exports.handler = async args => { |
| 18 | + if (!isLoggedIn()) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // services request url |
| 23 | + const remoteUrl = `${userConfig.endpoint}/secrets`; |
| 24 | + // get command |
| 25 | + const {cmd} = args; |
| 26 | + // if remove or ls - fetch secrets from remote, then do work |
| 27 | + if (cmd === 'ls' || cmd === 'rm') { |
| 28 | + console.log( |
| 29 | + chalk.bold(`${cmd === 'ls' ? 'Listing' : 'Removing'} deployment secret${cmd === 'ls' ? 's' : ''} for:`), |
| 30 | + userConfig.endpoint |
| 31 | + ); |
| 32 | + |
| 33 | + // get secrets from server |
| 34 | + // construct shared request params |
| 35 | + const options = { |
| 36 | + method: 'GET', |
| 37 | + headers: { |
| 38 | + Authorization: `Bearer ${userConfig.token}`, |
| 39 | + }, |
| 40 | + json: true, |
| 41 | + }; |
| 42 | + // try sending request |
| 43 | + let secrets = []; |
| 44 | + try { |
| 45 | + const {body} = await got(remoteUrl, options); |
| 46 | + secrets = body.secrets; |
| 47 | + } catch (e) { |
| 48 | + // if authorization is expired/broken/etc |
| 49 | + if (e.statusCode === 401) { |
| 50 | + logout(userConfig); |
| 51 | + console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + console.log(chalk.red('Error getting deployment secrets:'), e.toString()); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (cmd === 'ls') { |
| 60 | + console.log(chalk.bold('Got saved secrets:')); |
| 61 | + console.log(''); |
| 62 | + secrets.map(t => |
| 63 | + console.log(` > ${chalk.green(`@${t.name}`)} ${chalk.gray(`[${new Date(t.meta.created).toLocaleString()}]`)}`) |
| 64 | + ); |
| 65 | + if (!secrets.length) { |
| 66 | + console.log(' > No deployment secrets available!'); |
| 67 | + } |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const prompts = []; |
| 72 | + prompts.push({ |
| 73 | + type: 'list', |
| 74 | + name: 'rmSecret', |
| 75 | + message: 'Choose secret to remove:', |
| 76 | + choices: secrets.map(t => t.name), |
| 77 | + }); |
| 78 | + const {rmSecret} = await inquirer.prompt(prompts); |
| 79 | + |
| 80 | + // construct shared request params |
| 81 | + const rmOptions = { |
| 82 | + method: 'DELETE', |
| 83 | + headers: { |
| 84 | + Authorization: `Bearer ${userConfig.token}`, |
| 85 | + }, |
| 86 | + json: true, |
| 87 | + body: { |
| 88 | + secretName: rmSecret, |
| 89 | + }, |
| 90 | + }; |
| 91 | + try { |
| 92 | + const {body, statusCode} = await got(remoteUrl, rmOptions); |
| 93 | + if (statusCode !== 204) { |
| 94 | + console.log(chalk.red('Error removing deployment secret!'), body.reason || 'Please try again!'); |
| 95 | + return; |
| 96 | + } |
| 97 | + console.log(chalk.green('Deployment secret successfully removed!')); |
| 98 | + } catch (e) { |
| 99 | + // if authorization is expired/broken/etc |
| 100 | + if (e.statusCode === 401) { |
| 101 | + logout(userConfig); |
| 102 | + console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.'); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + console.log(chalk.red('Error removing secret:'), e.toString()); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + console.log(chalk.bold('Generating new deployment secret for:'), userConfig.endpoint); |
| 114 | + |
| 115 | + // ask for secret name and value |
| 116 | + const prompts = []; |
| 117 | + prompts.push({ |
| 118 | + type: 'input', |
| 119 | + name: 'secretName', |
| 120 | + message: 'Secret name:', |
| 121 | + validate: input => input && input.length > 0, |
| 122 | + filter: input => input.trim(), |
| 123 | + }); |
| 124 | + prompts.push({ |
| 125 | + type: 'input', |
| 126 | + name: 'secretValue', |
| 127 | + message: 'Secret value:', |
| 128 | + validate: input => input && input.length > 0, |
| 129 | + filter: input => input.trim(), |
| 130 | + }); |
| 131 | + const {secretName, secretValue} = await inquirer.prompt(prompts); |
| 132 | + |
| 133 | + // construct shared request params |
| 134 | + const options = { |
| 135 | + method: 'POST', |
| 136 | + headers: { |
| 137 | + Authorization: `Bearer ${userConfig.token}`, |
| 138 | + }, |
| 139 | + json: true, |
| 140 | + body: { |
| 141 | + secretName, |
| 142 | + secretValue, |
| 143 | + }, |
| 144 | + }; |
| 145 | + // try sending request |
| 146 | + try { |
| 147 | + const {body} = await got(remoteUrl, options); |
| 148 | + console.log(chalk.bold('New secret generated:')); |
| 149 | + console.log(''); |
| 150 | + console.log(`Name: ${body.name}`); |
| 151 | + console.log(`Value: ${body.value}`); |
| 152 | + console.log(''); |
| 153 | + console.log(chalk.yellow('WARNING!'), `Make sure to write it down, you will not be able to get it's value again!`); |
| 154 | + } catch (e) { |
| 155 | + // if authorization is expired/broken/etc |
| 156 | + if (e.statusCode === 401) { |
| 157 | + logout(userConfig); |
| 158 | + console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.'); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + console.log(chalk.red('Error generating deployment secret:'), e.toString()); |
| 163 | + } |
| 164 | +}; |
0 commit comments