Skip to content

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

3.2 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,
  FieldFeedback,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import { useDatabricksConnectionListServicePrincipals } from "@app/hooks/api/appConnections/databricks";
import { SecretRotation } from "@app/hooks/api/secretRotationsV2";

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

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

  const { data: servicePrincipals, isPending: isServicePrincipalsPending } =
    useDatabricksConnectionListServicePrincipals(connectionId, {
      enabled: Boolean(connectionId)
    });

  return (
    <Controller
      name="parameters.servicePrincipalId"
      control={control}
      render={({ field: { value, onChange, onBlur }, fieldState: { error } }) => (
        <Field data-invalid={Boolean(error)}>
          <FieldLabelWithTooltip htmlFor="databricks-service-account-principal">
            Service Principal
          </FieldLabelWithTooltip>
          <Combobox
            id="databricks-service-account-principal"
            isLoading={isServicePrincipalsPending && Boolean(connectionId)}
            isDisabled={!connectionId}
            value={servicePrincipals?.find((sp) => sp.id === value) ?? null}
            onBlur={onBlur}
            onValueChange={(selectedSp) => {
              onChange(selectedSp.id);
              setValue("parameters.servicePrincipalName", selectedSp.name);
              setValue("parameters.clientId", selectedSp.clientId);
            }}
            isClearable={false}
            options={servicePrincipals}
            placeholder="Select a service principal..."
            getOptionLabel={(option) => option.name}
            getOptionValue={(option) => option.id}
            getOptionKeywords={(option) => [option.id]}
            isError={Boolean(error)}
            aria-describedby="databricks-service-account-principal-feedback"
          />
          <FieldFeedback
            id="databricks-service-account-principal-feedback"
            description={
              <Tooltip>
                <TooltipTrigger asChild>
                  <button type="button" className="inline-flex items-center gap-1 text-left">
                    <span>Don&#39;t see the service principal you&#39;re looking for?</span>
                    <InfoIcon className="size-3 shrink-0 text-muted" />
                  </button>
                </TooltipTrigger>
                <TooltipContent className="max-w-md">
                  Ensure that your connection has the necessary permissions to list and manage
                  service principals in your Databricks workspace.
                </TooltipContent>
              </Tooltip>
            }
            error={error?.message}
          />
        </Field>
      )}
    />
  );
};