From ac17ab1436986b37f9d0a3969aff70e728481cf7 Mon Sep 17 00:00:00 2001 From: Esteban Beltran Date: Tue, 29 Jun 2021 08:58:37 +0200 Subject: [PATCH] Add signal and abort controller to agent metadata and TakeAction button (#103217) --- .../containers/detection_engine/alerts/api.ts | 10 ++++++++-- .../alerts/use_host_isolation_status.tsx | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/api.ts b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/api.ts index 05706981a681d..72a9bf6e84441 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/api.ts +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/api.ts @@ -178,8 +178,14 @@ export const getCaseIdsFromAlertId = async ({ * * @param host id */ -export const getHostMetadata = async ({ agentId }: { agentId: string }): Promise => +export const getHostMetadata = async ({ + agentId, + signal, +}: { + agentId: string; + signal?: AbortSignal; +}): Promise => KibanaServices.get().http.fetch( resolvePathVariables(HOST_METADATA_GET_ROUTE, { id: agentId }), - { method: 'get' } + { method: 'GET', signal } ); diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_host_isolation_status.tsx b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_host_isolation_status.tsx index 3bdd8c9813785..259a377b10b79 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_host_isolation_status.tsx +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_host_isolation_status.tsx @@ -38,18 +38,23 @@ export const useHostIsolationStatus = ({ const { addError } = useAppToasts(); useEffect(() => { + const abortCtrl = new AbortController(); // isMounted tracks if a component is mounted before changing state let isMounted = true; let fleetAgentId: string; const fetchData = async () => { try { - const metadataResponse = await getHostMetadata({ agentId }); + const metadataResponse = await getHostMetadata({ agentId, signal: abortCtrl.signal }); if (isMounted) { setIsIsolated(isEndpointHostIsolated(metadataResponse.metadata)); setAgentStatus(metadataResponse.host_status); fleetAgentId = metadataResponse.metadata.elastic.agent.id; } } catch (error) { + // don't show self-aborted requests errors to the user + if (error.name === 'AbortError') { + return; + } addError(error.message, { title: ISOLATION_STATUS_FAILURE }); } @@ -80,6 +85,7 @@ export const useHostIsolationStatus = ({ return () => { // updates to show component is unmounted isMounted = false; + abortCtrl.abort(); }; }, [addError, agentId]); return { loading, isIsolated, agentStatus, pendingIsolation, pendingUnisolation };