diff --git a/x-pack/plugins/fleet/server/routes/agent/handlers.test.ts b/x-pack/plugins/fleet/server/routes/agent/handlers.test.ts index 8e064a92a96a1..e5071b5272c27 100644 --- a/x-pack/plugins/fleet/server/routes/agent/handlers.test.ts +++ b/x-pack/plugins/fleet/server/routes/agent/handlers.test.ts @@ -7,7 +7,9 @@ import { coreMock, httpServerMock } from '@kbn/core/server/mocks'; -import { getAvailableVersionsHandler } from './handlers'; +import { getAgentStatusForAgentPolicy } from '../../services/agents/status'; + +import { getAgentStatusForAgentPolicyHandler, getAvailableVersionsHandler } from './handlers'; jest.mock('../../services/agents/versions', () => { return { @@ -24,16 +26,60 @@ jest.mock('../../services/app_context', () => { }; }); -describe('getAvailableVersionsHandler', () => { - it('should return the value from getAvailableVersions', async () => { - const ctx = coreMock.createCustomRequestHandlerContext(coreMock.createRequestHandlerContext()); - const response = httpServerMock.createResponseFactory(); +jest.mock('../../services/agents/status', () => ({ + getAgentStatusForAgentPolicy: jest.fn(), +})); + +describe('Handlers', () => { + describe('getAgentStatusForAgentPolicyHandler', () => { + it.each([ + { requested: 'policy-id-1', called: ['policy-id-1'] }, + { requested: ['policy-id-2'], called: ['policy-id-2'] }, + { requested: ['policy-id-3', 'policy-id-4'], called: ['policy-id-3', 'policy-id-4'] }, + ...[undefined, '', []].map((requested) => ({ requested, called: undefined })), + ])('calls getAgentStatusForAgentPolicy with correct parameters', async (item) => { + const request = { + query: { + policyId: 'policy-id', + kuery: 'kuery', + policyIds: item.requested, + }, + }; + const response = httpServerMock.createResponseFactory(); + + await getAgentStatusForAgentPolicyHandler( + { + core: coreMock.createRequestHandlerContext(), + fleet: { internalSoClient: {} }, + } as any, + request as any, + response + ); + + expect(getAgentStatusForAgentPolicy).toHaveBeenCalledWith( + expect.anything(), + expect.anything(), + 'policy-id', + 'kuery', + undefined, + item.called + ); + }); + }); + + describe('getAvailableVersionsHandler', () => { + it('should return the value from getAvailableVersions', async () => { + const ctx = coreMock.createCustomRequestHandlerContext( + coreMock.createRequestHandlerContext() + ); + const response = httpServerMock.createResponseFactory(); - await getAvailableVersionsHandler(ctx, httpServerMock.createKibanaRequest(), response); + await getAvailableVersionsHandler(ctx, httpServerMock.createKibanaRequest(), response); - expect(response.ok).toBeCalled(); - expect(response.ok.mock.calls[0][0]?.body).toEqual({ - items: ['8.1.0', '8.0.0', '7.17.0'], + expect(response.ok).toBeCalled(); + expect(response.ok.mock.calls[0][0]?.body).toEqual({ + items: ['8.1.0', '8.0.0', '7.17.0'], + }); }); }); }); diff --git a/x-pack/plugins/fleet/server/routes/agent/handlers.ts b/x-pack/plugins/fleet/server/routes/agent/handlers.ts index 67703bba5caae..4f4b5592f2d04 100644 --- a/x-pack/plugins/fleet/server/routes/agent/handlers.ts +++ b/x-pack/plugins/fleet/server/routes/agent/handlers.ts @@ -323,13 +323,22 @@ export const getAgentStatusForAgentPolicyHandler: FleetRequestHandler< const [coreContext, fleetContext] = await Promise.all([context.core, context.fleet]); const esClient = coreContext.elasticsearch.client.asInternalUser; const soClient = fleetContext.internalSoClient; + + const parsePolicyIds = (policyIds: string | string[] | undefined): string[] | undefined => { + if (!policyIds || !policyIds.length) { + return undefined; + } + + return Array.isArray(policyIds) ? policyIds : [policyIds]; + }; + const results = await getAgentStatusForAgentPolicy( esClient, soClient, request.query.policyId, request.query.kuery, coreContext.savedObjects.client.getCurrentNamespace(), - request.query.policyIds + parsePolicyIds(request.query.policyIds) ); const body: GetAgentStatusResponse = { results }; diff --git a/x-pack/plugins/fleet/server/types/rest_spec/agent.ts b/x-pack/plugins/fleet/server/types/rest_spec/agent.ts index 82cae68602e94..8f44d5554501e 100644 --- a/x-pack/plugins/fleet/server/types/rest_spec/agent.ts +++ b/x-pack/plugins/fleet/server/types/rest_spec/agent.ts @@ -241,7 +241,7 @@ export const PostBulkUpdateAgentTagsRequestSchema = { export const GetAgentStatusRequestSchema = { query: schema.object({ policyId: schema.maybe(schema.string()), - policyIds: schema.maybe(schema.arrayOf(schema.string())), + policyIds: schema.maybe(schema.oneOf([schema.arrayOf(schema.string()), schema.string()])), kuery: schema.maybe( schema.string({ validate: (value: string) => { diff --git a/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware/policy_settings_middleware.ts b/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware/policy_settings_middleware.ts index e74403777523c..bf87eab2a7293 100644 --- a/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware/policy_settings_middleware.ts +++ b/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware/policy_settings_middleware.ts @@ -96,7 +96,7 @@ export const policySettingsMiddlewareRunner: MiddlewareRunner = async ( // Agent summary is secondary data, so its ok for it to come after the details // page is populated with the main content - if (policyItem.policy_id) { + if (policyItem.policy_ids?.length) { const { results } = await sendGetFleetAgentStatusForPolicy(http, policyItem.policy_ids); dispatch({ type: 'serverReturnedPolicyDetailsAgentSummaryData', diff --git a/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts b/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts index 523b1b9a858b1..c125464bffdb9 100644 --- a/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts +++ b/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts @@ -83,7 +83,7 @@ export const sendPutPackagePolicy = ( }; /** - * Get a status summary for all Agents that are currently assigned to a given agent policy + * Get a status summary for all Agents that are currently assigned to a given agent policies * * @param http * @param policyIds @@ -91,7 +91,7 @@ export const sendPutPackagePolicy = ( */ export const sendGetFleetAgentStatusForPolicy = ( http: HttpStart, - /** the Agent (fleet) policy id */ + /** the Agent (fleet) policy ids */ policyIds: string[], options: Exclude = {} ): Promise => {