diff --git a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.test.ts b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.test.ts index 9e9c2bd92c354..3b200ac5115cf 100644 --- a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.test.ts @@ -10,20 +10,13 @@ import type { SavedObjectsImportSuccess, SavedObjectsImportResponse, } from '@kbn/core/server'; -import { - loggingSystemMock, - savedObjectsClientMock, - savedObjectsServiceMock, -} from '@kbn/core/server/mocks'; +import { loggingSystemMock } from '@kbn/core/server/mocks'; -import { appContextService } from '../../../app_context'; - -import { type ArchiveAsset, getSpaceAwareSaveobjectsClients } from './install'; +import { type ArchiveAsset } from './install'; jest.mock('timers/promises', () => ({ async setTimeout() {}, })); -jest.mock('../../../app_context'); import { createSavedObjectKibanaAsset, installKibanaSavedObjects } from './install'; @@ -152,30 +145,3 @@ describe('createSavedObjectKibanaAsset', () => { expect(result.coreMigrationVersion).toEqual('8.7.0'); }); }); - -describe('getSpaceAwareSaveobjectsClients', () => { - it('return space scopped clients', () => { - const soStartMock = savedObjectsServiceMock.createStartContract(); - const mockedSavedObjectTagging = { - createInternalAssignmentService: jest.fn(), - createTagClient: jest.fn(), - }; - - const scoppedSoClient = savedObjectsClientMock.create(); - jest - .mocked(appContextService.getInternalUserSOClientForSpaceId) - .mockReturnValue(scoppedSoClient); - - jest.mocked(appContextService.getSavedObjects).mockReturnValue(soStartMock); - jest.mocked(appContextService.getSavedObjectsTagging).mockReturnValue(mockedSavedObjectTagging); - - getSpaceAwareSaveobjectsClients('test1'); - - expect(appContextService.getInternalUserSOClientForSpaceId).toBeCalledWith('test1'); - expect(soStartMock.createImporter).toBeCalledWith(scoppedSoClient, expect.anything()); - expect(mockedSavedObjectTagging.createInternalAssignmentService).toBeCalledWith({ - client: scoppedSoClient, - }); - expect(mockedSavedObjectTagging.createTagClient).toBeCalledWith({ client: scoppedSoClient }); - }); -}); diff --git a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts index 7399ea43c9a5a..e7e453648a596 100644 --- a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts @@ -31,10 +31,10 @@ import { import { kibanaAssetsToAssetsRef, saveKibanaAssetsRefs } from '../../packages/install'; import { deleteKibanaSavedObjectsAssets } from '../../packages/remove'; import { FleetError, KibanaSOReferenceError } from '../../../../errors'; -import { appContextService } from '../../../app_context'; import { withPackageSpan } from '../../packages/utils'; import { tagKibanaAssets } from './tag_assets'; +import { getSpaceAwareSaveobjectsClients } from './saved_objects'; type SavedObjectsImporterContract = Pick; const formatImportErrorsForLog = (errors: SavedObjectsImportFailure[]) => @@ -219,30 +219,6 @@ export async function installKibanaAssetsAndReferencesMultispace({ }); } -export function getSpaceAwareSaveobjectsClients(spaceId?: string) { - // Saved object client need to be scopped with the package space for saved object tagging - const savedObjectClientWithSpace = appContextService.getInternalUserSOClientForSpaceId(spaceId); - - const savedObjectsImporter = appContextService - .getSavedObjects() - .createImporter(savedObjectClientWithSpace, { importSizeLimit: 15_000 }); - - const savedObjectTagAssignmentService = appContextService - .getSavedObjectsTagging() - .createInternalAssignmentService({ client: savedObjectClientWithSpace }); - - const savedObjectTagClient = appContextService - .getSavedObjectsTagging() - .createTagClient({ client: savedObjectClientWithSpace }); - - return { - savedObjectClientWithSpace, - savedObjectsImporter, - savedObjectTagAssignmentService, - savedObjectTagClient, - }; -} - export async function installKibanaAssetsAndReferences({ savedObjectsClient, logger, diff --git a/x-pack/plugins/fleet/server/services/epm/kibana/assets/saved_object.test.ts b/x-pack/plugins/fleet/server/services/epm/kibana/assets/saved_object.test.ts new file mode 100644 index 0000000000000..8ac5703265075 --- /dev/null +++ b/x-pack/plugins/fleet/server/services/epm/kibana/assets/saved_object.test.ts @@ -0,0 +1,41 @@ +/* + * 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 { savedObjectsClientMock, savedObjectsServiceMock } from '@kbn/core/server/mocks'; + +import { appContextService } from '../../../app_context'; + +import { getSpaceAwareSaveobjectsClients } from './saved_objects'; + +jest.mock('../../../app_context'); + +describe('getSpaceAwareSaveobjectsClients', () => { + it('return space scopped clients', () => { + const soStartMock = savedObjectsServiceMock.createStartContract(); + const mockedSavedObjectTagging = { + createInternalAssignmentService: jest.fn(), + createTagClient: jest.fn(), + }; + + const scoppedSoClient = savedObjectsClientMock.create(); + jest + .mocked(appContextService.getInternalUserSOClientForSpaceId) + .mockReturnValue(scoppedSoClient); + + jest.mocked(appContextService.getSavedObjects).mockReturnValue(soStartMock); + jest.mocked(appContextService.getSavedObjectsTagging).mockReturnValue(mockedSavedObjectTagging); + + getSpaceAwareSaveobjectsClients('test1'); + + expect(appContextService.getInternalUserSOClientForSpaceId).toBeCalledWith('test1'); + expect(soStartMock.createImporter).toBeCalledWith(scoppedSoClient, expect.anything()); + expect(mockedSavedObjectTagging.createInternalAssignmentService).toBeCalledWith({ + client: scoppedSoClient, + }); + expect(mockedSavedObjectTagging.createTagClient).toBeCalledWith({ client: scoppedSoClient }); + }); +}); diff --git a/x-pack/plugins/fleet/server/services/epm/kibana/assets/saved_objects.ts b/x-pack/plugins/fleet/server/services/epm/kibana/assets/saved_objects.ts new file mode 100644 index 0000000000000..c4ff55ca8ac82 --- /dev/null +++ b/x-pack/plugins/fleet/server/services/epm/kibana/assets/saved_objects.ts @@ -0,0 +1,32 @@ +/* + * 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 { appContextService } from '../../../app_context'; + +export function getSpaceAwareSaveobjectsClients(spaceId?: string) { + // Saved object client need to be scopped with the package space for saved object tagging + const savedObjectClientWithSpace = appContextService.getInternalUserSOClientForSpaceId(spaceId); + + const savedObjectsImporter = appContextService + .getSavedObjects() + .createImporter(savedObjectClientWithSpace, { importSizeLimit: 15_000 }); + + const savedObjectTagAssignmentService = appContextService + .getSavedObjectsTagging() + .createInternalAssignmentService({ client: savedObjectClientWithSpace }); + + const savedObjectTagClient = appContextService + .getSavedObjectsTagging() + .createTagClient({ client: savedObjectClientWithSpace }); + + return { + savedObjectClientWithSpace, + savedObjectsImporter, + savedObjectTagAssignmentService, + savedObjectTagClient, + }; +}