[Cloud Posture] - Fixing CSP no fleet agent error (#148632)

This commit is contained in:
ofiriro3 2023-01-10 23:47:31 +02:00 committed by GitHub
parent 943437a551
commit 72b9940aed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 11 deletions

View file

@ -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<string, GetAgentStatusResponse[
export const getAgentStatusesByAgentPolicies = async (
agentService: AgentService,
agentPolicies: AgentPolicy[] | undefined
agentPolicies: AgentPolicy[] | undefined,
logger: Logger
): Promise<AgentStatusByAgentPolicyMap> => {
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;

View file

@ -115,7 +115,8 @@ export const defineGetBenchmarksRoute = (router: CspRouter): void =>
const agentStatusesByAgentPolicyId = await getAgentStatusesByAgentPolicies(
cspContext.agentService,
agentPolicies
agentPolicies,
cspContext.logger
);
const benchmarks = await createBenchmarks(

View file

@ -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<number> => {
// 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;