Skip to content

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

fix(gateway): address review feedback on the update route

bernie-g984dc0c6bd1b
diff --git a/backend/src/ee/routes/v3/gateway-router.ts b/backend/src/ee/routes/v3/gateway-router.ts
index 859970c36a..646e0f3d2e 100644
--- a/backend/src/ee/routes/v3/gateway-router.ts
+++ b/backend/src/ee/routes/v3/gateway-router.ts
@@ -347,26 +347,17 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {
handler: async (req) => {
// The auth method goes first: it does the network work that actually fails, so a rejected
// change cannot leave behind a rename. Neither half is transactional.
- const authResult = req.body.authMethod
- ? await server.services.resourceAuthMethod.setMethod({
- resource: { type: "gateway", id: req.params.gatewayId },
- authMethod: toSetAuthMethodArg(req.body.authMethod),
- actor: req.permission
- })
- : null;
-
- const rename = req.body.name
- ? await server.services.gatewayV2.renameGateway({
- orgPermission: req.permission,
- gatewayId: req.params.gatewayId,
- name: req.body.name
- })
- : null;
+ if (req.body.authMethod) {
+ const result = await server.services.resourceAuthMethod.setMethod({
+ resource: { type: "gateway", id: req.params.gatewayId },
+ authMethod: toSetAuthMethodArg(req.body.authMethod),
+ actor: req.permission
+ });
- const gateway = await server.services.gatewayV2.getGatewayById({ gatewayId: req.params.gatewayId });
+ const updated = await server.services.gatewayV2.getGatewayById({ gatewayId: req.params.gatewayId });
- // Both events are written after the rename so they agree on what the gateway is called.
- if (authResult) {
+ // Written before the rename is attempted. Deferring it would lose the record entirely
+ // when a rename fails, leaving an auth method change that nothing audited.
await server.services.auditLog.createAuditLog({
...req.auditLogInfo,
orgId: req.permission.orgId,
@@ -375,8 +366,8 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {
metadata: resourceAuthMethodAuditMetadata({
resourceType: "gateway",
resourceId: req.params.gatewayId,
- resourceName: gateway.name,
- view: authResult
+ resourceName: updated.name,
+ view: result
})
}
});
@@ -390,7 +381,7 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {
resourceType: "gateway",
resourceId: req.params.gatewayId,
orgId: req.permission.orgId,
- method: authResult.method as TSettableAuthMethod
+ method: result.method as TSettableAuthMethod
}
})
.catch((err) => {
@@ -398,17 +389,28 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {
});
}
- if (rename && rename.previousName !== gateway.name) {
- await server.services.auditLog.createAuditLog({
- ...req.auditLogInfo,
- orgId: req.permission.orgId,
- event: {
- type: EventType.GATEWAY_UPDATE,
- metadata: { gatewayId: gateway.id, name: gateway.name, previousName: rename.previousName }
- }
+ if (req.body.name) {
+ // renameGateway returns the row it wrote on the primary. Re-reading here instead would
+ // route through a replica, where a lagging read can hide the rename and skip the event.
+ const { gateway: renamed, previousName } = await server.services.gatewayV2.renameGateway({
+ orgPermission: req.permission,
+ gatewayId: req.params.gatewayId,
+ name: req.body.name
});
+
+ if (previousName !== renamed.name) {
+ await server.services.auditLog.createAuditLog({
+ ...req.auditLogInfo,
+ orgId: req.permission.orgId,
+ event: {
+ type: EventType.GATEWAY_UPDATE,
+ metadata: { gatewayId: renamed.id, name: renamed.name, previousName }
+ }
+ });
+ }
}
+ const gateway = await server.services.gatewayV2.getGatewayById({ gatewayId: req.params.gatewayId });
const view = await server.services.resourceAuthMethod.getByGatewayId({
resource: { type: "gateway", id: req.params.gatewayId },
actor: req.permission
diff --git a/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayPageHeader/RenameGatewayModal.tsx b/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayPageHeader/RenameGatewayModal.tsx
index dee04fe89e..f7790ef39f 100644
--- a/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayPageHeader/RenameGatewayModal.tsx
+++ b/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayPageHeader/RenameGatewayModal.tsx
@@ -1,4 +1,6 @@
-import { useEffect, useMemo, useState } from "react";
+import { useEffect } from "react";
+import { Controller, useForm } from "react-hook-form";
+import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { createNotification } from "@app/components/notifications";
@@ -11,6 +13,7 @@ import {
DialogHeader,
DialogTitle,
Field,
+ FieldContent,
FieldError,
FieldLabel,
Input
@@ -23,6 +26,8 @@ const formSchema = z.object({
name: slugSchema({ field: "name" })
});
+type TFormData = z.infer<typeof formSchema>;
+
type Props = {
isOpen: boolean;
onToggle: (isOpen: boolean) => void;
@@ -31,39 +36,29 @@ type Props = {
export const RenameGatewayModal = ({ isOpen, onToggle, gateway }: Props) => {
const updateGateway = useUpdateGateway();
- const [name, setName] = useState(gateway.name);
- const [formErrors, setFormErrors] = useState<z.ZodIssue[]>([]);
- const errors = useMemo(() => {
- const errorMap: Record<string, string | undefined> = {};
- formErrors.forEach((issue) => {
- if (issue.path.length > 0) errorMap[String(issue.path[0])] = issue.message;
- });
- return errorMap;
- }, [formErrors]);
+ const {
+ control,
+ handleSubmit,
+ reset,
+ formState: { isSubmitting }
+ } = useForm<TFormData>({
+ resolver: zodResolver(formSchema),
+ defaultValues: { name: gateway.name }
+ });
useEffect(() => {
- if (isOpen) {
- setName(gateway.name);
- setFormErrors([]);
- }
- }, [isOpen, gateway.name]);
-
- const handleSubmit = async () => {
- setFormErrors([]);
- const validation = formSchema.safeParse({ name });
- if (!validation.success) {
- setFormErrors(validation.error.issues);
- return;
- }
+ if (isOpen) reset({ name: gateway.name });
+ }, [isOpen, gateway.name, reset]);
- if (name.trim() === gateway.name) {
+ const onFormSubmit = async ({ name }: TFormData) => {
+ if (name === gateway.name) {
onToggle(false);
return;
}
try {
- await updateGateway.mutateAsync({ gatewayId: gateway.id, name: name.trim() });
+ await updateGateway.mutateAsync({ gatewayId: gateway.id, name });
createNotification({ type: "success", text: "Gateway renamed" });
onToggle(false);
} catch (err: unknown) {
@@ -75,30 +70,35 @@ export const RenameGatewayModal = ({ isOpen, onToggle, gateway }: Props) => {
return (
<Dialog open={isOpen} onOpenChange={onToggle}>
<DialogContent>
- <DialogHeader>
- <DialogTitle>Rename Gateway</DialogTitle>
- <DialogDescription>
- The gateway keeps its ID, so anything already pointing at it carries on working.
- </DialogDescription>
- </DialogHeader>
- <Field data-invalid={Boolean(errors.name)}>
- <FieldLabel htmlFor="rename-gateway-name">Name</FieldLabel>
- <Input
- id="rename-gateway-name"
- value={name}
- onChange={(e) => setName(e.target.value)}
- isError={Boolean(errors.name)}
+ <form onSubmit={handleSubmit(onFormSubmit)}>
+ <DialogHeader>
+ <DialogTitle>Rename Gateway</DialogTitle>
+ <DialogDescription>
+ The gateway keeps its ID, so anything already pointing at it carries on working.
+ </DialogDescription>
+ </DialogHeader>
+ <Controller
+ control={control}
+ name="name"
+ render={({ field, fieldState: { error } }) => (
+ <Field>
+ <FieldLabel>Name</FieldLabel>
+ <FieldContent>
+ <Input {...field} isError={Boolean(error)} placeholder="prod-us-east" />
+ </FieldContent>
+ <FieldError>{error?.message}</FieldError>
+ </Field>
+ )}
/>
- <FieldError>{errors.name}</FieldError>
- </Field>
- <DialogFooter>
- <Button variant="ghost" onClick={() => onToggle(false)} type="button">
- Cancel
- </Button>
- <Button variant="org" onClick={handleSubmit} isPending={updateGateway.isPending}>
- Save Changes
- </Button>
- </DialogFooter>
+ <DialogFooter>
+ <Button variant="ghost" type="button" onClick={() => onToggle(false)}>
+ Cancel
+ </Button>
+ <Button variant="org" type="submit" isPending={isSubmitting}>
+ Save Changes
+ </Button>
+ </DialogFooter>
+ </form>
</DialogContent>
</Dialog>
);