diff --git a/x-pack/plugins/fleet/common/types/models/agent.ts b/x-pack/plugins/fleet/common/types/models/agent.ts index 525bac241e66f..9d8034fd1d4bc 100644 --- a/x-pack/plugins/fleet/common/types/models/agent.ts +++ b/x-pack/plugins/fleet/common/types/models/agent.ts @@ -202,7 +202,8 @@ export interface FleetServerAgentComponent { type: string; status: FleetServerAgentComponentStatus; message: string; - units: FleetServerAgentComponentUnit[]; + // In some case units could be missing + units?: FleetServerAgentComponentUnit[]; } /** diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/agent_details_integration_inputs.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/agent_details_integration_inputs.test.tsx index 059ce60f16799..59139a09a792f 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/agent_details_integration_inputs.test.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/agent_details_integration_inputs.test.tsx @@ -106,4 +106,21 @@ describe('AgentDetailsIntegrationInputs', () => { component.getByTestId('agentDetailsIntegrationsInputStatusHealthSuccess') ).toBeInTheDocument(); }); + + it('does not render when there is no units array', () => { + agent.components = [ + { + id: 'endpoint-default', + type: 'endpoint', + status: 'HEALTHY', + message: 'Healthy', + }, + ]; + + const component = renderComponent(); + userEvent.click(component.getByTestId('agentIntegrationsInputsTitle')); + expect( + component.queryByTestId('agentDetailsIntegrationsInputStatusHealthSuccess') + ).not.toBeInTheDocument(); + }); }); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.test.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.test.ts index c34a562b0832f..94abcfd1e1c00 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.test.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.test.ts @@ -168,6 +168,13 @@ describe('getInputUnitsByPackage', () => { }, ], }, + // test a component with no units + { + id: 'beat/metrics-monitoring', + type: 'beat/metrics', + status: 'HEALTHY', + message: "Healthy: communicating with pid '2234'", + }, ]; const packageMock = createPackagePolicyMock(); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.ts index 5a761919d62e9..14cdb0318a39d 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_details/input_status_utils.ts @@ -49,8 +49,9 @@ export const getInputUnitsByPackage = ( packagePolicy: PackagePolicy ): FleetServerAgentComponentUnit[] => { const re = new RegExp(packagePolicy.id); + return agentComponents - .map((c) => c.units) + .map((c) => c?.units || []) .flat() - .filter((u) => u.id.match(re)); + .filter((u) => !!u && u.id.match(re)); }; diff --git a/x-pack/plugins/fleet/server/services/agents/helpers.test.ts b/x-pack/plugins/fleet/server/services/agents/helpers.test.ts index 92a1d3165cc04..8e13c3bfe6a28 100644 --- a/x-pack/plugins/fleet/server/services/agents/helpers.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/helpers.test.ts @@ -163,4 +163,128 @@ describe('searchHitToAgent', () => { }, }); }); + + it('should work when units is not present', () => { + const hit = { + _source: { + access_api_key_id: 'EH_RlIgBn_WkCEINY-qh', + active: true, + enrolled_at: '2023-06-07T07:45:30Z', + local_metadata: { + elastic: { + agent: { + 'build.original': + '8.9.0-SNAPSHOT (build: 953fda060f317c2389ef6fd1cac8806a2bfe92ac at 2023-05-29 14:51:32 +0000 UTC)', + }, + }, + }, + agent: { + id: '504b3006-52df-46a6-b7db-f3dc67aca7ac', + version: '8.9.0', + }, + policy_id: '76c5b020-0486-11ee-97a3-c3856dd800f7', + type: 'PERMANENT', + outputs: { + '68233290-0486-11ee-97a3-c3856dd800f7': { + api_key: 'En_RlIgBn_WkCEINb-pQ:mfeV4ji6RNGyCOBs25gteg', + permissions_hash: '6ac9e595a2f8cba8893f9ea1fbfb6cba4b4d6f16d935c17a6368f11ee0b0a5d8', + type: 'elasticsearch', + api_key_id: 'En_RlIgBn_WkCEINb-pQ', + to_retire_api_key_ids: [], + }, + }, + policy_revision_idx: 2, + components: [ + { + id: 'system/metrics-68233290-0486-11ee-97a3-c3856dd800f7', + type: 'system/metrics', + message: "Healthy: communicating with pid '36'", + status: 'HEALTHY', + }, + ], + last_checkin_message: 'Running', + last_checkin_status: 'online', + last_checkin: '2023-06-07T08:39:03Z', + unenrolled_at: '2023-06-07T07:45:30Z', + unenrollment_started_at: '2023-06-07T07:45:30Z', + upgraded_at: '2023-06-07T07:45:30Z', + upgrade_started_at: '2023-06-07T07:45:30Z', + default_api_key_id: 'EH_RlIgBn_WkCEINY-qh', + packages: ['system'], + tags: ['agent'], + user_provided_metadata: { + key: 'val', + }, + default_api_key_history: [ + { + retired_at: '', + }, + ], + }, + sort: [1686123930000, 'beb13bf6a73e'], + fields: { + status: ['online'], + }, + _id: '504b3006-52df-46a6-b7db-f3dc67aca7ac', + }; + const agent = searchHitToAgent(hit as any); + expect(agent).toEqual({ + id: '504b3006-52df-46a6-b7db-f3dc67aca7ac', + type: 'PERMANENT', + active: true, + enrolled_at: '2023-06-07T07:45:30Z', + access_api_key_id: 'EH_RlIgBn_WkCEINY-qh', + policy_id: '76c5b020-0486-11ee-97a3-c3856dd800f7', + last_checkin: '2023-06-07T08:39:03Z', + last_checkin_status: 'online', + last_checkin_message: 'Running', + policy_revision: 2, + sort: [1686123930000, 'beb13bf6a73e'], + outputs: { + '68233290-0486-11ee-97a3-c3856dd800f7': { + api_key_id: 'En_RlIgBn_WkCEINb-pQ', + type: 'elasticsearch', + to_retire_api_key_ids: [], + }, + }, + components: [ + { + id: 'system/metrics-68233290-0486-11ee-97a3-c3856dd800f7', + type: 'system/metrics', + status: 'HEALTHY', + message: "Healthy: communicating with pid '36'", + units: undefined, + }, + ], + local_metadata: { + elastic: { + agent: { + 'build.original': + '8.9.0-SNAPSHOT (build: 953fda060f317c2389ef6fd1cac8806a2bfe92ac at 2023-05-29 14:51:32 +0000 UTC)', + }, + }, + }, + status: 'online', + unenrolled_at: '2023-06-07T07:45:30Z', + unenrollment_started_at: '2023-06-07T07:45:30Z', + upgraded_at: '2023-06-07T07:45:30Z', + upgrade_started_at: '2023-06-07T07:45:30Z', + default_api_key_id: 'EH_RlIgBn_WkCEINY-qh', + packages: ['system'], + tags: ['agent'], + user_provided_metadata: { + key: 'val', + }, + default_api_key_history: [ + { + id: undefined, + retired_at: '', + }, + ], + agent: { + id: '504b3006-52df-46a6-b7db-f3dc67aca7ac', + version: '8.9.0', + }, + }); + }); }); diff --git a/x-pack/plugins/fleet/server/services/agents/helpers.ts b/x-pack/plugins/fleet/server/services/agents/helpers.ts index e255b7ea6ce70..4aaab95e9d1ac 100644 --- a/x-pack/plugins/fleet/server/services/agents/helpers.ts +++ b/x-pack/plugins/fleet/server/services/agents/helpers.ts @@ -47,7 +47,7 @@ export function searchHitToAgent( type: component.type, status: component.status, message: component.message, - units: component.units.map((unit) => ({ + units: component.units?.map((unit) => ({ id: unit.id, type: unit.type, status: unit.status,