From 92b521b0b0edb8a0ded51f05f0880d42d78cc259 Mon Sep 17 00:00:00 2001 From: David Sanchez Soler Date: Tue, 27 Jun 2023 17:00:35 +0200 Subject: [PATCH] Adds elasticsearch client to manifest manager context and uses it for bulkUpdate --- .../artifacts/manifest_manager/manifest_manager.mock.ts | 7 ++++++- .../artifacts/manifest_manager/manifest_manager.test.ts | 4 ++-- .../artifacts/manifest_manager/manifest_manager.ts | 7 +++++-- x-pack/plugins/security_solution/server/plugin.ts | 1 + 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.mock.ts b/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.mock.ts index cd414f0c3cb9..cb947b5221db 100644 --- a/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.mock.ts +++ b/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.mock.ts @@ -5,7 +5,11 @@ * 2.0. */ -import { savedObjectsClientMock, loggingSystemMock } from '@kbn/core/server/mocks'; +import { + savedObjectsClientMock, + loggingSystemMock, + elasticsearchServiceMock, +} from '@kbn/core/server/mocks'; import type { Logger } from '@kbn/core/server'; import type { PackagePolicyClient } from '@kbn/fleet-plugin/server'; import { createPackagePolicyServiceMock } from '@kbn/fleet-plugin/server/mocks'; @@ -89,6 +93,7 @@ export const buildManifestManagerContextMock = ( logger: loggingSystemMock.create().get() as jest.Mocked, experimentalFeatures: parseExperimentalConfigValue([]).features, packagerTaskPackagePolicyUpdateBatchSize: 10, + esClient: elasticsearchServiceMock.createElasticsearchClient(), }; }; diff --git a/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.test.ts b/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.test.ts index 380bdf478b00..f6859015f42f 100644 --- a/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.test.ts +++ b/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.test.ts @@ -919,7 +919,7 @@ describe('ManifestManager', () => { expect(context.packagePolicyService.bulkUpdate).toHaveBeenNthCalledWith( 1, expect.anything(), - undefined, + context.esClient, [ createPackagePolicyWithConfigMock({ id: TEST_POLICY_ID_1, @@ -990,7 +990,7 @@ describe('ManifestManager', () => { expect(context.packagePolicyService.bulkUpdate).toHaveBeenNthCalledWith( 1, expect.anything(), - undefined, + context.esClient, [ createPackagePolicyWithConfigMock({ id: TEST_POLICY_ID_1, diff --git a/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.ts b/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.ts index 8e43f8fa6154..f2953cb53d8f 100644 --- a/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.ts +++ b/x-pack/plugins/security_solution/server/endpoint/services/artifacts/manifest_manager/manifest_manager.ts @@ -8,6 +8,7 @@ import pMap from 'p-map'; import semver from 'semver'; import { isEqual, isEmpty, chunk } from 'lodash'; +import type { ElasticsearchClient } from '@kbn/core/server'; import { type Logger, type SavedObjectsClientContract } from '@kbn/core/server'; import { ENDPOINT_EVENT_FILTERS_LIST_ID, @@ -92,6 +93,7 @@ export interface ManifestManagerContext { logger: Logger; experimentalFeatures: ExperimentalFeatures; packagerTaskPackagePolicyUpdateBatchSize: number; + esClient: ElasticsearchClient; } const getArtifactIds = (manifest: ManifestSchema) => @@ -112,6 +114,7 @@ export class ManifestManager { protected experimentalFeatures: ExperimentalFeatures; protected cachedExceptionsListsByOs: Map; protected packagerTaskPackagePolicyUpdateBatchSize: number; + protected esClient: ElasticsearchClient; constructor(context: ManifestManagerContext) { this.artifactClient = context.artifactClient; @@ -124,6 +127,7 @@ export class ManifestManager { this.cachedExceptionsListsByOs = new Map(); this.packagerTaskPackagePolicyUpdateBatchSize = context.packagerTaskPackagePolicyUpdateBatchSize; + this.esClient = context.esClient; } /** @@ -606,8 +610,7 @@ export class ManifestManager { for (const currentBatch of updateBatches) { const response = await this.packagePolicyService.bulkUpdate( this.savedObjectsClient, - // @ts-expect-error TS2345 - undefined, + this.esClient, currentBatch ); diff --git a/x-pack/plugins/security_solution/server/plugin.ts b/x-pack/plugins/security_solution/server/plugin.ts index bbc325282ee9..badc35993ab0 100644 --- a/x-pack/plugins/security_solution/server/plugin.ts +++ b/x-pack/plugins/security_solution/server/plugin.ts @@ -460,6 +460,7 @@ export class Plugin implements ISecuritySolutionPlugin { logger, experimentalFeatures: config.experimentalFeatures, packagerTaskPackagePolicyUpdateBatchSize: config.packagerTaskPackagePolicyUpdateBatchSize, + esClient: core.elasticsearch.client.asInternalUser, }); // Migrate artifacts to fleet and then start the minifest task after that is done