Skip to content

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

3.8 KB
Raw Blame
/* eslint-disable no-nested-ternary */
import { ReactNode } from "react";
import { format } from "date-fns";

import { truncateSerialNumber } from "@app/components/utilities/serialNumberUtils";
import {
  Badge,
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
  Empty,
  EmptyDescription,
  EmptyHeader,
  EmptyTitle,
  RowLink,
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "@app/components/v3";
import { useProject } from "@app/context";
import { getInstancePinningLink } from "@app/helpers/project";
import { TPkiInstallationCert } from "@app/hooks/api";

import { getCertValidUntilBadgeDetails } from "../../CertificatesPage/components/CertificatesTable.utils";

type Props = {
  certificates: TPkiInstallationCert[];
  emptyState?: ReactNode;
};

export const InstallationCertificatesSection = ({ certificates, emptyState }: Props) => {
  const { currentProject } = useProject();

  return (
    <Card>
      <CardHeader className="border-b">
        <CardTitle>Discovered Certificates</CardTitle>
        <CardDescription>Certificates found on this installation</CardDescription>
      </CardHeader>
      <CardContent className="p-0">
        {certificates.length === 0 && emptyState ? (
          emptyState
        ) : certificates.length === 0 ? (
          <Empty>
            <EmptyHeader>
              <EmptyTitle>No certificates found on this installation</EmptyTitle>
              <EmptyDescription>Run a scan to discover certificates</EmptyDescription>
            </EmptyHeader>
          </Empty>
        ) : (
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead>SAN / CN</TableHead>
                <TableHead>Status</TableHead>
                <TableHead>Serial Number</TableHead>
                <TableHead>Expires</TableHead>
                <TableHead>Last Seen</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {certificates.map((cert) => {
                const expiryDetails = cert.notAfter
                  ? getCertValidUntilBadgeDetails(cert.notAfter)
                  : null;

                return (
                  <TableRow key={cert.id} linked>
                    <TableCell>
                      <RowLink
                        {...getInstancePinningLink(
                          currentProject.orgId,
                          currentProject,
                          `certificates/${cert.certificateId}`
                        )!}
                      >
                        {cert.commonName || "N/A"}
                      </RowLink>
                    </TableCell>
                    <TableCell>
                      {cert.deletedAt && <Badge variant="neutral">Deleted</Badge>}
                      {!cert.deletedAt && expiryDetails && (
                        <Badge variant={expiryDetails.variant}>{expiryDetails.label}</Badge>
                      )}
                      {!cert.deletedAt && !expiryDetails && (
                        <Badge variant="neutral">Unknown</Badge>
                      )}
                    </TableCell>
                    <TableCell isTruncatable>
                      {truncateSerialNumber(cert.serialNumber || "")}
                    </TableCell>
                    <TableCell>
                      {cert.notAfter ? format(new Date(cert.notAfter), "MMM dd, yyyy") : "-"}
                    </TableCell>
                    <TableCell>
                      {cert.lastSeenAt
                        ? format(new Date(cert.lastSeenAt), "MMM dd, yyyy HH:mm")
                        : "-"}
                    </TableCell>
                  </TableRow>
                );
              })}
            </TableBody>
          </Table>
        )}
      </CardContent>
    </Card>
  );
};