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 e83b4e45fbd92..5ab15a1f52e75 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 @@ -9,6 +9,8 @@ import type { SavedObject, SavedObjectsBulkCreateObject, SavedObjectsClientContract, + SavedObjectsImporter, + Logger, } from 'src/core/server'; import type { SavedObjectsImportSuccess, SavedObjectsImportFailure } from 'src/core/server/types'; @@ -21,8 +23,8 @@ import { KibanaAssetType, KibanaSavedObjectType } from '../../../../types'; import type { AssetType, AssetReference, AssetParts } from '../../../../types'; import { savedObjectTypes } from '../../packages'; import { indexPatternTypes, getIndexPatternSavedObjects } from '../index_pattern/install'; -import { appContextService } from '../../../../services'; +type SavedObjectsImporterContract = Pick; const formatImportErrorsForLog = (errors: SavedObjectsImportFailure[]) => JSON.stringify( errors.map(({ type, id, error }) => ({ type, id, error })) // discard other fields @@ -75,11 +77,12 @@ export function createSavedObjectKibanaAsset(asset: ArchiveAsset): SavedObjectTo } export async function installKibanaAssets(options: { - savedObjectsClient: SavedObjectsClientContract; + savedObjectsImporter: SavedObjectsImporterContract; + logger: Logger; pkgName: string; kibanaAssets: Record; }): Promise { - const { savedObjectsClient, kibanaAssets } = options; + const { kibanaAssets, savedObjectsImporter, logger } = options; const assetsToInstall = Object.entries(kibanaAssets).flatMap(([assetType, assets]) => { if (!validKibanaAssetTypes.has(assetType as KibanaAssetType)) { return []; @@ -107,7 +110,8 @@ export async function installKibanaAssets(options: { const indexPatternSavedObjects = getIndexPatternSavedObjects() as ArchiveAsset[]; const installedAssets = await installKibanaSavedObjects({ - savedObjectsClient, + logger, + savedObjectsImporter, kibanaAssets: [...indexPatternSavedObjects, ...assetsToInstall], }); @@ -163,11 +167,13 @@ export async function getKibanaAssets( } async function installKibanaSavedObjects({ - savedObjectsClient, + savedObjectsImporter, kibanaAssets, + logger, }: { - savedObjectsClient: SavedObjectsClientContract; kibanaAssets: ArchiveAsset[]; + savedObjectsImporter: SavedObjectsImporterContract; + logger: Logger; }) { const toBeSavedObjects = await Promise.all( kibanaAssets.map((asset) => createSavedObjectKibanaAsset(asset)) @@ -178,10 +184,6 @@ async function installKibanaSavedObjects({ if (toBeSavedObjects.length === 0) { return []; } else { - const savedObjectsImporter = appContextService - .getSavedObjects() - .createImporter(savedObjectsClient); - const { successResults: importSuccessResults = [], errors: importErrors = [] } = await savedObjectsImporter.import({ overwrite: true, @@ -210,8 +212,6 @@ async function installKibanaSavedObjects({ can still be installed, but if a warning is logged it should be reported to the integrations team. */ if (referenceErrors.length) { - const logger = appContextService.getLogger(); - logger.debug( `Resolving ${ referenceErrors.length diff --git a/x-pack/plugins/fleet/server/services/epm/packages/_install_package.test.ts b/x-pack/plugins/fleet/server/services/epm/packages/_install_package.test.ts index 72587d7d37200..dbec18851cfc9 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/_install_package.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/_install_package.test.ts @@ -59,6 +59,8 @@ describe('_installPackage', () => { const installationPromise = _installPackage({ savedObjectsClient: soClient, + // @ts-ignore + savedObjectsImporter: jest.fn(), esClient, logger: loggerMock.create(), paths: [], diff --git a/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts b/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts index 04f936b889942..ac0c7e1729913 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/_install_package.ts @@ -10,6 +10,7 @@ import type { Logger, SavedObject, SavedObjectsClientContract, + SavedObjectsImporter, } from 'src/core/server'; import { @@ -36,7 +37,6 @@ import { installMlModel } from '../elasticsearch/ml_model/'; import { installIlmForDataStream } from '../elasticsearch/datastream_ilm/install'; import { saveArchiveEntries } from '../archive/storage'; import { ConcurrentInstallOperationError } from '../../../errors'; - import { packagePolicyService } from '../..'; import { createInstallation, saveKibanaAssetsRefs, updateVersion } from './install'; @@ -48,6 +48,7 @@ import { deleteKibanaSavedObjectsAssets } from './remove'; export async function _installPackage({ savedObjectsClient, + savedObjectsImporter, esClient, logger, installedPkg, @@ -57,6 +58,7 @@ export async function _installPackage({ installSource, }: { savedObjectsClient: SavedObjectsClientContract; + savedObjectsImporter: Pick; esClient: ElasticsearchClient; logger: Logger; installedPkg?: SavedObject; @@ -112,8 +114,10 @@ export async function _installPackage({ pkgName, kibanaAssets ); + await installKibanaAssets({ - savedObjectsClient, + logger, + savedObjectsImporter, pkgName, kibanaAssets, }); diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts b/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts index a9bb235c22cb8..261a0d9a6d688 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts @@ -26,6 +26,9 @@ jest.mock('../../app_context', () => { return { error: jest.fn(), debug: jest.fn(), warn: jest.fn() }; }), getTelemetryEventsSender: jest.fn(), + getSavedObjects: jest.fn(() => ({ + createImporter: jest.fn(), + })), }, }; }); diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install.ts b/x-pack/plugins/fleet/server/services/epm/packages/install.ts index 4522b32f39616..a580248b43731 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.ts @@ -302,10 +302,15 @@ async function installPackageFromRegistry({ return { error: err, installType }; } + const savedObjectsImporter = appContextService + .getSavedObjects() + .createImporter(savedObjectsClient); + // try installing the package, if there was an error, call error handler and rethrow // @ts-expect-error status is string instead of InstallResult.status 'installed' | 'already_installed' return _installPackage({ savedObjectsClient, + savedObjectsImporter, esClient, logger, installedPkg, @@ -406,9 +411,15 @@ async function installPackageByUpload({ version: packageInfo.version, packageInfo, }); + + const savedObjectsImporter = appContextService + .getSavedObjects() + .createImporter(savedObjectsClient); + // @ts-expect-error status is string instead of InstallResult.status 'installed' | 'already_installed' return _installPackage({ savedObjectsClient, + savedObjectsImporter, esClient, logger, installedPkg,