diff --git a/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.test.ts b/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.test.ts index 62dc3cac54034..9a723a5bc0e8b 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.test.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.test.ts @@ -632,7 +632,7 @@ describe('RiskEngineDataClient', () => { mockTaskManagerStart = taskManagerMock.createStart(); }); - it('should return error if saved object not exist', async () => { + it('returns an error if saved object does not exist', async () => { mockSavedObjectClient.find.mockResolvedValue({ page: 1, per_page: 20, @@ -642,10 +642,10 @@ describe('RiskEngineDataClient', () => { await expect( riskEngineDataClient.enableRiskEngine({ taskManager: mockTaskManagerStart }) - ).rejects.toThrow('There no saved object configuration for risk engine'); + ).rejects.toThrow('Risk engine configuration not found'); }); - it('should update saved object attrubute', async () => { + it('should update saved object attribute', async () => { await riskEngineDataClient.enableRiskEngine({ taskManager: mockTaskManagerStart }); expect(mockSavedObjectClient.update).toHaveBeenCalledWith( @@ -659,6 +659,29 @@ describe('RiskEngineDataClient', () => { } ); }); + + describe('if task manager throws an error', () => { + beforeEach(() => { + mockTaskManagerStart.ensureScheduled.mockRejectedValueOnce(new Error('Task Manager error')); + }); + + it('disables the risk engine and re-throws the error', async () => { + await expect( + riskEngineDataClient.enableRiskEngine({ taskManager: mockTaskManagerStart }) + ).rejects.toThrow('Task Manager error'); + + expect(mockSavedObjectClient.update).toHaveBeenCalledWith( + 'risk-engine-configuration', + 'de8ca330-2d26-11ee-bc86-f95bf6192ee6', + { + enabled: false, + }, + { + refresh: 'wait_for', + } + ); + }); + }); }); describe('disableRiskEngine', () => { @@ -680,7 +703,7 @@ describe('RiskEngineDataClient', () => { try { await riskEngineDataClient.disableRiskEngine({ taskManager: mockTaskManagerStart }); } catch (e) { - expect(e.message).toEqual('There no saved object configuration for risk engine'); + expect(e.message).toEqual('Risk engine configuration not found'); } }); diff --git a/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.ts b/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.ts index 0b359046b9df6..69380d43fac37 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_engine/risk_engine_data_client.ts @@ -160,21 +160,29 @@ export class RiskEngineDataClient { } public async enableRiskEngine({ taskManager }: { taskManager: TaskManagerStartContract }) { - const configurationResult = await updateSavedObjectAttribute({ - savedObjectsClient: this.options.soClient, - attributes: { - enabled: true, - }, - }); + try { + const configurationResult = await updateSavedObjectAttribute({ + savedObjectsClient: this.options.soClient, + attributes: { + enabled: true, + }, + }); - await startRiskScoringTask({ - logger: this.options.logger, - namespace: this.options.namespace, - riskEngineDataClient: this, - taskManager, - }); + await startRiskScoringTask({ + logger: this.options.logger, + namespace: this.options.namespace, + riskEngineDataClient: this, + taskManager, + }); - return configurationResult; + return configurationResult; + } catch (e) { + this.options.logger.error(`Error while enabling risk engine: ${e.message}`); + + await this.disableRiskEngine({ taskManager }); + + throw e; + } } public async disableRiskEngine({ taskManager }: { taskManager: TaskManagerStartContract }) { @@ -184,7 +192,7 @@ export class RiskEngineDataClient { logger: this.options.logger, }); - return updateSavedObjectAttribute({ + return await updateSavedObjectAttribute({ savedObjectsClient: this.options.soClient, attributes: { enabled: false, diff --git a/x-pack/plugins/security_solution/server/lib/risk_engine/utils/saved_object_configuration.ts b/x-pack/plugins/security_solution/server/lib/risk_engine/utils/saved_object_configuration.ts index ec5cb7f75ed29..415e9f84777e3 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_engine/utils/saved_object_configuration.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_engine/utils/saved_object_configuration.ts @@ -62,7 +62,7 @@ export const updateSavedObjectAttribute = async ({ }); if (!savedObjectConfiguration) { - throw new Error('There no saved object configuration for risk engine'); + throw new Error('Risk engine configuration not found'); } const result = await savedObjectsClient.update(