-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SIEM] Fixes Modification of ML Rules (#60662)
* Fix updating of ML rules * Add a regression test for updating ML Rules * Allow ML Rules to be patched And adds a regression unit test. * Allow ML rule params to be imported when overwriting * Add a basic regression test for creating a rule with ML params * Prevent users from changing an existing Rule's type
- Loading branch information
Showing
9 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
x-pack/legacy/plugins/siem/server/lib/detection_engine/rules/create_rules.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { alertsClientMock } from '../../../../../../../plugins/alerting/server/mocks'; | ||
import { actionsClientMock } from '../../../../../../../plugins/actions/server/mocks'; | ||
import { getMlResult } from '../routes/__mocks__/request_responses'; | ||
import { createRules } from './create_rules'; | ||
|
||
describe('createRules', () => { | ||
let actionsClient: ReturnType<typeof actionsClientMock.create>; | ||
let alertsClient: ReturnType<typeof alertsClientMock.create>; | ||
|
||
beforeEach(() => { | ||
actionsClient = actionsClientMock.create(); | ||
alertsClient = alertsClientMock.create(); | ||
}); | ||
|
||
it('calls the alertsClient with ML params', async () => { | ||
const params = { | ||
...getMlResult().params, | ||
anomalyThreshold: 55, | ||
machineLearningJobId: 'new_job_id', | ||
}; | ||
|
||
await createRules({ | ||
alertsClient, | ||
actionsClient, | ||
...params, | ||
ruleId: 'new-rule-id', | ||
enabled: true, | ||
interval: '', | ||
name: '', | ||
tags: [], | ||
}); | ||
|
||
expect(alertsClient.create).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
params: expect.objectContaining({ | ||
anomalyThreshold: 55, | ||
machineLearningJobId: 'new_job_id', | ||
}), | ||
}), | ||
}) | ||
); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
x-pack/legacy/plugins/siem/server/lib/detection_engine/rules/patch_rules.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { savedObjectsClientMock } from '../../../../../../../../src/core/server/mocks'; | ||
import { alertsClientMock } from '../../../../../../../plugins/alerting/server/mocks'; | ||
import { actionsClientMock } from '../../../../../../../plugins/actions/server/mocks'; | ||
import { getMlResult } from '../routes/__mocks__/request_responses'; | ||
import { patchRules } from './patch_rules'; | ||
|
||
describe('patchRules', () => { | ||
let actionsClient: ReturnType<typeof actionsClientMock.create>; | ||
let alertsClient: ReturnType<typeof alertsClientMock.create>; | ||
let savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>; | ||
|
||
beforeEach(() => { | ||
actionsClient = actionsClientMock.create(); | ||
alertsClient = alertsClientMock.create(); | ||
savedObjectsClient = savedObjectsClientMock.create(); | ||
}); | ||
|
||
it('calls the alertsClient with ML params', async () => { | ||
alertsClient.get.mockResolvedValue(getMlResult()); | ||
const params = { | ||
...getMlResult().params, | ||
anomalyThreshold: 55, | ||
machineLearningJobId: 'new_job_id', | ||
}; | ||
|
||
await patchRules({ | ||
alertsClient, | ||
actionsClient, | ||
savedObjectsClient, | ||
id: '04128c15-0d1b-4716-a4c5-46997ac7f3bd', | ||
...params, | ||
}); | ||
|
||
expect(alertsClient.update).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
params: expect.objectContaining({ | ||
anomalyThreshold: 55, | ||
machineLearningJobId: 'new_job_id', | ||
}), | ||
}), | ||
}) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
x-pack/legacy/plugins/siem/server/lib/detection_engine/rules/update_rules.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { savedObjectsClientMock } from '../../../../../../../../src/core/server/mocks'; | ||
import { alertsClientMock } from '../../../../../../../plugins/alerting/server/mocks'; | ||
import { actionsClientMock } from '../../../../../../../plugins/actions/server/mocks'; | ||
import { getMlResult } from '../routes/__mocks__/request_responses'; | ||
import { updateRules } from './update_rules'; | ||
|
||
describe('updateRules', () => { | ||
let actionsClient: ReturnType<typeof actionsClientMock.create>; | ||
let alertsClient: ReturnType<typeof alertsClientMock.create>; | ||
let savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>; | ||
|
||
beforeEach(() => { | ||
actionsClient = actionsClientMock.create(); | ||
alertsClient = alertsClientMock.create(); | ||
savedObjectsClient = savedObjectsClientMock.create(); | ||
}); | ||
|
||
it('calls the alertsClient with ML params', async () => { | ||
alertsClient.get.mockResolvedValue(getMlResult()); | ||
|
||
const params = { | ||
...getMlResult().params, | ||
anomalyThreshold: 55, | ||
machineLearningJobId: 'new_job_id', | ||
}; | ||
|
||
await updateRules({ | ||
alertsClient, | ||
actionsClient, | ||
savedObjectsClient, | ||
id: '04128c15-0d1b-4716-a4c5-46997ac7f3bd', | ||
...params, | ||
enabled: true, | ||
interval: '', | ||
name: '', | ||
tags: [], | ||
}); | ||
|
||
expect(alertsClient.update).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
params: expect.objectContaining({ | ||
anomalyThreshold: 55, | ||
machineLearningJobId: 'new_job_id', | ||
}), | ||
}), | ||
}) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters