-
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.
[SIEM][Detection Engine] Adds a tags service and optimizes alert_id l…
…ookups ## Summary * Adds a tags services for use by UI's that want to get a list of all the unique tags that are on all of the rules just like an aggregation * Removes the horribly inefficient `alert_id` look up that was a full alert scan and instead uses an internal structure that it augments to the tags for fast `alert_id` look ups. * Adds unit tests for the tags and internal structure tags * Updates other unit tests Usage for the UI: ```sh GET /api/detection_engine/tags ``` or shell script: ```sh ./get_tags.sh ``` Returns: ```sh [ "tag_1", "tag_2" ] ``` Testing: Ensure that the internal structure does not leak when doing any of these script/API calls * ./get_tags.sh * ./post_rule.sh ./rules/queries/query_with_tags.json * ./update_rule.sh ./rules/queries/query_with_tags.json * ./delete_rule.sh * ./find_rules.sh * ./find_rule_by_filter.sh "alert.attributes.enabled:%20true" * ./find_rule_by_filter.sh "alert.attributes.tags:tag_1" Caveat: You can do filter searches against tags that have the double underscore such as: ```sh ./find_rule_by_filter.sh "alert.attributes.tags:%20__*" ``` But that shouldn't be a big problem and more than likely no one will be naming something with double underscores. ### Checklist Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR. ~~- [ ] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)~~ ~~- [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~~ ~~- [ ] [Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~~ - [x] [Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios ~~- [ ] This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~~ ### For maintainers ~~- [ ] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~~ - [x] This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)
- Loading branch information
1 parent
27c4a8b
commit f02eb7b
Showing
19 changed files
with
727 additions
and
212 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
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
46 changes: 46 additions & 0 deletions
46
x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/tags/read_tags_route.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,46 @@ | ||
/* | ||
* 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 Hapi from 'hapi'; | ||
import { isFunction } from 'lodash/fp'; | ||
import { DETECTION_ENGINE_TAGS_URL } from '../../../../../common/constants'; | ||
import { ServerFacade, RequestFacade } from '../../../../types'; | ||
import { transformError } from '../utils'; | ||
import { readTags } from '../../tags/read_tags'; | ||
|
||
export const createReadTagsRoute: Hapi.ServerRoute = { | ||
method: 'GET', | ||
path: DETECTION_ENGINE_TAGS_URL, | ||
options: { | ||
tags: ['access:siem'], | ||
validate: { | ||
options: { | ||
abortEarly: false, | ||
}, | ||
}, | ||
}, | ||
async handler(request: RequestFacade, headers) { | ||
const alertsClient = isFunction(request.getAlertsClient) ? request.getAlertsClient() : null; | ||
const actionsClient = isFunction(request.getActionsClient) ? request.getActionsClient() : null; | ||
|
||
if (!alertsClient || !actionsClient) { | ||
return headers.response().code(404); | ||
} | ||
|
||
try { | ||
const tags = await readTags({ | ||
alertsClient, | ||
}); | ||
return tags; | ||
} catch (err) { | ||
return transformError(err); | ||
} | ||
}, | ||
}; | ||
|
||
export const readTagsRoute = (server: ServerFacade) => { | ||
server.route(createReadTagsRoute); | ||
}; |
35 changes: 35 additions & 0 deletions
35
x-pack/legacy/plugins/siem/server/lib/detection_engine/rules/add_rule_id_to_tags.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,35 @@ | ||
/* | ||
* 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 { addRuleIdToTags } from './add_rule_id_to_tags'; | ||
import { INTERNAL_RULE_ID_KEY } from '../../../../common/constants'; | ||
|
||
describe('add_rule_id_to_tags', () => { | ||
test('it should add a rule id as an internal structure to a single tag', () => { | ||
const tags = addRuleIdToTags(['tag 1'], 'rule-1'); | ||
expect(tags).toEqual(['tag 1', `${INTERNAL_RULE_ID_KEY}:rule-1`]); | ||
}); | ||
|
||
test('it should add a rule id as an internal structure to two tags', () => { | ||
const tags = addRuleIdToTags(['tag 1', 'tag 2'], 'rule-1'); | ||
expect(tags).toEqual(['tag 1', 'tag 2', `${INTERNAL_RULE_ID_KEY}:rule-1`]); | ||
}); | ||
|
||
test('it should add a rule id as an internal structure with empty tags', () => { | ||
const tags = addRuleIdToTags([], 'rule-1'); | ||
expect(tags).toEqual([`${INTERNAL_RULE_ID_KEY}:rule-1`]); | ||
}); | ||
|
||
test('it should add not add an internal structure if rule id is undefined', () => { | ||
const tags = addRuleIdToTags(['tag 1'], undefined); | ||
expect(tags).toEqual(['tag 1']); | ||
}); | ||
|
||
test('it should add not add an internal structure if rule id is null', () => { | ||
const tags = addRuleIdToTags(['tag 1'], null); | ||
expect(tags).toEqual(['tag 1']); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
x-pack/legacy/plugins/siem/server/lib/detection_engine/rules/add_rule_id_to_tags.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,15 @@ | ||
/* | ||
* 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 { INTERNAL_RULE_ID_KEY } from '../../../../common/constants'; | ||
|
||
export const addRuleIdToTags = (tags: string[], ruleId: string | null | undefined): string[] => { | ||
if (ruleId == null) { | ||
return tags; | ||
} else { | ||
return [...tags, `${INTERNAL_RULE_ID_KEY}:${ruleId}`]; | ||
} | ||
}; |
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.