mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 17:27:40 +00:00
improvement: add conditional display to access tree for dynamic secret metadata
This commit is contained in:
@@ -7,6 +7,7 @@ import React, {
|
|||||||
useMemo,
|
useMemo,
|
||||||
useState
|
useState
|
||||||
} from "react";
|
} from "react";
|
||||||
|
import { FormProvider, useForm } from "react-hook-form";
|
||||||
|
|
||||||
import { ViewMode } from "../types";
|
import { ViewMode } from "../types";
|
||||||
|
|
||||||
@@ -23,8 +24,11 @@ interface AccessTreeProviderProps {
|
|||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type AccessTreeForm = { metadata: { key: string; value: string }[] };
|
||||||
|
|
||||||
export const AccessTreeProvider: React.FC<AccessTreeProviderProps> = ({ children }) => {
|
export const AccessTreeProvider: React.FC<AccessTreeProviderProps> = ({ children }) => {
|
||||||
const [secretName, setSecretName] = useState("");
|
const [secretName, setSecretName] = useState("");
|
||||||
|
const formMethods = useForm<AccessTreeForm>({ defaultValues: { metadata: [] } });
|
||||||
const [viewMode, setViewMode] = useState(ViewMode.Docked);
|
const [viewMode, setViewMode] = useState(ViewMode.Docked);
|
||||||
|
|
||||||
const value = useMemo(
|
const value = useMemo(
|
||||||
@@ -37,7 +41,11 @@ export const AccessTreeProvider: React.FC<AccessTreeProviderProps> = ({ children
|
|||||||
[secretName, setSecretName, viewMode, setViewMode]
|
[secretName, setSecretName, viewMode, setViewMode]
|
||||||
);
|
);
|
||||||
|
|
||||||
return <AccessTreeContext.Provider value={value}>{children}</AccessTreeContext.Provider>;
|
return (
|
||||||
|
<FormProvider {...formMethods}>
|
||||||
|
<AccessTreeContext.Provider value={value}>{children}</AccessTreeContext.Provider>
|
||||||
|
</FormProvider>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useAccessTreeContext = (): AccessTreeContextProps => {
|
export const useAccessTreeContext = (): AccessTreeContextProps => {
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { Dispatch, SetStateAction, useState } from "react";
|
import { Dispatch, SetStateAction, useState } from "react";
|
||||||
|
import { useFormContext } from "react-hook-form";
|
||||||
import { faChevronDown, faChevronUp } from "@fortawesome/free-solid-svg-icons";
|
import { faChevronDown, faChevronUp } from "@fortawesome/free-solid-svg-icons";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { Panel } from "@xyflow/react";
|
import { Panel } from "@xyflow/react";
|
||||||
|
|
||||||
import { Button, FormLabel, IconButton, Input, Select, SelectItem } from "@app/components/v2";
|
import { Button, FormLabel, IconButton, Input, Select, SelectItem } from "@app/components/v2";
|
||||||
import { ProjectPermissionSub } from "@app/context";
|
import { ProjectPermissionSub } from "@app/context";
|
||||||
|
import { MetadataForm } from "@app/pages/secret-manager/SecretDashboardPage/components/DynamicSecretListView/MetadataForm";
|
||||||
|
|
||||||
import { ViewMode } from "../types";
|
import { ViewMode } from "../types";
|
||||||
|
|
||||||
@@ -32,6 +34,7 @@ export const PermissionSimulation = ({
|
|||||||
setSecretName
|
setSecretName
|
||||||
}: TProps) => {
|
}: TProps) => {
|
||||||
const [expand, setExpand] = useState(false);
|
const [expand, setExpand] = useState(false);
|
||||||
|
const { control } = useFormContext();
|
||||||
|
|
||||||
const handlePermissionSimulation = () => {
|
const handlePermissionSimulation = () => {
|
||||||
setExpand(true);
|
setExpand(true);
|
||||||
@@ -139,6 +142,11 @@ export const PermissionSimulation = ({
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{subject === ProjectPermissionSub.DynamicSecrets && (
|
||||||
|
<div>
|
||||||
|
<MetadataForm control={control} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { useFormContext, useWatch } from "react-hook-form";
|
||||||
import { MongoAbility, MongoQuery } from "@casl/ability";
|
import { MongoAbility, MongoQuery } from "@casl/ability";
|
||||||
import { Edge, Node, useEdgesState, useNodesState } from "@xyflow/react";
|
import { Edge, Node, useEdgesState, useNodesState } from "@xyflow/react";
|
||||||
|
|
||||||
@@ -7,7 +8,7 @@ import { ProjectPermissionSet } from "@app/context/ProjectPermissionContext";
|
|||||||
import { useListProjectEnvironmentsFolders } from "@app/hooks/api/secretFolders/queries";
|
import { useListProjectEnvironmentsFolders } from "@app/hooks/api/secretFolders/queries";
|
||||||
import { TSecretFolderWithPath } from "@app/hooks/api/secretFolders/types";
|
import { TSecretFolderWithPath } from "@app/hooks/api/secretFolders/types";
|
||||||
|
|
||||||
import { useAccessTreeContext } from "../components";
|
import { AccessTreeForm, useAccessTreeContext } from "../components";
|
||||||
import { PermissionAccess } from "../types";
|
import { PermissionAccess } from "../types";
|
||||||
import {
|
import {
|
||||||
createBaseEdge,
|
createBaseEdge,
|
||||||
@@ -36,6 +37,8 @@ export const useAccessTree = (
|
|||||||
) => {
|
) => {
|
||||||
const { currentWorkspace } = useWorkspace();
|
const { currentWorkspace } = useWorkspace();
|
||||||
const { secretName, setSecretName, setViewMode, viewMode } = useAccessTreeContext();
|
const { secretName, setSecretName, setViewMode, viewMode } = useAccessTreeContext();
|
||||||
|
const { control } = useFormContext<AccessTreeForm>();
|
||||||
|
const metadata = useWatch({ control, name: "metadata" });
|
||||||
const [nodes, setNodes] = useNodesState<Node>([]);
|
const [nodes, setNodes] = useNodesState<Node>([]);
|
||||||
const [edges, setEdges] = useEdgesState<Edge>([]);
|
const [edges, setEdges] = useEdgesState<Edge>([]);
|
||||||
const [subject, setSubject] = useState(ProjectPermissionSub.Secrets);
|
const [subject, setSubject] = useState(ProjectPermissionSub.Secrets);
|
||||||
@@ -168,7 +171,8 @@ export const useAccessTree = (
|
|||||||
environment,
|
environment,
|
||||||
subject,
|
subject,
|
||||||
secretName,
|
secretName,
|
||||||
actionRuleMap
|
actionRuleMap,
|
||||||
|
metadata
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -266,7 +270,8 @@ export const useAccessTree = (
|
|||||||
subject,
|
subject,
|
||||||
secretName,
|
secretName,
|
||||||
setNodes,
|
setNodes,
|
||||||
setEdges
|
setEdges,
|
||||||
|
metadata
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
+72
-35
@@ -17,6 +17,28 @@ type Props = {
|
|||||||
access: PermissionAccess;
|
access: PermissionAccess;
|
||||||
} & Pick<ReturnType<typeof createFolderNode>["data"], "actionRuleMap" | "subject">;
|
} & Pick<ReturnType<typeof createFolderNode>["data"], "actionRuleMap" | "subject">;
|
||||||
|
|
||||||
|
type ConditionDisplayProps = {
|
||||||
|
_key: string;
|
||||||
|
operator: string;
|
||||||
|
value: string | string[];
|
||||||
|
inverted?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
const Display = ({ _key: key, value, operator, inverted }: ConditionDisplayProps) => {
|
||||||
|
return (
|
||||||
|
<li>
|
||||||
|
<span className="font-medium capitalize text-mineshaft-100">{camelCaseToSpaces(key)}</span>{" "}
|
||||||
|
<span className="text-mineshaft-200">
|
||||||
|
{formatedConditionsOperatorNames[operator as PermissionConditionOperators]}
|
||||||
|
</span>{" "}
|
||||||
|
<span className={inverted ? "text-red" : "text-green"}>
|
||||||
|
{typeof value === "string" ? value : value.join(", ")}
|
||||||
|
</span>
|
||||||
|
.
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const FolderNodeTooltipContent = ({ action, access, actionRuleMap, subject }: Props) => {
|
export const FolderNodeTooltipContent = ({ action, access, actionRuleMap, subject }: Props) => {
|
||||||
let component: ReactElement;
|
let component: ReactElement;
|
||||||
|
|
||||||
@@ -56,43 +78,58 @@ export const FolderNodeTooltipContent = ({ action, access, actionRuleMap, subjec
|
|||||||
{actionRuleMap.map((ruleMap, index) => {
|
{actionRuleMap.map((ruleMap, index) => {
|
||||||
const rule = ruleMap[action];
|
const rule = ruleMap[action];
|
||||||
|
|
||||||
if (
|
if (!rule || !rule.conditions) return null;
|
||||||
!rule ||
|
|
||||||
!rule.conditions ||
|
|
||||||
(!rule.conditions.secretName && !rule.conditions.secretTags)
|
|
||||||
)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return (
|
if (
|
||||||
<li key={`${action}_${index + 1}`}>
|
rule.conditions.secretName ||
|
||||||
<span className={`italic ${rule.inverted ? "text-red" : "text-green"} `}>
|
rule.conditions.secretTags ||
|
||||||
{rule.inverted ? "Forbids" : "Allows"}
|
rule.conditions.metadata
|
||||||
</span>
|
) {
|
||||||
<span> when:</span>
|
return (
|
||||||
{Object.entries(rule.conditions).map(([key, condition]) => (
|
<li key={`${action}_${index + 1}`}>
|
||||||
<ul key={`${action}_${index + 1}_${key}`} className="list-[square] pl-4">
|
<span className={`italic ${rule.inverted ? "text-red" : "text-green"} `}>
|
||||||
{Object.entries(condition as object).map(([operator, value]) => (
|
{rule.inverted ? "Forbids" : "Allows"}
|
||||||
<li key={`${action}_${index + 1}_${key}_${operator}`}>
|
</span>
|
||||||
<span className="font-medium capitalize text-mineshaft-100">
|
<span> when:</span>
|
||||||
{camelCaseToSpaces(key)}
|
{Object.entries(rule.conditions).map(([key, condition]) => {
|
||||||
</span>{" "}
|
return (
|
||||||
<span className="text-mineshaft-200">
|
<ul key={`${action}_${index + 1}_${key}`} className="list-[square] pl-4">
|
||||||
{
|
{Object.entries(condition as object).map(([operator, value]) => {
|
||||||
formatedConditionsOperatorNames[
|
if (operator === "$elemMatch") {
|
||||||
operator as PermissionConditionOperators
|
return Object.entries(value as object).map(
|
||||||
]
|
([nestedKey, nestedCondition]) =>
|
||||||
|
Object.entries(nestedCondition as object).map(
|
||||||
|
([nestedOperator, nestedValue]) => (
|
||||||
|
<Display
|
||||||
|
inverted={rule.inverted}
|
||||||
|
_key={`${key} ${nestedKey}`}
|
||||||
|
operator={nestedOperator}
|
||||||
|
value={nestedValue}
|
||||||
|
key={`${action}_${index + 1}_${key}_${operator}_${nestedKey}_${nestedOperator}`}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
</span>{" "}
|
|
||||||
<span className={rule.inverted ? "text-red" : "text-green"}>
|
return (
|
||||||
{typeof value === "string" ? value : value.join(", ")}
|
<Display
|
||||||
</span>
|
inverted={rule.inverted}
|
||||||
.
|
_key={key}
|
||||||
</li>
|
operator={operator}
|
||||||
))}
|
value={value}
|
||||||
</ul>
|
key={`${action}_${index + 1}_${key}_${operator}`}
|
||||||
))}
|
/>
|
||||||
</li>
|
);
|
||||||
);
|
})}
|
||||||
|
</ul>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
})}
|
})}
|
||||||
</ul>
|
</ul>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Dispatch, SetStateAction } from "react";
|
import { Dispatch, SetStateAction } from "react";
|
||||||
import { faFileImport, faFolder, faKey, faLock } from "@fortawesome/free-solid-svg-icons";
|
import { faFileImport, faFingerprint, faFolder, faKey } from "@fortawesome/free-solid-svg-icons";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { Handle, NodeProps, Position } from "@xyflow/react";
|
import { Handle, NodeProps, Position } from "@xyflow/react";
|
||||||
|
|
||||||
@@ -12,15 +12,15 @@ import { createRoleNode } from "../utils";
|
|||||||
const getSubjectIcon = (subject: ProjectPermissionSub) => {
|
const getSubjectIcon = (subject: ProjectPermissionSub) => {
|
||||||
switch (subject) {
|
switch (subject) {
|
||||||
case ProjectPermissionSub.Secrets:
|
case ProjectPermissionSub.Secrets:
|
||||||
return <FontAwesomeIcon icon={faLock} className="h-4 w-4 text-yellow-700" />;
|
return <FontAwesomeIcon icon={faKey} className="h-4 w-4 text-bunker-300" />;
|
||||||
case ProjectPermissionSub.SecretFolders:
|
case ProjectPermissionSub.SecretFolders:
|
||||||
return <FontAwesomeIcon icon={faFolder} className="h-4 w-4 text-yellow-700" />;
|
return <FontAwesomeIcon icon={faFolder} className="h-4 w-4 text-yellow-700" />;
|
||||||
case ProjectPermissionSub.DynamicSecrets:
|
case ProjectPermissionSub.DynamicSecrets:
|
||||||
return <FontAwesomeIcon icon={faKey} className="h-4 w-4 text-yellow-700" />;
|
return <FontAwesomeIcon icon={faFingerprint} className="h-4 w-4 text-yellow-700" />;
|
||||||
case ProjectPermissionSub.SecretImports:
|
case ProjectPermissionSub.SecretImports:
|
||||||
return <FontAwesomeIcon icon={faFileImport} className="h-4 w-4 text-yellow-700" />;
|
return <FontAwesomeIcon icon={faFileImport} className="h-4 w-4 text-green-700" />;
|
||||||
default:
|
default:
|
||||||
return <FontAwesomeIcon icon={faLock} className="h-4 w-4 text-yellow-700" />;
|
return <FontAwesomeIcon icon={faKey} className="h-4 w-4 text-bunker-300" />;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,8 @@ export const createFolderNode = ({
|
|||||||
environment,
|
environment,
|
||||||
subject,
|
subject,
|
||||||
secretName,
|
secretName,
|
||||||
actionRuleMap
|
actionRuleMap,
|
||||||
|
metadata
|
||||||
}: {
|
}: {
|
||||||
folder: TSecretFolderWithPath;
|
folder: TSecretFolderWithPath;
|
||||||
permissions: MongoAbility<ProjectPermissionSet, MongoQuery>;
|
permissions: MongoAbility<ProjectPermissionSet, MongoQuery>;
|
||||||
@@ -66,6 +67,7 @@ export const createFolderNode = ({
|
|||||||
subject: ProjectPermissionSub;
|
subject: ProjectPermissionSub;
|
||||||
secretName: string;
|
secretName: string;
|
||||||
actionRuleMap: TActionRuleMap;
|
actionRuleMap: TActionRuleMap;
|
||||||
|
metadata: Array<{ key: string; value: string }>;
|
||||||
}) => {
|
}) => {
|
||||||
const actions = Object.fromEntries(
|
const actions = Object.fromEntries(
|
||||||
Object.values(ACTION_MAP[subject] ?? Object.values(ProjectPermissionActions)).map((action) => {
|
Object.values(ACTION_MAP[subject] ?? Object.values(ProjectPermissionActions)).map((action) => {
|
||||||
@@ -79,7 +81,8 @@ export const createFolderNode = ({
|
|||||||
secretPath: folder.path,
|
secretPath: folder.path,
|
||||||
environment,
|
environment,
|
||||||
secretName: secretName || "*",
|
secretName: secretName || "*",
|
||||||
secretTags: ["*"]
|
secretTags: ["*"],
|
||||||
|
metadata: metadata.length ? metadata : ["*"]
|
||||||
};
|
};
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -101,40 +104,98 @@ export const createFolderNode = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (hasPermission) {
|
if (hasPermission) {
|
||||||
// we want to show yellow/conditional access if user hasn't specified secret name to fully resolve access
|
if (subject === ProjectPermissionSub.Secrets) {
|
||||||
if (
|
// we want to show yellow/conditional access if user hasn't specified secret name to fully resolve access
|
||||||
!secretName &&
|
if (
|
||||||
actionRuleMap.some((el) => {
|
!secretName &&
|
||||||
// we only show conditional if secretName/secretTags are present - environment and path can be directly determined
|
actionRuleMap.some((el) => {
|
||||||
if (!el[action]?.conditions?.secretName && !el[action]?.conditions?.secretTags)
|
// we only show conditional if secretName/secretTags are present - environment and path can be directly determined
|
||||||
return false;
|
if (!el[action]?.conditions?.secretName && !el[action]?.conditions?.secretTags)
|
||||||
|
|
||||||
// make sure condition applies to env
|
|
||||||
if (el[action]?.conditions?.environment) {
|
|
||||||
if (
|
|
||||||
!Object.entries(el[action]?.conditions?.environment).every(([operator, value]) =>
|
|
||||||
evaluateCondition(environment, operator as PermissionConditionOperators, value)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// and applies to path
|
// make sure condition applies to env
|
||||||
if (el[action]?.conditions?.secretPath) {
|
if (el[action]?.conditions?.environment) {
|
||||||
if (
|
if (
|
||||||
!Object.entries(el[action]?.conditions?.secretPath).every(([operator, value]) =>
|
!Object.entries(el[action]?.conditions?.environment).every(
|
||||||
evaluateCondition(folder.path, operator as PermissionConditionOperators, value)
|
([operator, value]) =>
|
||||||
)
|
evaluateCondition(
|
||||||
) {
|
environment,
|
||||||
return false;
|
operator as PermissionConditionOperators,
|
||||||
|
value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
// and applies to path
|
||||||
})
|
if (el[action]?.conditions?.secretPath) {
|
||||||
) {
|
if (
|
||||||
access = PermissionAccess.Partial;
|
!Object.entries(el[action]?.conditions?.secretPath).every(([operator, value]) =>
|
||||||
|
evaluateCondition(
|
||||||
|
folder.path,
|
||||||
|
operator as PermissionConditionOperators,
|
||||||
|
value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
access = PermissionAccess.Partial;
|
||||||
|
} else {
|
||||||
|
access = PermissionAccess.Full;
|
||||||
|
}
|
||||||
|
} else if (subject === ProjectPermissionSub.DynamicSecrets) {
|
||||||
|
if (
|
||||||
|
!metadata.length &&
|
||||||
|
actionRuleMap.some((el) => {
|
||||||
|
// we only show conditional if metadata present - environment and path can be directly determined
|
||||||
|
if (!el[action]?.conditions?.metadata) return false;
|
||||||
|
|
||||||
|
// make sure condition applies to env
|
||||||
|
if (el[action]?.conditions?.environment) {
|
||||||
|
if (
|
||||||
|
!Object.entries(el[action]?.conditions?.environment).every(
|
||||||
|
([operator, value]) =>
|
||||||
|
evaluateCondition(
|
||||||
|
environment,
|
||||||
|
operator as PermissionConditionOperators,
|
||||||
|
value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// and applies to path
|
||||||
|
if (el[action]?.conditions?.secretPath) {
|
||||||
|
if (
|
||||||
|
!Object.entries(el[action]?.conditions?.secretPath).every(([operator, value]) =>
|
||||||
|
evaluateCondition(
|
||||||
|
folder.path,
|
||||||
|
operator as PermissionConditionOperators,
|
||||||
|
value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
access = PermissionAccess.Partial;
|
||||||
|
} else {
|
||||||
|
access = PermissionAccess.Full;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
access = PermissionAccess.Full;
|
access = PermissionAccess.Full;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ export const SecretPathInput = ({
|
|||||||
maxHeight: "var(--radix-select-content-available-height)"
|
maxHeight: "var(--radix-select-content-available-height)"
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="max-h-[25vh] w-full flex-col items-center justify-center overflow-y-scroll rounded-md text-white">
|
<div className="thin-scrollbar max-h-[25vh] w-full flex-col items-center justify-center overflow-y-scroll rounded-md text-white">
|
||||||
{suggestions.map((suggestion, i) => (
|
{suggestions.map((suggestion, i) => (
|
||||||
<div
|
<div
|
||||||
tabIndex={0}
|
tabIndex={0}
|
||||||
|
|||||||
Reference in New Issue
Block a user