Skip to content

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

feat: manual gateway health check trigger

Sheen Capadngana73dd7922d6c
diff --git a/backend/src/ee/routes/v2/gateway-router.ts b/backend/src/ee/routes/v2/gateway-router.ts
index 03cea15b3f..d53e99cef5 100644
--- a/backend/src/ee/routes/v2/gateway-router.ts
+++ b/backend/src/ee/routes/v2/gateway-router.ts
@@ -136,6 +136,34 @@ export const registerGatewayV2Router = async (server: FastifyZodProvider) => {
}
});
+ server.route({
+ method: "POST",
+ url: "/:id/heartbeat",
+ config: {
+ rateLimit: writeLimit
+ },
+ schema: {
+ operationId: "triggerGatewayHeartbeat",
+ params: z.object({
+ id: z.string()
+ }),
+ response: {
+ 200: z.object({
+ message: z.string()
+ })
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async (req) => {
+ await server.services.gatewayV2.triggerHeartbeat({
+ orgPermission: req.permission,
+ id: req.params.id
+ });
+
+ return { message: "Successfully triggered heartbeat" };
+ }
+ });
+
server.route({
method: "GET",
url: "/pam-session-key",
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
index 9ae9099a3a..6180b66102 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -696,6 +696,103 @@ export const gatewayV2ServiceFactory = ({
}
};
+ const triggerHeartbeat = async ({ orgPermission, id }: { orgPermission: OrgServiceActor; id: string }) => {
+ const gateway = await gatewayV2DAL.findOne({ id, orgId: orgPermission.orgId });
+ if (!gateway) {
+ throw new NotFoundError({ message: `Gateway ${id} not found` });
+ }
+
+ const { permission } = await permissionService.getOrgPermission({
+ actor: orgPermission.type,
+ actorId: orgPermission.id,
+ orgId: gateway.orgId,
+ actorAuthMethod: orgPermission.authMethod,
+ actorOrgId: orgPermission.orgId,
+ scope: OrganizationActionScope.Any
+ });
+
+ ForbiddenError.from(permission).throwUnlessCan(
+ OrgPermissionGatewayActions.ListGateways,
+ OrgPermissionSubjects.Gateway
+ );
+
+ const gatewayV2ConnectionDetails = await getPlatformConnectionDetailsByGatewayId({
+ gatewayId: gateway.id,
+ targetHost: "health-check",
+ targetPort: 443
+ });
+
+ if (!gatewayV2ConnectionDetails) {
+ throw new NotFoundError({ message: `Gateway connection details for gateway ${gateway.id} not found.` });
+ }
+
+ const isGatewayReachable = await withGatewayV2Proxy(
+ async (port) => {
+ return new Promise<boolean>((resolve, reject) => {
+ const socket = new net.Socket();
+ let responseReceived = false;
+ let isResolved = false;
+
+ socket.setTimeout(10000);
+
+ const cleanup = () => {
+ if (!socket.destroyed) {
+ socket.destroy();
+ }
+ };
+
+ socket.on("data", (data: Buffer) => {
+ const response = data.toString().trim();
+ if (response === "PONG" && !isResolved) {
+ isResolved = true;
+ responseReceived = true;
+ cleanup();
+ resolve(true);
+ }
+ });
+
+ socket.on("error", (err: Error) => {
+ if (!isResolved) {
+ isResolved = true;
+ cleanup();
+ reject(new Error(`TCP connection error: ${err.message}`));
+ }
+ });
+
+ socket.on("timeout", () => {
+ if (!isResolved) {
+ isResolved = true;
+ cleanup();
+ reject(new Error("TCP connection timeout"));
+ }
+ });
+
+ socket.on("close", () => {
+ if (!isResolved && !responseReceived) {
+ isResolved = true;
+ cleanup();
+ reject(new Error("Connection closed without receiving PONG"));
+ }
+ });
+
+ socket.connect(port, "localhost");
+ });
+ },
+ {
+ protocol: GatewayProxyProtocol.Ping,
+ relayHost: gatewayV2ConnectionDetails.relayHost,
+ gateway: gatewayV2ConnectionDetails.gateway,
+ relay: gatewayV2ConnectionDetails.relay
+ }
+ );
+
+ if (!isGatewayReachable) {
+ throw new BadRequestError({ message: `Gateway ${gateway.id} is not reachable` });
+ }
+
+ await gatewayV2DAL.updateById(gateway.id, { heartbeat: new Date() });
+ };
+
const heartbeat = async ({ orgPermission }: { orgPermission: OrgServiceActor }) => {
await $validateIdentityAccessToGateway(orgPermission.orgId, orgPermission.id, orgPermission.authMethod);
@@ -934,6 +1031,7 @@ export const gatewayV2ServiceFactory = ({
getPAMConnectionDetails,
deleteGatewayById,
heartbeat,
+ triggerHeartbeat,
getPamSessionKey,
healthcheckNotify
};
diff --git a/frontend/src/hooks/api/gateways-v2/mutations.tsx b/frontend/src/hooks/api/gateways-v2/mutations.tsx
index c3bf8bd1bb..f41f360c9d 100644
--- a/frontend/src/hooks/api/gateways-v2/mutations.tsx
+++ b/frontend/src/hooks/api/gateways-v2/mutations.tsx
@@ -15,3 +15,15 @@ export const useDeleteGatewayV2ById = () => {
}
});
};
+
+export const useTriggerGatewayV2Heartbeat = () => {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: (id: string) => {
+ return apiRequest.post(`/api/v2/gateways/${id}/heartbeat`);
+ },
+ onSuccess: () => {
+ queryClient.invalidateQueries(gatewaysQueryKeys.list());
+ }
+ });
+};
diff --git a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/GatewayTab.tsx b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/GatewayTab.tsx
index 4b5015cab9..d8a1b7b31f 100644
--- a/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/GatewayTab.tsx
+++ b/frontend/src/pages/organization/NetworkingPage/components/GatewayTab/GatewayTab.tsx
@@ -4,6 +4,7 @@ import {
faDoorClosed,
faEdit,
faEllipsisV,
+ faHeartPulse,
faInfoCircle,
faMagnifyingGlass,
faPlus,
@@ -45,7 +46,7 @@ import {
import { withPermission } from "@app/hoc";
import { usePopUp } from "@app/hooks";
import { gatewaysQueryKeys, useDeleteGatewayById } from "@app/hooks/api/gateways";
-import { useDeleteGatewayV2ById } from "@app/hooks/api/gateways-v2";
+import { useDeleteGatewayV2ById, useTriggerGatewayV2Heartbeat } from "@app/hooks/api/gateways-v2";
import { EditGatewayDetailsModal } from "./components/EditGatewayDetailsModal";
import { GatewayDeployModal } from "./components/GatewayDeployModal";
@@ -82,6 +83,22 @@ export const GatewayTab = withPermission(
const deleteGatewayById = useDeleteGatewayById();
const deleteGatewayV2ById = useDeleteGatewayV2ById();
+ const triggerGatewayV2Heartbeat = useTriggerGatewayV2Heartbeat();
+
+ const handleTriggerHealthCheck = async (id: string) => {
+ try {
+ await triggerGatewayV2Heartbeat.mutateAsync(id);
+ createNotification({
+ type: "success",
+ text: "Health check successful - gateway is healthy"
+ });
+ } catch {
+ createNotification({
+ type: "error",
+ text: "Health check failed - gateway is unreachable"
+ });
+ }
+ };
const handleDeleteGateway = async () => {
const data = popUp.deleteGateway.data as { id: string; isV1: boolean };
@@ -186,6 +203,14 @@ export const GatewayTab = withPermission(
>
Copy ID
</DropdownMenuItem>
+ {!el.isV1 && (
+ <DropdownMenuItem
+ icon={<FontAwesomeIcon icon={faHeartPulse} />}
+ onClick={() => handleTriggerHealthCheck(el.id)}
+ >
+ Trigger Health Check
+ </DropdownMenuItem>
+ )}
{el.isV1 && (
<OrgPermissionCan
I={OrgGatewayPermissionActions.EditGateways}