Skip to content

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

11.0 KB
Raw Blame
import { Control, Controller, UseFormSetValue } from "react-hook-form";
import { Info } from "lucide-react";

import {
  Combobox,
  Field,
  FieldError,
  FieldLabel,
  Input,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import { APP_CONNECTION_MAP } from "@app/helpers/appConnections";
import { TAvailableAppConnection } from "@app/hooks/api/appConnections";
import { AcmeDnsProvider } from "@app/hooks/api/ca";
import {
  ACME_DNS_PROVIDER_APP_CONNECTION_MAP,
  ACME_DNS_PROVIDER_NAME_MAP
} from "@app/hooks/api/ca/constants";

import { AppConnectionSelectField } from "./AppConnectionSelectField";
import { REQUIRED_EAB_DIRECTORIES } from "./constants";
import { GcpCloudDnsZoneFields } from "./GcpCloudDnsZoneFields";
import { FormData } from "./schema";

export type TAcmeDnsZone = { id: string; name: string };

type Props = {
  control: Control<FormData>;
  isExistingCa: boolean;
  dnsProvider?: AcmeDnsProvider;
  directoryUrl?: string;
  dnsAppConnection: { id: string; name: string };
  availableConnections: TAvailableAppConnection[];
  isPending: boolean;
  zones: TAcmeDnsZone[];
  isZonesPending: boolean;
  setValue: UseFormSetValue<FormData>;
  onDnsSelectionChange: () => void;
};

export const AcmeFields = ({
  control,
  isExistingCa,
  dnsProvider,
  directoryUrl,
  dnsAppConnection,
  availableConnections,
  isPending,
  zones,
  isZonesPending,
  setValue,
  onDnsSelectionChange
}: Props) => (
  <>
    <Controller
      control={control}
      name="configuration.dnsProviderConfig.provider"
      defaultValue={AcmeDnsProvider.ROUTE53}
      render={({ field: { onChange, value }, fieldState: { error } }) => (
        <Field className="mb-4">
          <FieldLabel>DNS Provider</FieldLabel>
          <Select
            value={value}
            onValueChange={(val) => {
              onChange(val);
              setValue("configuration.dnsAppConnection", { id: "", name: "" });
              onDnsSelectionChange();
            }}
            disabled={isExistingCa}
          >
            <SelectTrigger className="w-full" isError={Boolean(error)}>
              <SelectValue />
            </SelectTrigger>
            <SelectContent position="popper">
              {Object.values(AcmeDnsProvider).map((provider) => (
                <SelectItem value={String(provider)} key={provider}>
                  {ACME_DNS_PROVIDER_NAME_MAP[provider]}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
          <FieldError errors={[error]} />
        </Field>
      )}
    />
    <AppConnectionSelectField
      control={control}
      name="configuration.dnsAppConnection"
      label="DNS App Connection"
      onAfterChange={onDnsSelectionChange}
      options={availableConnections}
      createApp={dnsProvider ? ACME_DNS_PROVIDER_APP_CONNECTION_MAP[dnsProvider] : undefined}
      isLoading={isPending}
      tooltip={
        dnsProvider
          ? `${ACME_DNS_PROVIDER_NAME_MAP[dnsProvider]} uses the ${APP_CONNECTION_MAP[ACME_DNS_PROVIDER_APP_CONNECTION_MAP[dnsProvider]].name} App Connection. You can create one in the Organization Settings page.`
          : "Select a DNS provider first"
      }
    />
    {dnsProvider === AcmeDnsProvider.ROUTE53 && (
      <Controller
        control={control}
        defaultValue=""
        name="configuration.dnsProviderConfig.hostedZoneId"
        render={({ field, fieldState: { error } }) => (
          <Field className="mb-4">
            <FieldLabel>
              Hosted Zone ID <span className="text-danger">*</span>
            </FieldLabel>
            <Input {...field} placeholder="Z040441124N1GOOMCQYX1" isError={Boolean(error)} />
            <FieldError errors={[error]} />
          </Field>
        )}
      />
    )}
    {dnsProvider &&
      dnsProvider !== AcmeDnsProvider.ROUTE53 &&
      dnsProvider !== AcmeDnsProvider.GcpCloudDns && (
        <Controller
          name="configuration.dnsProviderConfig.hostedZoneId"
          control={control}
          render={({ field: { value, onChange, ref }, fieldState: { error } }) => (
            <Field className="mb-4">
              <FieldLabel>
                Zone <span className="text-danger">*</span>
                <Tooltip>
                  <TooltipTrigger asChild>
                    <Info />
                  </TooltipTrigger>
                  <TooltipContent className="max-w-md">
                    The DNS zone where Infisical creates the challenge TXT records. It must contain
                    the domains you request certificates for.
                  </TooltipContent>
                </Tooltip>
              </FieldLabel>
              <Combobox
                isClearable={false}
                includeMissingSelectedOptions={false}
                ref={ref}
                isLoading={isZonesPending && !!dnsAppConnection.id}
                isDisabled={!dnsAppConnection.id}
                value={
                  zones.find((zone) => zone.id === value) ||
                  (value ? { id: value, name: value } : null)
                }
                onValueChange={(option) => {
                  onChange(option.id);
                }}
                options={zones}
                placeholder="Select a zone..."
                getOptionLabel={(option) => option.name.replace(/\.$/, "")}
                getOptionValue={(option) => option.id}
                isError={Boolean(error)}
              />
              <FieldError errors={[error]} />
            </Field>
          )}
        />
      )}
    {dnsProvider === AcmeDnsProvider.GcpCloudDns && (
      <GcpCloudDnsZoneFields
        key={dnsAppConnection.id}
        control={control}
        connectionId={dnsAppConnection.id}
      />
    )}
    <Controller
      control={control}
      defaultValue=""
      name="configuration.directoryUrl"
      render={({ field, fieldState: { error } }) => (
        <Field className="mb-4">
          <FieldLabel>
            Directory URL <span className="text-danger">*</span>
            <Tooltip>
              <TooltipTrigger asChild>
                <Info />
              </TooltipTrigger>
              <TooltipContent className="max-w-md">
                The ACME directory of the CA that issues your certificates, such as Let&apos;s
                Encrypt or ZeroSSL.
              </TooltipContent>
            </Tooltip>
          </FieldLabel>
          <Input
            {...field}
            placeholder="https://acme-v02.api.letsencrypt.org/directory"
            isError={Boolean(error)}
          />
          {field.value?.startsWith("http://") && (
            <p className="mt-2 flex items-center gap-1.5 text-xs text-warning">
              <Info className="size-3.5" />
              This directory is served over plain HTTP. Account credentials and EAB keys will be
              sent unencrypted. Use HTTPS unless this is a local test directory.
            </p>
          )}
          <FieldError errors={[error]} />
        </Field>
      )}
    />
    <Controller
      control={control}
      defaultValue=""
      name="configuration.accountEmail"
      render={({ field, fieldState: { error } }) => (
        <Field className="mb-4">
          <FieldLabel>
            Account Email <span className="text-danger">*</span>
            <Tooltip>
              <TooltipTrigger asChild>
                <Info />
              </TooltipTrigger>
              <TooltipContent className="max-w-md">
                The email address registered with the CA for this ACME account. CAs such as
                Let&apos;s Encrypt use it for expiry and account notices.
              </TooltipContent>
            </Tooltip>
          </FieldLabel>
          <Input {...field} placeholder="[email protected]" isError={Boolean(error)} />
          <FieldError errors={[error]} />
        </Field>
      )}
    />
    <Controller
      control={control}
      defaultValue=""
      name="configuration.eabKid"
      render={({ field, fieldState: { error } }) => {
        const eabRequired = REQUIRED_EAB_DIRECTORIES.includes(directoryUrl || "");
        return (
          <Field className="mb-4">
            <FieldLabel>
              EAB Key Identifier (KID){" "}
              {eabRequired ? (
                <span className="text-danger">*</span>
              ) : (
                <span className="text-muted">(optional)</span>
              )}
              <Tooltip>
                <TooltipTrigger asChild>
                  <Info />
                </TooltipTrigger>
                <TooltipContent className="max-w-md">
                  The key identifier your CA issued for External Account Binding. It ties this ACME
                  account to your existing account with the CA. Required by CAs such as DigiCert,
                  ZeroSSL and Google Trust Services.
                </TooltipContent>
              </Tooltip>
            </FieldLabel>
            <Input
              {...field}
              placeholder="abc123def456ghi789jkl012mno345pqr678stu901vwx234yz"
              isError={Boolean(error)}
            />
            <FieldError errors={[error]} />
          </Field>
        );
      }}
    />
    <Controller
      control={control}
      defaultValue=""
      name="configuration.eabHmacKey"
      render={({ field, fieldState: { error } }) => (
        <Field className="mb-4">
          <FieldLabel>
            EAB HMAC Key <span className="text-muted">(optional)</span>
            <Tooltip>
              <TooltipTrigger asChild>
                <Info />
              </TooltipTrigger>
              <TooltipContent className="max-w-md">
                The base64-encoded HMAC key issued alongside the EAB Key Identifier. Provide both or
                neither.
              </TooltipContent>
            </Tooltip>
          </FieldLabel>
          <Input
            type="password"
            autoComplete="new-password"
            {...field}
            isError={Boolean(error)}
            placeholder={
              isExistingCa
                ? undefined
                : "dGhpc2lzYW5leGFtcGxlaG1hY2tleWZvcmRpZ2ljZXJ0YWNtZXRlc3RpbmcxMjM0NTY3ODkw"
            }
          />
          <FieldError errors={[error]} />
        </Field>
      )}
    />
    <Controller
      control={control}
      defaultValue=""
      name="configuration.dnsResolver"
      render={({ field, fieldState: { error } }) => (
        <Field className="mb-4">
          <FieldLabel>
            DNS Resolver IP <span className="text-muted">(optional)</span>
            <Tooltip>
              <TooltipTrigger asChild>
                <Info />
              </TooltipTrigger>
              <TooltipContent className="max-w-md">
                A custom DNS resolver IP address used to verify DNS propagation during ACME
                challenges. Must be a valid IP (e.g. 8.8.8.8). Leave empty to use the system
                default.
              </TooltipContent>
            </Tooltip>
          </FieldLabel>
          <Input {...field} placeholder="8.8.8.8" isError={Boolean(error)} />
          <FieldError errors={[error]} />
        </Field>
      )}
    />
  </>
);