Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import z from "zod";
import { GatewaysV2Schema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { gatewayTransports } from "@app/ee/services/gateway-v2/gateway-v2-transport-fns";
import { GATEWAYS } from "@app/lib/api-docs";
import { zodBuffer } from "@app/lib/zod";
import { gatewayMetricsReportLimit, readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { slugSchema } from "@app/server/lib/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
const SanitizedGatewayV2Schema = GatewaysV2Schema.pick({
id: true,
identityId: true,
relayId: true,
name: true,
createdAt: true,
updatedAt: true,
heartbeat: true,
directAddress: true,
directHeartbeat: true,
heartbeatTTL: true,
capabilities: true
});
export const registerGatewayV2Router = async (server: FastifyZodProvider) => {
server.route({
method: "POST",
url: "/",
schema: {
operationId: "registerGateway",
body: z
.object({
relayName: slugSchema({ min: 1, max: 32, field: "relayName" }).optional(),
directAddress: z.string().trim().min(3).max(255).optional(),
name: slugSchema({ min: 1, max: 64, field: "name" })
})
.refine((body) => Boolean(body.relayName || body.directAddress), {
message: "Either relayName or directAddress is required"
}),
response: {
200: z.object({
gatewayId: z.string(),
directAddress: z.string().optional(),
relayHost: z.string().optional(),
pki: z.object({
serverCertificate: z.string(),
serverPrivateKey: z.string(),
clientCertificateChain: z.string()
}),
ssh: z
.object({
clientCertificate: z.string(),
clientPrivateKey: z.string(),
serverCAPublicKey: z.string()
})
.optional()
})
}
},
config: {
rateLimit: writeLimit
},
onRequest: verifyAuth([AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.GATEWAY_ACCESS_TOKEN]),
handler: async (req) => {
const registered = await server.services.gatewayV2.registerGateway({
orgId: req.permission.orgId,
relayName: req.body.relayName,
actorId: req.permission.id,
actorType: req.permission.type,
actorAuthMethod: req.permission.authMethod,
directAddress: req.body.directAddress,
name: req.body.name,
tokenVersion: req.auth.authMode === AuthMode.GATEWAY_ACCESS_TOKEN ? req.auth.token.tokenVersion : undefined
});
await server.services.auditLog.createAuditLog({
...req.auditLogInfo,
orgId: req.permission.orgId,
event: {
type: EventType.GATEWAY_CONNECT,
metadata: {
gatewayId: registered.gatewayId,
name: registered.gatewayName,
transports: gatewayTransports(registered),
directAddress: registered.directAddress,
relayName: req.body.relayName
}
}
});
return registered;
}
});
// Heartbeat is expensive: the platform dials back through the relay to check the gateway is
// really there, then writes to Postgres. Every gateway reports load every 10 seconds, which is
// far too often for that. This one just writes a Redis key. It does not touch heartbeat, so a
// gateway cannot look alive just by reporting load.
server.route({
method: "POST",
url: "/metrics",
config: {
rateLimit: gatewayMetricsReportLimit
},
schema: {
operationId: "gatewayMetricsReport",
body: z.object({
activeChannels: z.number().int().min(0).max(1_000_000).describe(GATEWAYS.METRICS_REPORT.activeChannels)
}),
response: {
200: z.object({
gatewayId: z.string().uuid().describe(GATEWAYS.METRICS_REPORT.gatewayId),
activeChannels: z.number().int().describe(GATEWAYS.METRICS_REPORT.activeChannels)
})
}
},
onRequest: verifyAuth([AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.GATEWAY_ACCESS_TOKEN]),
handler: async (req) => {
return server.services.gatewayV2.reportMetrics({
orgPermission: req.permission,
activeChannels: req.body.activeChannels
});
}
});
server.route({
method: "POST",
url: "/heartbeat",
config: {
rateLimit: writeLimit
},
schema: {
operationId: "gatewayHeartbeat",
body: z
.object({
capabilities: z
.object({
pkcs11: z.boolean().optional(),
pkcs11MlDsa: z.boolean().optional(),
sessionLogMaskingBuiltInDetection: z.boolean().optional(),
clickhouseNativeProtocol: z.boolean().optional(),
certificateScan: z.boolean().optional(),
windowsCertificateScan: z.boolean().optional(),
supported_account_types: z.array(z.string().trim().max(64)).max(64).optional()
})
.optional()
})
.nullish(),
response: {
200: z.object({
message: z.string()
})
}
},
onRequest: verifyAuth([AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.GATEWAY_ACCESS_TOKEN]),
handler: async (req) => {
await server.services.gatewayV2.heartbeat({
orgPermission: req.permission,
capabilities: req.body?.capabilities
});
return { message: "Successfully triggered heartbeat" };
}
});
server.route({
method: "GET",
url: "/",
schema: {
operationId: "listGateways",
response: {
200: SanitizedGatewayV2Schema.extend({
identity: z.object({ name: z.string(), id: z.string() }).nullable(),
connectedResourcesCount: z.number()
}).array()
}
},
config: {
rateLimit: readLimit
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.OAUTH]),
handler: async (req) => {
const gateways = await server.services.gatewayV2.listGateways({
orgPermission: req.permission
});
return gateways;
}
});
server.route({
method: "DELETE",
url: "/:id",
config: {
rateLimit: writeLimit
},
schema: {
operationId: "deleteGateway",
params: z.object({
id: z.string()
}),
response: {
200: SanitizedGatewayV2Schema
}
},
onRequest: verifyAuth([AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.JWT]),
handler: async (req) => {
const gateway = await server.services.gatewayV2.deleteGatewayById({
orgPermission: req.permission,
id: req.params.id
});
await server.services.auditLog.createAuditLog({
...req.auditLogInfo,
orgId: req.permission.orgId,
event: {
type: EventType.GATEWAY_DELETE,
metadata: { gatewayId: gateway.id, name: gateway.name }
}
});
return gateway;
}
});
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",
config: {
rateLimit: readLimit
},
schema: {
operationId: "getGatewayPamSessionKey",
response: {
200: zodBuffer
}
},
onRequest: verifyAuth([AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.GATEWAY_ACCESS_TOKEN]),
handler: async (req) => {
const pamSessionKey = await server.services.gatewayV2.getPamSessionKey({
orgPermission: req.permission
});
return pamSessionKey;
}
});
server.route({
method: "GET",
url: "/:id/resources",
config: {
rateLimit: readLimit
},
schema: {
operationId: "getGatewayConnectedResources",
params: z.object({
id: z.string().uuid()
}),
response: {
200: z.object({
appConnections: z.array(
z.object({
id: z.string(),
name: z.string(),
app: z.string(),
projectId: z.string().nullish(),
projectName: z.string().nullish()
})
),
dynamicSecrets: z.array(
z.object({
id: z.string(),
name: z.string(),
folderId: z.string(),
projectId: z.string(),
projectName: z.string(),
environmentSlug: z.string()
})
),
kubernetesAuths: z.array(
z.object({
id: z.string(),
identityId: z.string(),
identityName: z.string()
})
),
pkiDiscoveryConfigs: z.array(
z.object({
id: z.string(),
name: z.string(),
projectId: z.string(),
projectName: z.string()
})
),
pamAccounts: z.array(
z.object({
id: z.string(),
name: z.string(),
accountType: z.string(),
folderName: z.string().nullable()
})
),
pamAccountTemplates: z.array(
z.object({
id: z.string(),
name: z.string(),
type: z.string()
})
)
})
}
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.OAUTH]),
handler: async (req) => {
const resources = await server.services.gatewayV2.getConnectedResources({
orgPermission: req.permission,
gatewayId: req.params.id
});
return resources;
}
});
};