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-07 11:53:26 +01:00
|
|
|
import { getCapabilities } from "../../api/getCapabilities";
|
2021-05-07 11:54:44 +01:00
|
|
|
import { getSecret } from "../../api/getSecret";
|
2021-05-07 22:23:52 +01:00
|
|
|
import { makeElement } from "../../htmlUtils";
|
2021-05-08 03:07:57 +01:00
|
|
|
import { pageState } from "../../globalPageState";
|
2021-05-07 22:23:52 +01:00
|
|
|
import { sortedObjectMap } from "../../utils";
|
2021-05-07 11:54:44 +01:00
|
|
|
import { undeleteSecret } from "../../api/undeleteSecret";
|
2021-04-15 13:01:58 +01:00
|
|
|
import Prism from "prismjs";
|
2021-04-20 22:49:33 +01:00
|
|
|
import i18next from 'i18next';
|
2021-04-15 13:01:58 +01:00
|
|
|
|
|
|
|
|
2021-04-20 22:49:33 +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 {
|
2021-05-05 17:49:08 +01:00
|
|
|
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",
|
2021-04-20 22:49:33 +01:00
|
|
|
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")) {
|
2021-04-20 22:55:07 +01:00
|
|
|
let deleteButtonText = i18next.t("kv_secret_delete_btn");
|
2021-05-05 17:49:08 +01:00
|
|
|
if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion == null) {
|
2021-04-20 22:55:07 +01:00
|
|
|
deleteButtonText = i18next.t("kv_secret_delete_all_btn");
|
2021-05-05 17:49:08 +01:00
|
|
|
} else if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion != null) {
|
2021-04-20 22:55:07 +01:00
|
|
|
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-04-17 11:24:43 +01:00
|
|
|
onclick: _ => { changePage("KEY_VALUE_DELETE"); },
|
2021-04-15 13:01:58 +01:00
|
|
|
text: deleteButtonText
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (caps.includes("update")) {
|
2021-05-05 17:49:08 +01:00
|
|
|
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-04-20 22:49:33 +01:00
|
|
|
onclick: _ => { changePage("KEY_VALUE_SECRET_EDIT"); },
|
2021-04-20 22:55:07 +01:00
|
|
|
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-04-17 11:24:43 +01:00
|
|
|
onclick: _ => { changePage("KEY_VALUE_VERSIONS"); },
|
2021-04-20 22:55:07 +01:00
|
|
|
text: i18next.t("kv_secret_versions_btn")
|
2021-04-15 13:01:58 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
getSecret(
|
|
|
|
pageState.currentBaseMount,
|
2021-05-05 21:56:25 +01:00
|
|
|
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",
|
2021-04-20 22:49:33 +01:00
|
|
|
text: i18next.t("kv_secret_deleted_text")
|
2021-04-15 13:01:58 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
kvList.appendChild(makeElement({
|
|
|
|
tag: "button",
|
2021-04-20 22:49:33 +01:00
|
|
|
text: i18next.t("kv_secret_restore_btn"),
|
2021-04-15 13:01:58 +01:00
|
|
|
id: "restoreButton",
|
|
|
|
class: ["uk-button", "uk-button-primary"],
|
|
|
|
onclick: () => {
|
|
|
|
undeleteSecret(
|
|
|
|
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-08 03:07:57 +01:00
|
|
|
const jsonText = JSON.stringify(sortedObjectMap(secretsMap as Record<any, any>), null, 4);
|
2021-04-15 13:01:58 +01:00
|
|
|
kvList.appendChild(makeElement({
|
|
|
|
tag: "pre",
|
|
|
|
class: ["code-block", "language-json", "line-numbers"],
|
|
|
|
html: Prism.highlight(jsonText, Prism.languages.json, 'json')
|
|
|
|
}));
|
|
|
|
} else {
|
2021-04-18 10:42:57 +01:00
|
|
|
secretsMap.forEach((value, key) => {
|
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 {
|
2021-04-20 22:49:33 +01:00
|
|
|
return i18next.t("kv_secret_title");
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
}
|