import { useCallback } from "react"; import { Controller, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { UpgradePlanModal } from "@app/components/license/UpgradePlanModal"; import { createNotification } from "@app/components/notifications"; import { Button, FormControl, Select, SelectItem } from "@app/components/v2"; import { useSubscription } from "@app/context"; import { usePopUp } from "@app/hooks"; import { useUpdateServerEncryptionStrategy } from "@app/hooks/api"; import { RootKeyEncryptionStrategy, TGetServerRootKmsEncryptionDetails } from "@app/hooks/api/admin/types"; const formSchema = z.object({ encryptionStrategy: z.nativeEnum(RootKeyEncryptionStrategy) }); const strategies: Record = { [RootKeyEncryptionStrategy.Software]: "Software-based Encryption", [RootKeyEncryptionStrategy.HSM]: "Hardware Security Module (HSM)" }; type TForm = z.infer; type Props = { rootKmsDetails?: TGetServerRootKmsEncryptionDetails; }; export const EncryptionPanel = ({ rootKmsDetails }: Props) => { const { mutateAsync: updateEncryptionStrategy } = useUpdateServerEncryptionStrategy(); const { subscription } = useSubscription(); const { popUp, handlePopUpOpen, handlePopUpToggle } = usePopUp(["upgradePlan"] as const); const { control, handleSubmit, formState: { isSubmitting, isDirty } } = useForm({ resolver: zodResolver(formSchema), values: { encryptionStrategy: rootKmsDetails?.strategies?.find((s) => s.enabled)?.strategy ?? RootKeyEncryptionStrategy.Software } }); const onSubmit = useCallback(async (formData: TForm) => { if (!subscription) return; if (!subscription.hsm) { handlePopUpOpen("upgradePlan", { description: "Hardware Security Module's (HSM's), are only available on Enterprise plans." }); return; } try { await updateEncryptionStrategy(formData.encryptionStrategy); createNotification({ type: "success", text: "Encryption strategy updated successfully" }); } catch { createNotification({ type: "error", text: "Failed to update encryption strategy" }); } }, []); return ( <>
KMS Encryption Strategy
Select which type of encryption strategy you want to use for your KMS root key. HSM is supported on Enterprise plans.
{!!rootKmsDetails && ( ( )} /> )}
handlePopUpToggle("upgradePlan", isOpen)} text={(popUp.upgradePlan?.data as { description: string })?.description} /> ); };