From 12a2abf6031ad72c98ec7718cbf8b565bb43c0a8 Mon Sep 17 00:00:00 2001 From: ofiriro3 Date: Tue, 10 Jan 2023 23:47:31 +0200 Subject: [PATCH] [Cloud Posture] - Fixing CSP no fleet agent error (#148632) --- .../server/lib/fleet_util.ts | 23 ++++++++++++++----- .../server/routes/benchmarks/benchmarks.ts | 3 ++- .../server/routes/status/status.ts | 11 +++++---- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/cloud_security_posture/server/lib/fleet_util.ts b/x-pack/plugins/cloud_security_posture/server/lib/fleet_util.ts index ada8548240ae97..8123fb34ee1766 100644 --- a/x-pack/plugins/cloud_security_posture/server/lib/fleet_util.ts +++ b/x-pack/plugins/cloud_security_posture/server/lib/fleet_util.ts @@ -5,7 +5,8 @@ * 2.0. */ import { map, uniq } from 'lodash'; -import type { SavedObjectsClientContract } from '@kbn/core/server'; +import * as t from 'io-ts'; +import type { SavedObjectsClientContract, Logger } from '@kbn/core/server'; import type { AgentPolicyServiceInterface, AgentService, @@ -26,6 +27,7 @@ import { } from '../../common/schemas/benchmark'; export const PACKAGE_POLICY_SAVED_OBJECT_TYPE = 'ingest-package-policies'; +const fleetError = t.type({ statusCode: t.number }); const isPolicyTemplate = (input: any): input is PosturePolicyTemplate => SUPPORTED_POLICY_TEMPLATES.includes(input); @@ -43,17 +45,26 @@ export type AgentStatusByAgentPolicyMap = Record => { if (!agentPolicies?.length) return {}; const internalAgentService = agentService.asInternalUser; const result: AgentStatusByAgentPolicyMap = {}; - for (const agentPolicy of agentPolicies) { - result[agentPolicy.id] = await internalAgentService.getAgentStatusForAgentPolicy( - agentPolicy.id - ); + try { + for (const agentPolicy of agentPolicies) { + result[agentPolicy.id] = await internalAgentService.getAgentStatusForAgentPolicy( + agentPolicy.id + ); + } + } catch (error) { + if (fleetError.is(error) && error.statusCode === 404) { + logger.debug('failed to get agent status for agent policy'); + } else { + throw error; + } } return result; diff --git a/x-pack/plugins/cloud_security_posture/server/routes/benchmarks/benchmarks.ts b/x-pack/plugins/cloud_security_posture/server/routes/benchmarks/benchmarks.ts index eafacfac12f4e5..cd019c189c76f7 100644 --- a/x-pack/plugins/cloud_security_posture/server/routes/benchmarks/benchmarks.ts +++ b/x-pack/plugins/cloud_security_posture/server/routes/benchmarks/benchmarks.ts @@ -115,7 +115,8 @@ export const defineGetBenchmarksRoute = (router: CspRouter): void => const agentStatusesByAgentPolicyId = await getAgentStatusesByAgentPolicies( cspContext.agentService, - agentPolicies + agentPolicies, + cspContext.logger ); const benchmarks = await createBenchmarks( diff --git a/x-pack/plugins/cloud_security_posture/server/routes/status/status.ts b/x-pack/plugins/cloud_security_posture/server/routes/status/status.ts index 33627389511a15..eead1dc267e60e 100644 --- a/x-pack/plugins/cloud_security_posture/server/routes/status/status.ts +++ b/x-pack/plugins/cloud_security_posture/server/routes/status/status.ts @@ -6,7 +6,7 @@ */ import { transformError } from '@kbn/securitysolution-es-utils'; -import type { SavedObjectsClientContract } from '@kbn/core/server'; +import type { SavedObjectsClientContract, Logger } from '@kbn/core/server'; import type { AgentPolicyServiceInterface, AgentService } from '@kbn/fleet-plugin/server'; import moment from 'moment'; import { PackagePolicy } from '@kbn/fleet-plugin/common'; @@ -37,7 +37,8 @@ const getHealthyAgents = async ( soClient: SavedObjectsClientContract, installedCspPackagePolicies: PackagePolicy[], agentPolicyService: AgentPolicyServiceInterface, - agentService: AgentService + agentService: AgentService, + logger: Logger ): Promise => { // Get agent policies of package policies (from installed package policies) const agentPolicies = await getCspAgentPolicies( @@ -49,7 +50,8 @@ const getHealthyAgents = async ( // Get agents statuses of the following agent policies const agentStatusesByAgentPolicyId = await getAgentStatusesByAgentPolicies( agentService, - agentPolicies + agentPolicies, + logger ); return Object.values(agentStatusesByAgentPolicyId).reduce( @@ -123,7 +125,8 @@ const getCspStatus = async ({ soClient, installedPackagePolicies.items, agentPolicyService, - agentService + agentService, + logger ); const installedPackagePoliciesTotal = installedPackagePolicies.total;