Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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