Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

26.5 KB
Raw Blame
import { useEffect, useMemo } from "react";
import { Controller, useFieldArray, useFormContext, useWatch } from "react-hook-form";
import { useIsFetching, useQueryClient } from "@tanstack/react-query";
import { AlertTriangleIcon, InfoIcon, PlusIcon, RefreshCwIcon, TrashIcon } from "lucide-react";
import { twMerge } from "tailwind-merge";

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
  Badge,
  Button,
  FieldError,
  IconButton,
  Input,
  Popover,
  PopoverContent,
  PopoverTrigger,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import { useProject } from "@app/context";
import {
  ConditionalProjectPermissionSubject,
  PermissionConditionOperators
} from "@app/context/ProjectPermissionContext/types";
import { projectKeys } from "@app/hooks/api";
import { useGetWsTags } from "@app/hooks/api/tags";

import {
  getConditionOperatorHelperInfo,
  renderOperatorSelectItems
} from "./PermissionConditionHelpers";
import { TFormSchema } from "./ProjectRoleModifySection.utils";

type ActionConditionsMap = Partial<Record<string, string[]>>;
type ActionLabelsMap = Record<string, string>;

type ConditionSelectOption = {
  value: string;
  label: string;
  description?: string;
};

type ConditionsFieldsProps = {
  isDisabled: boolean | undefined;
  subject: ConditionalProjectPermissionSubject;
  position: number;
  selectOptions: [ConditionSelectOption, ...ConditionSelectOption[]];
  selectedActions?: string[];
  actionConditionsMap?: ActionConditionsMap;
  actionLabelsMap?: ActionLabelsMap;
};

const computeAllowedConditions = (
  selectedActions: string[] | undefined,
  actionConditionsMap: ActionConditionsMap | undefined,
  allConditions: string[]
): string[] => {
  if (!selectedActions || selectedActions.length === 0 || !actionConditionsMap) {
    return allConditions;
  }

  const actionsWithRestrictions = selectedActions.filter(
    (action) => actionConditionsMap[action] !== undefined
  );

  if (actionsWithRestrictions.length === 0) {
    return allConditions;
  }

  // Return intersection of all allowed conditions for actions with restrictions
  // Using null as sentinel to distinguish "not yet initialized" from "empty intersection"
  const result = actionsWithRestrictions.reduce<string[] | null>((acc, action) => {
    const allowed = actionConditionsMap[action];
    if (!allowed) return acc;
    if (acc === null) return allowed;
    return acc.filter((cond) => allowed.includes(cond));
  }, null);

  return result ?? allConditions;
};

const getDisallowingActions = (
  conditionValue: string,
  selectedActions: string[] | undefined,
  actionConditionsMap: ActionConditionsMap | undefined
): string[] => {
  if (!selectedActions || !actionConditionsMap) return [];

  return selectedActions.filter((action) => {
    const allowed = actionConditionsMap[action];
    return allowed !== undefined && !allowed.includes(conditionValue);
  });
};

export const ConditionsFields = ({
  isDisabled,
  subject,
  position,
  selectOptions,
  selectedActions,
  actionConditionsMap,
  actionLabelsMap
}: ConditionsFieldsProps) => {
  const { currentProject, projectId } = useProject();
  const queryClient = useQueryClient();
  const isProjectRefreshing =
    useIsFetching({ queryKey: projectKeys.getProjectById(projectId) }) > 0;
  const { control, setValue, clearErrors, setError } = useFormContext<TFormSchema>();
  const items = useFieldArray({
    control,
    name: `permissions.${subject}.${position}.conditions` as const
  });

  const allConditionValues = useMemo(() => selectOptions.map((opt) => opt.value), [selectOptions]);

  const allowedConditions = useMemo(
    () => computeAllowedConditions(selectedActions, actionConditionsMap, allConditionValues),
    [selectedActions, actionConditionsMap, allConditionValues]
  );

  const watchedConditions = useWatch({
    control,
    name: `permissions.${subject}.${position}.conditions` as const
  });
  const conditionValues = watchedConditions as
    | Array<{ lhs: string; rhs: string; operator: string }>
    | undefined;
  const hasReferenceConditions = conditionValues?.some(
    ({ lhs }) => lhs === "environment" || lhs === "secretTags"
  );
  const hasTagCondition = conditionValues?.some(({ lhs }) => lhs === "secretTags");
  const {
    data: projectTags,
    isPending: isTagsPending,
    isError: isTagsError,
    isFetching: isTagsFetching,
    refetch: refetchTags
  } = useGetWsTags(projectId, Boolean(hasTagCondition));

  useEffect(() => {
    if (!conditionValues?.length) return;
    if (!actionConditionsMap || !actionLabelsMap) return;

    conditionValues.forEach((condition, index) => {
      const conditionKey = condition?.lhs;
      if (!conditionKey) return;

      const disallowingActions = getDisallowingActions(
        conditionKey,
        selectedActions,
        actionConditionsMap
      );

      const fieldPath = `permissions.${subject}.${position}.conditions.${index}.lhs` as const;

      if (disallowingActions.length === 0) {
        clearErrors(fieldPath);
      } else {
        const actionNames = disallowingActions
          .map((action) => actionLabelsMap[action] || action)
          .join(", ");
        setError(fieldPath, {
          type: "custom",
          message: `This condition is not available for the actions: ${actionNames}.`
        });
      }
    });
  }, [
    selectedActions,
    conditionValues,
    actionConditionsMap,
    actionLabelsMap,
    subject,
    position,
    clearErrors,
    setError
  ]);

  const usedConditionTypes = useMemo((): string[] => {
    if (!conditionValues) return [];
    return conditionValues.map((c) => c.lhs).filter(Boolean);
  }, [conditionValues]);

  const canAddCondition = useMemo(() => {
    const availableToAdd = selectOptions.filter(
      ({ value }) => allowedConditions.includes(value) && !usedConditionTypes.includes(value)
    );
    return availableToAdd.length > 0;
  }, [selectOptions, allowedConditions, usedConditionTypes]);

  const incompatibleConditions = useMemo(() => {
    if (!conditionValues?.length) return [];
    if (!actionConditionsMap || !actionLabelsMap) return [];

    const incompatible: Array<{
      conditionLabel: string;
      conditionValue: string;
      disallowingActionLabels: string;
    }> = [];

    conditionValues.forEach((condition) => {
      const conditionKey = condition?.lhs;
      if (!conditionKey) return;

      const disallowingActions = getDisallowingActions(
        conditionKey,
        selectedActions,
        actionConditionsMap
      );

      if (disallowingActions.length > 0) {
        const conditionOption = selectOptions.find((opt) => opt.value === conditionKey);
        const conditionLabel = conditionOption?.label || conditionKey;
        const actionNames = disallowingActions
          .map((action) => actionLabelsMap[action] || action)
          .join(", ");

        incompatible.push({
          conditionLabel,
          conditionValue: conditionKey,
          disallowingActionLabels: actionNames
        });
      }
    });

    return incompatible;
  }, [conditionValues, selectedActions, actionConditionsMap, actionLabelsMap, selectOptions]);

  const getDefaultOperator = (conditionType: string): PermissionConditionOperators => {
    switch (conditionType) {
      case "secretTags":
        return PermissionConditionOperators.$IN;
      default:
        return PermissionConditionOperators.$EQ;
    }
  };

  const getFirstAvailableCondition = () => {
    const available = selectOptions.find(
      ({ value }) => allowedConditions.includes(value) && !usedConditionTypes.includes(value)
    );
    const conditionType = available?.value || selectOptions[0].value;
    return {
      lhs: conditionType,
      operator: getDefaultOperator(conditionType)
    };
  };

  if (isDisabled && items.fields.length === 0) {
    return null;
  }

  return (
    <div className="mt-6 border-t border-t-border bg-card pt-2">
      <div className="flex w-full flex-wrap items-center justify-between gap-x-2">
        <div className="mt-2.5 flex items-center text-foreground">
          <span>Conditions</span>
          <Tooltip>
            <TooltipTrigger asChild>
              <InfoIcon className="ml-1 size-4 text-muted" />
            </TooltipTrigger>
            <TooltipContent className="max-w-sm text-wrap">
              <p>
                Conditions determine when a policy will be applied (always if no conditions are
                present).
              </p>
              <p className="mt-3">
                All conditions must evaluate to true for the policy to take effect.
              </p>
            </TooltipContent>
          </Tooltip>
        </div>
        <div className="flex flex-wrap items-center justify-end gap-2">
          {hasReferenceConditions && (
            <Button
              type="button"
              variant="ghost"
              size="xs"
              className="mt-2"
              isDisabled={isProjectRefreshing || isTagsFetching}
              onClick={() => {
                queryClient.invalidateQueries({ queryKey: projectKeys.getProjectById(projectId) });
                if (hasTagCondition) refetchTags();
              }}
            >
              <RefreshCwIcon />
              Refresh References
            </Button>
          )}
          <Tooltip>
            <TooltipTrigger asChild>
              <span>
                <Button
                  type="button"
                  variant="outline"
                  size="xs"
                  className="mt-2"
                  isDisabled={isDisabled || !canAddCondition}
                  onClick={() => {
                    const { lhs, operator } = getFirstAvailableCondition();
                    items.append({
                      lhs,
                      operator,
                      rhs: ""
                    });
                  }}
                >
                  <PlusIcon className="size-4" />
                  Add Condition
                </Button>
              </span>
            </TooltipTrigger>
            {!canAddCondition && !isDisabled && (
              <TooltipContent side="top">
                {allowedConditions.length === 0
                  ? "No conditions available for the selected group of actions."
                  : "All available conditions have been added"}
              </TooltipContent>
            )}
          </Tooltip>
        </div>
      </div>
      {incompatibleConditions.length > 0 && (
        <Accordion type="single" collapsible className="mt-3 border-danger/35">
          <AccordionItem value="errors" className="border-none">
            <AccordionTrigger className="min-h-10 flex-row-reverse justify-between bg-danger/[0.075] px-3 py-2 text-foreground hover:bg-danger/10 data-[state=open]:bg-danger/10 [&>svg]:shrink-0">
              <div className="flex items-center gap-2">
                <AlertTriangleIcon className="size-4 text-danger" />
                <span>
                  {incompatibleConditions.length} condition
                  {incompatibleConditions.length > 1 ? "s" : ""} incompatible with selected actions
                </span>
              </div>
            </AccordionTrigger>
            <AccordionContent className="space-y-1 bg-danger/[0.025] px-3 py-2">
              {incompatibleConditions.map((item) => (
                <div key={item.conditionValue} className="text-sm">
                  <span className="font-medium text-danger">
                    {item.conditionLabel} not available for the actions:
                  </span>{" "}
                  <span className="">{item.disallowingActionLabels}</span>
                </div>
              ))}

              <span className="text-sm text-accent">
                Remove the incompatible conditions or update the selected actions to make them
                compatible.
              </span>
            </AccordionContent>
          </AccordionItem>
        </Accordion>
      )}
      <div className="mt-2 flex flex-col space-y-2">
        {items.fields.length > 0 &&
          items.fields.map((el, index) => {
            const condition = conditionValues?.[index] || { lhs: "", rhs: "", operator: "" };
            const isReferenceCondition =
              condition.lhs === "environment" || condition.lhs === "secretTags";
            const isLiteralOperator =
              condition.operator === PermissionConditionOperators.$EQ ||
              condition.operator === PermissionConditionOperators.$NEQ ||
              condition.operator === PermissionConditionOperators.$IN ||
              condition.operator === PermissionConditionOperators.$ALL;
            const isTagLookupUnavailable =
              condition.lhs === "secretTags" &&
              (isTagsPending || isTagsFetching || isTagsError || !projectTags);
            const isReferenceRefreshing = condition.lhs === "environment" && isProjectRefreshing;
            const knownSlugs =
              condition.lhs === "environment"
                ? currentProject.environments?.map(({ slug }) => slug)
                : projectTags?.map(({ slug }) => slug);
            const unknownSlugs =
              isReferenceCondition &&
              isLiteralOperator &&
              !isTagLookupUnavailable &&
              !isReferenceRefreshing &&
              knownSlugs
                ? [
                    ...new Set(
                      condition.operator === PermissionConditionOperators.$IN ||
                        condition.operator === PermissionConditionOperators.$ALL
                        ? condition.rhs.split(",")
                        : [condition.rhs]
                    )
                  ].filter((slug) => slug !== "" && !knownSlugs.includes(slug))
                : [];

            // Filter out already used conditions (except current row's condition)
            const availableOptionsForRow = selectOptions.filter(
              ({ value }) => !usedConditionTypes.includes(value) || value === condition.lhs
            );

            return (
              <Controller
                key={el.id}
                control={control}
                name={`permissions.${subject}.${position}.conditions.${index}.rhs` as const}
                render={({ field: rhsField, fieldState: { error: rhsError } }) => (
                  <Controller
                    control={control}
                    name={`permissions.${subject}.${position}.conditions.${index}.lhs` as const}
                    render={({ field: lhsField, fieldState: { error: lhsError } }) => (
                      <div className="bg-card first:rounded-t-md last:rounded-b-md">
                        <div className="flex items-center gap-2">
                          <div className="flex w-1/4 items-center gap-2">
                            {index > 0 && (
                              <Tooltip>
                                <TooltipTrigger asChild>
                                  <div className="shrink-0">
                                    <Badge variant="neutral" className="text-xs">
                                      and
                                    </Badge>
                                  </div>
                                </TooltipTrigger>
                                <TooltipContent side="top" className="max-w-xs text-wrap">
                                  All conditions must be true for this policy to apply
                                </TooltipContent>
                              </Tooltip>
                            )}
                            <Select
                              value={lhsField.value}
                              onValueChange={(newConditionType) => {
                                setValue(
                                  `permissions.${subject}.${position}.conditions.${index}.operator` as const,
                                  getDefaultOperator(newConditionType) as never
                                );
                                lhsField.onChange(newConditionType);
                              }}
                            >
                              <SelectTrigger
                                className={twMerge("w-full", lhsError?.message && "border-danger")}
                              >
                                <SelectValue />
                              </SelectTrigger>
                              <SelectContent position="popper">
                                {availableOptionsForRow.map(({ value, label }) => {
                                  const isAllowed = allowedConditions.includes(value);
                                  const disallowingActions = getDisallowingActions(
                                    value,
                                    selectedActions,
                                    actionConditionsMap
                                  );

                                  if (!isAllowed) {
                                    return (
                                      <Tooltip key={value}>
                                        <TooltipTrigger asChild>
                                          <div>
                                            <SelectItem
                                              value={value}
                                              disabled
                                              className={twMerge("cursor-not-allowed opacity-50")}
                                            >
                                              {label}
                                            </SelectItem>
                                          </div>
                                        </TooltipTrigger>
                                        <TooltipContent side="right" className="max-w-xs">
                                          This condition is not available for the actions:{" "}
                                          {disallowingActions
                                            .map((action) => actionLabelsMap?.[action] || action)
                                            .join(", ")}
                                        </TooltipContent>
                                      </Tooltip>
                                    );
                                  }

                                  return (
                                    <SelectItem key={value} value={value}>
                                      {label}
                                    </SelectItem>
                                  );
                                })}
                              </SelectContent>
                            </Select>
                            {selectOptions.find((opt) => opt.value === condition.lhs)
                              ?.description && (
                              <Tooltip>
                                <TooltipTrigger asChild>
                                  <InfoIcon className="size-4 shrink-0 text-muted" />
                                </TooltipTrigger>
                                <TooltipContent side="right" className="max-w-xs text-wrap">
                                  {
                                    selectOptions.find((opt) => opt.value === condition.lhs)
                                      ?.description
                                  }
                                </TooltipContent>
                              </Tooltip>
                            )}
                          </div>
                          <div className="flex w-44 items-center space-x-2">
                            <Controller
                              control={control}
                              name={
                                `permissions.${subject}.${position}.conditions.${index}.operator` as const
                              }
                              render={({ field }) => (
                                <Select
                                  key={`${condition.lhs}-operator`}
                                  value={field.value}
                                  onValueChange={(e) => field.onChange(e)}
                                >
                                  <SelectTrigger className="w-full">
                                    <SelectValue />
                                  </SelectTrigger>
                                  <SelectContent position="popper">
                                    {renderOperatorSelectItems(condition.lhs, subject)}
                                  </SelectContent>
                                </Select>
                              )}
                            />
                            <div>
                              {condition?.operator === PermissionConditionOperators.$GLOB ? (
                                // The glob helper contains inputs for testing patterns, so it
                                // needs a container that stays open while interacting with it
                                <Popover>
                                  <Tooltip>
                                    <TooltipTrigger asChild>
                                      <PopoverTrigger asChild>
                                        <IconButton
                                          aria-label="Glob pattern reference"
                                          variant="ghost-muted"
                                          size="xs"
                                        >
                                          <InfoIcon />
                                        </IconButton>
                                      </PopoverTrigger>
                                    </TooltipTrigger>
                                    <TooltipContent>Glob pattern reference</TooltipContent>
                                  </Tooltip>
                                  <PopoverContent className="w-96">
                                    {getConditionOperatorHelperInfo(
                                      condition.operator as PermissionConditionOperators
                                    )}
                                  </PopoverContent>
                                </Popover>
                              ) : (
                                <Tooltip>
                                  <TooltipTrigger asChild>
                                    <InfoIcon className="size-4 text-muted" />
                                  </TooltipTrigger>
                                  <TooltipContent className="max-w-xs text-wrap">
                                    {getConditionOperatorHelperInfo(
                                      condition?.operator as PermissionConditionOperators
                                    )}
                                  </TooltipContent>
                                </Tooltip>
                              )}
                            </div>
                          </div>
                          <div className="grow">
                            <Input
                              {...rhsField}
                              className={twMerge(rhsError?.message && "border-danger")}
                            />
                          </div>
                          <Tooltip>
                            <TooltipTrigger asChild>
                              <IconButton
                                aria-label="remove"
                                variant="outline"
                                className="hover:border-danger/30 hover:bg-danger/15"
                                isDisabled={isDisabled}
                                onClick={() => items.remove(index)}
                              >
                                <TrashIcon className="size-4" />
                              </IconButton>
                            </TooltipTrigger>
                            <TooltipContent side="right">Remove Condition</TooltipContent>
                          </Tooltip>
                        </div>
                        {rhsError?.message && (
                          <div className="flex items-start gap-2 pb-1">
                            <div className="w-1/4" />
                            <div className="w-44" />
                            <div className="grow">
                              <FieldError>{rhsError.message}</FieldError>
                            </div>
                            <div className="w-10" />
                          </div>
                        )}
                        {isReferenceCondition &&
                          isLiteralOperator &&
                          (unknownSlugs.length > 0 ||
                            isTagLookupUnavailable ||
                            isReferenceRefreshing) && (
                            <div className="mt-2 flex items-start gap-2 pb-1 text-xs">
                              <div className="hidden w-1/4 xl:block" />
                              <div className="hidden w-44 xl:block" />
                              <div
                                className={twMerge(
                                  "min-w-0 grow basis-full rounded-md border px-2 py-1 xl:basis-0",
                                  unknownSlugs.length > 0
                                    ? "border-warning/20 bg-warning/5"
                                    : "border-border bg-card"
                                )}
                              >
                                {unknownSlugs.length > 0 ? (
                                  <p className="break-words text-warning" role="status">
                                    <AlertTriangleIcon className="mr-1 inline size-3.5 align-[-0.125em]" />
                                    {condition.lhs === "environment" ? "Environment" : "Tag"}
                                    {unknownSlugs.length > 1 ? "s" : ""}{" "}
                                    {unknownSlugs.map((slug) => JSON.stringify(slug)).join(", ")}{" "}
                                    {unknownSlugs.length === 1 ? "doesn't" : "don't"} exist in your
                                    project. You can still save.
                                  </p>
                                ) : (
                                  <p className="text-muted" role="status">
                                    {isTagsPending || isTagsFetching || isReferenceRefreshing
                                      ? "Checking project references…"
                                      : "Could not check project secret tags. You can still save this policy."}
                                  </p>
                                )}
                              </div>
                              <div className="hidden w-10 xl:block" />
                            </div>
                          )}
                      </div>
                    )}
                  />
                )}
              />
            );
          })}
      </div>
    </div>
  );
};