Skip to content

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

refactor(gateways): make the transport a real enum

bernie-g21b7a47dd9a8
diff --git a/backend/src/ee/routes/v1/pam-routers/pam-session-router.ts b/backend/src/ee/routes/v1/pam-routers/pam-session-router.ts
index 6365e1a9df..2a9bfde4b2 100644
--- a/backend/src/ee/routes/v1/pam-routers/pam-session-router.ts
+++ b/backend/src/ee/routes/v1/pam-routers/pam-session-router.ts
@@ -3,6 +3,7 @@ import z from "zod";
import { PamSessionsSchema } from "@app/db/schemas";
import { EventType, UserAgentType } from "@app/ee/services/audit-log/audit-log-types";
+import { GatewayTransport } from "@app/ee/services/gateway-v2/gateway-v2-constants";
import { PamAccessMethod, PamAccountType, PamSessionStatus } from "@app/ee/services/pam/pam-enums";
import { PamPolicyRulesSchema } from "@app/ee/services/pam/pam-policies";
import { hostPattern } from "@app/ee/services/pam-account/pam-account-schemas";
@@ -325,7 +326,7 @@ export const registerPamWebAccessRouter = async (server: FastifyZodProvider) =>
.optional()
.describe("Target host to connect to, for accounts that allow multiple hosts"),
supportedTransports: z
- .array(z.enum(["relay", "direct"]))
+ .array(z.nativeEnum(GatewayTransport))
.max(2)
.optional()
.describe(
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index e2ac8dfe29..236631890b 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -1,4 +1,5 @@
import { ProjectType } from "@app/db/schemas";
+import { GatewayTransport } from "@app/ee/services/gateway-v2/gateway-v2-constants";
import { HoneyTokenType } from "@app/ee/services/honey-token/honey-token-enums";
import { ScepChallengeType } from "@app/ee/services/pki-scep/challenge";
import { ScepEnrollmentStatus } from "@app/ee/services/pki-scep/pki-scep-types";
@@ -7001,7 +7002,7 @@ interface GatewayConnectEvent {
metadata: {
gatewayId: string;
name: string;
- transports: ("direct" | "relay")[];
+ transports: GatewayTransport[];
directAddress?: string;
relayName?: string;
};
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts b/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts
index 992e8ecd08..6ef7a92f70 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts
@@ -39,7 +39,12 @@ export const buildGatewayReachableSql = (table: string) => {
export const buildGatewayProbedSql = (table: string) =>
`("${table}"."directHeartbeat" IS NOT NULL OR "${table}"."heartbeat" IS NOT NULL)`;
-export type TGatewayTransport = "direct" | "relay";
+export enum GatewayTransport {
+ Direct = "direct",
+ Relay = "relay"
+}
+
+export type TGatewayTransport = GatewayTransport;
export const resolveTransports = ({
gateway,
@@ -55,12 +60,12 @@ export const resolveTransports = ({
}) => {
const directUsable =
Boolean(gateway.directAddress) &&
- (transport === "direct" ||
+ (transport === GatewayTransport.Direct ||
!gateway.relayId ||
isTransportHealthy({ probedAt: gateway.directHeartbeat, heartbeatTTL: gateway.heartbeatTTL }));
- const useDirect = transport !== "relay" && directUsable;
+ const useDirect = transport !== GatewayTransport.Relay && directUsable;
// Relay credentials ride along even when direct is chosen, so a dial can retry before the probe goes stale.
- const useRelay = transport !== "direct" && Boolean(gateway.relayId);
+ const useRelay = transport !== GatewayTransport.Direct && Boolean(gateway.relayId);
return { useDirect, useRelay, hasTransport: useDirect || useRelay };
};
@@ -73,8 +78,8 @@ export const gatewayTransports = ({
relayHost?: string;
}): TGatewayTransport[] => {
const transports: TGatewayTransport[] = [];
- if (directAddress) transports.push("direct");
- if (relayHost) transports.push("relay");
+ if (directAddress) transports.push(GatewayTransport.Direct);
+ if (relayHost) transports.push(GatewayTransport.Relay);
return transports;
};
@@ -91,9 +96,11 @@ export const resolveClientTransports = ({
};
supportedTransports?: TGatewayTransport[];
}) => {
- const clientAllowsDirect = supportedTransports === undefined || supportedTransports.includes("direct");
+ const clientAllowsDirect = supportedTransports === undefined || supportedTransports.includes(GatewayTransport.Direct);
const allowRelay =
- supportedTransports === undefined || supportedTransports.length === 0 || supportedTransports.includes("relay");
+ supportedTransports === undefined ||
+ supportedTransports.length === 0 ||
+ supportedTransports.includes(GatewayTransport.Relay);
// Skip a stale direct address only when the relay can take over; otherwise it is the client's only path.
const canFallBackToRelay = allowRelay && Boolean(gateway.relayId);
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 07db072a73..6f0f3c0f39 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -47,6 +47,7 @@ import {
DEFAULT_HEARTBEAT_TTL,
GATEWAY_ACTOR_OID,
GATEWAY_ROUTING_INFO_OID,
+ GatewayTransport,
PAM_INFO_OID,
parseDirectAddress,
resolveClientTransports,
@@ -353,7 +354,7 @@ export const gatewayV2ServiceFactory = ({
gatewayId: string;
targetHost: string;
targetPort: number;
- transport?: "direct" | "relay";
+ transport?: GatewayTransport;
}): Promise<TGatewayV2ConnectionDetails | undefined> => {
const gateway = await gatewayV2DAL.findById(gatewayId);
if (!gateway) {
@@ -514,7 +515,7 @@ export const gatewayV2ServiceFactory = ({
host: string;
port?: number;
actorMetadata: { id: string; type: ActorType; name: string };
- supportedTransports?: ("relay" | "direct")[];
+ supportedTransports?: GatewayTransport[];
}) => {
const gateway = await gatewayV2DAL.findById(gatewayId);
if (!gateway) {
@@ -887,7 +888,7 @@ export const gatewayV2ServiceFactory = ({
}
};
- const $checkGatewayHealth = async (gatewayId: string, transport?: "direct" | "relay") => {
+ const $checkGatewayHealth = async (gatewayId: string, transport?: GatewayTransport) => {
const gatewayV2ConnectionDetails = await getPlatformConnectionDetailsByGatewayId({
gatewayId,
targetHost: "health-check",
@@ -988,7 +989,9 @@ export const gatewayV2ServiceFactory = ({
await gatewayV2DAL.updateById(
gatewayId,
- transport === "direct" ? { directHeartbeat: new Date(), heartbeatTTL } : { heartbeat: new Date(), heartbeatTTL }
+ transport === GatewayTransport.Direct
+ ? { directHeartbeat: new Date(), heartbeatTTL }
+ : { heartbeat: new Date(), heartbeatTTL }
);
};
@@ -996,9 +999,9 @@ export const gatewayV2ServiceFactory = ({
const gateway = await gatewayV2DAL.findById(gatewayId);
if (!gateway) throw new NotFoundError({ message: `Gateway ${gatewayId} not found` });
- const transports: ("direct" | "relay")[] = [];
- if (gateway.directAddress) transports.push("direct");
- if (gateway.relayId) transports.push("relay");
+ const transports: GatewayTransport[] = [];
+ if (gateway.directAddress) transports.push(GatewayTransport.Direct);
+ if (gateway.relayId) transports.push(GatewayTransport.Relay);
if (transports.length === 0) {
throw new BadRequestError({ message: `Gateway ${gatewayId} has no configured connection transport` });
}
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-transport.test.ts b/backend/src/ee/services/gateway-v2/gateway-v2-transport.test.ts
index 454439ed81..dd54f78e21 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-transport.test.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-transport.test.ts
@@ -1,4 +1,5 @@
import {
+ GatewayTransport,
HEARTBEAT_BUFFER_SECONDS,
isTransportHealthy,
parseDirectAddress,
@@ -98,7 +99,9 @@ describe("resolveTransports", () => {
describe("an explicit transport is honoured, because a health probe has to target one path", () => {
test("direct is used even when its own probe is stale", () => {
- expect(resolveTransports({ gateway: { ...dual, directHeartbeat: stale }, transport: "direct" })).toEqual({
+ expect(
+ resolveTransports({ gateway: { ...dual, directHeartbeat: stale }, transport: GatewayTransport.Direct })
+ ).toEqual({
useDirect: true,
useRelay: false,
hasTransport: true
@@ -106,7 +109,9 @@ describe("resolveTransports", () => {
});
test("relay is used even when direct is healthy", () => {
- expect(resolveTransports({ gateway: { ...dual, directHeartbeat: fresh }, transport: "relay" })).toEqual({
+ expect(
+ resolveTransports({ gateway: { ...dual, directHeartbeat: fresh }, transport: GatewayTransport.Relay })
+ ).toEqual({
useDirect: false,
useRelay: true,
hasTransport: true
@@ -114,7 +119,9 @@ describe("resolveTransports", () => {
});
test("asking for direct on a relay-only gateway leaves nothing to dial", () => {
- expect(resolveTransports({ gateway: { ...relay, directHeartbeat: null }, transport: "direct" })).toMatchObject({
+ expect(
+ resolveTransports({ gateway: { ...relay, directHeartbeat: null }, transport: GatewayTransport.Direct })
+ ).toMatchObject({
useDirect: false,
hasTransport: false
});
@@ -147,20 +154,26 @@ describe("resolveClientTransports", () => {
expect(
resolveClientTransports({
gateway: { ...dual, directHeartbeat: stale },
- supportedTransports: ["direct", "relay"]
+ supportedTransports: [GatewayTransport.Direct, GatewayTransport.Relay]
})
).toMatchObject({ useDirect: false, useRelay: true, hasTransport: true });
});
test("a stale direct probe is still used when it is the only path the client has", () => {
expect(
- resolveClientTransports({ gateway: { ...direct, directHeartbeat: stale }, supportedTransports: ["direct"] })
+ resolveClientTransports({
+ gateway: { ...direct, directHeartbeat: stale },
+ supportedTransports: [GatewayTransport.Direct]
+ })
).toMatchObject({ useDirect: true, useRelay: false, hasTransport: true });
});
test("a stale direct probe is still used when the client cannot dial the relay", () => {
expect(
- resolveClientTransports({ gateway: { ...dual, directHeartbeat: stale }, supportedTransports: ["direct"] })
+ resolveClientTransports({
+ gateway: { ...dual, directHeartbeat: stale },
+ supportedTransports: [GatewayTransport.Direct]
+ })
).toMatchObject({ useDirect: true, useRelay: false, hasTransport: true });
});
diff --git a/backend/src/ee/services/pam-session/pam-session-service.ts b/backend/src/ee/services/pam-session/pam-session-service.ts
index dca3ae3b5b..dd0060c67c 100644
--- a/backend/src/ee/services/pam-session/pam-session-service.ts
+++ b/backend/src/ee/services/pam-session/pam-session-service.ts
@@ -1,6 +1,7 @@
import RE2 from "re2";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
+import { GatewayTransport } from "@app/ee/services/gateway-v2/gateway-v2-constants";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
@@ -457,7 +458,7 @@ export const pamSessionServiceFactory = ({
tokenVersionId?: string;
accessMethod?: PamAccessMethod;
targetHost?: string;
- supportedTransports?: ("relay" | "direct")[];
+ supportedTransports?: GatewayTransport[];
}) => {
const account = await resolveAccountByPath(projectId, path);