Procházet zdrojové kódy

Safe undefined resources, new viewer and dev policy and search on multiple resources on isAuthorized function

jnfrati před 4 roky
rodič
revize
41aee61b49
1 změnil soubory, kde provedl 53 přidání a 18 odebrání
  1. 53 18
      dashboard/src/shared/auth/authorization-helpers.ts

+ 53 - 18
dashboard/src/shared/auth/authorization-helpers.ts

@@ -3,19 +3,52 @@ import { HIERARCHY_TREE, PolicyDocType, ScopeType, Verbs } from "./types";
 export const ADMIN_POLICY_MOCK: PolicyDocType = {
 export const ADMIN_POLICY_MOCK: PolicyDocType = {
   scope: "project",
   scope: "project",
   verbs: ["get", "list", "create", "update", "delete"],
   verbs: ["get", "list", "create", "update", "delete"],
+};
+
+export const DEV_POLICY_MOCK: PolicyDocType = {
+  scope: "project",
+  verbs: ["get", "list", "create", "update", "delete"],
+  resources: [],
+  children: {
+    settings: {
+      scope: "settings",
+      verbs: ["get", "list"],
+      resources: [],
+    },
+  },
+};
+
+export const VIEWER_POLICY_MOCK: PolicyDocType = {
+  scope: "project",
+  verbs: ["get", "list"],
   resources: [],
   resources: [],
   children: {
   children: {
     settings: {
     settings: {
       scope: "settings",
       scope: "settings",
       verbs: [],
       verbs: [],
+      resources: [],
+    },
+  },
+};
+
+export const POLICY_HIERARCHY_TREE: HIERARCHY_TREE = {
+  project: {
+    cluster: {
+      namespace: {
+        application: {},
+        job: {},
+        env_group: {},
+      },
     },
     },
-  } as Record<ScopeType, PolicyDocType>,
+    settings: {},
+    integrations: {},
+  },
 };
 };
 
 
 export const isAuthorized = (
 export const isAuthorized = (
   policy: PolicyDocType,
   policy: PolicyDocType,
   scope: string,
   scope: string,
-  resource: string,
+  resource: string | Array<string>,
   verb: Verbs | Array<Verbs>
   verb: Verbs | Array<Verbs>
 ): boolean => {
 ): boolean => {
   if (!policy) {
   if (!policy) {
@@ -23,11 +56,21 @@ export const isAuthorized = (
   }
   }
 
 
   if (policy?.scope === scope) {
   if (policy?.scope === scope) {
-    return (policy.resources.length === 0 ||
-      policy.resources.includes(resource)) &&
-      typeof verb === "string"
-      ? policy.verbs.includes(verb)
-      : (verb as Array<Verbs>).every((v) => policy.verbs.includes(v));
+    let isResourceIncluded = false;
+    if (policy.resources.length === 0) {
+      isResourceIncluded = true;
+    } else if (typeof resource === "string") {
+      isResourceIncluded = policy.resources.includes(resource);
+    } else {
+      isResourceIncluded = resource.every((r) => policy.resources.includes(r));
+    }
+
+    return (
+      isResourceIncluded &&
+      (typeof verb === "string"
+        ? policy.verbs.includes(verb)
+        : verb.every((v) => policy.verbs.includes(v)))
+    );
   } else {
   } else {
     const isValid =
     const isValid =
       policy?.children &&
       policy?.children &&
@@ -43,17 +86,6 @@ export const isAuthorized = (
   }
   }
 };
 };
 
 
-export const POLICY_HIERARCHY_TREE: HIERARCHY_TREE = {
-  project: {
-    cluster: {
-      namespace: {
-        application: {},
-      },
-    },
-    settings: {},
-  },
-};
-
 export const populatePolicy = (
 export const populatePolicy = (
   currPolicy: PolicyDocType,
   currPolicy: PolicyDocType,
   tree: HIERARCHY_TREE,
   tree: HIERARCHY_TREE,
@@ -64,6 +96,9 @@ export const populatePolicy = (
 
 
   const treeKeys = Object.keys(currTree) as Array<ScopeType>;
   const treeKeys = Object.keys(currTree) as Array<ScopeType>;
 
 
+  currPolicy.children = currPolicy?.children || {};
+  currPolicy.resources = currPolicy?.resources || [];
+
   for (const child of treeKeys) {
   for (const child of treeKeys) {
     let childPolicy = currPolicy?.children && currPolicy?.children[child];
     let childPolicy = currPolicy?.children && currPolicy?.children[child];
     if (!childPolicy) {
     if (!childPolicy) {