Skip to content

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

9.8 KB
Raw Blame
import { useTranslation } from "react-i18next";
import { Link, useNavigate, useParams } from "@tanstack/react-router";
import { ChevronLeftIcon, EllipsisIcon } from "lucide-react";

import { createNotification } from "@app/components/notifications";
import { PageTitle } from "@app/components/PageTitle";
import { OrgPermissionCan } from "@app/components/permissions";
import {
  Alert,
  AlertDescription,
  AlertTitle,
  Button,
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
  ConfirmationAlertDialog,
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  OrgIcon
} from "@app/components/v3";
import { PageHeader } from "@app/components/v3/platform";
import { ROUTE_PATHS } from "@app/const/routes";
import {
  OrgPermissionActions,
  OrgPermissionIdentityActions,
  OrgPermissionSubjects,
  useOrganization
} from "@app/context";
import { useDeleteOrgIdentity, useGetOrgIdentityMembershipById } from "@app/hooks/api";
import { usePopUp } from "@app/hooks/usePopUp";
import { OrgAccessControlTabSections } from "@app/types/org";

import { IdentityAuthMethodModal } from "../AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityAuthMethodModal";
import { OrgIdentityModal } from "../AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/OrgIdentityModal";
import {
  IdentityAlertAction,
  IdentityAuthenticationSection,
  IdentityDetailsSection,
  IdentityProjectsSection
} from "./components";

const Page = () => {
  const navigate = useNavigate();
  const params = useParams({
    from: ROUTE_PATHS.Organization.IdentityDetailsByIDPage.id
  });
  const identityId = params.identityId as string;
  const { currentOrg, isSubOrganization } = useOrganization();
  const orgId = currentOrg?.id || "";
  const { data } = useGetOrgIdentityMembershipById(identityId);
  const { mutateAsync: deleteIdentity } = useDeleteOrgIdentity();
  const isAuthHidden = orgId !== data?.identity?.orgId;

  const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([
    "identity",
    "deleteIdentity",
    "identityAuthMethod",
    "upgradePlan",
    "viewAuthMethod"
  ] as const);

  const onDeleteIdentitySubmit = async (id: string) => {
    await deleteIdentity({
      identityId: id,
      orgId
    });

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

    handlePopUpClose("deleteIdentity");
    navigate({
      to: "/organizations/$orgId/access-management" as const,
      params: { orgId },
      search: {
        selectedTab: OrgAccessControlTabSections.Identities
      }
    });
  };

  const isScopeIdentity = data?.identity.orgId === currentOrg.id;

  return (
    <div className="@container mx-auto flex max-w-8xl flex-col">
      {data && (
        <>
          <Link
            to="/organizations/$orgId/access-management"
            params={{ orgId }}
            search={{
              selectedTab: OrgAccessControlTabSections.Identities
            }}
            className="mb-4 flex w-fit items-center gap-x-1 text-sm text-muted transition duration-100 hover:text-muted/80"
          >
            <ChevronLeftIcon size={16} />
            {isSubOrganization ? "Sub-" : ""}Organization Machine Identities
          </Link>
          <PageHeader
            scope={isSubOrganization ? "namespace" : "org"}
            description={`Configure and manage${isScopeIdentity ? " machine identity and " : " "}${isSubOrganization ? "sub-" : ""}organization access control`}
            title={data.identity.name}
          >
            {isScopeIdentity && !data.identity.projectId && (
              <IdentityAlertAction identityId={identityId} />
            )}
            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <Button variant="outline">
                  Options
                  <EllipsisIcon />
                </Button>
              </DropdownMenuTrigger>
              <DropdownMenuContent align="end">
                <DropdownMenuItem
                  onClick={() => {
                    navigator.clipboard.writeText(data.identity.id);
                    createNotification({
                      text: "Machine identity ID copied to clipboard",
                      type: "info"
                    });
                  }}
                >
                  Copy Machine Identity ID
                </DropdownMenuItem>
                <OrgPermissionCan
                  I={OrgPermissionActions.Delete}
                  a={OrgPermissionSubjects.Identity}
                >
                  {(isAllowed) => (
                    <DropdownMenuItem
                      variant="danger"
                      isDisabled={!isAllowed}
                      onClick={() =>
                        handlePopUpOpen("deleteIdentity", {
                          identityId: data.identity.id,
                          name: data.identity.name
                        })
                      }
                    >
                      {isScopeIdentity ? "Delete Machine Identity" : "Remove From Sub-Organization"}
                    </DropdownMenuItem>
                  )}
                </OrgPermissionCan>
              </DropdownMenuContent>
            </DropdownMenu>
          </PageHeader>
          <div className="flex flex-col gap-5 @4xl:flex-row">
            <IdentityDetailsSection
              isCurrentOrgIdentity={data.identity.orgId === currentOrg.id}
              identityId={identityId}
              handlePopUpOpen={handlePopUpOpen}
            />
            <div className="flex min-w-0 flex-1 flex-col gap-y-5">
              {isAuthHidden ? (
                <Card>
                  <CardHeader>
                    <CardTitle>Authentication</CardTitle>
                    <CardDescription>Configure authentication methods</CardDescription>
                  </CardHeader>
                  <CardContent>
                    <Alert variant="org">
                      <OrgIcon />
                      <AlertTitle>Machine identity managed by organization</AlertTitle>
                      <AlertDescription>
                        <p>
                          This machine identity&apos;s authentication methods are managed by your{" "}
                          organization
                          <OrgPermissionCan
                            I={OrgPermissionIdentityActions.Read}
                            an={OrgPermissionSubjects.Identity}
                          >
                            {(isAllowed) =>
                              isAllowed ? (
                                <>
                                  <span>
                                    <br /> To make changes,{" "}
                                  </span>
                                  <Link
                                    to="/organizations/$orgId/identities/$identityId"
                                    className="inline-block cursor-pointer text-foreground underline underline-offset-2"
                                    params={{
                                      identityId,
                                      orgId: data.identity.orgId
                                    }}
                                  >
                                    go to organization access control
                                  </Link>
                                </>
                              ) : null
                            }
                          </OrgPermissionCan>
                          .
                        </p>
                      </AlertDescription>
                    </Alert>
                  </CardContent>
                </Card>
              ) : (
                <IdentityAuthenticationSection
                  identityId={identityId}
                  handlePopUpOpen={handlePopUpOpen}
                />
              )}
              <IdentityProjectsSection identityId={identityId} />
            </div>
          </div>
        </>
      )}
      <Dialog
        open={popUp?.identity?.isOpen}
        onOpenChange={(isOpen) => handlePopUpToggle("identity", isOpen)}
      >
        <DialogContent className="max-w-xl">
          <DialogHeader>
            <DialogTitle>
              {`${popUp?.identity?.data ? "Update" : "Create"} Machine Identity`}
            </DialogTitle>
            <DialogDescription>
              {popUp?.identity?.data
                ? "Update the identity's name, role, and metadata."
                : "Create a new machine identity in the organization."}
            </DialogDescription>
          </DialogHeader>
          <OrgIdentityModal popUp={popUp} handlePopUpToggle={handlePopUpToggle} />
        </DialogContent>
      </Dialog>
      <IdentityAuthMethodModal
        popUp={popUp}
        handlePopUpOpen={handlePopUpOpen}
        handlePopUpToggle={handlePopUpToggle}
      />
      <ConfirmationAlertDialog
        isOpen={popUp.deleteIdentity.isOpen}
        title={`Are you sure you want to delete ${
          (popUp?.deleteIdentity?.data as { name: string })?.name || ""
        }?`}
        description="This action is irreversible."
        onOpenChange={(isOpen) => handlePopUpToggle("deleteIdentity", isOpen)}
        confirmKey="confirm"
        onConfirm={() =>
          onDeleteIdentitySubmit(
            (popUp?.deleteIdentity?.data as { identityId: string })?.identityId
          )
        }
      />
    </div>
  );
};

export const IdentityDetailsByIDPage = () => {
  const { t } = useTranslation();
  return (
    <>
      <PageTitle title={t("page-titles.machine-identity")} />
      <link rel="icon" href="/infisical.ico" />
      <OrgPermissionCan
        passThrough={false}
        I={OrgPermissionIdentityActions.Read}
        a={OrgPermissionSubjects.Identity}
      >
        <Page />
      </OrgPermissionCan>
    </>
  );
};