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';
|
2018-05-25 20:33:22 +00:00
|
|
|
|
|
|
|
const supportedCommands = ['read', 'write', 'list', 'delete'];
|
2018-06-01 22:18:31 +00:00
|
|
|
const uiCommands = ['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 =
|
|
|
|
let [item, value] = val.split(/=(.+)/);
|
|
|
|
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;
|
2018-05-25 20:33:22 +00:00
|
|
|
|
2018-11-05 16:56:59 +00:00
|
|
|
return accumulator;
|
|
|
|
},
|
|
|
|
{ data: {}, flags: {} }
|
|
|
|
);
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 22:18:31 +00:00
|
|
|
export function executeUICommand(command, logAndOutput, clearLog, toggleFullscreen, refreshFn) {
|
2018-05-25 20:33:22 +00:00
|
|
|
const isUICommand = uiCommands.includes(command);
|
2018-05-29 14:14:31 +00:00
|
|
|
if (isUICommand) {
|
2018-05-25 20:33:22 +00:00
|
|
|
logAndOutput(command);
|
|
|
|
}
|
2018-05-29 14:14:31 +00:00
|
|
|
switch (command) {
|
2018-05-25 20:33:22 +00:00
|
|
|
case 'clearall':
|
|
|
|
clearLog(true);
|
|
|
|
break;
|
|
|
|
case 'clear':
|
|
|
|
clearLog();
|
|
|
|
break;
|
|
|
|
case 'fullscreen':
|
|
|
|
toggleFullscreen();
|
|
|
|
break;
|
2018-06-01 22:18:31 +00:00
|
|
|
case 'refresh':
|
|
|
|
refreshFn();
|
|
|
|
break;
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return isUICommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseCommand(command, shouldThrow) {
|
2019-03-01 16:08:30 +00:00
|
|
|
// encode everything but spaces
|
|
|
|
let cmd = encodeURIComponent(command).replace(/%20/g, decodeURIComponent);
|
|
|
|
let args = argTokenizer(cmd);
|
2018-05-25 20:33:22 +00:00
|
|
|
if (args[0] === 'vault') {
|
|
|
|
args.shift();
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:08:30 +00:00
|
|
|
args = args.map(decodeURIComponent);
|
2018-05-25 20:33:22 +00:00
|
|
|
let [method, ...rest] = args;
|
|
|
|
let path;
|
|
|
|
let flags = [];
|
|
|
|
let data = [];
|
|
|
|
|
|
|
|
rest.forEach(arg => {
|
|
|
|
if (arg.startsWith('-')) {
|
|
|
|
flags.push(arg);
|
|
|
|
} else {
|
|
|
|
if (path) {
|
|
|
|
data.push(arg);
|
|
|
|
} 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' };
|
|
|
|
}
|
|
|
|
}
|