Skip to content

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

feat(pam): real-time session log sync via incremental batch uploads

Bernie Gandinc1faca1aba7b
diff --git a/backend/src/@types/knex.d.ts b/backend/src/@types/knex.d.ts
index 354b77ea1a..27f5c05471 100644
--- a/backend/src/@types/knex.d.ts
+++ b/backend/src/@types/knex.d.ts
@@ -712,6 +712,11 @@ import {
TPamResourceRotationRulesUpdate
} from "@app/db/schemas/pam-resource-rotation-rules";
import { TPamResources, TPamResourcesInsert, TPamResourcesUpdate } from "@app/db/schemas/pam-resources";
+import {
+ TPamSessionEventBatches,
+ TPamSessionEventBatchesInsert,
+ TPamSessionEventBatchesUpdate
+} from "@app/db/schemas/pam-session-event-batches";
import { TPamSessions, TPamSessionsInsert, TPamSessionsUpdate } from "@app/db/schemas/pam-sessions";
import {
TProjectMicrosoftTeamsConfigs,
@@ -1592,6 +1597,11 @@ declare module "knex/types/tables" {
>;
[TableName.PamAccount]: KnexOriginal.CompositeTableType<TPamAccounts, TPamAccountsInsert, TPamAccountsUpdate>;
[TableName.PamSession]: KnexOriginal.CompositeTableType<TPamSessions, TPamSessionsInsert, TPamSessionsUpdate>;
+ [TableName.PamSessionEventBatch]: KnexOriginal.CompositeTableType<
+ TPamSessionEventBatches,
+ TPamSessionEventBatchesInsert,
+ TPamSessionEventBatchesUpdate
+ >;
[TableName.PamDiscoverySource]: KnexOriginal.CompositeTableType<
TPamDiscoverySources,
TPamDiscoverySourcesInsert,
diff --git a/backend/src/db/migrations/20260407000001_pam-session-event-batches.ts b/backend/src/db/migrations/20260407000001_pam-session-event-batches.ts
new file mode 100644
index 0000000000..3fb679596f
--- /dev/null
+++ b/backend/src/db/migrations/20260407000001_pam-session-event-batches.ts
@@ -0,0 +1,30 @@
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+import { createOnUpdateTrigger, dropOnUpdateTrigger } from "../utils";
+
+export async function up(knex: Knex): Promise<void> {
+ if (!(await knex.schema.hasTable(TableName.PamSessionEventBatch))) {
+ await knex.schema.createTable(TableName.PamSessionEventBatch, (t) => {
+ t.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid());
+
+ t.uuid("sessionId").notNullable();
+ t.foreign("sessionId").references("id").inTable(TableName.PamSession).onDelete("CASCADE");
+ t.index("sessionId");
+
+ t.bigInteger("startOffset").notNullable();
+ t.binary("encryptedEventsBlob").notNullable();
+
+ t.timestamps(true, true, true);
+
+ t.unique(["sessionId", "startOffset"]);
+ });
+
+ await createOnUpdateTrigger(knex, TableName.PamSessionEventBatch);
+ }
+}
+
+export async function down(knex: Knex): Promise<void> {
+ await dropOnUpdateTrigger(knex, TableName.PamSessionEventBatch);
+ await knex.schema.dropTableIfExists(TableName.PamSessionEventBatch);
+}
diff --git a/backend/src/db/schemas/index.ts b/backend/src/db/schemas/index.ts
index 3dbfc57412..1ee9a97033 100644
--- a/backend/src/db/schemas/index.ts
+++ b/backend/src/db/schemas/index.ts
@@ -120,6 +120,7 @@ export * from "./pam-folders";
export * from "./pam-resource-favorites";
export * from "./pam-resource-rotation-rules";
export * from "./pam-resources";
+export * from "./pam-session-event-batches";
export * from "./pam-sessions";
export * from "./pki-acme-accounts";
export * from "./pki-acme-auths";
diff --git a/backend/src/db/schemas/models.ts b/backend/src/db/schemas/models.ts
index 134cde6731..33752dd9ef 100644
--- a/backend/src/db/schemas/models.ts
+++ b/backend/src/db/schemas/models.ts
@@ -227,6 +227,7 @@ export enum TableName {
PamResource = "pam_resources",
PamAccount = "pam_accounts",
PamSession = "pam_sessions",
+ PamSessionEventBatch = "pam_session_event_batches",
PamDiscoverySource = "pam_discovery_sources",
PamDiscoverySourceRun = "pam_discovery_source_runs",
PamDiscoverySourceResource = "pam_discovery_source_resources",
diff --git a/backend/src/db/schemas/pam-session-event-batches.ts b/backend/src/db/schemas/pam-session-event-batches.ts
new file mode 100644
index 0000000000..7f565c5361
--- /dev/null
+++ b/backend/src/db/schemas/pam-session-event-batches.ts
@@ -0,0 +1,25 @@
+// Code generated by automation script, DO NOT EDIT.
+// Automated by pulling database and generating zod schema
+// To update. Just run npm run generate:schema
+// Written by akhilmhdh.
+
+import { z } from "zod";
+
+import { zodBuffer } from "@app/lib/zod";
+
+import { TImmutableDBKeys } from "./models";
+
+export const PamSessionEventBatchesSchema = z.object({
+ id: z.string().uuid(),
+ sessionId: z.string().uuid(),
+ startOffset: z.coerce.number(),
+ encryptedEventsBlob: zodBuffer,
+ createdAt: z.date(),
+ updatedAt: z.date()
+});
+
+export type TPamSessionEventBatches = z.infer<typeof PamSessionEventBatchesSchema>;
+export type TPamSessionEventBatchesInsert = Omit<z.input<typeof PamSessionEventBatchesSchema>, TImmutableDBKeys>;
+export type TPamSessionEventBatchesUpdate = Partial<
+ Omit<z.input<typeof PamSessionEventBatchesSchema>, TImmutableDBKeys>
+>;
diff --git a/backend/src/ee/routes/v1/pam-session-router.ts b/backend/src/ee/routes/v1/pam-session-router.ts
index bc9face220..f481b9b984 100644
--- a/backend/src/ee/routes/v1/pam-session-router.ts
+++ b/backend/src/ee/routes/v1/pam-session-router.ts
@@ -28,6 +28,10 @@ const SessionCredentialsSchema = z.union([
]);
export const registerPamSessionRouter = async (server: FastifyZodProvider) => {
+ server.addContentTypeParser("application/octet-stream", { parseAs: "buffer" }, (_req, body, done) => {
+ done(null, body);
+ });
+
// Meant to be hit solely by gateway identities
server.route({
method: "GET",
@@ -320,4 +324,52 @@ export const registerPamSessionRouter = async (server: FastifyZodProvider) => {
return response;
}
});
+
+ // Meant to be hit solely by gateway identities
+ server.route({
+ method: "POST",
+ url: "/:sessionId/event-batches",
+ config: {
+ rateLimit: writeLimit
+ },
+ schema: {
+ description: "Upload a PAM session event batch",
+ params: z.object({
+ sessionId: z.string().uuid()
+ }),
+ querystring: z.object({
+ startOffset: z.coerce.number().int().nonnegative()
+ }),
+ body: z.instanceof(Buffer),
+ response: {
+ 200: z.object({ ok: z.literal(true) })
+ }
+ },
+ onRequest: verifyAuth([AuthMode.IDENTITY_ACCESS_TOKEN]),
+ handler: async (req) => {
+ const { projectId } = await server.services.pamSession.uploadEventBatch(
+ {
+ sessionId: req.params.sessionId,
+ startOffset: req.query.startOffset,
+ events: req.body
+ },
+ req.permission
+ );
+
+ await server.services.auditLog.createAuditLog({
+ ...req.auditLogInfo,
+ orgId: req.permission.orgId,
+ projectId,
+ event: {
+ type: EventType.PAM_SESSION_EVENT_BATCH_UPLOAD,
+ metadata: {
+ sessionId: req.params.sessionId,
+ startOffset: req.query.startOffset
+ }
+ }
+ });
+
+ return { ok: true as const };
+ }
+ });
};
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 817b71b417..543a142065 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -596,6 +596,7 @@ export enum EventType {
PAM_SESSION_TERMINATE = "pam-session-terminate",
PAM_SESSION_GET = "pam-session-get",
PAM_SESSION_LIST = "pam-session-list",
+ PAM_SESSION_EVENT_BATCH_UPLOAD = "pam-session-event-batch-upload",
PAM_FOLDER_CREATE = "pam-folder-create",
PAM_FOLDER_UPDATE = "pam-folder-update",
PAM_FOLDER_DELETE = "pam-folder-delete",
@@ -4746,6 +4747,14 @@ interface PamSessionListEvent {
};
}
+interface PamSessionEventBatchUploadEvent {
+ type: EventType.PAM_SESSION_EVENT_BATCH_UPLOAD;
+ metadata: {
+ sessionId: string;
+ startOffset: number;
+ };
+}
+
interface PamFolderCreateEvent {
type: EventType.PAM_FOLDER_CREATE;
metadata: {
@@ -6152,6 +6161,7 @@ export type Event =
| PamSessionTerminateEvent
| PamSessionGetEvent
| PamSessionListEvent
+ | PamSessionEventBatchUploadEvent
| PamFolderCreateEvent
| PamFolderUpdateEvent
| PamFolderDeleteEvent
diff --git a/backend/src/ee/services/pam-session/pam-session-event-batch-dal.ts b/backend/src/ee/services/pam-session/pam-session-event-batch-dal.ts
new file mode 100644
index 0000000000..8194608e12
--- /dev/null
+++ b/backend/src/ee/services/pam-session/pam-session-event-batch-dal.ts
@@ -0,0 +1,27 @@
+import { Knex } from "knex";
+
+import { TDbClient } from "@app/db";
+import { TableName } from "@app/db/schemas";
+import { ormify } from "@app/lib/knex";
+
+export type TPamSessionEventBatchDALFactory = ReturnType<typeof pamSessionEventBatchDALFactory>;
+
+export const pamSessionEventBatchDALFactory = (db: TDbClient) => {
+ const orm = ormify(db, TableName.PamSessionEventBatch);
+
+ const findBySessionId = async (sessionId: string, tx?: Knex) => {
+ return (tx || db.replicaNode())(TableName.PamSessionEventBatch)
+ .where("sessionId", sessionId)
+ .orderBy("startOffset", "asc")
+ .select("*");
+ };
+
+ const upsertBatch = async (sessionId: string, startOffset: number, encryptedEventsBlob: Buffer, tx?: Knex) => {
+ return (tx || db)(TableName.PamSessionEventBatch)
+ .insert({ sessionId, startOffset, encryptedEventsBlob })
+ .onConflict(["sessionId", "startOffset"])
+ .merge(["encryptedEventsBlob"]); // on re-upload of the same offset, overwrite the blob instead of erroring or skipping
+ };
+
+ return { ...orm, findBySessionId, upsertBatch };
+};
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 38e9f6239b..80fe78fa90 100644
--- a/backend/src/ee/services/pam-session/pam-session-fns.ts
+++ b/backend/src/ee/services/pam-session/pam-session-fns.ts
@@ -1,4 +1,4 @@
-import { TPamSessions } from "@app/db/schemas";
+import { TPamSessionEventBatches, TPamSessions } from "@app/db/schemas";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { KmsDataKey } from "@app/services/kms/kms-types";
@@ -25,6 +25,25 @@ export const decryptSessionCommandLogs = async ({
return JSON.parse(decryptedPlainTextBlob.toString()) as (TPamSessionCommandLog | TTerminalEvent)[];
};
+export const decryptBatches = async (
+ batches: TPamSessionEventBatches[],
+ projectId: string,
+ kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">
+) => {
+ const { decryptor } = await kmsService.createCipherPairWithDataKey({
+ type: KmsDataKey.SecretManager,
+ projectId
+ });
+
+ const events: (TPamSessionCommandLog | TTerminalEvent)[] = [];
+ for (const batch of batches) {
+ const plain = decryptor({ cipherTextBlob: batch.encryptedEventsBlob });
+ const batchEvents = JSON.parse(plain.toString()) as (TPamSessionCommandLog | TTerminalEvent)[];
+ events.push(...batchEvents);
+ }
+ return events;
+};
+
export const decryptSession = async (
session: TPamSessions,
projectId: string,
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 2aeebf9d66..75269ad65e 100644
--- a/backend/src/ee/services/pam-session/pam-session-service.ts
+++ b/backend/src/ee/services/pam-session/pam-session-service.ts
@@ -18,13 +18,15 @@ import { TGatewayV2ServiceFactory } from "../gateway-v2/gateway-v2-service";
import { PamResource } from "../pam-resource/pam-resource-enums";
import { OrgPermissionGatewayActions, OrgPermissionSubjects } from "../permission/org-permission";
import { ProjectPermissionPamSessionActions, ProjectPermissionSub } from "../permission/project-permission";
+import { TPamSessionEventBatchDALFactory } from "./pam-session-event-batch-dal";
import { TPamSessionDALFactory } from "./pam-session-dal";
import { PamSessionStatus } from "./pam-session-enums";
-import { decryptSession } from "./pam-session-fns";
-import { TUpdateSessionLogsDTO } from "./pam-session-types";
+import { decryptBatches, decryptSession } from "./pam-session-fns";
+import { TUpdateSessionLogsDTO, TUploadEventBatchDTO } from "./pam-session-types";
type TPamSessionServiceFactoryDep = {
pamSessionDAL: TPamSessionDALFactory;
+ pamSessionEventBatchDAL: TPamSessionEventBatchDALFactory;
projectDAL: TProjectDALFactory;
userDAL: Pick<TUserDALFactory, "findById">;
permissionService: Pick<TPermissionServiceFactory, "getProjectPermission" | "getOrgPermission">;
@@ -36,6 +38,7 @@ export type TPamSessionServiceFactory = ReturnType<typeof pamSessionServiceFacto
export const pamSessionServiceFactory = ({
pamSessionDAL,
+ pamSessionEventBatchDAL,
projectDAL,
userDAL,
permissionService,
@@ -91,6 +94,12 @@ export const pamSessionServiceFactory = ({
ProjectPermissionSub.PamSessions
);
+ const batches = await pamSessionEventBatchDAL.findBySessionId(sessionId);
+ if (batches.length > 0) {
+ const logs = await decryptBatches(batches, session.projectId, kmsService);
+ return { session: { ...session, logs, encryptedLogsBlob: undefined } as unknown as Awaited<ReturnType<typeof decryptSession>> };
+ }
+
return {
session: await decryptSession(session, session.projectId, kmsService)
};
@@ -288,5 +297,29 @@ export const pamSessionServiceFactory = ({
return { session: updatedSession, projectId: project.id, alreadyEnded: false };
};
- return { getById, list, updateLogsById, endSessionById, terminateSessionById };
+ const uploadEventBatch = async ({ sessionId, startOffset, events }: TUploadEventBatchDTO, actor: OrgServiceActor) => {
+ if (actor.type !== ActorType.IDENTITY) {
+ throw new ForbiddenRequestError({ message: "Only gateways can perform this action" });
+ }
+
+ const session = await pamSessionDAL.findById(sessionId);
+ if (!session) throw new NotFoundError({ message: `Session with ID '${sessionId}' not found` });
+
+ if (session.gatewayIdentityId && session.gatewayIdentityId !== actor.id) {
+ throw new ForbiddenRequestError({ message: "Identity does not have access to upload events for this session" });
+ }
+
+ const { encryptor } = await kmsService.createCipherPairWithDataKey({
+ type: KmsDataKey.SecretManager,
+ projectId: session.projectId
+ });
+
+ const { cipherTextBlob } = encryptor({ plainText: events });
+
+ await pamSessionEventBatchDAL.upsertBatch(sessionId, startOffset, cipherTextBlob);
+
+ return { projectId: session.projectId };
+ };
+
+ return { getById, list, updateLogsById, endSessionById, terminateSessionById, uploadEventBatch };
};
diff --git a/backend/src/ee/services/pam-session/pam-session-types.ts b/backend/src/ee/services/pam-session/pam-session-types.ts
index 8f202b6723..646e47337c 100644
--- a/backend/src/ee/services/pam-session/pam-session-types.ts
+++ b/backend/src/ee/services/pam-session/pam-session-types.ts
@@ -17,3 +17,9 @@ export type TUpdateSessionLogsDTO = {
sessionId: string;
logs: (TPamSessionCommandLog | TTerminalEvent | THttpEvent)[];
};
+
+export type TUploadEventBatchDTO = {
+ sessionId: string;
+ startOffset: number;
+ events: Buffer;
+};
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index aca5ed3e16..6feeb6620b 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -100,6 +100,7 @@ import { pamResourceRotationRulesDALFactory } from "@app/ee/services/pam-resourc
import { pamResourceRotationRulesServiceFactory } from "@app/ee/services/pam-resource/pam-resource-rotation-rules-service";
import { pamResourceServiceFactory } from "@app/ee/services/pam-resource/pam-resource-service";
import { pamSessionDALFactory } from "@app/ee/services/pam-session/pam-session-dal";
+import { pamSessionEventBatchDALFactory } from "@app/ee/services/pam-session/pam-session-event-batch-dal";
import { pamSessionServiceFactory } from "@app/ee/services/pam-session/pam-session-service";
import { pamWebAccessServiceFactory } from "@app/ee/services/pam-web-access/pam-web-access-service";
import { permissionDALFactory } from "@app/ee/services/permission/permission-dal";
@@ -2787,6 +2788,7 @@ export const registerRoutes = async (
const pamResourceFavoriteDAL = pamResourceFavoriteDALFactory(db);
const pamAccountDAL = pamAccountDALFactory(db);
const pamSessionDAL = pamSessionDALFactory(db);
+ const pamSessionEventBatchDAL = pamSessionEventBatchDALFactory(db);
const pamDiscoveryRunDAL = pamDiscoveryRunDALFactory(db);
const pamDiscoverySourceResourcesDAL = pamDiscoverySourceResourcesDALFactory(db);
const pamDiscoverySourceAccountsDAL = pamDiscoverySourceAccountsDALFactory(db);
@@ -2863,6 +2865,7 @@ export const registerRoutes = async (
const pamSessionService = pamSessionServiceFactory({
pamSessionDAL,
+ pamSessionEventBatchDAL,
projectDAL,
userDAL,
permissionService,
diff --git a/frontend/src/pages/pam/PamSessionsByIDPage/PamSessionByIDPage.tsx b/frontend/src/pages/pam/PamSessionsByIDPage/PamSessionByIDPage.tsx
index b281c5b3f3..46692b5591 100644
--- a/frontend/src/pages/pam/PamSessionsByIDPage/PamSessionByIDPage.tsx
+++ b/frontend/src/pages/pam/PamSessionsByIDPage/PamSessionByIDPage.tsx
@@ -29,7 +29,13 @@ const Page = () => {
from: ROUTE_PATHS.Pam.PamSessionByIDPage.id,
select: (el) => el.sessionId
});
- const { data: session } = useGetPamSessionById(sessionId);
+ const { data: session } = useGetPamSessionById(sessionId, {
+ refetchInterval: (query) => {
+ const s = query.state.data;
+ return s?.status === PamSessionStatus.Active || s?.status === PamSessionStatus.Starting ? 5000 : false;
+ },
+ refetchIntervalInBackground: false
+ });
const { currentOrg } = useOrganization();
const { currentProject } = useProject();
const { popUp, handlePopUpOpen, handlePopUpToggle } = usePopUp(["terminateSession"] as const);
@@ -37,7 +43,6 @@ const Page = () => {
const isActive =
session?.status === PamSessionStatus.Active || session?.status === PamSessionStatus.Starting;
const isGatewaySession = !!session?.gatewayIdentityId;
-
return (
<div className="mx-auto flex flex-col justify-between bg-bunker-800 text-white">
{session && (