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 5 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 Down Expand Up @@ -43,17 +44,27 @@ 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) {
const httpError = t.type({ statusCode: t.number });
orouz marked this conversation as resolved.
Show resolved Hide resolved
if (httpError.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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
const ruleName2 = data[1].rule.name;

// Failing: See https://github.com/elastic/kibana/issues/147998
describe.skip('Findings Page', () => {
describe('Findings Page', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove changes to this file, we're going to do it in a separate PR with a few other FTR changes as well.

let findings: typeof pageObjects.findings;
let table: typeof pageObjects.findings.table;
let distributionBar: typeof pageObjects.findings.distributionBar;
Expand Down