2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2018-05-25 20:33:22 +00:00
|
|
|
import keys from 'vault/lib/keycodes';
|
2023-05-17 16:41:02 +00:00
|
|
|
import AdapterError from '@ember-data/adapter/error';
|
2019-08-01 22:12:04 +00:00
|
|
|
import { parse } from 'shell-quote';
|
2018-05-25 20:33:22 +00:00
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
import argTokenizer from './arg-tokenizer';
|
|
|
|
import { StringMap } from 'vault/vault/app-types';
|
|
|
|
|
|
|
|
// Add new commands to `log-help` component for visibility
|
|
|
|
const supportedCommands = ['read', 'write', 'list', 'delete', 'kv-get'];
|
2019-07-02 22:41:23 +00:00
|
|
|
const uiCommands = ['api', 'clearall', 'clear', 'fullscreen', 'refresh'];
|
2018-05-25 20:33:22 +00:00
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
interface DataObj {
|
|
|
|
[key: string]: string | string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function extractDataFromStrings(dataArray: string[]): DataObj {
|
|
|
|
if (!dataArray) return {};
|
|
|
|
return dataArray.reduce((accumulator: DataObj, val: string) => {
|
|
|
|
// will be "key=value" or "foo=bar=baz"
|
|
|
|
// split on the first =
|
|
|
|
// default to value of empty string
|
|
|
|
const [item = '', value = ''] = val.split(/=(.+)?/);
|
|
|
|
if (!item) return accumulator;
|
|
|
|
|
|
|
|
// if it exists in data already, then we have multiple
|
|
|
|
// foo=bar in the list and need to make it an array
|
|
|
|
const existingValue = accumulator[item];
|
|
|
|
if (existingValue) {
|
|
|
|
accumulator[item] = Array.isArray(existingValue) ? [...existingValue, value] : [existingValue, value];
|
2018-11-05 16:56:59 +00:00
|
|
|
return accumulator;
|
2023-05-17 16:41:02 +00:00
|
|
|
}
|
|
|
|
accumulator[item] = value;
|
|
|
|
return accumulator;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Flags {
|
|
|
|
field?: string;
|
|
|
|
format?: string;
|
|
|
|
force?: boolean;
|
|
|
|
wrapTTL?: boolean;
|
|
|
|
[key: string]: string | boolean | undefined;
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
2023-05-17 16:41:02 +00:00
|
|
|
export function extractFlagsFromStrings(flagArray: string[], method: string): Flags {
|
|
|
|
if (!flagArray) return {};
|
|
|
|
return flagArray.reduce((accumulator: Flags, val: string) => {
|
|
|
|
// val will be "-flag=value" or "--force"
|
|
|
|
// split on the first =
|
|
|
|
// default to value or true
|
|
|
|
const [item, value] = val.split(/=(.+)?/);
|
|
|
|
if (!item) return accumulator;
|
2018-05-25 20:33:22 +00:00
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
let flagName = item.replace(/^-/, '');
|
|
|
|
if (flagName === 'wrap-ttl') {
|
|
|
|
flagName = 'wrapTTL';
|
|
|
|
} else if (method === 'write') {
|
|
|
|
if (flagName === 'f' || flagName === '-force') {
|
|
|
|
flagName = 'force';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accumulator[flagName] = value || true;
|
|
|
|
return accumulator;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommandFns {
|
|
|
|
[key: string]: CallableFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function executeUICommand(
|
|
|
|
command: string,
|
|
|
|
logAndOutput: CallableFunction,
|
|
|
|
commandFns: CommandFns
|
|
|
|
): boolean {
|
2022-11-09 23:15:31 +00:00
|
|
|
const cmd = command.startsWith('api') ? 'api' : command;
|
|
|
|
const isUICommand = uiCommands.includes(cmd);
|
2018-05-29 14:14:31 +00:00
|
|
|
if (isUICommand) {
|
2018-05-25 20:33:22 +00:00
|
|
|
logAndOutput(command);
|
|
|
|
}
|
2023-05-17 16:41:02 +00:00
|
|
|
const execCommand = commandFns[cmd];
|
|
|
|
if (execCommand && typeof execCommand === 'function') {
|
|
|
|
execCommand();
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
return isUICommand;
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
interface ParsedCommand {
|
|
|
|
method: string;
|
|
|
|
path: string;
|
|
|
|
flagArray: string[];
|
|
|
|
dataArray: string[];
|
|
|
|
}
|
|
|
|
export function parseCommand(command: string): ParsedCommand {
|
|
|
|
const args: string[] = argTokenizer(parse(command));
|
2018-05-25 20:33:22 +00:00
|
|
|
if (args[0] === 'vault') {
|
|
|
|
args.shift();
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
const [method = '', ...rest] = args;
|
|
|
|
let path = '';
|
|
|
|
const flags: string[] = [];
|
|
|
|
const data: string[] = [];
|
2018-05-25 20:33:22 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
rest.forEach((arg) => {
|
2018-05-25 20:33:22 +00:00
|
|
|
if (arg.startsWith('-')) {
|
|
|
|
flags.push(arg);
|
|
|
|
} else {
|
|
|
|
if (path) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const strippedArg = arg
|
2019-05-22 21:07:42 +00:00
|
|
|
// 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
|
2021-12-17 03:44:29 +00:00
|
|
|
.filter((str) => str !== '')
|
2019-05-22 21:07:42 +00:00
|
|
|
// glue the data back together
|
|
|
|
.join('=');
|
|
|
|
data.push(strippedArg);
|
2018-05-25 20:33:22 +00:00
|
|
|
} else {
|
|
|
|
path = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!supportedCommands.includes(method)) {
|
2023-05-17 16:41:02 +00:00
|
|
|
throw new Error('invalid command');
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
2023-05-17 16:41:02 +00:00
|
|
|
return { method, flagArray: flags, path, dataArray: data };
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
interface LogResponse {
|
|
|
|
auth?: StringMap;
|
|
|
|
data?: StringMap;
|
|
|
|
wrap_info?: StringMap;
|
|
|
|
[key: string]: unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logFromResponse(response: LogResponse, path: string, method: string, flags: Flags) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const { format, field } = flags;
|
2023-05-17 16:41:02 +00:00
|
|
|
const respData: StringMap | undefined = response && (response.auth || response.data || response.wrap_info);
|
|
|
|
const secret: StringMap | LogResponse = respData || response;
|
|
|
|
|
|
|
|
if (!respData) {
|
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}` };
|
|
|
|
}
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (field) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const fieldValue = secret[field];
|
2018-05-25 20:33:22 +00:00
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
interface CustomError extends AdapterError {
|
|
|
|
httpStatus: number;
|
|
|
|
path: string;
|
|
|
|
errors: string[];
|
|
|
|
}
|
|
|
|
export function logFromError(error: CustomError, vaultPath: string, method: string) {
|
2018-05-25 20:33:22 +00:00
|
|
|
let content;
|
2022-11-09 23:15:31 +00:00
|
|
|
const { httpStatus, path } = error;
|
|
|
|
const verbClause = {
|
2018-05-25 20:33:22 +00:00
|
|
|
read: 'reading from',
|
2023-05-17 16:41:02 +00:00
|
|
|
'kv-get': 'reading secret',
|
2018-05-25 20:33:22 +00:00
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
interface CommandLog {
|
|
|
|
type: string;
|
|
|
|
content?: string;
|
|
|
|
}
|
|
|
|
export function shiftCommandIndex(keyCode: number, history: CommandLog[], index: number) {
|
2018-05-25 20:33:22 +00:00
|
|
|
let newInputValue;
|
2022-11-09 23:15:31 +00:00
|
|
|
const commandHistoryLength = history.length;
|
2018-05-25 20:33:22 +00:00
|
|
|
|
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 !== '') {
|
2023-05-17 16:41:02 +00:00
|
|
|
newInputValue = history.objectAt(index)?.content;
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [index, newInputValue];
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:41:02 +00:00
|
|
|
export function formattedErrorFromInput(path: string, method: string, flags: Flags, dataArray: string[]) {
|
2018-05-25 20:33:22 +00:00
|
|
|
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' };
|
|
|
|
}
|
2023-05-17 16:41:02 +00:00
|
|
|
return;
|
2018-05-25 20:33:22 +00:00
|
|
|
}
|