From 681f617988950ca9901c90ef00692d0439543697 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Wed, 26 May 2021 12:38:40 +0100 Subject: [PATCH] Add PolicyView page. --- src/PageState.ts | 7 +++ src/allPages.ts | 2 + src/api/sys/getPolicy.ts | 10 ++++ src/elements/CodeBlock.tsx | 22 +++++++++ src/main.tsx | 1 + src/pages/Policies/PoliciesHome.tsx | 9 +++- src/pages/Policies/PolicyView.tsx | 48 +++++++++++++++++++ src/pages/Secrets/KeyValue/KeyValueSecret.tsx | 10 +--- src/translations/en.js | 5 ++ 9 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 src/api/sys/getPolicy.ts create mode 100644 src/elements/CodeBlock.tsx create mode 100644 src/pages/Policies/PolicyView.tsx diff --git a/src/PageState.ts b/src/PageState.ts index 67a8eb6..da0cf24 100644 --- a/src/PageState.ts +++ b/src/PageState.ts @@ -92,6 +92,13 @@ export class PageState { localStorage.setItem("secretMountType", value); } + get policyItem(): string { + return localStorage.getItem("policyItem") || ""; + } + set policyItem(value: string) { + localStorage.setItem("policyItem", value); + } + get authPath(): string { return localStorage.getItem("authPath") || ""; } diff --git a/src/allPages.ts b/src/allPages.ts index 970cdde..75a8326 100644 --- a/src/allPages.ts +++ b/src/allPages.ts @@ -22,6 +22,7 @@ import { NewTransitEnginePage } from "./pages/Secrets/NewEngines/NewTransitEngin import { NewTransitKeyPage } from "./pages/Secrets/Transit/NewTransitKey"; import { Page } from "./types/Page"; import { PoliciesHomePage } from "./pages/Policies/PoliciesHome"; +import { PolicyViewPage } from "./pages/Policies/PolicyView"; import { PwGenPage } from "./pages/PwGen"; import { SecretsHomePage } from "./pages/Secrets/SecretsHome"; import { SetLanguagePage } from "./pages/SetLanguage"; @@ -56,6 +57,7 @@ export const allPages: pagesList = { PW_GEN: new PwGenPage(), POLICIES_HOME: new PoliciesHomePage(), + POLICY_VIEW: new PolicyViewPage(), ACCESS_HOME: new AccessHomePage(), diff --git a/src/api/sys/getPolicy.ts b/src/api/sys/getPolicy.ts new file mode 100644 index 0000000..1d5ab5e --- /dev/null +++ b/src/api/sys/getPolicy.ts @@ -0,0 +1,10 @@ +import { appendAPIURL, getHeaders } from "../apiUtils"; + +export async function getPolicy(name: string): Promise { + const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { + headers: getHeaders(), + }); + const response = await fetch(request); + const data = (await response.json()) as { data: { policy: string } }; + return data.data.policy; +} diff --git a/src/elements/CodeBlock.tsx b/src/elements/CodeBlock.tsx new file mode 100644 index 0000000..a517466 --- /dev/null +++ b/src/elements/CodeBlock.tsx @@ -0,0 +1,22 @@ +import { JSX } from "preact"; +import Prism from "prismjs"; + +export type CodeBlockProps = { + language: string; + code: string; +}; + +export function CodeBlock(props: CodeBlockProps): JSX.Element { + const highlightedCode = Prism.highlight( + props.code, + Prism.languages[props.language], + props.language, + ); + + return ( +
+  );
+}
diff --git a/src/main.tsx b/src/main.tsx
index 4082e0e..d489589 100644
--- a/src/main.tsx
+++ b/src/main.tsx
@@ -13,6 +13,7 @@ UIkit.use(Icons);
 import Prism from "prismjs";
 // Don't Sort These!
 import "prismjs/components/prism-json";
+import "prismjs/components/prism-hcl";
 
 Prism.highlightAll();
 /* eslint-enable */
diff --git a/src/pages/Policies/PoliciesHome.tsx b/src/pages/Policies/PoliciesHome.tsx
index ebf245a..d14b572 100644
--- a/src/pages/Policies/PoliciesHome.tsx
+++ b/src/pages/Policies/PoliciesHome.tsx
@@ -34,7 +34,14 @@ export class PoliciesHomePage extends Page {
           
diff --git a/src/pages/Policies/PolicyView.tsx b/src/pages/Policies/PolicyView.tsx
new file mode 100644
index 0000000..dedbee5
--- /dev/null
+++ b/src/pages/Policies/PolicyView.tsx
@@ -0,0 +1,48 @@
+import { CodeBlock } from "../../elements/CodeBlock";
+import { Margin } from "../../elements/Margin";
+import { Page } from "../../types/Page";
+import { getPolicy } from "../../api/sys/getPolicy";
+import { notImplemented, prePageChecks } from "../../pageUtils";
+import { render } from "preact";
+import i18next from "i18next";
+
+export class PolicyViewPage extends Page {
+  constructor() {
+    super();
+  }
+  async goBack(): Promise {
+    await this.router.changePage("POLICIES_HOME");
+  }
+  async render(): Promise {
+    await this.router.setPageContent("");
+    if (!(await prePageChecks(this.router))) return;
+
+    const policy = await getPolicy(this.state.policyItem);
+
+    render(
+      
+

+ + {this.state.policyItem !== "default" && ( + + )} +

+ + + + +
, + this.router.pageContentElement, + ); + } + + get name(): string { + return i18next.t("policy_view_title", { + policy: this.state.policyItem, + }); + } +} diff --git a/src/pages/Secrets/KeyValue/KeyValueSecret.tsx b/src/pages/Secrets/KeyValue/KeyValueSecret.tsx index a145670..ff6c694 100644 --- a/src/pages/Secrets/KeyValue/KeyValueSecret.tsx +++ b/src/pages/Secrets/KeyValue/KeyValueSecret.tsx @@ -1,3 +1,4 @@ +import { CodeBlock } from "../../../elements/CodeBlock"; import { Component, JSX, render } from "preact"; import { CopyableInputBox } from "../../../elements/CopyableInputBox"; import { Grid, GridSizes } from "../../../elements/Grid"; @@ -7,7 +8,6 @@ import { getCapabilities } from "../../../api/sys/getCapabilities"; import { getSecret } from "../../../api/kv/getSecret"; import { sortedObjectMap } from "../../../utils"; import { undeleteSecret } from "../../../api/kv/undeleteSecret"; -import Prism from "prismjs"; import i18next from "i18next"; export type KVSecretViewProps = { @@ -25,13 +25,7 @@ export class KVSecretVew extends Component { if (isMultiLevelJSON) { const jsonText = JSON.stringify(Object.fromEntries(secretsMap), null, 4); - const highlightedJson = Prism.highlight(jsonText, Prism.languages.json, "json"); - return ( -
-      );
+      return ;
     } else {
       return (
         <>
diff --git a/src/translations/en.js b/src/translations/en.js
index ece6fd8..a824c4d 100644
--- a/src/translations/en.js
+++ b/src/translations/en.js
@@ -292,4 +292,9 @@ module.exports = {
   // Policies Home
   policies_home_title: "Policies",
   policies_home_new_btn: "New",
+
+  // Policy View
+  policy_view_title: "Policy View ({{policy}})",
+  policy_view_edit_btn: "Edit",
+  policy_view_delete_btn: "Delete",
 };