-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#169957) # Backport This will backport the following commits from `main` to `8.11`: - [[Security Solution][DE] Migrate investigation_fields (#169061)](#169061) <!--- Backport version: 8.9.8 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Yara Tercero","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-10-26T13:58:35Z","message":"[Security Solution][DE] Migrate investigation_fields (#169061)\n\n## Summary\r\n\r\n**TLDR:** SO will support both `string[]` and `{ field_names: string[]\r\n}`, but detection engine APIs will only support the object format in\r\n8.11+.","sha":"bb3673f2eb24013b11c736986928c3b73370f6bf","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:breaking","Team: SecuritySolution","Breaking Change","Team:Detection Engine","v8.11.0","v8.12.0","v8.11.1"],"number":169061,"url":"https://github.com/elastic/kibana/pull/169061","mergeCommit":{"message":"[Security Solution][DE] Migrate investigation_fields (#169061)\n\n## Summary\r\n\r\n**TLDR:** SO will support both `string[]` and `{ field_names: string[]\r\n}`, but detection engine APIs will only support the object format in\r\n8.11+.","sha":"bb3673f2eb24013b11c736986928c3b73370f6bf"}},"sourceBranch":"main","suggestedTargetBranches":["8.11"],"targetPullRequestStates":[{"branch":"8.11","label":"v8.11.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/169061","number":169061,"mergeCommit":{"message":"[Security Solution][DE] Migrate investigation_fields (#169061)\n\n## Summary\r\n\r\n**TLDR:** SO will support both `string[]` and `{ field_names: string[]\r\n}`, but detection engine APIs will only support the object format in\r\n8.11+.","sha":"bb3673f2eb24013b11c736986928c3b73370f6bf"}}]}] BACKPORT--> --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
9829476
commit b3385d5
Showing
31 changed files
with
2,352 additions
and
462 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
...ity_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.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,122 @@ | ||
/* | ||
* 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 type { InvestigationFields } from '../../../../common/api/detection_engine'; | ||
import type { Rule } from './types'; | ||
import { transformRuleFromAlertHit } from './use_rule_with_fallback'; | ||
|
||
export const getMockAlertSearchResponse = (rule: Rule) => ({ | ||
took: 1, | ||
timeout: false, | ||
_shards: { | ||
total: 1, | ||
successful: 1, | ||
skipped: 0, | ||
failed: 0, | ||
}, | ||
hits: { | ||
total: { | ||
value: 75, | ||
relation: 'eq', | ||
}, | ||
max_score: null, | ||
hits: [ | ||
{ | ||
_id: '1234', | ||
_index: '.kibana', | ||
_source: { | ||
'@timestamp': '12334232132', | ||
kibana: { | ||
alert: { | ||
rule, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
describe('use_rule_with_fallback', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
describe('transformRuleFromAlertHit', () => { | ||
// Testing edge case, where if hook does not find the rule and turns to the alert document, | ||
// the alert document could still have an unmigrated, legacy version of investigation_fields. | ||
// We are not looking to do any migrations to these legacy fields in the alert document, so need | ||
// to transform it on read in this case. | ||
describe('investigation_fields', () => { | ||
it('sets investigation_fields to undefined when set as legacy array', () => { | ||
const mockRule = getMockRule({ | ||
investigation_fields: ['foo'] as unknown as InvestigationFields, | ||
}); | ||
const mockHit = getMockAlertSearchResponse(mockRule); | ||
const result = transformRuleFromAlertHit(mockHit); | ||
expect(result?.investigation_fields).toBeUndefined(); | ||
}); | ||
|
||
it('sets investigation_fields to undefined when set as legacy empty array', () => { | ||
// Ideally, we would have the client side types pull from the same types | ||
// as server side so we could denote here that the SO can have investigation_fields | ||
// as array or object, but our APIs now only support object. We don't have that here | ||
// and would need to adjust the client side type to support both, which we do not want | ||
// to do in this instance as we try to migrate folks away from the array version. | ||
const mockRule = getMockRule({ | ||
investigation_fields: [] as unknown as InvestigationFields, | ||
}); | ||
const mockHit = getMockAlertSearchResponse(mockRule); | ||
const result = transformRuleFromAlertHit(mockHit); | ||
expect(result?.investigation_fields).toBeUndefined(); | ||
}); | ||
|
||
it('does no transformation when "investigation_fields" is intended type', () => { | ||
const mockRule = getMockRule({ investigation_fields: { field_names: ['bar'] } }); | ||
const mockHit = getMockAlertSearchResponse(mockRule); | ||
const result = transformRuleFromAlertHit(mockHit); | ||
expect(result?.investigation_fields).toEqual({ field_names: ['bar'] }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
const getMockRule = (overwrites: Partial<Rule>): Rule => ({ | ||
id: 'myfakeruleid', | ||
author: [], | ||
severity_mapping: [], | ||
risk_score_mapping: [], | ||
rule_id: 'rule-1', | ||
risk_score: 50, | ||
description: 'some description', | ||
from: 'now-5m', | ||
to: 'now', | ||
name: 'some-name', | ||
severity: 'low', | ||
type: 'query', | ||
query: 'some query', | ||
index: ['index-1'], | ||
interval: '5m', | ||
references: [], | ||
actions: [], | ||
enabled: false, | ||
false_positives: [], | ||
max_signals: 100, | ||
tags: [], | ||
threat: [], | ||
throttle: null, | ||
version: 1, | ||
exceptions_list: [], | ||
created_at: '2020-04-09T09:43:51.778Z', | ||
created_by: 'elastic', | ||
immutable: false, | ||
updated_at: '2020-04-09T09:43:51.778Z', | ||
updated_by: 'elastic', | ||
related_integrations: [], | ||
required_fields: [], | ||
setup: '', | ||
...overwrites, | ||
}); |
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
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
Oops, something went wrong.