Skip to content

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

feat(pam): replace infinite scroll with cursor-based live log polling

bernie-ga68415144a6b
diff --git a/backend/src/ee/services/pam-session/pam-session-schemas.ts b/backend/src/ee/services/pam-session/pam-session-schemas.ts
index 5a700e95b3..568a9a2fb9 100644
--- a/backend/src/ee/services/pam-session/pam-session-schemas.ts
+++ b/backend/src/ee/services/pam-session/pam-session-schemas.ts
@@ -55,5 +55,6 @@ export const SanitizedSessionSchema = PamSessionsSchema.omit({
export const SessionLogsPageSchema = z.object({
logs: z.array(z.union([PamSessionCommandLogSchema, TerminalEventSchema, HttpEventSchema])),
- hasMore: z.boolean()
+ hasMore: z.boolean(),
+ batchCount: z.number()
});
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 c75e5c9f4f..ec5f681b7e 100644
--- a/backend/src/ee/services/pam-session/pam-session-service.ts
+++ b/backend/src/ee/services/pam-session/pam-session-service.ts
@@ -320,7 +320,7 @@ export const pamSessionServiceFactory = ({
const hasMore = batches.length > limit;
const pageBatches = hasMore ? batches.slice(0, limit) : batches;
const logs = pageBatches.length > 0 ? await decryptBatches(pageBatches, sessionFromDb.projectId, kmsService) : [];
- return { logs, hasMore };
+ return { logs, hasMore, batchCount: pageBatches.length };
}
// Legacy blob-based session — bounded by Fastify body limit on upload
@@ -330,10 +330,10 @@ export const pamSessionServiceFactory = ({
encryptedLogs: sessionFromDb.encryptedLogsBlob,
kmsService
});
- return { logs, hasMore: false };
+ return { logs, hasMore: false, batchCount: 0 };
}
- return { logs: [], hasMore: false };
+ return { logs: [], hasMore: false, batchCount: 0 };
};
const uploadEventBatch = async ({ sessionId, startOffset, events }: TUploadEventBatchDTO, actor: OrgServiceActor) => {
diff --git a/frontend/src/hooks/api/pam/queries.tsx b/frontend/src/hooks/api/pam/queries.tsx
index 494b80cbfc..3df2ce53ee 100644
--- a/frontend/src/hooks/api/pam/queries.tsx
+++ b/frontend/src/hooks/api/pam/queries.tsx
@@ -1,4 +1,5 @@
-import { useInfiniteQuery, useQuery, UseQueryOptions } from "@tanstack/react-query";
+import { useEffect, useRef, useState } from "react";
+import { useQuery, UseQueryOptions } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
@@ -303,27 +304,78 @@ export const useGetPamSessionById = (
});
};
-const LOGS_PAGE_LIMIT = 20;
+const LOGS_FETCH_LIMIT = 100;
+const LOGS_POLL_INTERVAL_MS = 5000;
-export const useInfiniteGetPamSessionLogs = (
- sessionId: string,
- options: { refetchInterval?: number | false } = {}
-) => {
- return useInfiniteQuery({
- initialPageParam: 0,
- queryKey: pamKeys.getSessionLogs(sessionId),
- queryFn: async ({ pageParam }) => {
- const { data } = await apiRequest.get<TPamSessionLogsPage>(
- `/api/v1/pam/sessions/${sessionId}/logs`,
- { params: { offset: pageParam, limit: LOGS_PAGE_LIMIT } }
- );
- return data;
- },
- getNextPageParam: (lastPage, allPages) =>
- lastPage.hasMore ? allPages.length * LOGS_PAGE_LIMIT : undefined,
- enabled: !!sessionId,
- ...options
- });
+export const useGetPamSessionLogs = (sessionId: string, isActive: boolean) => {
+ const [logs, setLogs] = useState<TPamSessionLogsPage["logs"]>([]);
+ const [isLoading, setIsLoading] = useState(true);
+ const batchCursorRef = useRef(0);
+
+ // Initial load: auto-paginate until all existing batches are fetched
+ useEffect(() => {
+ if (!sessionId) return undefined;
+ let cancelled = false;
+
+ const fetchAll = async () => {
+ setIsLoading(true);
+ let offset = 0;
+ const allLogs: TPamSessionLogsPage["logs"] = [];
+
+ try {
+ let hasMore = true;
+ while (hasMore) {
+ if (cancelled) return;
+ // eslint-disable-next-line no-await-in-loop
+ const { data } = await apiRequest.get<TPamSessionLogsPage>(
+ `/api/v1/pam/sessions/${sessionId}/logs`,
+ { params: { offset, limit: LOGS_FETCH_LIMIT } }
+ );
+ allLogs.push(...data.logs);
+ offset += data.batchCount;
+ hasMore = data.hasMore;
+ }
+ } catch {
+ // ignore — leave logs as whatever was fetched so far
+ }
+
+ if (!cancelled) {
+ batchCursorRef.current = offset;
+ setLogs(allLogs);
+ setIsLoading(false);
+ }
+ };
+
+ fetchAll().catch(() => {});
+ return () => {
+ cancelled = true;
+ };
+ }, [sessionId]);
+
+ // Polling: only fetch new batches (offset = cursor)
+ useEffect(() => {
+ if (!isActive || !sessionId) return undefined;
+
+ const interval = setInterval(async () => {
+ try {
+ const { data } = await apiRequest.get<TPamSessionLogsPage>(
+ `/api/v1/pam/sessions/${sessionId}/logs`,
+ { params: { offset: batchCursorRef.current, limit: LOGS_FETCH_LIMIT } }
+ );
+
+ if (data.batchCount > 0) {
+ batchCursorRef.current += data.batchCount;
+ setLogs((prev) => [...prev, ...data.logs]);
+ }
+ } catch {
+ // ignore transient errors — next tick will retry
+ }
+ }, LOGS_POLL_INTERVAL_MS);
+
+ return () => clearInterval(interval);
+ }, [sessionId, isActive]);
+
+ return { logs, isLoading };
};
export const useListPamSessions = (
diff --git a/frontend/src/hooks/api/pam/types/index.ts b/frontend/src/hooks/api/pam/types/index.ts
index ff5070964f..d82829aaf1 100644
--- a/frontend/src/hooks/api/pam/types/index.ts
+++ b/frontend/src/hooks/api/pam/types/index.ts
@@ -273,4 +273,5 @@ export type TPamResourceDependency = TPamAccountDependency & {
export type TPamSessionLogsPage = {
logs: TPamSessionLog[];
hasMore: boolean;
+ batchCount: number;
};
diff --git a/frontend/src/pages/pam/PamSessionsByIDPage/components/PamSessionLogsSection.tsx b/frontend/src/pages/pam/PamSessionsByIDPage/components/PamSessionLogsSection.tsx
index 38f70645be..799408bf77 100644
--- a/frontend/src/pages/pam/PamSessionsByIDPage/components/PamSessionLogsSection.tsx
+++ b/frontend/src/pages/pam/PamSessionsByIDPage/components/PamSessionLogsSection.tsx
@@ -1,14 +1,14 @@
import { faUpRightFromSquare } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
-import { Button } from "@app/components/v2";
import {
PamResourceType,
+ PamSessionStatus,
THttpEvent,
TPamCommandLog,
TPamSession,
TTerminalEvent,
- useInfiniteGetPamSessionLogs
+ useGetPamSessionLogs
} from "@app/hooks/api/pam";
import { CommandLogView } from "./CommandLogView";
@@ -20,10 +20,10 @@ type Props = {
};
export const PamSessionLogsSection = ({ session }: Props) => {
- const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isFetching } =
- useInfiniteGetPamSessionLogs(session.id);
+ const isActive =
+ session.status === PamSessionStatus.Active || session.status === PamSessionStatus.Starting;
- const allLogs = data?.pages.flatMap((page) => page.logs) ?? [];
+ const { logs, isLoading } = useGetPamSessionLogs(session.id, isActive);
const isSSHSession = session.resourceType === PamResourceType.SSH;
const isDatabaseSession =
@@ -34,7 +34,7 @@ export const PamSessionLogsSection = ({ session }: Props) => {
session.resourceType === PamResourceType.Redis;
const isHttpSession = session.resourceType === PamResourceType.Kubernetes;
const isAwsIamSession = session.resourceType === PamResourceType.AwsIam;
- const hasLogs = allLogs.length > 0;
+ const hasLogs = logs.length > 0;
return (
<div className="flex h-full w-full flex-col gap-4 rounded-lg border border-mineshaft-600 bg-mineshaft-900 p-4">
@@ -42,9 +42,9 @@ export const PamSessionLogsSection = ({ session }: Props) => {
<h3 className="text-lg font-medium text-mineshaft-100">Session Logs</h3>
</div>
- {isDatabaseSession && hasLogs && <CommandLogView logs={allLogs as TPamCommandLog[]} />}
- {isSSHSession && hasLogs && <TerminalEventView events={allLogs as TTerminalEvent[]} />}
- {isHttpSession && hasLogs && <HttpEventView events={allLogs as THttpEvent[]} />}
+ {isDatabaseSession && hasLogs && <CommandLogView logs={logs as TPamCommandLog[]} />}
+ {isSSHSession && hasLogs && <TerminalEventView events={logs as TTerminalEvent[]} />}
+ {isHttpSession && hasLogs && <HttpEventView events={logs as THttpEvent[]} />}
{isAwsIamSession && (
<div className="flex grow items-center justify-center text-bunker-300">
<div className="text-center">
@@ -65,7 +65,7 @@ export const PamSessionLogsSection = ({ session }: Props) => {
</div>
</div>
)}
- {!hasLogs && !isAwsIamSession && !isFetching && (
+ {!hasLogs && !isAwsIamSession && !isLoading && (
<div className="flex grow items-center justify-center text-bunker-300">
<div className="text-center">
<div className="mb-2">Session logs are not yet available</div>
@@ -77,19 +77,6 @@ export const PamSessionLogsSection = ({ session }: Props) => {
</div>
</div>
)}
-
- {hasNextPage && !isAwsIamSession && (
- <div className="flex justify-center pt-2">
- <Button
- variant="outline_bg"
- size="xs"
- isLoading={isFetchingNextPage}
- onClick={() => fetchNextPage()}
- >
- Load more
- </Button>
- </div>
- )}
</div>
);
};