diff --git a/x-pack/plugins/security_solution/server/endpoint/migrations/ensure_indices_exists_for_policies.test.ts b/x-pack/plugins/security_solution/server/endpoint/migrations/ensure_indices_exists_for_policies.test.ts new file mode 100644 index 0000000000000..834f6c12a067f --- /dev/null +++ b/x-pack/plugins/security_solution/server/endpoint/migrations/ensure_indices_exists_for_policies.test.ts @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { createMockEndpointAppContextService } from '../mocks'; +import { ensureIndicesExistsForPolicies } from './ensure_indices_exists_for_policies'; +import { createPolicyDataStreamsIfNeeded as _createPolicyDataStreamsIfNeeded } from '../../fleet_integration/handlers/create_policy_datastreams'; + +jest.mock('../../fleet_integration/handlers/create_policy_datastreams'); +const createPolicyDataStreamsIfNeededMock = _createPolicyDataStreamsIfNeeded as jest.Mock; + +describe('Ensure indices exists for policies migration', () => { + let endpointAppContextServicesMock: ReturnType; + + beforeEach(() => { + endpointAppContextServicesMock = createMockEndpointAppContextService(); + + endpointAppContextServicesMock + .getInternalFleetServices() + .packagePolicy.listIds.mockResolvedValue({ + items: ['foo-1', 'foo-2', 'foo-3'], + }); + }); + + it('should query fleet looking for all endpoint integration policies', async () => { + const fleetServicesMock = endpointAppContextServicesMock.getInternalFleetServices(); + await ensureIndicesExistsForPolicies(endpointAppContextServicesMock); + + expect(fleetServicesMock.packagePolicy.listIds).toHaveBeenCalledWith(expect.anything(), { + kuery: fleetServicesMock.endpointPolicyKuery, + perPage: 10000, + }); + }); + + it('should call createPolicyDataStreamsIfNeeded() with list of existing policies', async () => { + await ensureIndicesExistsForPolicies(endpointAppContextServicesMock); + + expect(createPolicyDataStreamsIfNeededMock).toHaveBeenCalledWith({ + endpointServices: endpointAppContextServicesMock, + endpointPolicyIds: ['foo-1', 'foo-2', 'foo-3'], + }); + }); +}); diff --git a/x-pack/plugins/security_solution/server/endpoint/migrations/ensure_indices_exists_for_policies.ts b/x-pack/plugins/security_solution/server/endpoint/migrations/ensure_indices_exists_for_policies.ts index 70b898dee29cd..778a333fc4818 100644 --- a/x-pack/plugins/security_solution/server/endpoint/migrations/ensure_indices_exists_for_policies.ts +++ b/x-pack/plugins/security_solution/server/endpoint/migrations/ensure_indices_exists_for_policies.ts @@ -11,6 +11,8 @@ import type { EndpointAppContextService } from '../endpoint_app_context_services export const ensureIndicesExistsForPolicies = async ( endpointServices: EndpointAppContextService ): Promise => { + const logger = endpointServices.createLogger('startupPolicyIndicesChecker'); + const fleetServices = endpointServices.getInternalFleetServices(); const soClient = fleetServices.savedObjects.createInternalUnscopedSoClient(); const endpointPoliciesIds = await fleetServices.packagePolicy.listIds(soClient, { @@ -18,6 +20,10 @@ export const ensureIndicesExistsForPolicies = async ( perPage: 10000, }); + logger.info( + `Checking to ensure [${endpointPoliciesIds.items.length}] endpoint policies have backing indices` + ); + await createPolicyDataStreamsIfNeeded({ endpointServices, endpointPolicyIds: endpointPoliciesIds.items,