1
0
Fork 0
VaultUI/src/pages/Secrets/Transit/TransitRewrap.ts

116 lines
3.7 KiB
TypeScript
Raw Normal View History

import { CopyableModal } from "../../../elements/CopyableModal";
import { Margin } from "../../../elements/Margin";
import { Option } from "../../../elements/Option";
import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../../../elements/SecretTitleElement";
import { getTransitKey } from "../../../api/transit/getTransitKey";
import { makeElement } from "z-makeelement";
import { objectToMap } from "../../../utils";
import { setErrorText } from "../../../pageUtils";
import { transitRewrap } from "../../../api/transit/transitRewrap";
2021-05-10 11:35:14 +01:00
import i18next from "i18next";
2021-05-09 16:02:04 +01:00
type versionOption = { version: string; label: string };
2021-05-09 16:02:04 +01:00
export class TransitRewrapPage extends Page {
constructor() {
super();
}
2021-05-12 17:37:09 +01:00
async goBack(): Promise<void> {
await this.router.changePage("TRANSIT_VIEW_SECRET");
2021-05-09 16:02:04 +01:00
}
transitRewrapForm: HTMLFormElement;
async render(): Promise<void> {
const transitKey = await getTransitKey(this.state.currentBaseMount, this.state.currentSecret);
2021-05-09 16:02:04 +01:00
const stringVersions = Array.from(
objectToMap(transitKey.keys).keys(),
).reverse() as unknown as string[];
2021-05-10 11:35:14 +01:00
const versions = stringVersions.map((val): number => parseInt(val, 10));
2021-05-09 16:02:04 +01:00
// get the selectable version options in the same
// format the official UI uses.
2021-05-09 16:02:04 +01:00
// e.g: ["2 (latest)", "1"]
2021-05-10 11:35:14 +01:00
const options: versionOption[] = versions.map((val): versionOption => {
const i18nkey =
val == Math.max(...versions)
? "transit_rewrap_latest_version_option_text"
: "transit_rewrap_version_option_text";
2021-05-09 16:02:04 +01:00
return {
version: String(val),
label: i18next.t(i18nkey, { version_num: String(val) }),
};
});
2021-05-09 16:02:04 +01:00
await this.router.setPageContent("");
2021-05-09 16:02:04 +01:00
this.transitRewrapForm = makeElement({
tag: "form",
children: [
makeElement({
tag: "select",
name: "version",
class: ["uk-select", "uk-width-1-2"],
children: options.map((option): HTMLElement => Option(option.label, option.version)),
2021-05-09 16:02:04 +01:00
}),
Margin(
makeElement({
tag: "textarea",
class: ["uk-textarea", "uk-width-1-2"],
attributes: {
placeholder: i18next.t("transit_rewrap_input_placeholder"),
name: "ciphertext",
},
}),
),
2021-05-09 16:02:04 +01:00
makeElement({
tag: "p",
id: "errorText",
class: "uk-text-danger",
2021-05-09 16:02:04 +01:00
}),
makeElement({
tag: "button",
class: ["uk-button", "uk-button-primary"],
text: i18next.t("transit_rewrap_rewrap_btn"),
attributes: {
type: "submit",
},
}),
],
2021-05-09 16:02:04 +01:00
}) as HTMLFormElement;
await this.router.setPageContent(this.transitRewrapForm);
2021-05-12 14:54:25 +01:00
2021-05-12 18:26:32 +01:00
this.transitRewrapForm.addEventListener("submit", async (e: Event) => {
2021-05-09 16:02:04 +01:00
e.preventDefault();
2021-05-12 18:26:32 +01:00
await this.transitRewrapFormHandler();
2021-05-12 14:54:25 +01:00
});
2021-05-09 16:02:04 +01:00
}
async transitRewrapFormHandler(): Promise<void> {
const formData = new FormData(this.transitRewrapForm);
try {
const res = await transitRewrap(this.state.currentBaseMount, this.state.currentSecret, {
ciphertext: formData.get("ciphertext") as string,
key_version: parseInt(formData.get("version") as string, 10),
});
2021-05-09 16:02:04 +01:00
const modal = CopyableModal(i18next.t("transit_rewrap_result_modal_title"), res.ciphertext);
2021-05-16 13:40:39 +01:00
this.router.pageContentElement.appendChild(modal);
2021-05-10 11:35:14 +01:00
modal.show();
} catch (e: unknown) {
const error = e as Error;
setErrorText(`API Error: ${error.message}`);
2021-05-09 16:02:04 +01:00
}
}
async getPageTitle(): Promise<Element | string> {
return await SecretTitleElement(this.router, i18next.t("transit_rewrap_suffix"));
2021-05-09 16:02:04 +01:00
}
get name(): string {
return i18next.t("transit_rewrap_title");
}
}