Skip to content

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

4.5 KB
Raw Blame
import { useState } from "react";
import { PlusIcon } from "lucide-react";

import { createNotification } from "@app/components/notifications";
import { ProjectPermissionCan } from "@app/components/permissions";
import {
  Button,
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
  ConfirmationAlertDialog,
  DocumentationLinkBadge
} from "@app/components/v3";
import {
  ProjectPermissionCertificateProfileActions,
  ProjectPermissionSub
} from "@app/context/ProjectPermissionContext/types";
import {
  TCertificateProfileWithDetails,
  useDeleteCertificateProfile
} from "@app/hooks/api/certificateProfiles";

import { PkiDocsUrls } from "../../../pki-docs-urls";
import { CreateProfileModal } from "./CreateProfileModal";
import { ProfileList } from "./ProfileList";

export const CertificateProfilesTab = () => {
  const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
  const [isEditModalOpen, setIsEditModalOpen] = useState(false);
  const [isCloneModalOpen, setIsCloneModalOpen] = useState(false);
  const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
  const [selectedProfile, setSelectedProfile] = useState<TCertificateProfileWithDetails | null>(
    null
  );
  const deleteProfile = useDeleteCertificateProfile();

  const handleEditProfile = (profile: TCertificateProfileWithDetails) => {
    setSelectedProfile(profile);
    setIsEditModalOpen(true);
  };

  const handleCloneProfile = (profile: TCertificateProfileWithDetails) => {
    setSelectedProfile(profile);
    setIsCloneModalOpen(true);
  };

  const handleDeleteProfile = (profile: TCertificateProfileWithDetails) => {
    setSelectedProfile(profile);
    setIsDeleteModalOpen(true);
  };

  const handleDeleteConfirm = async () => {
    if (!selectedProfile) return;

    await deleteProfile.mutateAsync({
      profileId: selectedProfile.id
    });
    setIsDeleteModalOpen(false);
    setSelectedProfile(null);
    createNotification({
      text: `Certificate profile "${selectedProfile.slug}" deleted successfully`,
      type: "success"
    });
  };

  return (
    <Card>
      <CardHeader>
        <CardTitle>
          Certificate Profiles
          <DocumentationLinkBadge href={PkiDocsUrls.settings.profiles} />
        </CardTitle>
        <CardDescription>
          Reusable presets for issuing certificates. Each profile combines a certificate authority
          (who signs the certificate) with a policy (the rules and settings applied).
        </CardDescription>
        <CardAction>
          <ProjectPermissionCan
            I={ProjectPermissionCertificateProfileActions.Create}
            a={ProjectPermissionSub.CertificateProfiles}
          >
            {(isAllowed) => (
              <Button
                variant="project"
                isDisabled={!isAllowed}
                onClick={() => setIsCreateModalOpen(true)}
              >
                <PlusIcon />
                Create Profile
              </Button>
            )}
          </ProjectPermissionCan>
        </CardAction>
      </CardHeader>
      <CardContent>
        <ProfileList
          onEditProfile={handleEditProfile}
          onCloneProfile={handleCloneProfile}
          onDeleteProfile={handleDeleteProfile}
        />
      </CardContent>

      <CreateProfileModal isOpen={isCreateModalOpen} onClose={() => setIsCreateModalOpen(false)} />

      {selectedProfile && (
        <>
          <CreateProfileModal
            isOpen={isEditModalOpen}
            onClose={() => {
              setIsEditModalOpen(false);
              setSelectedProfile(null);
            }}
            profile={selectedProfile}
            mode="edit"
          />

          <CreateProfileModal
            isOpen={isCloneModalOpen}
            onClose={() => {
              setIsCloneModalOpen(false);
              setSelectedProfile(null);
            }}
            profile={selectedProfile}
            mode="clone"
          />

          <ConfirmationAlertDialog
            isOpen={isDeleteModalOpen}
            onOpenChange={(isOpen) => {
              setIsDeleteModalOpen(isOpen);
              if (!isOpen) {
                setSelectedProfile(null);
              }
            }}
            title={`Delete Certificate Profile ${selectedProfile.slug}?`}
            description="This permanently removes the profile. Certificates already issued from it are not affected."
            confirmKey={selectedProfile.slug}
            confirmLabel="Delete Profile"
            onConfirm={handleDeleteConfirm}
          />
        </>
      )}
    </Card>
  );
};