Skip to content

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

improvement(pki-discovery): auto-refresh scan runs and installations while a scan is running

Claude4b808a3c15cf
diff --git a/frontend/src/hooks/api/pkiDiscovery/index.tsx b/frontend/src/hooks/api/pkiDiscovery/index.tsx
index d9d1533325..f9ad676926 100644
--- a/frontend/src/hooks/api/pkiDiscovery/index.tsx
+++ b/frontend/src/hooks/api/pkiDiscovery/index.tsx
@@ -7,6 +7,7 @@ export {
useUpdatePkiInstallation
} from "./mutations";
export {
+ isPkiDiscoveryScanInFlight,
pkiDiscoveryKeys,
pkiInstallationKeys,
useGetLatestScan,
diff --git a/frontend/src/hooks/api/pkiDiscovery/mutations.tsx b/frontend/src/hooks/api/pkiDiscovery/mutations.tsx
index 648b876917..03952ddc41 100644
--- a/frontend/src/hooks/api/pkiDiscovery/mutations.tsx
+++ b/frontend/src/hooks/api/pkiDiscovery/mutations.tsx
@@ -97,7 +97,11 @@ export const useTriggerPkiDiscoveryScan = () => {
onSuccess: (_, { discoveryId, projectId }) => {
queryClient.invalidateQueries({ queryKey: pkiDiscoveryKeys.discovery(discoveryId) });
queryClient.invalidateQueries({ queryKey: pkiDiscoveryKeys.latestScan(discoveryId) });
+ queryClient.invalidateQueries({
+ queryKey: pkiDiscoveryKeys.scanHistoryByDiscovery(discoveryId)
+ });
queryClient.invalidateQueries({ queryKey: pkiDiscoveryKeys.list(projectId) });
+ queryClient.invalidateQueries({ queryKey: pkiInstallationKeys.list(projectId) });
createNotification({
text: "Scan triggered successfully",
type: "success"
diff --git a/frontend/src/hooks/api/pkiDiscovery/queries.tsx b/frontend/src/hooks/api/pkiDiscovery/queries.tsx
index 836fa17289..7ab288f46b 100644
--- a/frontend/src/hooks/api/pkiDiscovery/queries.tsx
+++ b/frontend/src/hooks/api/pkiDiscovery/queries.tsx
@@ -3,6 +3,7 @@ import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import {
+ PkiDiscoveryScanStatus,
TGetLatestScanDTO,
TGetPkiDiscoveryDTO,
TGetPkiInstallationDTO,
@@ -25,8 +26,10 @@ export const pkiDiscoveryKeys = {
discovery: (discoveryId: string) => [...pkiDiscoveryKeys.all, "detail", discoveryId] as const,
latestScan: (discoveryId: string) =>
[...pkiDiscoveryKeys.all, "latestScan", discoveryId] as const,
+ scanHistoryByDiscovery: (discoveryId: string) =>
+ [...pkiDiscoveryKeys.all, "scanHistory", discoveryId] as const,
scanHistory: (filters: TGetScanHistoryDTO) =>
- [...pkiDiscoveryKeys.all, "scanHistory", filters.discoveryId, filters] as const
+ [...pkiDiscoveryKeys.scanHistoryByDiscovery(filters.discoveryId), filters] as const
};
export const pkiInstallationKeys = {
@@ -38,6 +41,13 @@ export const pkiInstallationKeys = {
[...pkiInstallationKeys.all, "detail", installationId] as const
};
+type TPollingOptions = { refetchInterval?: number | false };
+
+const SCAN_IN_FLIGHT_POLL_MS = 5000;
+
+export const isPkiDiscoveryScanInFlight = (status?: PkiDiscoveryScanStatus | null) =>
+ status === PkiDiscoveryScanStatus.Pending || status === PkiDiscoveryScanStatus.Running;
+
export const useListPkiDiscoveries = ({
projectId,
offset = 0,
@@ -74,7 +84,9 @@ export const useGetPkiDiscovery = ({ discoveryId }: TGetPkiDiscoveryDTO) => {
);
return data;
},
- enabled: Boolean(discoveryId)
+ enabled: Boolean(discoveryId),
+ refetchInterval: (query) =>
+ isPkiDiscoveryScanInFlight(query.state.data?.lastScanStatus) ? SCAN_IN_FLIGHT_POLL_MS : false
});
};
@@ -91,7 +103,10 @@ export const useGetLatestScan = ({ discoveryId }: TGetLatestScanDTO) => {
});
};
-export const useGetScanHistory = ({ discoveryId, offset = 0, limit = 25 }: TGetScanHistoryDTO) => {
+export const useGetScanHistory = (
+ { discoveryId, offset = 0, limit = 25 }: TGetScanHistoryDTO,
+ options?: TPollingOptions
+) => {
return useQuery({
queryKey: pkiDiscoveryKeys.scanHistory({ discoveryId, offset, limit }),
queryFn: async () => {
@@ -105,18 +120,22 @@ export const useGetScanHistory = ({ discoveryId, offset = 0, limit = 25 }: TGetS
return data;
},
enabled: Boolean(discoveryId),
- placeholderData: (previousData) => previousData
+ placeholderData: (previousData) => previousData,
+ refetchInterval: options?.refetchInterval
});
};
-export const useListPkiInstallations = ({
- projectId,
- discoveryId,
- certificateId,
- offset = 0,
- limit = 25,
- search
-}: TListPkiInstallationsDTO) => {
+export const useListPkiInstallations = (
+ {
+ projectId,
+ discoveryId,
+ certificateId,
+ offset = 0,
+ limit = 25,
+ search
+ }: TListPkiInstallationsDTO,
+ options?: TPollingOptions
+) => {
return useQuery({
queryKey: pkiInstallationKeys.listWithOpts({
projectId,
@@ -147,7 +166,8 @@ export const useListPkiInstallations = ({
return data;
},
enabled: Boolean(projectId),
- placeholderData: (previousData) => previousData
+ placeholderData: (previousData) => previousData,
+ refetchInterval: options?.refetchInterval
});
};
diff --git a/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/DiscoveryDetailsByIDPage.tsx b/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/DiscoveryDetailsByIDPage.tsx
index 8f7bfc598b..b3ea365cf2 100644
--- a/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/DiscoveryDetailsByIDPage.tsx
+++ b/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/DiscoveryDetailsByIDPage.tsx
@@ -1,4 +1,6 @@
+import { useEffect, useRef } from "react";
import { Helmet } from "react-helmet";
+import { useQueryClient } from "@tanstack/react-query";
import { Link, useNavigate, useParams } from "@tanstack/react-router";
import { ChevronLeftIcon, EllipsisIcon } from "lucide-react";
@@ -20,7 +22,9 @@ import {
useProject
} from "@app/context";
import {
- PkiDiscoveryScanStatus,
+ isPkiDiscoveryScanInFlight,
+ pkiDiscoveryKeys,
+ pkiInstallationKeys,
TPkiDiscovery,
useDeletePkiDiscovery,
useGetPkiDiscovery,
@@ -45,10 +49,25 @@ const Page = () => {
from: "/_authenticate/_inject-org-details/_org-layout/organizations/$orgId/projects/cert-manager/$projectId/_cert-manager-layout/discovery/$discoveryId"
});
+ const queryClient = useQueryClient();
const { data: discovery, isLoading } = useGetPkiDiscovery({ discoveryId });
const triggerScan = useTriggerPkiDiscoveryScan();
const deleteDiscovery = useDeletePkiDiscovery();
+ const isScanRunning = isPkiDiscoveryScanInFlight(discovery?.lastScanStatus);
+ const wasScanRunningRef = useRef(isScanRunning);
+
+ // Once a scan finishes, refresh scan runs and installations so the final state is not stale
+ useEffect(() => {
+ if (wasScanRunningRef.current && !isScanRunning) {
+ queryClient.invalidateQueries({
+ queryKey: pkiDiscoveryKeys.scanHistoryByDiscovery(discoveryId)
+ });
+ queryClient.invalidateQueries({ queryKey: pkiInstallationKeys.list(projectId) });
+ }
+ wasScanRunningRef.current = isScanRunning;
+ }, [isScanRunning, discoveryId, projectId, queryClient]);
+
const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([
"editJob",
"deleteJob"
@@ -83,10 +102,6 @@ const Page = () => {
}
};
- const isScanRunning =
- discovery.lastScanStatus === PkiDiscoveryScanStatus.Running ||
- discovery.lastScanStatus === PkiDiscoveryScanStatus.Pending;
-
return (
<div className="mx-auto flex flex-col justify-between text-white">
<div className="mx-auto mb-6 w-full max-w-8xl">
@@ -162,8 +177,13 @@ const Page = () => {
onTriggerScan={handleTriggerScan}
isTriggerDisabled={!discovery.isActive || isScanRunning}
isTriggerPending={triggerScan.isPending}
+ isScanRunning={isScanRunning}
+ />
+ <DiscoveryInstallationsSection
+ discoveryId={discoveryId}
+ projectId={projectId}
+ isScanRunning={isScanRunning}
/>
- <DiscoveryInstallationsSection discoveryId={discoveryId} projectId={projectId} />
</div>
</div>
</div>
diff --git a/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryInstallationsSection.tsx b/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryInstallationsSection.tsx
index 1aa1de6eb5..1fbedfb756 100644
--- a/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryInstallationsSection.tsx
+++ b/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryInstallationsSection.tsx
@@ -27,11 +27,13 @@ import { getEndpoint, getGatewayLabel } from "@app/pages/cert-manager/pki-discov
type Props = {
discoveryId: string;
projectId: string;
+ isScanRunning: boolean;
};
const PER_PAGE_INIT = 10;
+const SCAN_IN_FLIGHT_POLL_MS = 5000;
-export const DiscoveryInstallationsSection = ({ discoveryId, projectId }: Props) => {
+export const DiscoveryInstallationsSection = ({ discoveryId, projectId, isScanRunning }: Props) => {
const navigate = useNavigate();
const [page, setPage] = useState(1);
const [perPage, setPerPage] = useState(PER_PAGE_INIT);
@@ -39,12 +41,15 @@ export const DiscoveryInstallationsSection = ({ discoveryId, projectId }: Props)
from: "/_authenticate/_inject-org-details/_org-layout/organizations/$orgId/projects/cert-manager/$projectId/_cert-manager-layout/discovery/$discoveryId"
});
- const { data, isPending } = useListPkiInstallations({
- projectId,
- discoveryId,
- offset: (page - 1) * perPage,
- limit: perPage
- });
+ const { data, isPending } = useListPkiInstallations(
+ {
+ projectId,
+ discoveryId,
+ offset: (page - 1) * perPage,
+ limit: perPage
+ },
+ { refetchInterval: isScanRunning ? SCAN_IN_FLIGHT_POLL_MS : false }
+ );
const installations = data?.installations || [];
const totalCount = data?.totalCount || 0;
diff --git a/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryScanLogsSection.tsx b/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryScanLogsSection.tsx
index 9c086001be..3ffe843078 100644
--- a/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryScanLogsSection.tsx
+++ b/frontend/src/pages/cert-manager/DiscoveryDetailsByIDPage/components/DiscoveryScanLogsSection.tsx
@@ -32,24 +32,30 @@ type Props = {
onTriggerScan: () => void;
isTriggerDisabled: boolean;
isTriggerPending: boolean;
+ isScanRunning: boolean;
};
const PER_PAGE_INIT = 10;
+const SCAN_IN_FLIGHT_POLL_MS = 5000;
export const DiscoveryScanLogsSection = ({
discoveryId,
onTriggerScan,
isTriggerDisabled,
- isTriggerPending
+ isTriggerPending,
+ isScanRunning
}: Props) => {
const [page, setPage] = useState(1);
const [perPage, setPerPage] = useState(PER_PAGE_INIT);
- const { data, isPending } = useGetScanHistory({
- discoveryId,
- offset: (page - 1) * perPage,
- limit: perPage
- });
+ const { data, isPending } = useGetScanHistory(
+ {
+ discoveryId,
+ offset: (page - 1) * perPage,
+ limit: perPage
+ },
+ { refetchInterval: isScanRunning ? SCAN_IN_FLIGHT_POLL_MS : false }
+ );
const scans = data?.scans || [];
const totalCount = data?.totalCount || 0;