2018-05-25 20:33:22 +00:00
|
|
|
import keys from 'vault/lib/keycodes';
|
2018-07-18 14:13:39 +00:00
|
|
|
import argTokenizer from 'yargs-parser/lib/tokenize-arg-string.js';
|
2019-08-01 22:12:04 +00:00
|
|
|
import { parse } from 'shell-quote';
|
2018-05-25 20:33:22 +00:00
|
|
|
|
|
|
|
const supportedCommands = ['read', 'write', 'list', 'delete'];
|
2019-07-02 22:41:23 +00:00
|
|
|
const uiCommands = ['api', 'clearall', 'clear', 'fullscreen', 'refresh'];
|
2018-05-25 20:33:22 +00:00
|
|
|
|
|
|
|
export function extractDataAndFlags(data, flags) {
|
2018-11-05 16:56:59 +00:00
|
|
|
return data.concat(flags).reduce(
|
|
|
|
(accumulator, val) => {
|
|
|
|
// will be "key=value" or "-flag=value" or "foo=bar=baz"
|
|
|
|
// split on the first =
|
2020-02-06 18:37:38 +00:00
|
|
|
// default to value of empty string
|
|
|
|
let [item, value = ''] = val.split(/=(.+)?/);
|
2018-11-05 16:56:59 +00:00
|
|
|
if (item.startsWith('-')) {
|
|
|
|
let flagName = item.replace(/^-/, '');
|
|
|
|
if (flagName === 'wrap-ttl') {
|
|
|
|
flagName = 'wrapTTL';
|
|
|
|
}
|
|
|
|
accumulator.flags[flagName] = value || true;
|
|
|
|
return accumulator;
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
2018-11-05 16:56:59 +00:00
|
|
|
// if it exists in data already, then we have multiple
|
|
|
|
// foo=bar in the list and need to make it an array
|
|
|
|
if (accumulator.data[item]) {
|
|
|
|
accumulator.data[item] = [].concat(accumulator.data[item], value);
|
|
|
|
return accumulator;
|
|
|
|
}
|
|
|
|
accumulator.data[item] = value;
|
|
|
|
return accumulator;
|
|
|
|
},
|
|
|
|
{ data: {}, flags: {} }
|
|
|
|
);
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 22:41:23 +00:00
|
|
|
export function executeUICommand(command, logAndOutput, commandFns) {
|
|
|
|
let cmd = command.startsWith('api') ? 'api' : command;
|
|
|
|
let isUICommand = uiCommands.includes(cmd);
|
2018-05-29 14:14:31 +00:00
|
|
|
if (isUICommand) {
|
2018-05-25 20:33:22 +00:00
|
|
|
logAndOutput(command);
|
|
|
|
}
|
2019-07-02 22:41:23 +00:00
|
|
|
if (typeof commandFns[cmd] === 'function') {
|
|
|
|
commandFns[cmd]();
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
return isUICommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseCommand(command, shouldThrow) {
|
2019-08-01 22:12:04 +00:00
|
|
|
let args = argTokenizer(parse(command));
|
2018-05-25 20:33:22 +00:00
|
|
|
if (args[0] === 'vault') {
|
|
|
|
args.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
let [method, ...rest] = args;
|
|
|
|
let path;
|
|
|
|
let flags = [];
|
|
|
|
let data = [];
|
|
|
|
|
|
|
|
rest.forEach(arg => {
|
|
|
|
if (arg.startsWith('-')) {
|
|
|
|
flags.push(arg);
|
|
|
|
} else {
|
|
|
|
if (path) {
|
2019-05-22 21:07:42 +00:00
|
|
|
let strippedArg = arg
|
|
|
|
// we'll have arg=something or arg="lol I need spaces", so need to split on the first =
|
|
|
|
.split(/=(.+)/)
|
|
|
|
// if there were quotes, there's an empty string as the last member in the array that we don't want,
|
|
|
|
// so filter it out
|
|
|
|
.filter(str => str !== '')
|
|
|
|
// glue the data back together
|
|
|
|
.join('=');
|
|
|
|
data.push(strippedArg);
|
2018-05-25 20:33:22 +00:00
|
|
|
} else {
|
|
|
|
path = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!supportedCommands.includes(method)) {
|
|
|
|
if (shouldThrow) {
|
|
|
|
throw new Error('invalid command');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return [method, flags, path, data];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logFromResponse(response, path, method, flags) {
|
2018-06-14 04:06:19 +00:00
|
|
|
let { format, field } = flags;
|
|
|
|
let secret = response && (response.auth || response.data || response.wrap_info);
|
|
|
|
if (!secret) {
|
2019-01-18 20:02:11 +00:00
|
|
|
if (method === 'write') {
|
|
|
|
return { type: 'success', content: `Success! Data written to: ${path}` };
|
|
|
|
} else if (method === 'delete') {
|
|
|
|
return { type: 'success', content: `Success! Data deleted (if it existed) at: ${path}` };
|
|
|
|
} else {
|
|
|
|
secret = response;
|
|
|
|
}
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (field) {
|
|
|
|
let fieldValue = secret[field];
|
|
|
|
let response;
|
|
|
|
if (fieldValue) {
|
|
|
|
if (format && format === 'json') {
|
|
|
|
return { type: 'json', content: fieldValue };
|
|
|
|
}
|
2019-01-18 16:15:37 +00:00
|
|
|
if (typeof fieldValue == 'string') {
|
|
|
|
response = { type: 'text', content: fieldValue };
|
|
|
|
} else if (typeof fieldValue == 'number') {
|
|
|
|
response = { type: 'text', content: JSON.stringify(fieldValue) };
|
|
|
|
} else if (typeof fieldValue == 'boolean') {
|
|
|
|
response = { type: 'text', content: JSON.stringify(fieldValue) };
|
|
|
|
} else if (Array.isArray(fieldValue)) {
|
|
|
|
response = { type: 'text', content: JSON.stringify(fieldValue) };
|
|
|
|
} else {
|
|
|
|
response = { type: 'object', content: fieldValue };
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
response = { type: 'error', content: `Field "${field}" not present in secret` };
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format && format === 'json') {
|
|
|
|
// just print whole response
|
|
|
|
return { type: 'json', content: response };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method === 'list') {
|
|
|
|
return { type: 'list', content: secret };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { type: 'object', content: secret };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logFromError(error, vaultPath, method) {
|
|
|
|
let content;
|
|
|
|
let { httpStatus, path } = error;
|
|
|
|
let verbClause = {
|
|
|
|
read: 'reading from',
|
|
|
|
write: 'writing to',
|
|
|
|
list: 'listing',
|
|
|
|
delete: 'deleting at',
|
|
|
|
}[method];
|
|
|
|
|
|
|
|
content = `Error ${verbClause}: ${vaultPath}.\nURL: ${path}\nCode: ${httpStatus}`;
|
|
|
|
|
|
|
|
if (typeof error.errors[0] === 'string') {
|
|
|
|
content = `${content}\nErrors:\n ${error.errors.join('\n ')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { type: 'error', content };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function shiftCommandIndex(keyCode, history, index) {
|
|
|
|
let newInputValue;
|
|
|
|
let commandHistoryLength = history.length;
|
|
|
|
|
2018-05-29 14:14:31 +00:00
|
|
|
if (!commandHistoryLength) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-05-25 20:33:22 +00:00
|
|
|
|
|
|
|
if (keyCode === keys.UP) {
|
|
|
|
index -= 1;
|
|
|
|
if (index < 0) {
|
|
|
|
index = commandHistoryLength - 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index += 1;
|
|
|
|
if (index === commandHistoryLength) {
|
|
|
|
newInputValue = '';
|
|
|
|
}
|
|
|
|
if (index > commandHistoryLength) {
|
|
|
|
index -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newInputValue !== '') {
|
|
|
|
newInputValue = history.objectAt(index).content;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [index, newInputValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logErrorFromInput(path, method, flags, dataArray) {
|
|
|
|
if (path === undefined) {
|
|
|
|
return { type: 'error', content: 'A path is required to make a request.' };
|
|
|
|
}
|
|
|
|
if (method === 'write' && !flags.force && dataArray.length === 0) {
|
|
|
|
return { type: 'error', content: 'Must supply data or use -force' };
|
|
|
|
}
|
|
|
|
}
|