From 258d18112cf72e0dd6b146cab6c6bd6b4e24d873 Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Sun, 3 Nov 2024 11:31:47 +0100 Subject: [PATCH] feat: add action to getTask and implement the last step in the workflow - launch scan --- actions/task/index.ts | 1 + actions/task/tasks.ts | 24 ++++ .../(set-up-provider)/launch-scan/page.tsx | 33 +++++ components/providers/workflow/forms/index.ts | 1 + .../workflow/forms/launch-scan-form.tsx | 113 ++++++++++++++++++ .../workflow/forms/via-credentials-form.tsx | 4 +- types/formSchemas.ts | 11 ++ 7 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 actions/task/index.ts create mode 100644 actions/task/tasks.ts create mode 100644 app/(prowler)/providers/(set-up-provider)/launch-scan/page.tsx create mode 100644 components/providers/workflow/forms/launch-scan-form.tsx diff --git a/actions/task/index.ts b/actions/task/index.ts new file mode 100644 index 0000000000..078ba8fc62 --- /dev/null +++ b/actions/task/index.ts @@ -0,0 +1 @@ +export * from "./tasks"; diff --git a/actions/task/tasks.ts b/actions/task/tasks.ts new file mode 100644 index 0000000000..fdf75c7cea --- /dev/null +++ b/actions/task/tasks.ts @@ -0,0 +1,24 @@ +"use server"; + +import { auth } from "@/auth.config"; +import { getErrorMessage, parseStringify } from "@/lib"; + +export const getTask = async (taskId: string) => { + const session = await auth(); + + const keyServer = process.env.API_BASE_URL; + const url = new URL(`${keyServer}/tasks/${taskId}`); + + try { + const response = await fetch(url.toString(), { + headers: { + Accept: "application/vnd.api+json", + Authorization: `Bearer ${session?.accessToken}`, + }, + }); + const data = await response.json(); + return parseStringify(data); + } catch (error) { + return { error: getErrorMessage(error) }; + } +}; diff --git a/app/(prowler)/providers/(set-up-provider)/launch-scan/page.tsx b/app/(prowler)/providers/(set-up-provider)/launch-scan/page.tsx new file mode 100644 index 0000000000..0fd9edb08c --- /dev/null +++ b/app/(prowler)/providers/(set-up-provider)/launch-scan/page.tsx @@ -0,0 +1,33 @@ +import { redirect } from "next/navigation"; +import React from "react"; + +import { getProvider } from "@/actions/providers"; +import { LaunchScanForm } from "@/components/providers/workflow/forms"; + +interface Props { + searchParams: { type: string; id: string; connected: boolean }; +} + +export default async function LaunchScanPage({ searchParams }: Props) { + const providerId = searchParams.id; + const connectedSearchParam = searchParams.connected; + + if (!providerId) { + redirect("/providers/connect-account"); + } + + const formData = new FormData(); + formData.append("id", providerId); + + const providerData = await getProvider(formData); + + const isConnected = providerData?.data?.attributes?.connection?.connected; + + if (!isConnected || connectedSearchParam !== isConnected.toString()) { + redirect("/providers/connect-account"); + } + + return ( + + ); +} diff --git a/components/providers/workflow/forms/index.ts b/components/providers/workflow/forms/index.ts index 539efb24ed..cf0417ed09 100644 --- a/components/providers/workflow/forms/index.ts +++ b/components/providers/workflow/forms/index.ts @@ -1,4 +1,5 @@ export * from "./connect-account-form"; +export * from "./launch-scan-form"; export * from "./radio-group-aws-via-credentials-form"; export * from "./test-connection-form"; export * from "./via-credentials-form"; diff --git a/components/providers/workflow/forms/launch-scan-form.tsx b/components/providers/workflow/forms/launch-scan-form.tsx new file mode 100644 index 0000000000..478cef5f62 --- /dev/null +++ b/components/providers/workflow/forms/launch-scan-form.tsx @@ -0,0 +1,113 @@ +"use client"; + +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; + +import { SaveIcon } from "@/components/icons"; +// import { useToast } from "@/components/ui"; +import { CustomButton } from "@/components/ui/custom"; +import { Form } from "@/components/ui/form"; +import { ProviderProps } from "@/types"; // Asegúrate de importar la interfaz correcta +import { launchScanFormSchema } from "@/types/formSchemas"; + +import { ProviderInfo } from "../../provider-info"; + +type FormValues = z.infer>; + +interface LaunchScanFormProps { + searchParams: { type: string; id: string; connected: boolean }; + providerData: { + data: { + type: string; + id: string; + attributes: ProviderProps["attributes"]; + }; + }; +} + +export const LaunchScanForm = ({ + searchParams, + providerData, +}: LaunchScanFormProps) => { + const providerType = searchParams.type; + const providerId = searchParams.id; + const connected = searchParams.connected; + + // const [apiErrorMessage, setApiErrorMessage] = useState(null); + + const formSchema = launchScanFormSchema(); + + const form = useForm({ + resolver: zodResolver(formSchema), + defaultValues: { + providerId, + providerType, + scannerArgs: { + checksToExecute: [], + }, + }, + }); + + console.log({ providerData }); + + const isLoading = form.formState.isSubmitting; + + const onSubmitClient = async (values: FormValues) => { + console.log({ values }, "values from test connection form"); + + if (connected) { + console.log("connected"); + } else { + console.log("not connected"); + } + }; + + return ( +
+ +
+
+ Launch scan +
+
+ Launch the scan now or schedule it for a later date and time. +
+
+ + {/* {apiErrorMessage && ( +
+

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

+
+ )} */} + + + + + + +
+ } + > + {isLoading ? <>Loading : Launch scan} + +
+ + + ); +}; diff --git a/components/providers/workflow/forms/via-credentials-form.tsx b/components/providers/workflow/forms/via-credentials-form.tsx index 5a6524ce0a..fb2507e304 100644 --- a/components/providers/workflow/forms/via-credentials-form.tsx +++ b/components/providers/workflow/forms/via-credentials-form.tsx @@ -152,7 +152,9 @@ export const ViaCredentialsForm = ({ } }); } else { - router.push(`/providers/test-connection?id=${providerId}`); + router.push( + `/providers/test-connection?type=${providerType}&id=${providerId}`, + ); } }; diff --git a/types/formSchemas.ts b/types/formSchemas.ts index da3dd3d8d9..30877d5894 100644 --- a/types/formSchemas.ts +++ b/types/formSchemas.ts @@ -108,6 +108,17 @@ export const testConnectionFormSchema = z.object({ providerId: z.string(), }); +export const launchScanFormSchema = () => + z.object({ + providerId: z.string(), + providerType: z.string(), + scannerArgs: z + .object({ + checksToExecute: z.array(z.string()).optional(), + }) + .optional(), + }); + export const editProviderFormSchema = (currentAlias: string) => z.object({ alias: z