Skip to content

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

7.8 KB
Raw Blame
import { subject } from "@casl/ability";
import { faChevronLeft } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Link, useNavigate, useParams, useSearch } from "@tanstack/react-router";
import { EllipsisIcon } from "lucide-react";

import { createNotification } from "@app/components/notifications";
import { PageTitle } from "@app/components/PageTitle";
import { ProjectPermissionCan } from "@app/components/permissions";
import {
  Button,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  PageHeader
} from "@app/components/v3";
import { ConfirmationAlertDialog } from "@app/components/v3/platform";
import { ROUTE_PATHS } from "@app/const/routes";
import {
  ProjectPermissionCertificateAuthorityActions,
  ProjectPermissionSub,
  useOrganization,
  useProject
} from "@app/context";
import { CaType, useDeleteCa, useGetCa } from "@app/hooks/api";
import { TInternalCertificateAuthority } from "@app/hooks/api/ca/types";
import { useGetCertificateProfileById } from "@app/hooks/api/certificateProfiles";
import { ProjectType } from "@app/hooks/api/projects/types";
import { usePopUp } from "@app/hooks/usePopUp";

import { CaInstallCertModal } from "../CertificateAuthoritiesPage/components/CaInstallCertModal";
import {
  CaCertDetailsSection,
  CaCertificatesSection,
  CaCrlsSection,
  CaDetailsSection,
  CaGenerateRootCertModal,
  CaIssuerUrlSection,
  CaRenewalModal,
  CaRevocationSection,
  CaSigningConfigSection
} from "./components";

const Page = () => {
  const { currentProject } = useProject();
  const { currentOrg } = useOrganization();
  const navigate = useNavigate();
  const params = useParams({
    from: ROUTE_PATHS.CertManager.CertAuthDetailsByIDPage.id
  });
  const { caId } = params as { caId: string };
  const search = useSearch({
    from: ROUTE_PATHS.CertManager.CertAuthDetailsByIDPage.id
  }) as {
    from?: "settings" | "profile";
    profileId?: string;
    profileFrom?: "settings" | "application";
    profileApplicationName?: string;
  };
  const { data } = useGetCa({
    caId,
    type: CaType.INTERNAL
  }) as { data: TInternalCertificateAuthority };

  const cameFromProfile = search.from === "profile" && Boolean(search.profileId);

  const { data: sourceProfile } = useGetCertificateProfileById({
    profileId: cameFromProfile && search.profileId ? search.profileId : ""
  });

  const { mutateAsync: deleteCa } = useDeleteCa();

  const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([
    "deleteCa",
    "installCaCert",
    "renewCa",
    "generateRootCaCert"
  ] as const);

  const onRemoveCaSubmit = async () => {
    if (!currentProject?.slug) return;

    await deleteCa({
      id: data.id,
      type: CaType.INTERNAL
    });

    createNotification({
      text: "Successfully deleted CA",
      type: "success"
    });

    handlePopUpClose("deleteCa");
    navigate({
      to: "/organizations/$orgId/cert-manager/certificate-authorities",
      params: {
        orgId: currentOrg.id
      }
    });
  };

  return (
    <div className="mx-auto flex flex-col justify-between bg-page text-foreground-inverse">
      {data && (
        <ProjectPermissionCan
          renderGuardBanner
          I={ProjectPermissionCertificateAuthorityActions.Read}
          a={subject(ProjectPermissionSub.CertificateAuthorities, {
            name: data.name
          })}
        >
          {(isAllowed) =>
            isAllowed ? (
              <div className="mx-auto mb-6 flex w-full max-w-8xl flex-col">
                <PageHeader
                  backLink={
                    cameFromProfile && search.profileId ? (
                      <Link
                        to="/organizations/$orgId/cert-manager/certificate-profiles/$profileId"
                        params={{
                          orgId: currentOrg.id,
                          profileId: search.profileId
                        }}
                        search={{
                          from: search.profileFrom,
                          applicationName: search.profileApplicationName
                        }}
                      >
                        <FontAwesomeIcon icon={faChevronLeft} />
                        {sourceProfile?.slug || "Certificate Profile"}
                      </Link>
                    ) : (
                      <Link
                        to="/organizations/$orgId/cert-manager/certificate-authorities"
                        params={{
                          orgId: currentOrg.id
                        }}
                      >
                        <FontAwesomeIcon icon={faChevronLeft} />
                        Certificate Authorities
                      </Link>
                    )
                  }
                  scope={ProjectType.CertificateManager}
                  description="Manage certificate authority"
                  title={data.name}
                >
                  <DropdownMenu>
                    <DropdownMenuTrigger asChild>
                      <Button variant="outline">
                        Options
                        <EllipsisIcon />
                      </Button>
                    </DropdownMenuTrigger>
                    <DropdownMenuContent align="end">
                      <ProjectPermissionCan
                        I={ProjectPermissionCertificateAuthorityActions.Delete}
                        a={subject(ProjectPermissionSub.CertificateAuthorities, {
                          name: data.name
                        })}
                      >
                        {(canDelete) => (
                          <DropdownMenuItem
                            variant="danger"
                            isDisabled={!canDelete}
                            onClick={() => handlePopUpOpen("deleteCa")}
                          >
                            Delete CA
                          </DropdownMenuItem>
                        )}
                      </ProjectPermissionCan>
                    </DropdownMenuContent>
                  </DropdownMenu>
                </PageHeader>
                <div className="flex flex-col gap-5 lg:flex-row">
                  <div className="w-full lg:max-w-[24rem]">
                    <CaDetailsSection caId={data.id} />
                    <CaSigningConfigSection caId={data.id} />
                    <CaCertDetailsSection caId={data.id} />
                  </div>
                  <div className="flex flex-1 flex-col gap-y-5">
                    <CaCertificatesSection
                      caId={data.id}
                      caName={data.name}
                      handlePopUpOpen={handlePopUpOpen}
                    />
                    <CaIssuerUrlSection caId={data.id} />
                    <CaCrlsSection caId={data.id} />
                    <CaRevocationSection caId={data.id} />
                  </div>
                </div>
              </div>
            ) : null
          }
        </ProjectPermissionCan>
      )}
      <CaRenewalModal popUp={popUp} handlePopUpToggle={handlePopUpToggle} />
      <CaInstallCertModal popUp={popUp} handlePopUpToggle={handlePopUpToggle} />
      <CaGenerateRootCertModal popUp={popUp} handlePopUpToggle={handlePopUpToggle} />
      <ConfirmationAlertDialog
        isOpen={popUp.deleteCa.isOpen}
        title={`Are you sure you want to remove the CA ${
          (popUp?.deleteCa?.data as { dn: string })?.dn || ""
        }?`}
        description="This action will delete other CAs and certificates below it in your CA hierarchy."
        onOpenChange={(isOpen) => handlePopUpToggle("deleteCa", isOpen)}
        confirmKey="confirm"
        onConfirm={onRemoveCaSubmit}
      />
    </div>
  );
};

export const CertAuthDetailsByIDPage = () => {
  return (
    <>
      <PageTitle title="Certificate Authority" />
      <link rel="icon" href="/infisical.ico" />
      <Page />
    </>
  );
};