vault/ui/app/utils/kv-path.ts
claire bontempo fe18e6ca87
UI: add subkey request to kv v2 adapter (#27804)
* add subkey request to ui

* WIP kv subkey display

* revert subkey changes to see view in ui

* finish subkey component

* remove reamining user facing changes

* update jsdoc

* add subtext depending on toggle

* finish tests

* organize adapter tests into modules

* add adapter tests

* woops, make beforeEach

* encode paths and add wrap secret test

* reword subkey component

* extract subkey path logic into util

* extract subkey path logic into util

* rename yielded subtext block
2024-07-29 13:12:40 -07:00

50 lines
1.9 KiB
TypeScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
/**
* This set of utils is for calculating the full path for a given KV V2 secret, which doubles as its ID.
* Additional methods for building URLs for other KV-V2 actions
*/
import { sanitizeStart } from 'core/utils/sanitize-path';
import { encodePath } from 'vault/utils/path-encoding-helpers';
// only exported for testing
export function buildKvPath(backend: string, path: string, type: string, version?: number | string) {
const sanitizedPath = sanitizeStart(path); // removing leading slashes
const url = `${encodePath(backend)}/${type}/${encodePath(sanitizedPath)}`;
return version ? `${url}?version=${version}` : url;
}
export function kvDataPath(backend: string, path: string, version?: number | string) {
return buildKvPath(backend, path, 'data', version);
}
export function kvDeletePath(backend: string, path: string, version?: number | string) {
return buildKvPath(backend, path, 'delete', version);
}
export function kvMetadataPath(backend: string, path: string) {
return buildKvPath(backend, path, 'metadata');
}
export function kvDestroyPath(backend: string, path: string) {
return buildKvPath(backend, path, 'destroy');
}
export function kvUndeletePath(backend: string, path: string) {
return buildKvPath(backend, path, 'undelete');
}
// TODO use query-param-string util when https://github.com/hashicorp/vault/pull/27455 is merged
export function kvSubkeysPath(
backend: string,
path: string,
depth?: number | string,
version?: number | string
) {
const apiPath = buildKvPath(backend, path, 'subkeys');
// if no version, defaults to latest
const versionParam = version ? `&version=${version}` : '';
// depth specifies the deepest nesting level the API should return
// depth=0 returns all subkeys (no limit), depth=1 returns only top-level keys
const queryParams = `?depth=${depth || '0'}${versionParam}`;
return `${apiPath}${queryParams}`;
}