Skip to content

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

10.1 KB
Raw Blame
import { useEffect, useState } from "react";
import { Controller, useFieldArray, useFormContext, useFormState, useWatch } from "react-hook-form";
import { CircleHelp, Plus, Trash2 } from "lucide-react";

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
  Button,
  Combobox,
  Field,
  FieldContent,
  FieldDescription,
  FieldError,
  FieldLabel,
  IconButton,
  Input,
  Label,
  Toggle,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import { useListAwsConnectionKmsKeys } from "@app/hooks/api/appConnections/aws";
import { SecretSync } from "@app/hooks/api/secretSyncs";

import { TSecretSyncForm } from "../schemas";
import { isAwsKmsKeyIdentifier } from "./aws-kms-key";

const AwsTagsEditor = () => {
  const { control } = useFormContext<
    TSecretSyncForm & { destination: SecretSync.AWSParameterStore }
  >();

  const tagFields = useFieldArray({
    control,
    name: "syncOptions.tags"
  });

  const canRemove = tagFields.fields.length > 1;

  return (
    <div className="mt-3 flex flex-col gap-3">
      <div className="flex flex-col gap-2">
        {tagFields.fields.map(({ id: tagFieldId }, i) => (
          <div key={tagFieldId} className="grid grid-cols-12 items-end gap-2">
            <div className="col-span-5">
              {i === 0 && <p className="mb-1 text-xs text-muted">Key</p>}
              <Controller
                control={control}
                name={`syncOptions.tags.${i}.key`}
                render={({ field, fieldState: { error } }) => (
                  <Input {...field} isError={Boolean(error)} className="h-8" />
                )}
              />
            </div>
            <div className="col-span-6">
              {i === 0 && <p className="mb-1 text-xs text-muted">Value (optional)</p>}
              <Controller
                control={control}
                name={`syncOptions.tags.${i}.value`}
                render={({ field, fieldState: { error } }) => (
                  <Input {...field} isError={Boolean(error)} className="h-8" />
                )}
              />
            </div>
            <div className="col-span-1 flex justify-end">
              <IconButton
                variant="ghost-muted"
                aria-label="Remove tag"
                size="sm"
                isDisabled={!canRemove}
                onClick={() => tagFields.remove(i)}
              >
                <Trash2 />
              </IconButton>
            </div>
          </div>
        ))}
      </div>
      <Button
        variant="outline"
        size="xs"
        type="button"
        className="w-fit"
        onClick={() => tagFields.append({ key: "", value: "" })}
      >
        <Plus />
        Add tag
      </Button>
    </div>
  );
};

const ITEM_VALUE = "aws-parameter-store-advanced";

export const AwsParameterStoreSyncOptionsFields = () => {
  const { control, watch, setValue } = useFormContext<
    TSecretSyncForm & { destination: SecretSync.AWSParameterStore }
  >();

  const region = watch("destinationConfig.region");
  const connectionId = useWatch({ name: "connection.id", control });
  const watchedTags = watch("syncOptions.tags");
  const watchedKeyId = watch("syncOptions.keyId");
  const watchedSyncMetadataAsTags = watch("syncOptions.syncSecretMetadataAsTags");

  const hasConfiguredOption =
    Boolean(watchedKeyId) || Array.isArray(watchedTags) || Boolean(watchedSyncMetadataAsTags);

  const { errors, submitCount } = useFormState({ control });
  const syncOptionsErrors = errors.syncOptions as Record<string, unknown> | undefined;
  const hasAccordionError = Boolean(
    syncOptionsErrors?.keyId ||
    syncOptionsErrors?.tags ||
    syncOptionsErrors?.syncSecretMetadataAsTags
  );

  const [openItem, setOpenItem] = useState<string>(
    hasConfiguredOption || hasAccordionError ? ITEM_VALUE : ""
  );

  useEffect(() => {
    if (hasAccordionError) setOpenItem(ITEM_VALUE);
  }, [hasAccordionError, submitCount]);

  const summaryParts = [
    watchedKeyId ? "Custom KMS key" : null,
    watchedSyncMetadataAsTags ? "Sync metadata as tags" : null,
    Array.isArray(watchedTags) ? "Resource tags" : null
  ].filter(Boolean);

  const summary = summaryParts.length ? summaryParts.join(" · ") : "All defaults";

  const { data: kmsKeys = [], isPending: isKmsKeysPending } = useListAwsConnectionKmsKeys(
    {
      connectionId,
      region,
      destination: SecretSync.AWSParameterStore
    },
    { enabled: Boolean(connectionId && region) }
  );

  return (
    <Accordion
      type="single"
      collapsible
      variant="ghost"
      value={openItem}
      onValueChange={setOpenItem}
    >
      <AccordionItem value={ITEM_VALUE}>
        <AccordionTrigger>
          <div className="flex w-0 flex-1 items-center justify-between gap-3">
            <span className="text-sm font-medium text-foreground">Advanced options</span>
            <span className="truncate text-xs text-muted">{summary}</span>
          </div>
        </AccordionTrigger>
        <AccordionContent className="flex flex-col">
          <Controller
            name="syncOptions.keyId"
            control={control}
            render={({ field: { value, onChange }, fieldState: { error } }) => (
              <Field className="mb-4">
                <FieldLabel className="flex items-center gap-1.5">
                  KMS Key
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <CircleHelp className="size-3 cursor-help text-muted" />
                    </TooltipTrigger>
                    <TooltipContent>
                      The AWS KMS key to encrypt parameters with: an alias, key ID, or ARN.
                    </TooltipContent>
                  </Tooltip>
                </FieldLabel>
                <FieldContent>
                  <Combobox
                    isLoading={isKmsKeysPending && Boolean(connectionId && region)}
                    isDisabled={!connectionId}
                    value={
                      value
                        ? (kmsKeys.find((kmsKey) => kmsKey.alias === value) ?? {
                            alias: value,
                            id: value
                          })
                        : null
                    }
                    onValueChange={(option) => onChange(option.alias ?? null)}
                    onClear={() => onChange(undefined)}
                    clearAriaLabel="Clear KMS key"
                    isError={Boolean(error)}
                    emptyMessage={(inputValue) =>
                      inputValue ? "No KMS keys match your search." : "No KMS keys found."
                    }
                    options={kmsKeys}
                    creation={{
                      onCreate: (inputValue) => onChange(inputValue.trim()),
                      formatLabel: (inputValue) => `Use "${inputValue}"`,
                      isDuplicate: (inputValue, option) => option.alias === inputValue,
                      isValid: isAwsKmsKeyIdentifier
                    }}
                    placeholder="Leave blank to use default KMS key"
                    searchPlaceholder="Search KMS keys..."
                    searchAriaLabel="Search KMS keys"
                    getOptionLabel={(option) =>
                      option.alias === "alias/aws/ssm" ? `${option.alias} (Default)` : option.alias
                    }
                    getOptionValue={(option) => option.alias}
                    modal
                  />
                  <FieldDescription>
                    Custom keys require <span className="font-mono">kms:ListAliases</span>,{" "}
                    <span className="font-mono">kms:DescribeKey</span>,{" "}
                    <span className="font-mono">kms:Encrypt</span>, and{" "}
                    <span className="font-mono">kms:Decrypt</span> on the selected IAM role.
                  </FieldDescription>
                </FieldContent>
                <FieldError errors={[error]} />
              </Field>
            )}
          />

          <Controller
            name="syncOptions.syncSecretMetadataAsTags"
            control={control}
            render={({ field: { value, onChange }, fieldState: { error } }) => (
              <Field className="mb-4">
                <Field orientation="horizontal">
                  <FieldContent>
                    <Label htmlFor="sync-secret-metadata-tags">
                      Sync secret metadata as resource tags
                    </Label>
                    <FieldDescription>
                      Metadata attached to secrets is added as resource tags on parameters synced by
                      Infisical. Manually configured tags take precedence when keys conflict.
                    </FieldDescription>
                  </FieldContent>
                  <Toggle
                    id="sync-secret-metadata-tags"
                    variant="project"
                    checked={value}
                    onCheckedChange={onChange}
                  />
                </Field>
                <FieldError errors={[error]} />
              </Field>
            )}
          />

          <Field className="mb-4">
            <Field orientation="horizontal">
              <FieldContent>
                <Label htmlFor="configure-resource-tags">Configure resource tags</Label>
                <FieldDescription>
                  Static tags applied to every synced parameter. Overwrites AWS resource tags on
                  synced parameters with the values defined below.
                </FieldDescription>
              </FieldContent>
              <Toggle
                id="configure-resource-tags"
                variant="project"
                checked={Array.isArray(watchedTags)}
                onCheckedChange={(isChecked) => {
                  if (isChecked) {
                    setValue("syncOptions.tags", [{ key: "", value: "" }]);
                  } else {
                    setValue("syncOptions.tags", undefined);
                  }
                }}
              />
            </Field>
            {Array.isArray(watchedTags) && <AwsTagsEditor />}
          </Field>
        </AccordionContent>
      </AccordionItem>
    </Accordion>
  );
};