1
0
Fork 0
VaultUI/src/pages/KeyValue/KeyValueSecret.ts

166 lines
5.3 KiB
TypeScript
Raw Normal View History

2021-05-08 00:28:31 +01:00
import { CopyableInputBox } from "../../elements/CopyableInputBox";
2021-05-07 23:33:58 +01:00
import { Page } from "../../types/Page";
2021-05-07 23:21:38 +01:00
import { changePage, setPageContent, setTitleElement } from "../../pageUtils";
2021-05-09 11:18:18 +01:00
import { getCapabilities } from "../../api/sys/getCapabilities";
import { getSecret } from "../../api/kv/getSecret";
import { makeElement } from "../../htmlUtils";
2021-05-08 03:07:57 +01:00
import { pageState } from "../../globalPageState";
import { sortedObjectMap } from "../../utils";
2021-05-09 11:18:18 +01:00
import { undeleteSecret } from "../../api/kv/undeleteSecret";
2021-04-15 13:01:58 +01:00
import Prism from "prismjs";
import i18next from 'i18next';
2021-04-15 13:01:58 +01:00
export class KeyValueSecretPage extends Page {
2021-04-15 13:01:58 +01:00
constructor() {
super();
}
2021-05-08 03:07:57 +01:00
goBack(): void {
if (pageState.currentSecretVersion != null) {
pageState.currentSecretVersion = null;
2021-04-17 11:24:43 +01:00
changePage("KEY_VALUE_VERSIONS");
2021-04-15 13:01:58 +01:00
} else {
pageState.currentSecret = "";
2021-04-17 11:24:43 +01:00
changePage("KEY_VALUE_VIEW");
2021-04-15 13:01:58 +01:00
}
}
2021-05-08 03:07:57 +01:00
async render(): Promise<void> {
2021-04-15 13:01:58 +01:00
setTitleElement(pageState);
setPageContent(makeElement({
tag: "div",
children: [
makeElement({
tag: "p",
id: "buttonsBlock"
}),
makeElement({
tag: "p",
text: i18next.t("kv_secret_loading"),
2021-04-15 13:01:58 +01:00
id: "loadingText"
}),
makeElement({
tag: "div",
id: "kvList"
}),
]
}));
2021-05-08 03:07:57 +01:00
const buttonsBlock = document.querySelector("#buttonsBlock");
const kvList = document.querySelector("#kvList");
2021-04-15 13:01:58 +01:00
let isSecretNestedJson = false;
2021-05-08 03:07:57 +01:00
const caps = await getCapabilities(pageState.currentBaseMount, pageState.currentSecretPath, pageState.currentSecret);
2021-04-15 13:01:58 +01:00
if (caps.includes("delete")) {
let deleteButtonText = i18next.t("kv_secret_delete_btn");
if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion == null) {
deleteButtonText = i18next.t("kv_secret_delete_all_btn");
} else if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion != null) {
deleteButtonText = i18next.t(
"kv_secret_delete_version_btn",
{
"version": pageState.currentSecretVersion
}
);
2021-04-15 13:01:58 +01:00
}
buttonsBlock.appendChild(makeElement({
tag: "button",
id: "deleteButton",
class: ["uk-button", "uk-button-danger"],
2021-05-10 11:35:14 +01:00
onclick: () => { changePage("KEY_VALUE_DELETE"); },
2021-04-15 13:01:58 +01:00
text: deleteButtonText
}));
}
if (caps.includes("update")) {
if (pageState.currentSecretVersion == null) {
2021-04-15 13:01:58 +01:00
buttonsBlock.appendChild(makeElement({
tag: "button",
id: "editButton",
class: ["uk-button", "uk-margin", "uk-button-primary"],
2021-05-10 11:35:14 +01:00
onclick: () => { changePage("KEY_VALUE_SECRET_EDIT"); },
text: i18next.t("kv_secret_edit_btn")
2021-04-15 13:01:58 +01:00
}));
}
}
if (pageState.currentMountType == "kv-v2") {
buttonsBlock.appendChild(makeElement({
tag: "button",
id: "versionsButton",
class: ["uk-button", "uk-button-secondary"],
2021-05-10 11:35:14 +01:00
onclick: () => { changePage("KEY_VALUE_VERSIONS"); },
text: i18next.t("kv_secret_versions_btn")
2021-04-15 13:01:58 +01:00
}));
}
2021-05-10 11:35:14 +01:00
void getSecret(
2021-04-15 13:01:58 +01:00
pageState.currentBaseMount,
pageState.currentMountType,
2021-04-15 13:01:58 +01:00
pageState.currentSecretPath,
pageState.currentSecret,
pageState.currentSecretVersion
).then(secretInfo => {
if (secretInfo == null && pageState.currentMountType == "kv-v2") {
document.querySelector("#buttonsBlock").remove();
document.getElementById("loadingText").remove();
kvList.appendChild(makeElement({
tag: "p",
text: i18next.t("kv_secret_deleted_text")
2021-04-15 13:01:58 +01:00
}));
kvList.appendChild(makeElement({
tag: "button",
text: i18next.t("kv_secret_restore_btn"),
2021-04-15 13:01:58 +01:00
id: "restoreButton",
class: ["uk-button", "uk-button-primary"],
onclick: () => {
2021-05-10 11:35:14 +01:00
void undeleteSecret(
2021-04-15 13:01:58 +01:00
pageState.currentBaseMount,
pageState.currentSecretPath,
pageState.currentSecret,
pageState.currentSecretVersion
2021-04-18 10:42:57 +01:00
).then(_ => {
2021-05-08 03:07:57 +01:00
changePage(pageState.currentPageString);
2021-04-15 13:01:58 +01:00
});
},
}));
return;
}
2021-05-07 14:19:34 +01:00
const secretsMap = sortedObjectMap(secretInfo);
2021-04-15 13:01:58 +01:00
2021-05-08 03:07:57 +01:00
for (const value of secretsMap.values()) {
2021-04-15 13:01:58 +01:00
if (typeof value == 'object') isSecretNestedJson = true;
}
if (isSecretNestedJson) {
2021-05-10 11:35:14 +01:00
const jsonText = JSON.stringify(
sortedObjectMap(secretsMap as unknown as Record<string, unknown>),
null,
4
);
2021-04-15 13:01:58 +01:00
kvList.appendChild(makeElement({
tag: "pre",
class: ["code-block", "language-json", "line-numbers"],
2021-05-10 12:07:19 +01:00
html: Prism.highlight(jsonText, Prism.languages.json, 'json')
2021-04-15 13:01:58 +01:00
}));
} else {
2021-05-10 11:35:14 +01:00
secretsMap.forEach((value: string, key: string) => {
2021-05-08 03:07:57 +01:00
const kvListElement = this.makeKVListElement(key, value);
2021-04-15 13:01:58 +01:00
kvList.appendChild(kvListElement);
}, this);
}
document.getElementById("loadingText").remove();
});
}
2021-05-08 03:07:57 +01:00
makeKVListElement(key: string, value: string): HTMLElement {
2021-04-15 13:01:58 +01:00
return makeElement({
tag: "div",
class: ["uk-grid", "uk-grid-small", "uk-text-left"],
children: [CopyableInputBox(key), CopyableInputBox(value)]
});
}
2021-05-08 03:07:57 +01:00
get name(): string {
return i18next.t("kv_secret_title");
2021-04-15 13:01:58 +01:00
}
}