Skip to content

Commit

Permalink
[Cloud Posture] - Fixing CSP no fleet agent error (elastic#148632)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofiriro3 authored and jennypavlova committed Jan 13, 2023
1 parent 5afad67 commit 12a2abf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
23 changes: 17 additions & 6 deletions x-pack/plugins/cloud_security_posture/server/lib/fleet_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export const defineGetBenchmarksRoute = (router: CspRouter): void =>

const agentStatusesByAgentPolicyId = await getAgentStatusesByAgentPolicies(
cspContext.agentService,
agentPolicies
agentPolicies,
cspContext.logger
);

const benchmarks = await createBenchmarks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -123,7 +125,8 @@ const getCspStatus = async ({
soClient,
installedPackagePolicies.items,
agentPolicyService,
agentService
agentService,
logger
);

const installedPackagePoliciesTotal = installedPackagePolicies.total;
Expand Down

0 comments on commit 12a2abf

Please sign in to comment.