From b11a33d3dab424236f888ee7fc405763decd66ad Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Wed, 6 Nov 2024 13:46:03 +0100 Subject: [PATCH] feat: reset credentials for gcp, azure and kubernetes if test connection fail --- actions/providers/providers.ts | 22 ++++ .../workflow/forms/test-connection-form.tsx | 116 ++++++++++++++---- lib/helper.ts | 1 + 3 files changed, 113 insertions(+), 26 deletions(-) diff --git a/actions/providers/providers.ts b/actions/providers/providers.ts index 3a651783df..cf079892b4 100644 --- a/actions/providers/providers.ts +++ b/actions/providers/providers.ts @@ -274,6 +274,28 @@ export const checkConnectionProvider = async (formData: FormData) => { } }; +export const deleteCredentials = async (secretId: string) => { + const session = await auth(); + const keyServer = process.env.API_BASE_URL; + const url = new URL(`${keyServer}/providers/secrets/${secretId}`); + + try { + const response = await fetch(url.toString(), { + method: "DELETE", + headers: { + Authorization: `Bearer ${session?.accessToken}`, + }, + }); + const data = await response.json(); + revalidatePath("/providers"); + return parseStringify(data); + } catch (error) { + return { + error: getErrorMessage(error), + }; + } +}; + export const deleteProvider = async (formData: FormData) => { const session = await auth(); const keyServer = process.env.API_BASE_URL; diff --git a/components/providers/workflow/forms/test-connection-form.tsx b/components/providers/workflow/forms/test-connection-form.tsx index 2c0d6375b6..6341faaf34 100644 --- a/components/providers/workflow/forms/test-connection-form.tsx +++ b/components/providers/workflow/forms/test-connection-form.tsx @@ -8,9 +8,12 @@ import { useState } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; -import { checkConnectionProvider } from "@/actions/providers/providers"; +import { + checkConnectionProvider, + deleteCredentials, +} from "@/actions/providers"; import { getTask } from "@/actions/task/tasks"; -import { SaveIcon } from "@/components/icons"; +import { CheckIcon, SaveIcon } from "@/components/icons"; import { useToast } from "@/components/ui"; import { CustomButton } from "@/components/ui/custom"; import { Form } from "@/components/ui/form"; @@ -29,12 +32,23 @@ export const TestConnectionForm = ({ providerData: { data: { id: string; + type: string; attributes: { connection: { - connected: boolean; + connected: boolean | null; + last_checked_at: string | null; }; provider: "aws" | "azure" | "gcp" | "kubernetes"; alias: string; + scanner_args: Record; + }; + relationships: { + secret: { + data: { + type: string; + id: string; + } | null; + }; }; }; }; @@ -43,13 +57,14 @@ export const TestConnectionForm = ({ const router = useRouter(); const providerType = searchParams.type; const providerId = searchParams.id; - + console.log({ providerData }, "providerData from test connection form"); const formSchema = testConnectionFormSchema; const [apiErrorMessage, setApiErrorMessage] = useState(null); const [connectionStatus, setConnectionStatus] = useState<{ connected: boolean; error: string | null; } | null>(null); + const [isResettingCredentials, setIsResettingCredentials] = useState(false); const form = useForm({ resolver: zodResolver(formSchema), @@ -118,6 +133,41 @@ export const TestConnectionForm = ({ } }; + const onResetCredentials = async () => { + setIsResettingCredentials(true); + // Check if provider has no credentials + const providerSecretId = + providerData?.data?.relationships?.secret?.data?.id; + const hasNoCredentials = !providerSecretId; + console.log({ providerSecretId }, "providerSecretId"); + console.log({ hasNoCredentials }, "hasNoCredentials"); + + if (hasNoCredentials) { + // If no credentials, redirect to add credentials page + console.log("no credentials"); + // router.push( + // `/providers/add-credentials?type=${providerType}&id=${providerId}`, + // ); + return; + } + + // If has credentials, delete them first + try { + // This function will need to be implemented + await deleteCredentials(providerSecretId); + // After successful deletion, redirect to add credentials page + console.log("deleted credentials with success"); + router.push( + `/providers/add-credentials?type=${providerType}&id=${providerId}`, + ); + } catch (error) { + // Handle error appropriately + console.error("Failed to delete credentials:", error); + } finally { + setIsResettingCredentials(false); + } + }; + return (
{apiErrorMessage && ( -
+

{`Provider ID ${apiErrorMessage.toLowerCase()}. Please check and try again.`}

)} {connectionStatus && !connectionStatus.connected && ( -
-
- + <> +
+
+ +
+
+

+ {connectionStatus.error || "Unknown error"} +

+
-
-

- {connectionStatus.error || "Unknown error"} -

-
-
+

+ It seems there was an issue with your credentials. Please review + your credentials and try again. +

+ )} Back to providers ) : connectionStatus?.error ? ( - } + isDisabled={isResettingCredentials} > - - Handle credentials - + {isResettingCredentials ? ( + <>Loading + ) : ( + Reset credentials + )} + ) : (