Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

4.8 KB
Raw Blame
import { Controller, useFormContext } from "react-hook-form";
import { InfoIcon } from "lucide-react";

import { TSecretRotationV2Form } from "@app/components/secret-rotations-v2/forms/schemas";
import { FieldLabelWithTooltip } from "@app/components/secret-rotations-v2/forms/shared";
import {
  Combobox,
  Field,
  FieldError,
  FieldFeedback,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import { useSnowflakeConnectionListUsers } from "@app/hooks/api/appConnections/snowflake";
import { SecretRotation } from "@app/hooks/api/secretRotationsV2";

const RSA_MODULUS_LENGTHS = [2048, 4096] as const;

export const SnowflakeUserKeyPairRotationParametersFields = () => {
  const { control, watch } = useFormContext<
    TSecretRotationV2Form & {
      type: SecretRotation.SnowflakeUserKeyPair;
    }
  >();

  const connectionId = watch("connection.id");
  const isUpdate = Boolean(watch("id"));

  const { data: users, isPending: isUsersPending } = useSnowflakeConnectionListUsers(connectionId, {
    enabled: Boolean(connectionId) && !isUpdate
  });

  return (
    <>
      <Controller
        name="parameters.username"
        control={control}
        render={({ field: { value, onChange, onBlur }, fieldState: { error } }) => (
          <Field data-invalid={Boolean(error)}>
            <FieldLabelWithTooltip htmlFor="snowflake-user">User</FieldLabelWithTooltip>
            <Combobox
              id="snowflake-user"
              isLoading={isUsersPending && Boolean(connectionId) && !isUpdate}
              isDisabled={!connectionId || isUpdate}
              value={users?.find((user) => user.name === value) ?? (value ? { name: value } : null)}
              onBlur={onBlur}
              onValueChange={(selectedUser) => onChange(selectedUser?.name ?? null)}
              creation={{
                mode: "inline",
                onCreate: (inputValue) => onChange(inputValue),
                formatLabel: (inputValue) => `Add user "${inputValue}"`,
                emptyLabel: "Add user"
              }}
              options={users}
              placeholder="Select or add a new user..."
              getOptionLabel={(option) => option.name}
              getOptionValue={(option) => option.name}
              isError={Boolean(error)}
              aria-describedby="snowflake-user-feedback"
            />
            <FieldFeedback
              id="snowflake-user-feedback"
              description={
                isUpdate ? (
                  "Cannot be updated."
                ) : (
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <button type="button" className="inline-flex items-center gap-1 text-left">
                        <span>Don&#39;t see the user you&#39;re looking for?</span>
                        <InfoIcon className="size-3 shrink-0 text-muted" />
                      </button>
                    </TooltipTrigger>
                    <TooltipContent className="max-w-md">
                      Select an existing Snowflake user, or type a new username to create it. New
                      users are created as key-pair-only SERVICE users. Ensure your connection&#39;s
                      role has the privileges to list users, alter the user&#39;s RSA public key,
                      and (for new users) create users. If the keys are rotated the user will
                      continue to exist.
                    </TooltipContent>
                  </Tooltip>
                )
              }
              error={error?.message}
            />
          </Field>
        )}
      />
      <Controller
        name="parameters.modulusLength"
        control={control}
        defaultValue={2048}
        render={({ field: { value, onChange }, fieldState: { error } }) => (
          <Field data-invalid={Boolean(error)}>
            <FieldLabelWithTooltip
              tooltip="The size in bits of the generated RSA key pairs. 2048-bit keys are faster to generate and use; 4096-bit keys provide a larger security margin."
              tooltipClassName="max-w-sm"
            >
              RSA Modulus Length
            </FieldLabelWithTooltip>
            <Select value={String(value)} onValueChange={(val) => onChange(Number(val))}>
              <SelectTrigger className="w-full" isError={Boolean(error)}>
                <SelectValue />
              </SelectTrigger>
              <SelectContent position="popper">
                {RSA_MODULUS_LENGTHS.map((length) => (
                  <SelectItem value={String(length)} key={length}>
                    {length}-bit
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
            <FieldError>{error?.message}</FieldError>
          </Field>
        )}
      />
    </>
  );
};