Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(frontend): validate and consolidate the stripe key permission copy UI
Calvin9b6acb47da39
diff --git a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/StripeApiKeyRotationParametersFields.tsx b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/StripeApiKeyRotationParametersFields.tsxindex 4ebffd96f1..1bb064e3aa 100644--- a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/StripeApiKeyRotationParametersFields.tsx+++ b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/StripeApiKeyRotationParametersFields.tsx@@ -1,16 +1,13 @@-import { useMemo } from "react";+import { useMemo, useState } from "react";import { Controller, useFormContext, useWatch } from "react-hook-form";+import { createNotification } from "@app/components/notifications";import { TSecretRotationV2Form } from "@app/components/secret-rotations-v2/forms/schemas";+import { Combobox, Field, FieldDescription, FieldError, FieldLabel } from "@app/components/v3";import {- Button,- Combobox,- Field,- FieldDescription,- FieldError,- FieldLabel-} from "@app/components/v3";-import { useStripeConnectionListApiKeys } from "@app/hooks/api/appConnections/stripe";+ TStripeApiKey,+ useStripeConnectionListApiKeys+} from "@app/hooks/api/appConnections/stripe";import { SecretRotation, useSecretRotationV2Option } from "@app/hooks/api/secretRotationsV2";export const StripeApiKeyRotationParametersFields = () => {@@ -18,14 +15,56 @@ export const StripeApiKeyRotationParametersFields = () => {TSecretRotationV2Form & { type: SecretRotation.StripeApiKey }>();+ const [copiedFromKey, setCopiedFromKey] = useState<TStripeApiKey | null>(null);+const connectionId = useWatch({ control, name: "connection.id" });- const { rotationOption } = useSecretRotationV2Option(SecretRotation.StripeApiKey);- const { data: apiKeys = [] } = useStripeConnectionListApiKeys(connectionId, {- enabled: Boolean(connectionId)- });+ const { rotationOption, isLoading: isRotationOptionLoading } = useSecretRotationV2Option(+ SecretRotation.StripeApiKey+ );+ const { data: apiKeys = [], isPending: isApiKeysPending } = useStripeConnectionListApiKeys(+ connectionId,+ { enabled: Boolean(connectionId) }+ );+ // A disabled query (no connection picked yet) reports isPending forever, so it must not be+ // read on its own as "loading".+ const isApiKeysLoading = isApiKeysPending && Boolean(connectionId);const permissions = useMemo(() => rotationOption?.template.permissions ?? [], [rotationOption]);+ // Only keys still usable as a source: an expired or revoked key's permissions are not a+ // meaningful default for a new one.+ const activeApiKeys = useMemo(+ () => apiKeys.filter((apiKey) => apiKey.status === "active"),+ [apiKeys]+ );++ const applyKeyPermissions = (apiKey: TStripeApiKey) => {+ setCopiedFromKey(apiKey);++ const allowedPermissions = new Set(permissions);+ const filteredPermissions = apiKey.permissions.filter((permission) =>+ allowedPermissions.has(permission)+ );+ const filteredConnectPermissions = apiKey.connectPermissions.filter((permission) =>+ allowedPermissions.has(permission)+ );++ setValue("parameters.permissions", filteredPermissions, { shouldDirty: true });+ setValue("parameters.connectPermissions", filteredConnectPermissions, { shouldDirty: true });++ const droppedCount =+ apiKey.permissions.length -+ filteredPermissions.length ++ (apiKey.connectPermissions.length - filteredConnectPermissions.length);++ if (droppedCount > 0) {+ createNotification({+ type: "warning",+ text: `${droppedCount} permission${droppedCount === 1 ? "" : "s"} on ${apiKey.name || apiKey.id} ${droppedCount === 1 ? "isn't" : "aren't"} recognized by Infisical and were not copied.`+ });+ }+ };+return (<><Controller@@ -45,11 +84,12 @@ export const StripeApiKeyRotationParametersFields = () => {searchPlaceholder="Search permissions..."searchAriaLabel="Search Stripe permissions"clearAriaLabel="Clear all permissions"+ isLoading={isRotationOptionLoading}/>{!error && (<FieldDescription>- Select all matching applies only to the permissions your search returns, not the- full list.+ Select All only selects the permissions your search currently matches, not the full+ list.</FieldDescription>)}<FieldError>{error?.message}</FieldError>@@ -58,26 +98,28 @@ export const StripeApiKeyRotationParametersFields = () => {control={control}name="parameters.permissions"/>- {apiKeys.length > 0 && (- <div className="flex flex-wrap items-center gap-2">- <span className="text-sm text-muted">Copy permissions from an existing key:</span>- {apiKeys.map((apiKey) => (- <Button- key={apiKey.id}- type="button"- size="xs"- variant="outline"- onClick={() => {- setValue("parameters.permissions", apiKey.permissions, { shouldDirty: true });- setValue("parameters.connectPermissions", apiKey.connectPermissions, {- shouldDirty: true- });- }}- >- {apiKey.name || apiKey.id}- </Button>- ))}- </div>+ {(activeApiKeys.length > 0 || isApiKeysLoading) && (+ <Field>+ <FieldLabel htmlFor="stripe-copy-from-key">+ Copy permissions from an existing key+ </FieldLabel>+ <Combobox+ id="stripe-copy-from-key"+ options={activeApiKeys}+ value={copiedFromKey}+ onValueChange={applyKeyPermissions}+ getOptionValue={(apiKey) => apiKey.id}+ getOptionLabel={(apiKey) => apiKey.name || apiKey.id}+ placeholder="Select a key..."+ searchPlaceholder="Search keys..."+ searchAriaLabel="Search Stripe API keys"+ isLoading={isApiKeysLoading}+ />+ <FieldDescription>+ Applies that key's permissions to the fields above. It does not link the rotation+ to the key.+ </FieldDescription>+ </Field>)}</>);