Skip to content

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

UI: reuse components that are used on both rotations

adilsonb529ffc71ef2
diff --git a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareApiTokenRotationParametersFields.tsx b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareApiTokenRotationParametersFields.tsx
index 82625f95f2..c1d1606ff4 100644
--- a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareApiTokenRotationParametersFields.tsx
+++ b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareApiTokenRotationParametersFields.tsx
@@ -1,5 +1,5 @@
import { useEffect, useMemo } from "react";
-import { Control, Controller, useFieldArray, useFormContext } from "react-hook-form";
+import { Controller, useFieldArray, useFormContext } from "react-hook-form";
import { MultiValue, SingleValue } from "react-select";
import { faPlus, faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
@@ -38,7 +38,7 @@ import {
isStoredPolicy
} from "../schemas/cloudflare-api-token-rotation-schema";
import { CLOUDFLARE_TOKEN_NAME_MAX_LENGTH } from "../schemas/shared";
-import { CloudflareIpListInput } from "./shared";
+import { CloudflareIpListField } from "./shared";
// The schema merges policy rows into the stored shape on submit, so `TSecretRotationV2Form` (the
// schema's *output* type) describes merged policies. The fields here edit rows, which is the input side.
@@ -56,34 +56,6 @@ enum ParameterTab {
Restrictions = "restrictions"
}
-const IpListField = ({
- control,
- name,
- label,
- tooltipText
-}: {
- control: Control<TCloudflareApiTokenForm>;
- name: "parameters.allowedIps" | "parameters.disallowedIps";
- label: string;
- tooltipText: string;
-}) => (
- <Controller
- control={control}
- name={name}
- render={({ field: { value, onChange }, fieldState: { error } }) => (
- <FormControl
- isOptional
- isError={Boolean(error)}
- errorText={error?.message}
- label={label}
- tooltipText={tooltipText}
- >
- <CloudflareIpListInput value={value} onChange={onChange} />
- </FormControl>
- )}
- />
-);
-
export const CloudflareApiTokenRotationParametersFields = () => {
const { control, watch, setValue, getValues } = useFormContext<TCloudflareApiTokenForm>();
@@ -337,13 +309,13 @@ export const CloudflareApiTokenRotationParametersFields = () => {
</div>
</TabPanel>
<TabPanel value={ParameterTab.Restrictions}>
- <IpListField
+ <CloudflareIpListField
control={control}
name="parameters.allowedIps"
label="Allowed IPs"
tooltipText="The generated token can only be used from these IP addresses or CIDR blocks. One entry per line. Leave empty to allow any IP."
/>
- <IpListField
+ <CloudflareIpListField
control={control}
name="parameters.disallowedIps"
label="Disallowed IPs"
diff --git a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareR2AccessKeyRotationParametersFields.tsx b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareR2AccessKeyRotationParametersFields.tsx
index cf7497ebf0..0c5e31bf2e 100644
--- a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareR2AccessKeyRotationParametersFields.tsx
+++ b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/CloudflareR2AccessKeyRotationParametersFields.tsx
@@ -1,5 +1,5 @@
-import { useEffect, useMemo } from "react";
-import { Control, Controller, useFormContext } from "react-hook-form";
+import { useMemo } from "react";
+import { Controller, useFormContext } from "react-hook-form";
import { MultiValue } from "react-select";
import {
@@ -20,12 +20,11 @@ import { TSecretRotationV2Form } from "../schemas";
import {
CLOUDFLARE_R2_ACCESS_LEVEL_MAP,
CLOUDFLARE_R2_JURISDICTION_MAP,
- CloudflareR2AccessLevel,
r2BucketKey,
TCloudflareR2BucketSelection
} from "../schemas/cloudflare-r2-access-key-rotation-schema";
import { CLOUDFLARE_TOKEN_NAME_MAX_LENGTH } from "../schemas/shared";
-import { CloudflareIpListInput } from "./shared";
+import { CloudflareIpListField } from "./shared";
type TCloudflareR2AccessKeyForm = TSecretRotationV2Form & {
type: SecretRotation.CloudflareR2AccessKey;
@@ -36,59 +35,30 @@ enum ParameterTab {
Restrictions = "restrictions"
}
-const formatBucketOptionLabel = ({ name, jurisdiction }: TCloudflareR2BucketSelection) => (
- <span>
- {name}{" "}
- <span className="text-mineshaft-400">{CLOUDFLARE_R2_JURISDICTION_MAP[jurisdiction]}</span>{" "}
- </span>
-);
+// One source for the picker label's content and order: the JSX dims the jurisdiction, and react-select
+// filters on the plain-text join, so the two must not drift apart.
+const bucketLabelParts = ({ name, jurisdiction }: TCloudflareR2BucketSelection) =>
+ [name, CLOUDFLARE_R2_JURISDICTION_MAP[jurisdiction]] as const;
-// react-select filters on this, so it has to cover the jurisdiction the option renders too
-const getBucketSearchLabel = ({ name, jurisdiction }: TCloudflareR2BucketSelection) =>
- `${CLOUDFLARE_R2_JURISDICTION_MAP[jurisdiction]} ${name}`;
+const formatBucketOptionLabel = (bucket: TCloudflareR2BucketSelection) => {
+ const [name, jurisdiction] = bucketLabelParts(bucket);
-const IpListField = ({
- control,
- name,
- label,
- tooltipText
-}: {
- control: Control<TCloudflareR2AccessKeyForm>;
- name: "parameters.allowedIps" | "parameters.disallowedIps";
- label: string;
- tooltipText: string;
-}) => (
- <Controller
- control={control}
- name={name}
- render={({ field: { value, onChange }, fieldState: { error } }) => (
- <FormControl
- isOptional
- isError={Boolean(error)}
- errorText={error?.message}
- label={label}
- tooltipText={tooltipText}
- >
- <CloudflareIpListInput value={value} onChange={onChange} />
- </FormControl>
- )}
- />
-);
+ return (
+ <span>
+ {name} <span className="text-mineshaft-400">{jurisdiction}</span>
+ </span>
+ );
+};
+
+const getBucketSearchLabel = (bucket: TCloudflareR2BucketSelection) =>
+ bucketLabelParts(bucket).join(" ");
export const CloudflareR2AccessKeyRotationParametersFields = () => {
- const { control, watch, setValue, getValues } = useFormContext<TCloudflareR2AccessKeyForm>();
+ const { control, watch } = useFormContext<TCloudflareR2AccessKeyForm>();
const connectionId = watch("connection.id");
const selectedBuckets = watch("parameters.buckets");
- // The rotation template only seeds the secrets mapping, so pick the least-privileged access level on
- // create rather than leaving the select empty.
- useEffect(() => {
- if (!getValues("parameters.accessLevel")) {
- setValue("parameters.accessLevel", CloudflareR2AccessLevel.ObjectRead);
- }
- }, []);
-
const { data: buckets, isPending: isBucketsPending } = useCloudflareConnectionListR2Buckets(
connectionId,
{ enabled: Boolean(connectionId) }
@@ -151,21 +121,10 @@ export const CloudflareR2AccessKeyRotationParametersFields = () => {
formatOptionLabel={formatBucketOptionLabel}
getOptionLabel={getBucketSearchLabel}
getOptionValue={r2BucketKey}
- value={
- value
- ? bucketOptions.filter((bucket) =>
- value.some((selection) => r2BucketKey(selection) === r2BucketKey(bucket))
- )
- : []
- }
- // narrowed to the two identifying fields so listing-only metadata is never persisted
- onChange={(option) =>
- onChange(
- (option as MultiValue<TCloudflareR2BucketSelection>).map(
- ({ name, jurisdiction }) => ({ name, jurisdiction })
- )
- )
- }
+ // the form field holds the option shape itself, and react-select matches selections by
+ // `getOptionValue`, so the stored value can be handed back as-is
+ value={value ?? []}
+ onChange={(option) => onChange(option as MultiValue<TCloudflareR2BucketSelection>)}
/>
</FormControl>
)}
@@ -197,13 +156,13 @@ export const CloudflareR2AccessKeyRotationParametersFields = () => {
/>
</TabPanel>
<TabPanel value={ParameterTab.Restrictions}>
- <IpListField
+ <CloudflareIpListField
control={control}
name="parameters.allowedIps"
label="Allowed IPs"
tooltipText="The generated access key can only be used from these IP addresses or CIDR blocks. One entry per line. Leave empty to allow any IP."
/>
- <IpListField
+ <CloudflareIpListField
control={control}
name="parameters.disallowedIps"
label="Disallowed IPs"
diff --git a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/shared/CloudflareIpListInput.tsx b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/shared/CloudflareIpListInput.tsx
index de62882e2e..448a2b8b46 100644
--- a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/shared/CloudflareIpListInput.tsx
+++ b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/shared/CloudflareIpListInput.tsx
@@ -1,12 +1,13 @@
import { useState } from "react";
+import { Control, Controller, FieldPath, FieldValues } from "react-hook-form";
-import { TextArea } from "@app/components/v2";
+import { FormControl, TextArea } from "@app/components/v2";
/**
* Textarea-backed editor for a list of IPs/CIDRs. The raw text lives in local state so partially
* typed entries and blank lines survive re-renders, while the form only ever holds the parsed list.
*/
-export const CloudflareIpListInput = ({
+const CloudflareIpListInput = ({
value,
onChange
}: {
@@ -34,3 +35,32 @@ export const CloudflareIpListInput = ({
/>
);
};
+
+/** The labelled, form-wired version — shared by every Cloudflare rotation with IP restrictions. */
+export const CloudflareIpListField = <T extends FieldValues>({
+ control,
+ name,
+ label,
+ tooltipText
+}: {
+ control: Control<T>;
+ name: FieldPath<T>;
+ label: string;
+ tooltipText: string;
+}) => (
+ <Controller
+ control={control}
+ name={name}
+ render={({ field: { value, onChange }, fieldState: { error } }) => (
+ <FormControl
+ isOptional
+ isError={Boolean(error)}
+ errorText={error?.message}
+ label={label}
+ tooltipText={tooltipText}
+ >
+ <CloudflareIpListInput value={value} onChange={onChange} />
+ </FormControl>
+ )}
+ />
+);
diff --git a/frontend/src/components/secret-rotations-v2/forms/schemas/cloudflare-r2-access-key-rotation-schema.ts b/frontend/src/components/secret-rotations-v2/forms/schemas/cloudflare-r2-access-key-rotation-schema.ts
index 3a502769cc..b6656a6a59 100644
--- a/frontend/src/components/secret-rotations-v2/forms/schemas/cloudflare-r2-access-key-rotation-schema.ts
+++ b/frontend/src/components/secret-rotations-v2/forms/schemas/cloudflare-r2-access-key-rotation-schema.ts
@@ -48,7 +48,12 @@ export const CloudflareR2AccessKeyRotationSchema = z
type: z.literal(SecretRotation.CloudflareR2AccessKey),
parameters: z.object({
name: CloudflareTokenNameSchema,
- buckets: CloudflareR2BucketSchema.array().min(1, "At least one bucket is required"),
+ buckets: CloudflareR2BucketSchema.array()
+ .min(1, "At least one bucket is required")
+ .refine(
+ (buckets) => new Set(buckets.map(r2BucketKey)).size === buckets.length,
+ "Buckets must be unique"
+ ),
accessLevel: z.nativeEnum(CloudflareR2AccessLevel),
allowedIps: CloudflareTokenIpRestrictionsSchema,
disallowedIps: CloudflareTokenIpRestrictionsSchema
diff --git a/frontend/src/hooks/api/appConnections/cloudflare/types.ts b/frontend/src/hooks/api/appConnections/cloudflare/types.ts
index 5dc86178ab..564cdcb52d 100644
--- a/frontend/src/hooks/api/appConnections/cloudflare/types.ts
+++ b/frontend/src/hooks/api/appConnections/cloudflare/types.ts
@@ -29,10 +29,8 @@ export enum CloudflareR2Jurisdiction {
FedRamp = "fedramp"
}
+/** Name and jurisdiction together identify a bucket, and are all a token policy needs to grant it. */
export type TCloudflareR2Bucket = {
name: string;
jurisdiction: CloudflareR2Jurisdiction;
- location?: string;
- storageClass?: string;
- creationDate?: string;
};
diff --git a/frontend/src/hooks/api/secretRotationsV2/types/cloudflare-r2-access-key-rotation.ts b/frontend/src/hooks/api/secretRotationsV2/types/cloudflare-r2-access-key-rotation.ts
index f34c2abaa2..5980739a9a 100644
--- a/frontend/src/hooks/api/secretRotationsV2/types/cloudflare-r2-access-key-rotation.ts
+++ b/frontend/src/hooks/api/secretRotationsV2/types/cloudflare-r2-access-key-rotation.ts
@@ -42,6 +42,7 @@ export type TCloudflareR2AccessKeyRotationOption = {
type: SecretRotation.CloudflareR2AccessKey;
connection: AppConnection.Cloudflare;
template: {
+ parameters: Pick<TCloudflareR2AccessKeyRotation["parameters"], "accessLevel">;
secretsMapping: TCloudflareR2AccessKeyRotation["secretsMapping"];
};
};