Skip to content

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

19.3 KB
Raw Blame
import { useState } from "react";
import { subject } from "@casl/ability";
import { Link, linkOptions } from "@tanstack/react-router";
import { format } from "date-fns";
import {
  BanIcon,
  ClockIcon,
  CopyIcon,
  EyeIcon,
  EyeOffIcon,
  HardDriveIcon,
  RotateCcwIcon,
  ServerCogIcon,
  Trash2Icon,
  UserIcon
} from "lucide-react";
import { twMerge } from "tailwind-merge";

import { createNotification } from "@app/components/notifications";
import { ProjectPermissionCan } from "@app/components/permissions";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogMedia,
  AlertDialogTitle,
  Badge,
  Empty,
  EmptyDescription,
  EmptyHeader,
  EmptyMedia,
  EmptyTitle,
  IconButton,
  Skeleton,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import { ProjectPermissionActions, ProjectPermissionSub, useProject } from "@app/context";
import { useGetSecretVersion, useRedactSecretValue, useUpdateSecretV3 } from "@app/hooks/api";
import { ActorType } from "@app/hooks/api/auditLogs/enums";
import { fetchSecretVersionValue } from "@app/hooks/api/secrets/queries";
import { SecretType, SecretVersions } from "@app/hooks/api/types";

type Props = {
  secretId: string;
  secretKey: string;
  environment: string;
  secretPath: string;
  isRotatedSecret: boolean;
  canReadValue: boolean;
};

type VersionItemProps = {
  secretId: string;
  secretKey: string;
  environment: string;
  secretPath: string;
  version: SecretVersions;
  currentVersion: number;
  isRotatedSecret: boolean;
  canReadValue: boolean;
  onRestoreSuccess: () => void;
  isLast: boolean;
};

function VersionItem({
  secretId,
  secretKey,
  environment,
  secretPath,
  version,
  currentVersion,
  isRotatedSecret,
  canReadValue,
  onRestoreSuccess,
  isLast
}: VersionItemProps) {
  const { currentProject } = useProject();
  const { mutateAsync: updateSecret, isPending: isRestoring } = useUpdateSecretV3();
  const { mutateAsync: redactSecretValue, isPending: isRedacting } = useRedactSecretValue();

  const [secretValue, setSecretValue] = useState<string | null>(null);
  const [isValueVisible, setIsValueVisible] = useState(false);
  const [isFetchingValue, setIsFetchingValue] = useState(false);
  const [isRestoreDialogOpen, setIsRestoreDialogOpen] = useState(false);
  const [isRedactDialogOpen, setIsRedactDialogOpen] = useState(false);

  const isCurrentVersion = version.version === currentVersion;
  const canRestore = !isRotatedSecret && canReadValue && !isCurrentVersion;

  const getModifiedByName = (
    userType: string | undefined | null,
    userName: string | null | undefined
  ) => {
    switch (userType) {
      case ActorType.PLATFORM:
        return "System";
      case ActorType.IDENTITY:
        return userName || "Deleted Identity";
      case ActorType.USER:
        return userName || "Deleted User";
      default:
        return "Unknown";
    }
  };

  const handleFetchSecretValue = async () => {
    if (secretValue !== null) return secretValue;
    try {
      setIsFetchingValue(true);
      const value = await fetchSecretVersionValue(secretId, version.version);
      setSecretValue(value);
      return value;
    } catch (e) {
      console.error(e);
      createNotification({ type: "error", text: "Failed to fetch secret version value" });
      throw e;
    } finally {
      setIsFetchingValue(false);
    }
  };

  const getActorLink = (actor: NonNullable<SecretVersions["actor"]>) => {
    if (!actor.membershipId || !actor.actorId) return null;
    const params = { orgId: currentProject.orgId, projectId: currentProject.id };

    if (actor.actorType === ActorType.USER && actor.groupId) {
      return linkOptions({
        to: "/organizations/$orgId/projects/secret-management/$projectId/groups/$groupId",
        params: { ...params, groupId: actor.groupId },
        search: { username: actor.name ?? undefined }
      });
    }
    if (actor.actorType === ActorType.USER) {
      return linkOptions({
        to: "/organizations/$orgId/projects/secret-management/$projectId/members/$membershipId",
        params: { ...params, membershipId: actor.membershipId }
      });
    }
    if (actor.actorType === ActorType.IDENTITY) {
      return linkOptions({
        to: "/organizations/$orgId/projects/secret-management/$projectId/identities/$identityId",
        params: { ...params, identityId: actor.actorId }
      });
    }
    return null;
  };

  const actorLink = version.actor ? getActorLink(version.actor) : null;
  const actorName = version.actor
    ? getModifiedByName(version.actor.actorType, version.actor.name)
    : "";

  const handleToggleVisibility = async () => {
    if (!isValueVisible) {
      await handleFetchSecretValue();
    }
    setIsValueVisible(!isValueVisible);
  };

  const handleCopyValue = async () => {
    try {
      const value = await handleFetchSecretValue();
      await navigator.clipboard.writeText(value || "");
      createNotification({ type: "success", text: "Value copied to clipboard" });
    } catch {
      // Error notification already handled in handleFetchSecretValue
    }
  };

  const handleRestore = async () => {
    // For redacted versions, restore with empty string
    const value = version.isRedacted ? "" : await handleFetchSecretValue();

    const result = await updateSecret({
      projectId: currentProject.id,
      environment,
      secretPath,
      secretKey,
      secretValue: value,
      type: SecretType.Shared
    });

    if ("approval" in result) {
      createNotification({
        type: "info",
        text: "Requested change has been sent for review"
      });
    } else {
      createNotification({
        type: "success",
        text: `Secret restored to version ${version.version}`
      });
    }

    setIsRestoreDialogOpen(false);
    onRestoreSuccess();
  };

  const handleRedact = async () => {
    await redactSecretValue({ versionId: version.id, secretId });
    createNotification({
      type: "success",
      text: "The secret value has been redacted successfully and is no longer persisted or viewable."
    });
    setIsRedactDialogOpen(false);
    onRestoreSuccess();
  };

  return (
    <div className="relative flex gap-4">
      {/* Timeline indicator */}
      <div className="relative flex flex-col items-center">
        <div
          className={`absolute top-2 left-0 size-3 rounded-full border-2 ${
            isCurrentVersion ? "border-info/60 bg-info/10" : "border-border bg-transparent"
          }`}
        />
        {!isLast && <div className="absolute top-5 -bottom-2 left-[5.5px] w-px bg-border" />}
      </div>

      {/* Content */}
      <div className="flex-1 pb-6 pl-2">
        {/* Header row */}
        <div className="mb-2 flex items-center justify-between">
          <div className="flex items-center gap-2">
            <span
              className={twMerge(
                "text-sm font-semibold",
                isCurrentVersion && "text-info",
                version.isRedacted && "text-muted",
                !isCurrentVersion && !version.isRedacted && "text-foreground"
              )}
            >
              v{version.version}
            </span>
            {isCurrentVersion && <Badge variant="info">Current</Badge>}
            {version.isRedacted && (
              <Badge variant="neutral" className="gap-1">
                <EyeOffIcon className="size-3" />
                Redacted
              </Badge>
            )}
            <div className="flex items-center gap-1 text-xs text-muted">
              <ClockIcon className="size-3" />
              {format(new Date(version.createdAt), "MMM d, yyyy, h:mm a")}
            </div>
          </div>

          {/* Actions */}
          {canReadValue && (
            <div className="flex items-center gap-1">
              <AlertDialog open={isRestoreDialogOpen} onOpenChange={setIsRestoreDialogOpen}>
                <AlertDialogContent>
                  <AlertDialogHeader>
                    <AlertDialogMedia>
                      <RotateCcwIcon />
                    </AlertDialogMedia>
                    <AlertDialogTitle>Restore Secret Version</AlertDialogTitle>
                    <AlertDialogDescription>
                      Are you sure you want to restore this secret to version {version.version}?
                      This will overwrite the current value.
                      {version.isRedacted && (
                        <span className="mt-2 block text-danger">
                          Note: This version was redacted, so the value will be set to empty.
                        </span>
                      )}
                    </AlertDialogDescription>
                  </AlertDialogHeader>
                  <AlertDialogFooter>
                    <AlertDialogCancel>Cancel</AlertDialogCancel>
                    <AlertDialogAction onClick={handleRestore}>Restore</AlertDialogAction>
                  </AlertDialogFooter>
                </AlertDialogContent>
              </AlertDialog>
              <AlertDialog open={isRedactDialogOpen} onOpenChange={setIsRedactDialogOpen}>
                <AlertDialogContent>
                  <AlertDialogHeader>
                    <AlertDialogMedia>
                      <Trash2Icon />
                    </AlertDialogMedia>
                    <AlertDialogTitle>Redact Secret Version</AlertDialogTitle>
                    <AlertDialogDescription>
                      Are you sure you want to redact the secret value on version {version.version}?
                      This action is irreversible.
                    </AlertDialogDescription>
                  </AlertDialogHeader>
                  <AlertDialogFooter>
                    <AlertDialogCancel>Cancel</AlertDialogCancel>
                    <AlertDialogAction variant="danger" onClick={handleRedact}>
                      Redact
                    </AlertDialogAction>
                  </AlertDialogFooter>
                </AlertDialogContent>
              </AlertDialog>

              {!version.secretValueHidden && !version.isRedacted && (
                <>
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <IconButton
                        variant="ghost"
                        size="xs"
                        onClick={handleCopyValue}
                        isDisabled={isFetchingValue}
                      >
                        <CopyIcon />
                      </IconButton>
                    </TooltipTrigger>
                    <TooltipContent>Copy Value</TooltipContent>
                  </Tooltip>
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <IconButton
                        variant="ghost"
                        size="xs"
                        onClick={handleToggleVisibility}
                        isDisabled={isFetchingValue}
                      >
                        {isValueVisible ? <EyeOffIcon /> : <EyeIcon />}
                      </IconButton>
                    </TooltipTrigger>
                    <TooltipContent>{isValueVisible ? "Hide Value" : "Show Value"}</TooltipContent>
                  </Tooltip>
                </>
              )}
              {canRestore && (
                <ProjectPermissionCan
                  I={ProjectPermissionActions.Edit}
                  a={subject(ProjectPermissionSub.Secrets, {
                    environment,
                    secretPath,
                    secretName: secretKey,
                    secretTags: ["*"]
                  })}
                >
                  {(isAllowed) => (
                    <Tooltip>
                      <TooltipTrigger>
                        <IconButton
                          variant="ghost"
                          size="xs"
                          onClick={() => setIsRestoreDialogOpen(true)}
                          isDisabled={isRestoring || isFetchingValue || !isAllowed}
                        >
                          <RotateCcwIcon />
                        </IconButton>
                      </TooltipTrigger>
                      <TooltipContent>
                        {isAllowed ? "Restore Version" : "Access Denied"}
                      </TooltipContent>
                    </Tooltip>
                  )}
                </ProjectPermissionCan>
              )}
              {!isRotatedSecret && !version.isRedacted && !isCurrentVersion && (
                <ProjectPermissionCan
                  I={ProjectPermissionActions.Edit}
                  a={subject(ProjectPermissionSub.Secrets, {
                    environment,
                    secretPath,
                    secretName: secretKey,
                    secretTags: ["*"]
                  })}
                >
                  {(isAllowed) => (
                    <Tooltip>
                      <TooltipTrigger>
                        <IconButton
                          variant="ghost"
                          size="xs"
                          className="hover:text-danger"
                          onClick={() => setIsRedactDialogOpen(true)}
                          isDisabled={isRedacting || !isAllowed}
                        >
                          <Trash2Icon />
                        </IconButton>
                      </TooltipTrigger>
                      <TooltipContent>
                        {isAllowed ? "Redact Secret Value" : "Access Denied"}
                      </TooltipContent>
                    </Tooltip>
                  )}
                </ProjectPermissionCan>
              )}
            </div>
          )}
        </div>

        {/* Value input display */}
        {version.isRedacted ? (
          <>
            <div className="mb-2 min-w-0 rounded-md border border-border bg-container px-3 py-2 font-mono text-sm text-foreground">
              <span className="blur-sm select-none">xxxxxxxxxxxxxxxxxxxx</span>
            </div>
            {version.redactedByActor && (
              <div className="mb-1 flex items-center gap-1.5 text-xs text-muted">
                <EyeOffIcon className="size-3" />
                <span>
                  Redacted by{" "}
                  <span className="font-medium">
                    {!version.redactedByActor.projectMembershipId
                      ? `${version.redactedByActor.username || version.redactedByActor.email} (Removed from project)`
                      : version.redactedByActor.username ||
                        version.redactedByActor.email ||
                        "Unknown User"}
                  </span>
                  {version.redactedAt && (
                    <span> on {format(new Date(version.redactedAt), "MMM d, yyyy, h:mm a")}</span>
                  )}
                </span>
              </div>
            )}
          </>
        ) : (
          <Tooltip open={canReadValue && !version.secretValueHidden ? false : undefined}>
            <TooltipTrigger asChild>
              <div className="mb-2 min-w-0 rounded-md border border-border bg-container px-3 py-2 font-mono text-sm [overflow-wrap:anywhere] whitespace-pre-wrap text-foreground">
                {/* eslint-disable-next-line no-nested-ternary */}
                {isValueVisible ? (
                  isFetchingValue ? (
                    <span className="tracking-widest">••••••••••••••••••••</span>
                  ) : (
                    secretValue || <span className="text-muted-foreground">EMPTY</span>
                  )
                ) : (
                  <span className="tracking-widest">••••••••••••••••••••</span>
                )}
              </div>
            </TooltipTrigger>
            <TooltipContent>Access Denied</TooltipContent>
          </Tooltip>
        )}

        {/* Modified by */}
        {version.actor && (
          <div className="flex items-center gap-1.5 text-xs text-muted">
            {/* eslint-disable-next-line no-nested-ternary */}
            {version.actor.actorType === ActorType.USER ? (
              <UserIcon className="size-3" />
            ) : version.actor.actorType === ActorType.IDENTITY ? (
              <HardDriveIcon className="size-3" />
            ) : (
              <ServerCogIcon className="size-3" />
            )}
            <span>
              {version.version === 1 ? "Created" : "Modified"} by{" "}
              {actorLink ? (
                <Link {...actorLink} className="font-medium text-accent underline">
                  {actorName}
                </Link>
              ) : (
                <span className="font-medium text-accent">{actorName}</span>
              )}
            </span>
          </div>
        )}
      </div>
    </div>
  );
}

export function SecretVersionHistory({
  secretId,
  secretKey,
  environment,
  secretPath,
  isRotatedSecret,
  canReadValue
}: Props) {
  const {
    data: secretVersions,
    isLoading,
    refetch
  } = useGetSecretVersion({
    limit: 100,
    offset: 0,
    secretId
  });

  if (isLoading) {
    return (
      <div className="flex flex-col px-4 pt-4">
        {Array.from(Array(6)).map((_, index, arr) => (
          <div key={(index + 1).toString()} className="relative flex h-[122px] gap-4">
            {/* Timeline indicator skeleton */}
            <div className="relative flex flex-col items-center">
              <Skeleton className="absolute top-1.5 left-0 size-3 rounded-full" />
              {index < arr.length - 1 && (
                <div className="absolute top-4.5 -bottom-2 left-[5.5px] w-px bg-border" />
              )}
            </div>

            {/* Content skeleton */}
            <div className="flex-1 pb-6 pl-2">
              {/* Header row skeleton */}
              <div className="mb-2 flex items-center gap-2">
                <Skeleton className="h-5 w-8" />
                {index === 0 && <Skeleton className="h-5 w-16" />}
                <Skeleton className="h-4 w-36" />
              </div>

              {/* Value input skeleton */}
              <Skeleton className="mb-2 h-10 w-full rounded-md" />

              {/* Modified by skeleton */}
              <div className="flex items-center gap-1.5">
                <Skeleton className="size-3 rounded-full" />
                <Skeleton className="h-4 w-32" />
              </div>
            </div>
          </div>
        ))}
      </div>
    );
  }

  // should never happen as we disable
  if (!secretVersions || secretVersions.length === 0) {
    return (
      <Empty className="bg-transparent">
        <EmptyHeader>
          <EmptyMedia variant="icon">
            <BanIcon />
          </EmptyMedia>
          <EmptyTitle>Access Denied</EmptyTitle>
          <EmptyDescription>
            You do not have permission to view this secrets history
          </EmptyDescription>
        </EmptyHeader>
      </Empty>
    );
  }

  const currentVersion = secretVersions[0].version;

  return (
    <div className="flex flex-col overflow-y-auto px-4 pt-4">
      {secretVersions.map((version, index) => (
        <VersionItem
          key={version.id}
          secretId={secretId}
          secretKey={secretKey}
          environment={environment}
          secretPath={secretPath}
          version={version}
          currentVersion={currentVersion}
          isRotatedSecret={isRotatedSecret}
          canReadValue={canReadValue}
          onRestoreSuccess={() => refetch()}
          isLast={index === secretVersions.length - 1}
        />
      ))}
    </div>
  );
}