Skip to content

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

fix(pam): send cancellation signal to gateway when a session expires

Andreyc46d47a9d4a7
diff --git a/backend/src/ee/services/pam-session/pam-session-expiration-queue.ts b/backend/src/ee/services/pam-session/pam-session-expiration-queue.ts
index 066d66b8c8..64506527c4 100644
--- a/backend/src/ee/services/pam-session/pam-session-expiration-queue.ts
+++ b/backend/src/ee/services/pam-session/pam-session-expiration-queue.ts
@@ -1,3 +1,4 @@
+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { getConfig } from "@app/lib/config/env";
import { logger } from "@app/lib/logger";
import { QueueJobs, QueueName, TQueueServiceFactory } from "@app/queue";
@@ -6,8 +7,9 @@ import { TTelemetryServiceFactory } from "@app/services/telemetry/telemetry-serv
import { TUserDALFactory } from "@app/services/user/user-dal";
import { PamSessionEndReason } from "../pam/pam-enums";
+import { resolveSessionActor } from "./pam-session-access-fns";
import { TPamSessionDALFactory } from "./pam-session-dal";
-import { reportPamSessionEnded } from "./pam-session-fns";
+import { reportPamSessionEnded, sendPamSessionCancellationSignal } from "./pam-session-fns";
type TPamSessionExpirationServiceFactoryDep = {
queueService: TQueueServiceFactory;
@@ -15,6 +17,7 @@ type TPamSessionExpirationServiceFactoryDep = {
projectDAL: Pick<TProjectDALFactory, "findById">;
telemetryService: Pick<TTelemetryServiceFactory, "sendPostHogEvents">;
userDAL: Pick<TUserDALFactory, "findById">;
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPAMConnectionDetails">;
};
export type TPamSessionExpirationServiceFactory = ReturnType<typeof pamSessionExpirationServiceFactory>;
@@ -24,7 +27,8 @@ export const pamSessionExpirationServiceFactory = ({
pamSessionDAL,
projectDAL,
telemetryService,
- userDAL
+ userDAL,
+ gatewayV2Service
}: TPamSessionExpirationServiceFactoryDep) => {
const appCfg = getConfig();
@@ -41,6 +45,21 @@ export const pamSessionExpirationServiceFactory = ({
if (updated) {
logger.info({ sessionId }, `${QueueName.PamSessionExpiration}: session expired [sessionId=${sessionId}]`);
+ // endSessionById is status-filtered, so reaching here means nothing else has signalled the gateway
+ if (updated.gatewayId) {
+ const sessionActor = resolveSessionActor(updated);
+ sendPamSessionCancellationSignal({
+ sessionId,
+ gatewayId: updated.gatewayId,
+ accountType: updated.accountType,
+ actorId: sessionActor?.id ?? "",
+ actorType: sessionActor?.type,
+ // Machine identity sessions carry no email
+ actorEmail: updated.actorEmail || updated.actorName,
+ gatewayV2Service
+ });
+ }
+
const project = await projectDAL.findById(updated.projectId);
if (project?.orgId) {
void reportPamSessionEnded({
diff --git a/backend/src/ee/services/pam-session/pam-session-fns.ts b/backend/src/ee/services/pam-session/pam-session-fns.ts
index 825c1afe31..ef2c894792 100644
--- a/backend/src/ee/services/pam-session/pam-session-fns.ts
+++ b/backend/src/ee/services/pam-session/pam-session-fns.ts
@@ -75,12 +75,13 @@ export const reportPamSessionEnded = async ({
// Flipping a session row to terminated does not cut a live tunnel; only this ALPN signal does. Sent
// best-effort (fire-and-forget) so callers don't block on the gateway round-trip, and shared by every
-// termination path (manual terminate, grant revocation) so they can't drift.
+// termination path (manual terminate, grant revocation, expiry) so they can't drift.
export const sendPamSessionCancellationSignal = ({
sessionId,
gatewayId,
accountType,
actorId,
+ actorType = ActorType.USER,
actorEmail,
gatewayV2Service
}: {
@@ -88,6 +89,7 @@ export const sendPamSessionCancellationSignal = ({
gatewayId: string;
accountType: string;
actorId: string;
+ actorType?: ActorType.USER | ActorType.IDENTITY;
actorEmail: string;
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPAMConnectionDetails">;
}) => {
@@ -100,7 +102,7 @@ export const sendPamSessionCancellationSignal = ({
accountType: accountType as PamAccountType,
host: "0.0.0.0",
port: 0,
- actorMetadata: { id: actorId, type: ActorType.USER, name: actorEmail }
+ actorMetadata: { id: actorId, type: actorType, name: actorEmail }
});
if (!certs) {
logger.error(
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 50da471dff..b13bb3bcfe 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1894,7 +1894,8 @@ export const registerRoutes = async (
pamSessionDAL,
projectDAL,
telemetryService,
- userDAL
+ userDAL,
+ gatewayV2Service
});
const pamAccessRequestService = pamAccessRequestServiceFactory({