Skip to content

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

12.1 KB
Raw Blame
import { useEffect, useState } from "react";
import { Link } from "@tanstack/react-router";
import { format } from "date-fns";
import { MoreHorizontalIcon, SearchIcon } from "lucide-react";

import { ProjectPermissionCan } from "@app/components/permissions";
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  Empty,
  EmptyDescription,
  EmptyHeader,
  EmptyTitle,
  IconButton,
  InputGroup,
  InputGroupAddon,
  InputGroupInput,
  Pagination,
  RowLink,
  Skeleton,
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import {
  ProjectPermissionPkiCertificateInstallationActions,
  ProjectPermissionSub,
  useProject
} from "@app/context";
import { getInstancePinningLink } from "@app/helpers/project";
import {
  TPkiInstallation,
  useDeletePkiInstallation,
  useListPkiInstallations
} from "@app/hooks/api";
import { useDebounce } from "@app/hooks/useDebounce";
import { usePopUp } from "@app/hooks/usePopUp";
import {
  canSetKeystorePassword,
  getEndpoint,
  getGatewayLabel,
  getInstallationDiscoveryType,
  getKeystoreStatusBadge,
  useCanRescanPkiInstallations
} from "@app/pages/cert-manager/pki-discovery-utils";

import { DeleteInstallationModal } from "./DeleteInstallationModal";
import { DiscoveryTypeIcon } from "./DiscoveryJobSheet/DiscoveryTypeIcon";
import { EditInstallationModal } from "./EditInstallationModal";
import {
  RESCAN_POLL_INTERVAL_MS,
  RESCAN_POLL_TIMEOUT_MS,
  SetKeystorePasswordDialog
} from "./SetKeystorePasswordDialog";

type Props = {
  projectId: string;
};

const PAGE_SIZE = 25;

export const InstallationsTab = ({ projectId }: Props) => {
  const { currentProject } = useProject();
  const [page, setPage] = useState(1);
  const [searchFilter, setSearchFilter] = useState("");
  const [debouncedSearch] = useDebounce(searchFilter, 300);

  const canRescanInstallations = useCanRescanPkiInstallations();

  const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([
    "editInstallation",
    "deleteInstallation",
    "setKeystorePassword"
  ] as const);

  const [pendingRescan, setPendingRescan] = useState<{
    installationId: string;
    previousCheckedAt?: string;
  } | null>(null);

  useEffect(() => {
    if (!pendingRescan) return undefined;
    const timeout = setTimeout(() => setPendingRescan(null), RESCAN_POLL_TIMEOUT_MS);
    return () => clearTimeout(timeout);
  }, [pendingRescan]);

  const { data, isPending } = useListPkiInstallations(
    {
      projectId,
      offset: (page - 1) * PAGE_SIZE,
      limit: PAGE_SIZE,
      search: debouncedSearch || undefined
    },
    { refetchInterval: pendingRescan ? RESCAN_POLL_INTERVAL_MS : false }
  );

  useEffect(() => {
    if (!pendingRescan) return;
    const rescanned = data?.installations.find((item) => item.id === pendingRescan.installationId);
    if (rescanned && rescanned.metadata?.lastCheckedAt !== pendingRescan.previousCheckedAt) {
      setPendingRescan(null);
    }
  }, [data, pendingRescan]);

  useEffect(() => {
    setPage(1);
  }, [debouncedSearch]);

  const deleteInstallation = useDeletePkiInstallation();

  const installations = data?.installations || [];
  const totalCount = data?.totalCount || 0;

  const handleDelete = async (): Promise<void> => {
    const installation = popUp.deleteInstallation.data as TPkiInstallation;
    await deleteInstallation.mutateAsync({ installationId: installation.id, projectId });
    handlePopUpClose("deleteInstallation");
  };

  return (
    <Card>
      <CardHeader>
        <CardTitle>Installations</CardTitle>
        <CardDescription>
          Every place a certificate was found during a scan, identified by its host and its port,
          file path or certificate store.
        </CardDescription>
      </CardHeader>
      <CardContent>
        <div className="mb-4">
          <InputGroup>
            <InputGroupAddon>
              <SearchIcon />
            </InputGroupAddon>
            <InputGroupInput
              value={searchFilter}
              onChange={(e) => setSearchFilter(e.target.value)}
              placeholder="Search by name, host, IP, domain, or path…"
            />
          </InputGroup>
        </div>

        {/* eslint-disable-next-line no-nested-ternary */}
        {isPending ? (
          <div className="space-y-2">
            <Skeleton className="h-10 w-full" />
            <Skeleton className="h-10 w-full" />
            <Skeleton className="h-10 w-full" />
          </div>
        ) : installations.length === 0 ? (
          <Empty className="border">
            <EmptyHeader>
              <EmptyTitle>No installations found</EmptyTitle>
              <EmptyDescription>
                Run a discovery job to surface the certificates currently served across your
                infrastructure.
              </EmptyDescription>
            </EmptyHeader>
          </Empty>
        ) : (
          <>
            <Table>
              <TableHeader>
                <TableRow>
                  <TableHead>Name</TableHead>
                  <TableHead>Certificate</TableHead>
                  <TableHead>Gateway</TableHead>
                  <TableHead>Certs</TableHead>
                  <TableHead>Last Seen</TableHead>
                  <TableHead className="w-5" />
                </TableRow>
              </TableHeader>
              <TableBody>
                {installations.map((installation) => (
                  <TableRow key={installation.id} linked>
                    <TableCell className="max-w-96">
                      <div className="flex items-center gap-2">
                        <DiscoveryTypeIcon
                          type={getInstallationDiscoveryType(installation)}
                          className="size-4 shrink-0"
                        />
                        <RowLink
                          {...getInstancePinningLink(
                            currentProject.orgId,
                            currentProject,
                            `discovery/installations/${installation.id}`
                          )!}
                          className="min-w-0"
                          title={installation.name || getEndpoint(installation)}
                        >
                          {installation.name || getEndpoint(installation)}
                        </RowLink>
                        {getKeystoreStatusBadge(installation)}
                      </div>
                    </TableCell>
                    <TableCell className="max-w-56">
                      {installation.primaryCertName ? (
                        <Tooltip>
                          <TooltipTrigger asChild>
                            <Link
                              {...getInstancePinningLink(
                                currentProject.orgId,
                                currentProject,
                                `discovery/installations/${installation.id}`
                              )!}
                              className="block truncate"
                            >
                              {installation.primaryCertName}
                            </Link>
                          </TooltipTrigger>
                          <TooltipContent>{installation.primaryCertName}</TooltipContent>
                        </Tooltip>
                      ) : (
                        <span className="text-accent">—</span>
                      )}
                    </TableCell>
                    <TableCell>{getGatewayLabel(installation) || "N/A"}</TableCell>
                    <TableCell>
                      {installation.certificatesCount ?? installation.certificates?.length ?? 0}
                    </TableCell>
                    <TableCell>
                      {format(new Date(installation.lastSeenAt), "MMM dd, yyyy HH:mm")}
                    </TableCell>
                    <TableCell data-row-interactive>
                      <DropdownMenu>
                        <DropdownMenuTrigger asChild>
                          <IconButton variant="ghost" size="xs">
                            <MoreHorizontalIcon />
                          </IconButton>
                        </DropdownMenuTrigger>
                        <DropdownMenuContent align="end">
                          <ProjectPermissionCan
                            I={ProjectPermissionPkiCertificateInstallationActions.Edit}
                            a={ProjectPermissionSub.PkiCertificateInstallations}
                          >
                            {(isAllowed) => (
                              <DropdownMenuItem
                                isDisabled={!isAllowed}
                                onClick={() => handlePopUpOpen("editInstallation", installation)}
                              >
                                Edit
                              </DropdownMenuItem>
                            )}
                          </ProjectPermissionCan>
                          {canSetKeystorePassword(installation) && (
                            <DropdownMenuItem
                              isDisabled={!canRescanInstallations}
                              onClick={() => handlePopUpOpen("setKeystorePassword", installation)}
                            >
                              {installation.hasKeystorePassword
                                ? "Change Password"
                                : "Set Password"}
                            </DropdownMenuItem>
                          )}
                          <ProjectPermissionCan
                            I={ProjectPermissionPkiCertificateInstallationActions.Delete}
                            a={ProjectPermissionSub.PkiCertificateInstallations}
                          >
                            {(isAllowed) => (
                              <DropdownMenuItem
                                isDisabled={!isAllowed}
                                onClick={() => handlePopUpOpen("deleteInstallation", installation)}
                              >
                                Delete
                              </DropdownMenuItem>
                            )}
                          </ProjectPermissionCan>
                        </DropdownMenuContent>
                      </DropdownMenu>
                    </TableCell>
                  </TableRow>
                ))}
              </TableBody>
            </Table>

            {totalCount > PAGE_SIZE && (
              <div className="mt-4 flex justify-end">
                <Pagination
                  count={totalCount}
                  page={page}
                  perPage={PAGE_SIZE}
                  onChangePage={setPage}
                  onChangePerPage={() => {}}
                />
              </div>
            )}
          </>
        )}
      </CardContent>

      <EditInstallationModal
        isOpen={popUp.editInstallation.isOpen}
        onClose={() => handlePopUpClose("editInstallation")}
        projectId={projectId}
        installation={popUp.editInstallation.data as TPkiInstallation | undefined}
      />

      <SetKeystorePasswordDialog
        isOpen={popUp.setKeystorePassword.isOpen}
        onOpenChange={(isOpen) => handlePopUpToggle("setKeystorePassword", isOpen)}
        projectId={projectId}
        installation={popUp.setKeystorePassword.data as TPkiInstallation | undefined}
        onSaved={() => {
          const saved = popUp.setKeystorePassword.data as TPkiInstallation | undefined;
          if (saved) {
            setPendingRescan({
              installationId: saved.id,
              previousCheckedAt: saved.metadata?.lastCheckedAt
            });
          }
        }}
      />

      <DeleteInstallationModal
        isOpen={popUp.deleteInstallation.isOpen}
        onOpenChange={(isOpen) => handlePopUpToggle("deleteInstallation", isOpen)}
        onConfirm={handleDelete}
        installationName={
          (popUp.deleteInstallation.data as TPkiInstallation)?.name ||
          (popUp.deleteInstallation.data as TPkiInstallation)?.locationDetails?.fqdn ||
          (popUp.deleteInstallation.data as TPkiInstallation)?.locationDetails?.ipAddress ||
          "this installation"
        }
      />
    </Card>
  );
};