From dfa448e3754cf06cd79d18ff49f70704f1a89f3e Mon Sep 17 00:00:00 2001 From: Kitteh Date: Wed, 5 May 2021 17:49:08 +0100 Subject: [PATCH] Rework versions and get deletion to work properly. --- src/PageState.js | 6 ++++-- src/api.js | 25 ++++++++++++++++++------- src/pageUtils.js | 6 +++--- src/pages/Home.js | 2 +- src/pages/KeyValue/KeyValueDelete.js | 4 ++-- src/pages/KeyValue/KeyValueNew.js | 1 - src/pages/KeyValue/KeyValueSecret.js | 10 +++++----- src/pages/KeyValue/KeyValueVersions.js | 4 ++-- 8 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/PageState.js b/src/PageState.js index e539310..dbf1dc3 100644 --- a/src/PageState.js +++ b/src/PageState.js @@ -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() { diff --git a/src/api.js b/src/api.js index 135d8a6..4eee1f7 100644 --- a/src/api.js +++ b/src/api.js @@ -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") { diff --git a/src/pageUtils.js b/src/pageUtils.js index b2e996d..07fd315 100644 --- a/src/pageUtils.js +++ b/src/pageUtils.js @@ -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"); diff --git a/src/pages/Home.js b/src/pages/Home.js index e59ecc9..81cea77 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -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); diff --git a/src/pages/KeyValue/KeyValueDelete.js b/src/pages/KeyValue/KeyValueDelete.js index c31ba74..c496fba 100644 --- a/src/pages/KeyValue/KeyValueDelete.js +++ b/src/pages/KeyValue/KeyValueDelete.js @@ -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 = ""; diff --git a/src/pages/KeyValue/KeyValueNew.js b/src/pages/KeyValue/KeyValueNew.js index 02a33bf..f7c6478 100644 --- a/src/pages/KeyValue/KeyValueNew.js +++ b/src/pages/KeyValue/KeyValueNew.js @@ -64,7 +64,6 @@ export class KeyValueNewPage extends Page { keyData = { "key": "value" }; } - console.log(splitPath) createOrUpdateSecret( pageState.currentBaseMount, pageState.currentSecretPath, diff --git a/src/pages/KeyValue/KeyValueSecret.js b/src/pages/KeyValue/KeyValueSecret.js index 871c1bb..6cf56d0 100644 --- a/src/pages/KeyValue/KeyValueSecret.js +++ b/src/pages/KeyValue/KeyValueSecret.js @@ -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", diff --git a/src/pages/KeyValue/KeyValueVersions.js b/src/pages/KeyValue/KeyValueVersions.js index 510e881..5745b51 100644 --- a/src/pages/KeyValue/KeyValueVersions.js +++ b/src/pages/KeyValue/KeyValueVersions.js @@ -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"); }