From 5c4beca5f3ff9c87e05a73a561a06876f14039ce Mon Sep 17 00:00:00 2001 From: Julia Bardi Date: Tue, 25 Apr 2023 16:57:28 +0200 Subject: [PATCH 1/3] always create agent upload write indices --- x-pack/plugins/fleet/server/services/setup.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/server/services/setup.ts b/x-pack/plugins/fleet/server/services/setup.ts index ab6b84c7999e3..a26f51508aed5 100644 --- a/x-pack/plugins/fleet/server/services/setup.ts +++ b/x-pack/plugins/fleet/server/services/setup.ts @@ -12,7 +12,11 @@ import pMap from 'p-map'; import type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server'; import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common/constants'; -import { AUTO_UPDATE_PACKAGES, FILE_STORAGE_INTEGRATION_NAMES } from '../../common/constants'; +import { + AUTO_UPDATE_PACKAGES, + FILE_STORAGE_INTEGRATION_NAMES, + FLEET_ELASTIC_AGENT_PACKAGE, +} from '../../common/constants'; import type { PreconfigurationError } from '../../common/constants'; import type { DefaultPackagesInstallationError, @@ -209,6 +213,9 @@ export async function ensureFleetFileUploadIndices( if (!installedFileUploadIntegrations.length) return []; const integrationNames = installedFileUploadIntegrations.map(({ name }) => name); + if (!integrationNames.includes(FLEET_ELASTIC_AGENT_PACKAGE)) { + integrationNames.push(FLEET_ELASTIC_AGENT_PACKAGE); + } logger.debug(`Ensuring file upload write indices for ${integrationNames}`); return ensureFileUploadWriteIndices({ esClient, From 65553b5c3aed35d9b040fd86f9fc497318dab4cb Mon Sep 17 00:00:00 2001 From: Julia Bardi Date: Tue, 25 Apr 2023 17:37:15 +0200 Subject: [PATCH 2/3] added unit test --- x-pack/plugins/fleet/server/mocks/index.ts | 2 +- .../plugins/fleet/server/services/setup.test.ts | 15 ++++++++++++++- x-pack/plugins/fleet/server/services/setup.ts | 1 - 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/fleet/server/mocks/index.ts b/x-pack/plugins/fleet/server/mocks/index.ts index 8e228f445977e..40137a260547d 100644 --- a/x-pack/plugins/fleet/server/mocks/index.ts +++ b/x-pack/plugins/fleet/server/mocks/index.ts @@ -63,7 +63,7 @@ export const createAppContextStartContractMock = ( securitySetup: securityMock.createSetup(), securityStart: securityMock.createStart(), logger: loggingSystemMock.create().get(), - experimentalFeatures: {} as ExperimentalFeatures, + experimentalFeatures: { diagnosticFileUploadEnabled: true } as ExperimentalFeatures, isProductionMode: true, configInitialValue: { agents: { enabled: true, elasticsearch: {} }, diff --git a/x-pack/plugins/fleet/server/services/setup.test.ts b/x-pack/plugins/fleet/server/services/setup.test.ts index 996701c920387..6a82a0c2f64ec 100644 --- a/x-pack/plugins/fleet/server/services/setup.test.ts +++ b/x-pack/plugins/fleet/server/services/setup.test.ts @@ -15,7 +15,9 @@ import { ensurePreconfiguredPackagesAndPolicies } from '.'; import { appContextService } from './app_context'; import { getInstallations } from './epm/packages'; import { upgradeManagedPackagePolicies } from './managed_package_policies'; -import { setupFleet } from './setup'; +import { setupFleet, ensureFleetFileUploadIndices } from './setup'; + +import { ensureFileUploadWriteIndices } from './epm/elasticsearch/template/install'; jest.mock('./preconfiguration'); jest.mock('./preconfiguration/outputs'); @@ -26,6 +28,7 @@ jest.mock('./download_source'); jest.mock('./epm/packages'); jest.mock('./managed_package_policies'); jest.mock('./setup/upgrade_package_install_version'); +jest.mock('./epm/elasticsearch/template/install'); const mockedMethodThrowsError = (mockFn: jest.Mock) => mockFn.mockImplementation(() => { @@ -62,6 +65,8 @@ describe('setupFleet', () => { soClient.find.mockResolvedValue({ saved_objects: [] } as any); soClient.bulkGet.mockResolvedValue({ saved_objects: [] } as any); + + (ensureFileUploadWriteIndices as jest.Mock).mockResolvedValue({}); }); afterEach(async () => { @@ -128,4 +133,12 @@ describe('setupFleet', () => { ], }); }); + + it('should create agent file upload write indices', async () => { + await ensureFleetFileUploadIndices(soClient, esClient); + + expect((ensureFileUploadWriteIndices as jest.Mock).mock.calls[0][0].integrationNames).toEqual([ + 'elastic_agent', + ]); + }); }); diff --git a/x-pack/plugins/fleet/server/services/setup.ts b/x-pack/plugins/fleet/server/services/setup.ts index a26f51508aed5..15228773a255d 100644 --- a/x-pack/plugins/fleet/server/services/setup.ts +++ b/x-pack/plugins/fleet/server/services/setup.ts @@ -211,7 +211,6 @@ export async function ensureFleetFileUploadIndices( pkgNames: [...FILE_STORAGE_INTEGRATION_NAMES], }); - if (!installedFileUploadIntegrations.length) return []; const integrationNames = installedFileUploadIntegrations.map(({ name }) => name); if (!integrationNames.includes(FLEET_ELASTIC_AGENT_PACKAGE)) { integrationNames.push(FLEET_ELASTIC_AGENT_PACKAGE); From 542b7330edca3cd4f6f9017d7b0c94065ee99cbd Mon Sep 17 00:00:00 2001 From: Julia Bardi Date: Wed, 26 Apr 2023 09:38:58 +0200 Subject: [PATCH 3/3] fix tests --- x-pack/plugins/fleet/server/services/setup.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/server/services/setup.test.ts b/x-pack/plugins/fleet/server/services/setup.test.ts index 6a82a0c2f64ec..7d98db879910b 100644 --- a/x-pack/plugins/fleet/server/services/setup.test.ts +++ b/x-pack/plugins/fleet/server/services/setup.test.ts @@ -28,7 +28,12 @@ jest.mock('./download_source'); jest.mock('./epm/packages'); jest.mock('./managed_package_policies'); jest.mock('./setup/upgrade_package_install_version'); -jest.mock('./epm/elasticsearch/template/install'); +jest.mock('./epm/elasticsearch/template/install', () => { + return { + ...jest.requireActual('./epm/elasticsearch/template/install'), + ensureFileUploadWriteIndices: jest.fn(), + }; +}); const mockedMethodThrowsError = (mockFn: jest.Mock) => mockFn.mockImplementation(() => {