import { ReactNode } from "react"; import { Controller, useForm } from "react-hook-form"; import { faCheck, faCopy } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { zodResolver } from "@hookform/resolvers/zod"; import { AnimatePresence, motion } from "framer-motion"; import ms from "ms"; import { z } from "zod"; import { TtlFormLabel } from "@app/components/features"; import { createNotification } from "@app/components/notifications"; import { Button, FormControl, IconButton, Input, SecretInput, Tooltip } from "@app/components/v2"; import { useTimedReset } from "@app/hooks"; import { useCreateDynamicSecretLease } from "@app/hooks/api"; import { DynamicSecretProviders } from "@app/hooks/api/dynamicSecret/types"; const OutputDisplay = ({ value, label, helperText }: { value: string; label: string; helperText?: ReactNode; }) => { const [copyText, isCopying, setCopyText] = useTimedReset({ initialState: "Copy to clipboard" }); return (
{ navigator.clipboard.writeText(value as string); setCopyText("Copied"); }} >
); }; const renderOutputForm = (provider: DynamicSecretProviders, data: unknown) => { if ( provider === DynamicSecretProviders.SqlDatabase || provider === DynamicSecretProviders.Cassandra || provider === DynamicSecretProviders.MongoAtlas || provider === DynamicSecretProviders.MongoDB ) { const { DB_PASSWORD, DB_USERNAME } = data as { DB_USERNAME: string; DB_PASSWORD: string }; return (
); } if (provider === DynamicSecretProviders.AwsIam) { const { USERNAME, ACCESS_KEY, SECRET_ACCESS_KEY } = data as { ACCESS_KEY: string; SECRET_ACCESS_KEY: string; USERNAME: string; }; return (
); } if (provider === DynamicSecretProviders.Redis) { const { DB_USERNAME, DB_PASSWORD } = data as { DB_USERNAME: string; DB_PASSWORD: string; }; return (
); } if (provider === DynamicSecretProviders.AwsElastiCache) { const { DB_USERNAME, DB_PASSWORD } = data as { DB_USERNAME: string; DB_PASSWORD: string; }; return (

Important: Copy these credentials now. You will not be able to see them again after you close the modal.

Please note that it may take a few minutes before the credentials are available for use.

} /> ); } if (provider === DynamicSecretProviders.RabbitMq) { const { DB_USERNAME, DB_PASSWORD } = data as { DB_USERNAME: string; DB_PASSWORD: string; }; return (
); } if (provider === DynamicSecretProviders.ElasticSearch) { const { DB_USERNAME, DB_PASSWORD } = data as { DB_USERNAME: string; DB_PASSWORD: string; }; return (
); } if (provider === DynamicSecretProviders.AzureEntraId) { const { email, password } = data as { email: string; password: string; }; return (
); } if (provider === DynamicSecretProviders.Ldap) { const { USERNAME, PASSWORD, DN_ARRAY } = data as { USERNAME: string; PASSWORD: string; DN_ARRAY: string[]; }; return (
); } if (provider === DynamicSecretProviders.SapHana) { const { DB_USERNAME, DB_PASSWORD } = data as { DB_USERNAME: string; DB_PASSWORD: string; }; return (
); } return null; }; const formSchema = z.object({ ttl: z.string().refine((val) => ms(val) > 0, "TTL must be a positive number") }); type TForm = z.infer; type Props = { onClose: () => void; dynamicSecretName: string; provider: DynamicSecretProviders; projectSlug: string; environment: string; secretPath: string; }; export const CreateDynamicSecretLease = ({ onClose, projectSlug, dynamicSecretName, provider, secretPath, environment }: Props) => { const { control, formState: { isSubmitting }, handleSubmit } = useForm({ resolver: zodResolver(formSchema), defaultValues: { ttl: "1h" } }); const createDynamicSecretLease = useCreateDynamicSecretLease(); const handleDynamicSecretLeaseCreate = async ({ ttl }: TForm) => { if (createDynamicSecretLease.isLoading) return; try { await createDynamicSecretLease.mutateAsync({ environmentSlug: environment, projectSlug, path: secretPath, ttl, dynamicSecretName }); createNotification({ type: "success", text: "Successfully leased dynamic secret" }); } catch (error) { console.log(error); createNotification({ type: "error", text: "Failed to lease dynamic secret" }); } }; const isOutputMode = Boolean(createDynamicSecretLease?.data); return (
{!isOutputMode && (
( } isError={Boolean(error?.message)} errorText={error?.message} > )} />
)} {isOutputMode && ( {renderOutputForm(provider, createDynamicSecretLease.data?.data)} )}
); };