Skip to content

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

pagination

x7034474d0cf5
diff --git a/backend/src/ee/routes/v1/pam-routers/pam-discovery-router.ts b/backend/src/ee/routes/v1/pam-routers/pam-discovery-router.ts
index e65f800f8c..681156dad4 100644
--- a/backend/src/ee/routes/v1/pam-routers/pam-discovery-router.ts
+++ b/backend/src/ee/routes/v1/pam-routers/pam-discovery-router.ts
@@ -362,22 +362,30 @@ export const registerPamDiscoveryRouter = async (server: FastifyZodProvider) =>
description: "List staged accounts discovered by a PAM discovery source",
tags: [ApiDocsTags.PamDiscovery],
params: z.object({ sourceId: z.string().uuid() }),
- querystring: z.object({ search: z.string().optional() }),
- response: { 200: z.object({ discoveredAccounts: z.array(DiscoveredAccountSchema) }) }
+ querystring: z.object({
+ search: z.string().optional(),
+ offset: z.coerce.number().min(0).default(0).optional(),
+ limit: z.coerce.number().min(1).max(100).default(20).optional()
+ }),
+ response: {
+ 200: z.object({ discoveredAccounts: z.array(DiscoveredAccountSchema), totalCount: z.number() })
+ }
},
config: { rateLimit: readLimit },
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
- const discoveredAccounts = await server.services.pamDiscovery.listDiscovered({
+ const { discoveredAccounts, totalCount } = await server.services.pamDiscovery.listDiscovered({
sourceId: req.params.sourceId,
projectId: req.internalPamProjectId,
search: req.query.search,
+ offset: req.query.offset,
+ limit: req.query.limit,
actorId: req.permission.id,
actor: req.permission.type,
actorOrgId: req.permission.orgId,
actorAuthMethod: req.permission.authMethod
});
- return { discoveredAccounts };
+ return { discoveredAccounts, totalCount };
}
});
diff --git a/backend/src/ee/services/pam-discovery/pam-discovered-account-dal.ts b/backend/src/ee/services/pam-discovery/pam-discovered-account-dal.ts
index e7346fe527..fd31a473c0 100644
--- a/backend/src/ee/services/pam-discovery/pam-discovered-account-dal.ts
+++ b/backend/src/ee/services/pam-discovery/pam-discovered-account-dal.ts
@@ -26,13 +26,25 @@ export const pamDiscoveredAccountDALFactory = (db: TDbClient) => {
return { isNew: true };
};
- const listStaged = async (discoverySourceId: string, search?: string) => {
- const query = db
+ const listStaged = async (
+ discoverySourceId: string,
+ { search, offset, limit }: { search?: string; offset?: number; limit?: number } = {}
+ ) => {
+ const baseQuery = db
.replicaNode()(TableName.PamDiscoveredAccount)
.where({ discoverySourceId })
.whereNull("importedAccountId");
- if (search) void query.andWhere("name", "ilike", `%${sanitizeSqlLikeString(search)}%`);
- return query.orderBy("name", "asc");
+ if (search) void baseQuery.andWhere("name", "ilike", `%${sanitizeSqlLikeString(search)}%`);
+
+ const countQuery = baseQuery.clone().clearSelect().count("id as count").first<{ count: string }>();
+
+ const dataQuery = baseQuery.clone().orderBy("name", "asc");
+ if (limit) void dataQuery.limit(limit);
+ if (offset) void dataQuery.offset(offset);
+
+ const [countResult, accounts] = await Promise.all([countQuery, dataQuery]);
+
+ return { accounts, totalCount: Number(countResult?.count ?? 0) };
};
return { ...orm, upsertByFingerprint, listStaged };
diff --git a/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts b/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts
index fc6b50d0a2..b22918073e 100644
--- a/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts
+++ b/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts
@@ -410,20 +410,23 @@ export const pamDiscoverySourceServiceFactory = (deps: TPamDiscoverySourceServic
return runs;
};
- const listDiscovered = async ({ projectId, sourceId, search, ...ctx }: TListDiscoveredDTO) => {
+ const listDiscovered = async ({ projectId, sourceId, search, offset, limit, ...ctx }: TListDiscoveredDTO) => {
await verifyAdmin(projectId, ctx);
const source = await pamDiscoverySourceDAL.findById(sourceId);
if (!source || source.projectId !== projectId) {
throw new NotFoundError({ message: `Discovery source with ID '${sourceId}' not found` });
}
- const accounts = await pamDiscoveredAccountDAL.listStaged(sourceId, search);
- return accounts.map((a) => ({
- id: a.id,
- accountType: a.accountType as PamAccountType,
- name: a.name,
- fingerprint: a.fingerprint,
- createdAt: a.createdAt
- }));
+ const { accounts, totalCount } = await pamDiscoveredAccountDAL.listStaged(sourceId, { search, offset, limit });
+ return {
+ discoveredAccounts: accounts.map((a) => ({
+ id: a.id,
+ accountType: a.accountType as PamAccountType,
+ name: a.name,
+ fingerprint: a.fingerprint,
+ createdAt: a.createdAt
+ })),
+ totalCount
+ };
};
const importAccounts = async ({ projectId, sourceId, folderId, accounts, ...ctx }: TImportDiscoveredDTO) => {
diff --git a/backend/src/ee/services/pam-discovery/pam-discovery-types.ts b/backend/src/ee/services/pam-discovery/pam-discovery-types.ts
index 30191ec408..2befd95b13 100644
--- a/backend/src/ee/services/pam-discovery/pam-discovery-types.ts
+++ b/backend/src/ee/services/pam-discovery/pam-discovery-types.ts
@@ -77,7 +77,13 @@ export type TTriggerScanDTO = {
discoveryType: PamDiscoveryType;
} & TActorContext;
export type TListRunsDTO = { projectId: string; sourceId: string; offset?: number; limit?: number } & TActorContext;
-export type TListDiscoveredDTO = { projectId: string; sourceId: string; search?: string } & TActorContext;
+export type TListDiscoveredDTO = {
+ projectId: string;
+ sourceId: string;
+ search?: string;
+ offset?: number;
+ limit?: number;
+} & TActorContext;
export type TImportDiscoveredDTO = {
projectId: string;
diff --git a/frontend/src/hooks/api/pam/queries.tsx b/frontend/src/hooks/api/pam/queries.tsx
index 2fdbdbe7e6..cc109a6ea6 100644
--- a/frontend/src/hooks/api/pam/queries.tsx
+++ b/frontend/src/hooks/api/pam/queries.tsx
@@ -78,8 +78,10 @@ export const pamKeys = {
listDiscoverySources: (params?: { search?: string }) =>
[...pamKeys.discovery(), "sources", params] as const,
listDiscoveryRuns: (sourceId: string) => [...pamKeys.discovery(), "runs", sourceId] as const,
- listDiscoveredAccounts: (sourceId: string, search?: string) =>
- [...pamKeys.discovery(), "discovered", sourceId, search] as const
+ listDiscoveredAccounts: (
+ sourceId: string,
+ params?: { search?: string; offset?: number; limit?: number }
+ ) => [...pamKeys.discovery(), "discovered", sourceId, params] as const
};
const fetchFolderPermissions = async (folderId: string) => {
@@ -492,15 +494,18 @@ export const useListPamDiscoveryRuns = (
});
};
-export const useListPamDiscoveredAccounts = (sourceId: string, search?: string) => {
+export const useListPamDiscoveredAccounts = (
+ sourceId: string,
+ params?: { search?: string; offset?: number; limit?: number }
+) => {
return useQuery({
- queryKey: pamKeys.listDiscoveredAccounts(sourceId, search),
+ queryKey: pamKeys.listDiscoveredAccounts(sourceId, params),
queryFn: async () => {
- const { data } = await apiRequest.get<{ discoveredAccounts: TPamDiscoveredAccount[] }>(
- `/api/v1/pam/discovery-sources/${sourceId}/discovered`,
- { params: { search } }
- );
- return data.discoveredAccounts;
+ const { data } = await apiRequest.get<{
+ discoveredAccounts: TPamDiscoveredAccount[];
+ totalCount: number;
+ }>(`/api/v1/pam/discovery-sources/${sourceId}/discovered`, { params });
+ return data;
},
enabled: Boolean(sourceId),
placeholderData: (prev) => prev
diff --git a/frontend/src/pages/pam/PamDiscoveryPage/components/DiscoverySourceDetailSheet.tsx b/frontend/src/pages/pam/PamDiscoveryPage/components/DiscoverySourceDetailSheet.tsx
index bfa7b5d508..2ab689d1e1 100644
--- a/frontend/src/pages/pam/PamDiscoveryPage/components/DiscoverySourceDetailSheet.tsx
+++ b/frontend/src/pages/pam/PamDiscoveryPage/components/DiscoverySourceDetailSheet.tsx
@@ -29,6 +29,7 @@ import {
InputGroup,
InputGroupAddon,
InputGroupInput,
+ Pagination,
Table,
TableBody,
TableCell,
@@ -38,10 +39,16 @@ import {
} from "@app/components/v3";
import { Checkbox } from "@app/components/v3/generic/Checkbox";
import { useOrganization } from "@app/context";
+import {
+ getUserTablePreference,
+ PreferenceKey,
+ setUserTablePreference
+} from "@app/helpers/userTablePreferences";
import {
PamAccountType,
PamDiscoverySchedule,
pamKeys,
+ TPamDiscoveredAccount,
TPamDiscoverySource,
useListPamAccountsAdmin,
useListPamDiscoveredAccounts,
@@ -211,11 +218,15 @@ type Props = {
};
export const DiscoverySourceDetailSheet = ({ isOpen, sourceId, onOpenChange }: Props) => {
- const [selected, setSelected] = useState<string[]>([]);
+ const [selected, setSelected] = useState<Record<string, TPamDiscoveredAccount>>({});
const [isImportOpen, setIsImportOpen] = useState(false);
const [isFormDirty, setIsFormDirty] = useState(false);
const [search, setSearch] = useState("");
const [debouncedSearch] = useDebounce(search);
+ const [page, setPage] = useState(1);
+ const [perPage, setPerPage] = useState(() =>
+ getUserTablePreference("pamDiscoveredAccountsTable", PreferenceKey.PerPage, 20)
+ );
const { currentOrg } = useOrganization();
const { tab, setTab } = usePamSheetState("discoverySourceId");
@@ -236,9 +247,19 @@ export const DiscoverySourceDetailSheet = ({ isOpen, sourceId, onOpenChange }: P
const { data: runs = [] } = useListPamDiscoveryRuns(sourceId ?? "", {
refetchInterval: isOpen ? 5000 : undefined
});
- const { data: staged = [] } = useListPamDiscoveredAccounts(sourceId ?? "", debouncedSearch);
+ const offset = (page - 1) * perPage;
+ const { data: { discoveredAccounts: staged = [], totalCount = 0 } = {} } =
+ useListPamDiscoveredAccounts(sourceId ?? "", {
+ search: debouncedSearch,
+ offset,
+ limit: perPage
+ });
const triggerScan = useTriggerPamDiscoveryScan();
+ useEffect(() => {
+ setPage(1);
+ }, [debouncedSearch]);
+
const queryClient = useQueryClient();
const latestRunStatus = runs[0]?.status;
const prevRunStatus = useRef(latestRunStatus);
@@ -266,17 +287,33 @@ export const DiscoverySourceDetailSheet = ({ isOpen, sourceId, onOpenChange }: P
const q = search.trim().toLowerCase();
const visibleStaged = q ? staged.filter((a) => a.name.toLowerCase().includes(q)) : staged;
- const selectedAccounts = staged.filter((a) => selected.includes(a.id));
- const allVisibleSelected =
- visibleStaged.length > 0 && visibleStaged.every((a) => selected.includes(a.id));
- const toggle = (id: string) =>
- setSelected((prev) => (prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id]));
+
+ const selectedAccounts = Object.values(selected);
+ const allPageSelected =
+ visibleStaged.length > 0 && visibleStaged.every((a) => Boolean(selected[a.id]));
+ const toggle = (account: TPamDiscoveredAccount) =>
+ setSelected((prev) => {
+ const next = { ...prev };
+ if (next[account.id]) {
+ delete next[account.id];
+ } else {
+ next[account.id] = account;
+ }
+ return next;
+ });
const toggleAll = () =>
- setSelected((prev) =>
- allVisibleSelected
- ? prev.filter((id) => !visibleStaged.some((a) => a.id === id))
- : Array.from(new Set([...prev, ...visibleStaged.map((a) => a.id)]))
- );
+ setSelected((prev) => {
+ if (allPageSelected) {
+ const next = { ...prev };
+ visibleStaged.forEach((a) => delete next[a.id]);
+ return next;
+ }
+ const next = { ...prev };
+ visibleStaged.forEach((a) => {
+ next[a.id] = a;
+ });
+ return next;
+ });
const stagedTab: ReactNode = (
<div className="flex flex-1 flex-col gap-4 p-4">
@@ -299,65 +336,83 @@ export const DiscoverySourceDetailSheet = ({ isOpen, sourceId, onOpenChange }: P
/>
</InputGroup>
- {visibleStaged.length === 0 ? (
+ {totalCount === 0 ? (
<Empty>
<EmptyHeader>
<EmptyTitle>
- {search
+ {debouncedSearch
? "No accounts match your search."
: "No staged accounts. Run a scan to discover accounts."}
</EmptyTitle>
</EmptyHeader>
</Empty>
) : (
- <Table>
- <TableHeader>
- <TableRow>
- <TableHead className="w-8">
- <Checkbox
- id="select-all-discovered"
- isChecked={allVisibleSelected}
- onCheckedChange={toggleAll}
- />
- </TableHead>
- <TableHead>Name</TableHead>
- <TableHead className="w-40">Type</TableHead>
- </TableRow>
- </TableHeader>
- <TableBody>
- {visibleStaged.map((account) => {
- const typeDetails = accountTypeMap[account.accountType as PamAccountType];
- return (
- <TableRow key={account.id}>
- <TableCell>
- <Checkbox
- id={`discovered-${account.id}`}
- isChecked={selected.includes(account.id)}
- onCheckedChange={() => toggle(account.id)}
- />
- </TableCell>
- <TableCell className="font-medium text-foreground">
- <HighlightText text={account.name} highlight={search} />
- </TableCell>
- <TableCell>
- <div className="flex items-center gap-2">
- {typeDetails && (
- <img
- src={`/images/integrations/${typeDetails.icon}`}
- alt={typeDetails.name}
- className="size-5 shrink-0 rounded-sm"
- />
- )}
- <span className="text-sm">
- {typeDetails?.name ?? account.accountType}
- </span>
- </div>
- </TableCell>
- </TableRow>
+ <>
+ <Table>
+ <TableHeader>
+ <TableRow>
+ <TableHead className="w-8">
+ <Checkbox
+ id="select-all-discovered"
+ isChecked={allPageSelected}
+ onCheckedChange={toggleAll}
+ />
+ </TableHead>
+ <TableHead>Name</TableHead>
+ <TableHead className="w-40">Type</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {visibleStaged.map((account) => {
+ const typeDetails = accountTypeMap[account.accountType as PamAccountType];
+ return (
+ <TableRow key={account.id}>
+ <TableCell>
+ <Checkbox
+ id={`discovered-${account.id}`}
+ isChecked={Boolean(selected[account.id])}
+ onCheckedChange={() => toggle(account)}
+ />
+ </TableCell>
+ <TableCell className="font-medium text-foreground">
+ <HighlightText text={account.name} highlight={search} />
+ </TableCell>
+ <TableCell>
+ <div className="flex items-center gap-2">
+ {typeDetails && (
+ <img
+ src={`/images/integrations/${typeDetails.icon}`}
+ alt={typeDetails.name}
+ className="size-5 shrink-0 rounded-sm"
+ />
+ )}
+ <span className="text-sm">
+ {typeDetails?.name ?? account.accountType}
+ </span>
+ </div>
+ </TableCell>
+ </TableRow>
+ );
+ })}
+ </TableBody>
+ </Table>
+
+ <Pagination
+ count={totalCount}
+ page={page}
+ perPage={perPage}
+ onChangePage={setPage}
+ onChangePerPage={(newPerPage) => {
+ setPerPage(newPerPage);
+ setPage(1);
+ setUserTablePreference(
+ "pamDiscoveredAccountsTable",
+ PreferenceKey.PerPage,
+ newPerPage
);
- })}
- </TableBody>
- </Table>
+ }}
+ />
+ </>
)}
</CardContent>
</Card>
@@ -365,7 +420,7 @@ export const DiscoverySourceDetailSheet = ({ isOpen, sourceId, onOpenChange }: P
{selectedAccounts.length > 0 && (
<div className="sticky bottom-0 -mx-4 mt-auto -mb-4 flex items-center gap-2 border-t border-border bg-popover px-4 py-3">
<span className="mr-auto text-sm text-muted">{selectedAccounts.length} selected</span>
- <Button type="button" variant="ghost" onClick={() => setSelected([])}>
+ <Button type="button" variant="ghost" onClick={() => setSelected({})}>
Clear
</Button>
<Button type="button" variant="pam" onClick={() => setIsImportOpen(true)}>
@@ -479,8 +534,9 @@ export const DiscoverySourceDetailSheet = ({ isOpen, sourceId, onOpenChange }: P
isOpen={isOpen}
onOpenChange={(open) => {
if (!open) {
- setSelected([]);
+ setSelected({});
setSearch("");
+ setPage(1);
}
onOpenChange(open);
}}
@@ -550,7 +606,7 @@ export const DiscoverySourceDetailSheet = ({ isOpen, sourceId, onOpenChange }: P
onOpenChange={setIsImportOpen}
sourceId={sourceId}
accounts={selectedAccounts}
- onImported={() => setSelected([])}
+ onImported={() => setSelected({})}
/>
)}
</>