diff --git a/frontend/src/hooks/api/kms/types.ts b/frontend/src/hooks/api/kms/types.ts index 32c2fa32a..d75fb571c 100644 --- a/frontend/src/hooks/api/kms/types.ts +++ b/frontend/src/hooks/api/kms/types.ts @@ -186,7 +186,10 @@ export const AddExternalKmsGcpFormSchema = z.discriminatedUnion("formType", [ .merge(AddExternalKmsGcpFormSchemaStandardInputs) .merge(AddExternalKmsSchema.pick({ name: true, description: true })), z - .object({ formType: z.literal("updateGcpKms") }) + .object({ formType: z.literal("updateGcpKmsDetails") }) + .merge(AddExternalKmsSchema.pick({ name: true, description: true })), + z + .object({ formType: z.literal("updateGcpKmsCredentials") }) .merge(AddExternalKmsGcpFormSchemaStandardInputs) .merge(AddExternalKmsSchema.pick({ name: true, description: true })) ]); diff --git a/frontend/src/pages/organization/SettingsPage/components/OrgEncryptionTab/GcpKmsForm.tsx b/frontend/src/pages/organization/SettingsPage/components/OrgEncryptionTab/GcpKmsForm.tsx index 7b80a33d2..5311936d8 100644 --- a/frontend/src/pages/organization/SettingsPage/components/OrgEncryptionTab/GcpKmsForm.tsx +++ b/frontend/src/pages/organization/SettingsPage/components/OrgEncryptionTab/GcpKmsForm.tsx @@ -81,6 +81,13 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props) const [isCredentialValid, setIsCredentialValid] = useState(false); const [keys, setKeys] = useState<{ value: string; label: string }[]>([]); + const getFormType = () => { + if (kms) { + return mode === "details" ? "updateGcpKmsDetails" : "updateGcpKmsCredentials"; + } + return "newGcpKms"; + }; + const { control, handleSubmit, @@ -93,7 +100,7 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props) } = useForm({ resolver: zodResolver(AddExternalKmsGcpFormSchema), defaultValues: { - formType: kms ? "updateGcpKms" : "newGcpKms", + formType: getFormType(), name: kms?.name ?? "", description: kms?.description ?? "", gcpRegion: kms @@ -114,6 +121,7 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props) currentOrg.id, ExternalKmsProvider.Gcp ); + const { mutateAsync: fetchGcpKeys, isPending: isFetchGcpKeysLoading } = useExternalKmsFetchGcpKeys(currentOrg?.id); @@ -144,11 +152,11 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props) // handles the form submission const handleGcpKmsFormSubmit = async (data: AddExternalKmsGcpFormSchemaType) => { - const { name, description, gcpRegion: gcpRegionObject, keyObject } = data; + const { name, description, formType } = data; try { if (kms) { - if (mode === "details") { + if (formType === "updateGcpKmsDetails") { await updateGcpExternalKms({ kmsId: kms.id, name, @@ -159,7 +167,9 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props) text: "Successfully updated GCP External KMS Details", type: "success" }); - } else if (mode === "credentials") { + } else if (formType === "updateGcpKmsCredentials") { + const { gcpRegion: gcpRegionObject, keyObject } = data; + const gcpRegion = gcpRegionObject?.value; if (!gcpRegion) { setError("gcpRegion", { @@ -192,7 +202,9 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props) type: "success" }); } - } else { + } else if (formType === "newGcpKms") { + const { gcpRegion: gcpRegionObject, keyObject } = data; + const gcpRegion = gcpRegionObject?.value; if (!gcpRegion) { setError("gcpRegion", { @@ -220,7 +232,7 @@ export const GcpKmsForm = ({ onCompleted, onCancel, kms, mode = "full" }: Props) type: ExternalKmsProvider.Gcp, inputs: { gcpRegion, - keyName: keyObject?.value, + keyName: keyObject?.value ?? "", credential: credentialJson } }