Skip to content

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

fix: refuse deleting a gateway an app connection routes through

Claude0df73e1a710b
diff --git a/backend/e2e-test/routes/v1/gateway-relay-health-alert.spec.ts b/backend/e2e-test/routes/v1/gateway-relay-health-alert.spec.ts
new file mode 100644
index 0000000000..f7ba630a0c
--- /dev/null
+++ b/backend/e2e-test/routes/v1/gateway-relay-health-alert.spec.ts
@@ -0,0 +1,167 @@
+// Health alerts are raised by a cron that runs every five minutes, and a relay only counts as down
+// once its heartbeat is an hour old. The test backdates the stored heartbeat, which is the state
+// that wait would leave, and runs the cron's handler itself rather than waiting for the schedule.
+// Notifications are written straight to the table, which is what the notification queue's worker does.
+import crypto from "node:crypto";
+
+import { TableName } from "@app/db/schemas";
+import { gatewayV2DalFactory } from "@app/ee/services/gateway-v2/gateway-v2-dal";
+import { gatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
+import { relayDalFactory } from "@app/ee/services/relay/relay-dal";
+import { relayServiceFactory } from "@app/ee/services/relay/relay-service";
+import { initLogger } from "@app/lib/logger";
+import { notificationServiceFactory } from "@app/services/notification/notification-service";
+import { NotificationType } from "@app/services/notification/notification-types";
+import { userNotificationDALFactory } from "@app/services/notification/user-notification-dal";
+import { orgDALFactory } from "@app/services/org/org-dal";
+import { userDALFactory } from "@app/services/user/user-dal";
+
+import { createIsolatedOrgAndProject } from "e2e-test/testUtils/fixtures";
+import { createUser, deleteUsers } from "e2e-test/testUtils/users";
+
+type TSentMail = { recipients: string[]; subjectLine: string; substitutions: Record<string, unknown> };
+
+const buildServices = () => {
+ const userNotificationDAL = userNotificationDALFactory(testDb);
+ const notificationService = notificationServiceFactory({
+ userNotificationDAL,
+ notificationQueue: {
+ pushUserNotifications: async (data) => {
+ await userNotificationDAL.batchInsert(data);
+ }
+ }
+ });
+ const sent: TSentMail[] = [];
+ const smtpService = {
+ sendMail: async (mail: TSentMail) => {
+ sent.push(mail);
+ }
+ };
+ const orgDAL = orgDALFactory(testDb);
+ const gatewayV2DAL = gatewayV2DalFactory(testDb);
+
+ const gatewayV2Service = gatewayV2ServiceFactory({
+ gatewayV2DAL,
+ orgDAL,
+ notificationService
+ } as unknown as Parameters<typeof gatewayV2ServiceFactory>[0]);
+ const relayService = relayServiceFactory({
+ relayDAL: relayDalFactory(testDb),
+ orgDAL,
+ notificationService,
+ smtpService,
+ userDAL: userDALFactory(testDb),
+ gatewayV2DAL
+ } as unknown as Parameters<typeof relayServiceFactory>[0]);
+
+ return { gatewayV2Service, relayService, sent };
+};
+
+const listNotifications = async (authToken: string) => {
+ const res = await testServer.inject({
+ method: "GET",
+ url: "/api/v1/notifications/user",
+ headers: { authorization: `Bearer ${authToken}` }
+ });
+ expect(res.statusCode, res.payload).toBe(200);
+ return res.json().notifications as { type: string; title: string; body: string }[];
+};
+
+const createGateway = async (authToken: string) => {
+ const name = `gw-${crypto.randomUUID().slice(0, 8)}`;
+ const res = await testServer.inject({
+ method: "POST",
+ url: "/api/v3/gateways",
+ headers: { authorization: `Bearer ${authToken}` },
+ body: { name, authMethod: { method: "token" } }
+ });
+ expect(res.statusCode, res.payload).toBe(200);
+ return { id: (res.json().gateway?.id ?? res.json().id) as string, name };
+};
+
+describe("Gateway and relay health alerts", () => {
+ let orgId: string;
+ let authToken: string;
+ let cleanup: () => Promise<void>;
+
+ beforeAll(async () => {
+ initLogger();
+ ({ orgId, authToken, cleanup } = await createIsolatedOrgAndProject("health-alert"));
+ });
+
+ afterAll(async () => {
+ await cleanup();
+ });
+
+ test("a gateway whose last probe failed raises one notification for the org admins", async () => {
+ const { gatewayV2Service } = buildServices();
+ const down = await createGateway(authToken);
+ const neverConnected = await createGateway(authToken);
+ await testDb(TableName.GatewayV2)
+ .where({ id: down.id })
+ .update({ heartbeat: new Date(Date.now() - 10 * 60_000), heartbeatTTL: 0 });
+
+ await gatewayV2Service.healthcheckNotify();
+
+ const alerts = (await listNotifications(authToken)).filter((n) => n.type === NotificationType.GATEWAY_HEALTH_ALERT);
+ expect(alerts).toHaveLength(1);
+ expect(alerts[0].title).toBe("Gateway Health Alert");
+ expect(alerts[0].body).toContain(`"${down.name}"`);
+ expect(alerts[0].body).not.toContain(neverConnected.name);
+ const stored = await testDb(TableName.GatewayV2).where({ id: down.id }).first();
+ expect(stored?.healthAlertedAt).toBeTruthy();
+
+ await gatewayV2Service.healthcheckNotify();
+
+ const again = (await listNotifications(authToken)).filter((n) => n.type === NotificationType.GATEWAY_HEALTH_ALERT);
+ expect(again).toHaveLength(1);
+ });
+
+ test("an org relay silent for over an hour raises one notification for the org admins", async () => {
+ const { relayService } = buildServices();
+ const name = `rl-${crypto.randomUUID().slice(0, 8)}`;
+ const fresh = `rl-${crypto.randomUUID().slice(0, 8)}`;
+ await testDb(TableName.Relay).insert([
+ { orgId, name, host: `${name}.relay.test`, heartbeat: new Date(Date.now() - 2 * 60 * 60_000) },
+ { orgId, name: fresh, host: `${fresh}.relay.test`, heartbeat: new Date() }
+ ]);
+
+ await relayService.healthcheckNotify();
+
+ const alerts = (await listNotifications(authToken)).filter((n) => n.type === NotificationType.RELAY_HEALTH_ALERT);
+ expect(alerts).toHaveLength(1);
+ expect(alerts[0].title).toBe("Relay Health Alert");
+ expect(alerts[0].body).toContain(`"${name}"`);
+ expect(alerts[0].body).not.toContain(fresh);
+
+ await relayService.healthcheckNotify();
+
+ const again = (await listNotifications(authToken)).filter((n) => n.type === NotificationType.RELAY_HEALTH_ALERT);
+ expect(again).toHaveLength(1);
+ });
+
+ test("an instance relay silent for over an hour is mailed to the server admins", async () => {
+ const { relayService, sent } = buildServices();
+ const admin = await createUser("relay-health-admin");
+ await testDb(TableName.Users).where({ id: admin.userId }).update({ superAdmin: true });
+ const name = `rl-${crypto.randomUUID().slice(0, 8)}`;
+ const [relay] = await testDb(TableName.Relay)
+ .insert({ name, host: `${name}.relay.test`, heartbeat: new Date(Date.now() - 2 * 60 * 60_000) })
+ .returning("id");
+
+ try {
+ await relayService.healthcheckNotify();
+
+ const mail = sent.find(
+ (m) => m.subjectLine === "Relay Health Alert" && String(m.substitutions.names).includes(name)
+ );
+ expect(mail?.recipients).toContain(admin.username);
+ expect(mail?.substitutions.type).toBe("instance-relay");
+ } finally {
+ await testDb(TableName.Relay)
+ .where({ id: (relay as { id: string }).id })
+ .delete();
+ await deleteUsers([admin.userId]);
+ }
+ });
+});
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 dcb8314164..e82036b9e2 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -1149,6 +1149,15 @@ export const gatewayV2ServiceFactory = ({
});
}
+ // app_connections.gatewayId has no foreign key, so nothing in the database stops a delete that
+ // would leave a connection routing through a gateway that no longer exists.
+ const appConnections = await appConnectionDAL.findByGatewayId(gateway.id);
+ if (appConnections.length) {
+ throw new BadRequestError({
+ message: `Gateway '${gateway.name}' is used by app connection(s) ${appConnections.map((c) => `'${c.name}'`).join(", ")}. Remove the gateway from those connections before deleting it.`
+ });
+ }
+
try {
return await gatewayV2DAL.deleteById(gateway.id);
} catch (err) {
diff --git a/tests/suites/gateway/gateway_test.go b/tests/suites/gateway/gateway_test.go
index d6bcdf2919..185b04085b 100644
--- a/tests/suites/gateway/gateway_test.go
+++ b/tests/suites/gateway/gateway_test.go
@@ -288,6 +288,56 @@ func TestGateway_MemberPermission(t *testing.T) {
})
}
+func TestGateway_Delete(t *testing.T) {
+ t.Parallel()
+ h := harness.From(t)
+
+ t.Run("should refuse deleting a gateway an app connection routes through, naming the connection", func(t *testing.T) {
+ t.Parallel()
+
+ // Setup
+ tn := h.NewTenant(t)
+ gw := newGateway(t, tn)
+ conn := fixture.NewAppConnection(t, tn, fixture.PostgresAppConnection(fixture.OpenPostgresTarget(t, tn)))
+ attached := updateConnection(t, tn.Admin.API, conn.ID, api.UpdatePostgresAppConnectionJSONRequestBody{GatewayId: &gw.ID})
+ requireStatus(t, http.StatusOK, attached.StatusCode(), attached.Body)
+
+ // Action
+ res, err := tn.Admin.API.DeleteGatewayWithResponse(t.Context(), gw.ID.String())
+
+ // Assert
+ require.NoError(t, err)
+ requireStatus(t, http.StatusBadRequest, res.StatusCode(), res.Body)
+ require.Contains(t, string(res.Body), conn.Name)
+ require.Equal(t, []uuid.UUID{gw.ID}, gatewayIDs(t, tn.Admin.API))
+ require.Equal(t, &gw.ID, connectionRouteOf(t, tn, conn.ID).GatewayID)
+ })
+
+ t.Run("should delete a gateway once no app connection routes through it", func(t *testing.T) {
+ t.Parallel()
+
+ // Setup
+ tn := h.NewTenant(t)
+ gw := newGateway(t, tn)
+ connID := postgresConnection(t, tn)
+ attached := updateConnection(t, tn.Admin.API, connID, api.UpdatePostgresAppConnectionJSONRequestBody{GatewayId: &gw.ID})
+ requireStatus(t, http.StatusOK, attached.StatusCode(), attached.Body)
+ detached, err := tn.Admin.API.UpdatePostgresAppConnectionWithBodyWithResponse(t.Context(), connID,
+ "application/json", jsonBody(t, map[string]any{"gatewayId": nil}))
+ require.NoError(t, err)
+ requireStatus(t, http.StatusOK, detached.StatusCode(), detached.Body)
+ require.Nil(t, connectionRouteOf(t, tn, connID).GatewayID)
+
+ // Action
+ res, err := tn.Admin.API.DeleteGatewayWithResponse(t.Context(), gw.ID.String())
+
+ // Assert
+ require.NoError(t, err)
+ requireStatus(t, http.StatusOK, res.StatusCode(), res.Body)
+ require.Empty(t, gatewayIDs(t, tn.Admin.API))
+ })
+}
+
func TestGateway_List(t *testing.T) {
t.Parallel()
h := harness.From(t)