1
0
Fork 0

Rework versions and get deletion to work properly.

This commit is contained in:
Kitteh 2021-05-05 17:49:08 +01:00
parent c13b7d04e8
commit dfa448e375
8 changed files with 35 additions and 23 deletions

View file

@ -68,10 +68,12 @@ export class PageState extends Page {
}
get currentSecretVersion() {
return localStorage.getItem('currentSecretVersion') || "0";
let result = localStorage.getItem('currentSecretVersion')
return result != "null" ? result || null : null;
}
set currentSecretVersion(value) {
localStorage.setItem('currentSecretVersion', value);
localStorage.setItem('currentSecretVersion', String(value));
}
get currentSecret() {

View file

@ -172,9 +172,20 @@ export async function getSecretMetadata(baseMount, secretPath, name) {
});
}
export async function undeleteSecret(baseMount, secretPath, name, version) {
let secretURL = `/v1/${baseMount}/undelete/${secretPath.join("")}/${name}`;
export async function undeleteSecret(baseMount, secretPath, name, version = null) {
let secretURL = `/v1/${baseMount}/undelete/${secretPath.join("/")}/${name}`;
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
if (version == null) {
let meta = await getSecretMetadata(
pageState.currentBaseMount,
pageState.currentSecretPath,
pageState.currentSecret
);
console.log(meta.versions);
let versions = Array.from(new Map(Object.entries(meta.versions)).keys())
version = String(versions[versions.length-1])
}
let request = new Request(getAPIURL() + secretURL, {
method: "POST",
headers: {
@ -191,11 +202,11 @@ export async function undeleteSecret(baseMount, secretPath, name, version) {
}
export async function getSecret(baseMount, secretPath, name, version = "0") {
export async function getSecret(baseMount, secretPath, name, version = null) {
let secretURL = "";
if (pageState.currentMountType == "kv-v2") {
secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`;
if (version != 0) secretURL += `?version=${version}`;
if (version != null) secretURL += `?version=${version}`;
} else {
secretURL = `/v1/${baseMount}/${secretPath.join("")}/${name}`;
}
@ -212,12 +223,12 @@ export async function getSecret(baseMount, secretPath, name, version = "0") {
});
}
export async function deleteSecret(baseMount, secretPath, name, version) {
export async function deleteSecret(baseMount, secretPath, name, version = null) {
let secretURL = "";
let request;
if (pageState.currentMountType == "kv-v2" && version != "0") {
if (pageState.currentMountType == "kv-v2" && version != null) {
secretURL = `/v1/${baseMount}/delete/${secretPath.join("")}/${name}`;
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
request = new Request(getAPIURL() + secretURL, {
@ -226,7 +237,7 @@ export async function deleteSecret(baseMount, secretPath, name, version) {
'X-Vault-Token': getToken(),
'Content-Type': 'application/json',
},
body: JSON.stringify({ "versions": [version] })
body: version != null ? JSON.stringify({ "versions": [version] }) : "{}"
});
} else {
if (pageState.currentMountType == "kv-v2") {

View file

@ -106,7 +106,7 @@ function currentTitleSecretText() {
let currentSecretText = pageState.currentSecret;
currentSecretText += pageState.currentPage.titleSuffix;
if (pageState.currentSecretVersion != "0") currentSecretText += ` (v${pageState.currentSecretVersion})`;
if (pageState.currentSecretVersion !== null) currentSecretText += ` (v${pageState.currentSecretVersion})`;
return currentSecretText;
}
@ -120,7 +120,7 @@ export function setTitleElement(pageState) {
onclick: _ => {
pageState.currentSecretPath = [];
pageState.currentSecret = "";
pageState.currentSecretVersion = "0";
pageState.currentSecretVersion = null;
if (pageState.currentMountType.startsWith("kv") || pageState.currentMountType == "cubbyhole") {
changePage("KEY_VALUE_VIEW");
@ -136,7 +136,7 @@ export function setTitleElement(pageState) {
tag: "a",
text: secretPath + " ",
onclick: _ => {
pageState.currentSecretVersion = "0";
pageState.currentSecretVersion = null;
if (pageState.currentMountType.startsWith("kv")) {
pageState.currentSecretPath = secretPaths.slice(0, index + 1);
changePage("KEY_VALUE_VIEW");

View file

@ -55,7 +55,7 @@ export class HomePage extends Page {
pageState.currentBaseMount = "";
pageState.currentSecretPath = [];
pageState.currentSecret = "";
pageState.currentSecretVersion = "0";
pageState.currentSecretVersion = null;
const navList = makeElement({ tag: "ul", class: ["uk-nav", "uk-nav-default", "uk-margin-top"] });
pageContent.appendChild(navList);

View file

@ -9,8 +9,8 @@ export class KeyValueDeletePage extends Page {
super();
}
goBack() {
if (pageState.currentSecretVersion != "0") {
pageState.currentSecretVersion = "0";
if (pageState.currentSecretVersion != null) {
pageState.currentSecretVersion = null;
changePage("KEY_VALUE_SECRET");
} else {
pageState.currentSecret = "";

View file

@ -64,7 +64,6 @@ export class KeyValueNewPage extends Page {
keyData = { "key": "value" };
}
console.log(splitPath)
createOrUpdateSecret(
pageState.currentBaseMount,
pageState.currentSecretPath,

View file

@ -12,8 +12,8 @@ export class KeyValueSecretPage extends Page {
super();
}
goBack() {
if (pageState.currentSecretVersion != "0") {
pageState.currentSecretVersion = "0";
if (pageState.currentSecretVersion != null) {
pageState.currentSecretVersion = null;
changePage("KEY_VALUE_VERSIONS");
} else {
pageState.currentSecret = "";
@ -48,9 +48,9 @@ export class KeyValueSecretPage extends Page {
let caps = await getCapabilities(pageState.currentBaseMount, pageState.currentSecretPath, pageState.currentSecret);
if (caps.includes("delete")) {
let deleteButtonText = i18next.t("kv_secret_delete_btn");
if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion == "0") {
if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion == null) {
deleteButtonText = i18next.t("kv_secret_delete_all_btn");
} else if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion != "0") {
} else if (pageState.currentMountType == "kv-v2" && pageState.currentSecretVersion != null) {
deleteButtonText = i18next.t(
"kv_secret_delete_version_btn",
{
@ -67,7 +67,7 @@ export class KeyValueSecretPage extends Page {
}));
}
if (caps.includes("update")) {
if (pageState.currentSecretVersion == "0") {
if (pageState.currentSecretVersion == null) {
buttonsBlock.appendChild(makeElement({
tag: "button",
id: "editButton",

View file

@ -10,8 +10,8 @@ export class KeyValueVersionsPage extends Page {
super();
}
goBack() {
if (pageState.currentSecretVersion != "0") {
pageState.currentSecretVersion = "0";
if (pageState.currentSecretVersion != null) {
pageState.currentSecretVersion = null;
}
changePage("KEY_VALUE_SECRET");
}