Skip to content

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

Revert "add background for resources"

adilsonc6db7a6af588
diff --git a/frontend/src/pages/secret-manager/SecretApprovalsPage/components/AccessApprovalRequest/components/RequestAccessForm.tsx b/frontend/src/pages/secret-manager/SecretApprovalsPage/components/AccessApprovalRequest/components/RequestAccessForm.tsx
index d9231400a1..579eea5485 100644
--- a/frontend/src/pages/secret-manager/SecretApprovalsPage/components/AccessApprovalRequest/components/RequestAccessForm.tsx
+++ b/frontend/src/pages/secret-manager/SecretApprovalsPage/components/AccessApprovalRequest/components/RequestAccessForm.tsx
@@ -1,11 +1,8 @@
import { useEffect, useMemo, useRef } from "react";
import { Controller, useFieldArray, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
-import { formatDistance } from "date-fns";
import {
BanIcon,
- ChevronDownIcon,
- ClockIcon,
EyeIcon,
FingerprintIcon,
FolderIcon,
@@ -35,6 +32,7 @@ import {
DropdownMenuItem,
DropdownMenuTrigger,
Field,
+ FieldDescription,
FieldError,
FieldLabel,
HoverCard,
@@ -42,9 +40,6 @@ import {
HoverCardTrigger,
IconButton,
Input,
- Popover,
- PopoverContent,
- PopoverTrigger,
Select,
SelectContent,
SelectItem,
@@ -71,6 +66,19 @@ import {
import { useCreateAccessRequest } from "@app/hooks/api";
import { TAccessApprovalPolicy } from "@app/hooks/api/types";
+const PERMANENT_DURATION_VALUE = "permanent";
+const CUSTOM_DURATION_VALUE = "custom";
+
+const DURATION_PRESETS = [
+ { label: "30 minutes", value: "30m" },
+ { label: "1 hour", value: "1h" },
+ { label: "12 hours", value: "12h" },
+ { label: "1 day", value: "1d" },
+ { label: "3 days", value: "3d" },
+ { label: "7 days", value: "7d" },
+ { label: "30 days", value: "30d" }
+];
+
type TResourceAction = {
value: string;
label: string;
@@ -345,18 +353,27 @@ const requestAccessSchema = z.object({
.refine((resources) => resources.some((resource) => resource.actions.length > 0), {
message: "Select at least one permission"
}),
- temporaryAccess: z.discriminatedUnion("isTemporary", [
- z.object({
- isTemporary: z.literal(true),
- temporaryRange: z.string().min(1),
- temporaryAccessStartTime: z.string().datetime(),
- temporaryAccessEndTime: z.string().datetime().nullable().optional()
- }),
- z.object({
- isTemporary: z.literal(false),
- temporaryRange: z.string().optional()
+ duration: z
+ .object({
+ preset: z.string().min(1),
+ customValue: z.string().optional()
})
- ]),
+ .superRefine((val, ctx) => {
+ if (val.preset !== CUSTOM_DURATION_VALUE) return;
+ let parsed: number | undefined;
+ try {
+ parsed = val.customValue ? ms(val.customValue) : undefined;
+ } catch {
+ parsed = undefined;
+ }
+ if (typeof parsed !== "number" || parsed <= 0) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ path: ["customValue"],
+ message: "Invalid duration. Use formats like 30m, 2h, 1d."
+ });
+ }
+ }),
note: z.string().max(255).optional()
});
@@ -383,7 +400,7 @@ export const RequestAccessForm = ({
environmentSlug: currentProject.environments?.[0]?.slug,
secretPath: initialSecretPath ?? "",
resources: [{ subject: ProjectPermissionSub.Secrets, actions: selectedActions }],
- temporaryAccess: { isTemporary: false, temporaryRange: "1h" },
+ duration: { preset: PERMANENT_DURATION_VALUE, customValue: "1h" },
note: ""
}
});
@@ -397,7 +414,7 @@ export const RequestAccessForm = ({
const selectedEnvironment = form.watch("environmentSlug");
const secretPath = form.watch("secretPath");
const resources = form.watch("resources");
- const temporaryAccessField = form.watch("temporaryAccess");
+ const durationPreset = form.watch("duration.preset");
const selectablePaths = useMemo(
() =>
@@ -417,10 +434,10 @@ export const RequestAccessForm = ({
[policies, selectedEnvironment, secretPath]
);
- const isTemporary = temporaryAccessField?.isTemporary;
- const isExpired =
- temporaryAccessField.isTemporary &&
- new Date() > new Date(temporaryAccessField.temporaryAccessEndTime || "");
+ const maxDurationMs = matchedPolicy?.maxTimePeriod ? ms(matchedPolicy.maxTimePeriod) : null;
+ const allowedPresets = maxDurationMs
+ ? DURATION_PRESETS.filter((preset) => ms(preset.value) <= maxDurationMs)
+ : DURATION_PRESETS;
const isInitialMount = useRef(true);
useEffect(() => {
@@ -431,6 +448,24 @@ export const RequestAccessForm = ({
form.setValue("secretPath", "", { shouldValidate: true });
}, [selectedEnvironment]);
+ // policies with a max time period forbid permanent access and cap the range
+ useEffect(() => {
+ if (!maxDurationMs || !matchedPolicy?.maxTimePeriod) return;
+ const { preset } = form.getValues("duration");
+ const exceedsMax =
+ preset === PERMANENT_DURATION_VALUE ||
+ (preset !== CUSTOM_DURATION_VALUE && ms(preset) > maxDurationMs);
+ if (!exceedsMax) return;
+ const fallback = allowedPresets[allowedPresets.length - 1];
+ form.setValue(
+ "duration",
+ fallback
+ ? { preset: fallback.value, customValue: form.getValues("duration.customValue") }
+ : { preset: CUSTOM_DURATION_VALUE, customValue: matchedPolicy.maxTimePeriod },
+ { shouldValidate: true }
+ );
+ }, [matchedPolicy]);
+
const addedSubjects = new Set(resources.map((resource) => resource.subject));
const availableResources = RESOURCE_CONFIGS.filter(
(config) =>
@@ -468,13 +503,15 @@ export const RequestAccessForm = ({
return;
}
- const { isTemporary: isTemporaryRequest, temporaryRange } = data.temporaryAccess;
+ const isTemporary = data.duration.preset !== PERMANENT_DURATION_VALUE;
+ const temporaryRange =
+ data.duration.preset === CUSTOM_DURATION_VALUE
+ ? data.duration.customValue
+ : data.duration.preset;
if (
matchedPolicy?.maxTimePeriod &&
- (!isTemporaryRequest ||
- !temporaryRange ||
- ms(temporaryRange) > ms(matchedPolicy.maxTimePeriod))
+ (!isTemporary || !temporaryRange || ms(temporaryRange) > ms(matchedPolicy.maxTimePeriod))
) {
createNotification({
type: "error",
@@ -494,8 +531,8 @@ export const RequestAccessForm = ({
await requestAccess.mutateAsync({
projectSlug: currentProject.slug,
- isTemporary: isTemporaryRequest,
- ...(isTemporaryRequest && temporaryRange && { temporaryRange }),
+ isTemporary,
+ ...(isTemporary && temporaryRange && { temporaryRange }),
permissions,
note: data.note || undefined
});
@@ -508,42 +545,6 @@ export const RequestAccessForm = ({
if (onClose) onClose();
};
- const handleGrant = () => {
- const temporaryRange = form.getValues("temporaryAccess.temporaryRange");
- if (!temporaryRange) {
- form.setError(
- "temporaryAccess.temporaryRange",
- { type: "required", message: "Required" },
- { shouldFocus: true }
- );
- return;
- }
- form.clearErrors("temporaryAccess.temporaryRange");
- form.setValue(
- "temporaryAccess",
- {
- isTemporary: true,
- temporaryAccessStartTime: new Date().toISOString(),
- temporaryRange,
- temporaryAccessEndTime: new Date(new Date().getTime() + ms(temporaryRange)).toISOString()
- },
- { shouldDirty: true }
- );
- };
-
- const handleCancelTemporary = () => {
- form.setValue("temporaryAccess", {
- isTemporary: false,
- temporaryRange: form.getValues("temporaryAccess.temporaryRange")
- });
- };
-
- const getAccessLabel = () => {
- if (isExpired) return "Access expired";
- if (!temporaryAccessField?.isTemporary) return "Permanent";
- return formatDistance(new Date(temporaryAccessField.temporaryAccessEndTime || ""), new Date());
- };
-
return (
<form
onSubmit={form.handleSubmit(handleRequestAccess)}
@@ -615,7 +616,11 @@ export const RequestAccessForm = ({
</div>
<Field>
<FieldLabel>Resources & Permissions</FieldLabel>
+<<<<<<< HEAD
<div className="flex flex-col gap-3 rounded-lg border border-border bg-black/20 p-3">
+=======
+ <div className="flex flex-col gap-3">
+>>>>>>> parent of 2aafcb29de (add background for resources)
{resourceFields.map((resourceField, index) => {
const config = RESOURCE_CONFIGS.find((c) => c.subject === resourceField.subject);
if (!config) return null;
@@ -714,51 +719,51 @@ export const RequestAccessForm = ({
)}
</div>
</Field>
- <Field>
- <FieldLabel>Duration</FieldLabel>
- <Popover>
- <PopoverTrigger asChild>
- <Button variant="outline" className="w-full justify-between capitalize">
- <span className="flex items-center gap-2">
- {isTemporary && <ClockIcon className="size-3.5" />}
- {getAccessLabel()}
- </span>
- <ChevronDownIcon className="size-3.5" />
- </Button>
- </PopoverTrigger>
- <PopoverContent align="start" className="flex flex-col gap-4">
- <div className="text-sm text-foreground">Configure timed access</div>
- {isExpired && (
- <Badge variant="danger" className="w-fit">
- Expired
- </Badge>
+ <Controller
+ control={form.control}
+ name="duration.preset"
+ render={({ field }) => (
+ <Field>
+ <FieldLabel htmlFor="duration">Duration</FieldLabel>
+ <Select value={field.value} onValueChange={field.onChange}>
+ <SelectTrigger id="duration" className="w-full">
+ <SelectValue placeholder="Select a duration" />
+ </SelectTrigger>
+ <SelectContent position="popper">
+ {!maxDurationMs && (
+ <SelectItem value={PERMANENT_DURATION_VALUE}>Permanent</SelectItem>
+ )}
+ {allowedPresets.map((preset) => (
+ <SelectItem value={preset.value} key={preset.value}>
+ {preset.label}
+ </SelectItem>
+ ))}
+ <SelectItem value={CUSTOM_DURATION_VALUE}>Custom</SelectItem>
+ </SelectContent>
+ </Select>
+ {matchedPolicy?.maxTimePeriod && (
+ <FieldDescription>
+ Maximum duration allowed by policy: {matchedPolicy.maxTimePeriod}
+ </FieldDescription>
)}
- <Controller
- control={form.control}
- name="temporaryAccess.temporaryRange"
- render={({ field, fieldState: { error } }) => (
- <Field>
- <FieldLabel htmlFor="temporaryRange">
- <TtlFormLabel label="Validity" />
- </FieldLabel>
- <Input id="temporaryRange" {...field} isError={Boolean(error?.message)} />
- <FieldError errors={[error]} />
- </Field>
- )}
- />
- <div className="flex items-center gap-2">
- <Button size="xs" variant="project" onClick={handleGrant}>
- Grant
- </Button>
- {temporaryAccessField.isTemporary && (
- <Button size="xs" variant="danger" onClick={handleCancelTemporary}>
- Cancel
- </Button>
- )}
- </div>
- </PopoverContent>
- </Popover>
- </Field>
+ </Field>
+ )}
+ />
+ {durationPreset === CUSTOM_DURATION_VALUE && (
+ <Controller
+ control={form.control}
+ name="duration.customValue"
+ render={({ field, fieldState: { error } }) => (
+ <Field>
+ <FieldLabel htmlFor="customDuration">
+ <TtlFormLabel label="Custom Duration" />
+ </FieldLabel>
+ <Input id="customDuration" {...field} isError={Boolean(error?.message)} />
+ <FieldError errors={[error]} />
+ </Field>
+ )}
+ />
+ )}
<Controller
control={form.control}
name="note"