forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Response Ops][Maintenance Window] Fix Maintenance Window Wildcard Sc…
…oped Queries (elastic#194777) ## Summary Issue: elastic/sdh-kibana#4923 Fixes maintenance window scoped query using wildcards by injecting the `analyze_wildcard` property to the DSL used to determine which alerts should be associated with the maintenance window. Also fixes the update route to correctly take into account the user's `allowLeadingWildcard` flag. It was implemented for the create route but not the update route. Fixes: elastic#194763 ### To test: 1. Install sample data: ![image](https://github.com/user-attachments/assets/4be72fc8-e4ab-47a3-b5db-48f97b1827ae) 2. Create a maintenance window with the following scoped query: ![image](https://github.com/user-attachments/assets/e2d37fd0-b957-4e76-bea3-8d954651c557) 3. Create a ES query rule and trigger actions: ![image](https://github.com/user-attachments/assets/551f5145-9ab7-48c4-a48e-e674b4f0509a) 4. Assert the `maintenance_window_id` on the 4 alerts are set ![image](https://github.com/user-attachments/assets/7ace95d3-d992-4305-a564-cf3004c9ae9e) ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios) --------- Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
95ed9ad
commit 7ad937d
Showing
6 changed files
with
306 additions
and
7 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
169 changes: 169 additions & 0 deletions
169
x-pack/plugins/alerting/server/alerts_client/lib/inject_analyze_wildcard.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,169 @@ | ||
/* | ||
* 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 { injectAnalyzeWildcard } from './inject_analyze_wildcard'; | ||
|
||
const getQuery = (query?: string) => { | ||
return { | ||
bool: { | ||
must: [], | ||
filter: [ | ||
{ | ||
bool: { | ||
filter: [ | ||
{ | ||
bool: { | ||
should: [ | ||
{ | ||
query_string: { | ||
fields: ['kibana.alert.instance.id'], | ||
query: query || '*elastic*', | ||
}, | ||
}, | ||
], | ||
minimum_should_match: 1, | ||
}, | ||
}, | ||
{ | ||
bool: { | ||
should: [ | ||
{ | ||
match: { | ||
'kibana.alert.action_group': 'test', | ||
}, | ||
}, | ||
], | ||
minimum_should_match: 1, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
should: [], | ||
must_not: [ | ||
{ | ||
match_phrase: { | ||
_id: 'assdasdasd', | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
}; | ||
describe('injectAnalyzeWildcard', () => { | ||
test('should inject analyze_wildcard field', () => { | ||
const query = getQuery(); | ||
injectAnalyzeWildcard(query); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"filter": Array [ | ||
Object { | ||
"bool": Object { | ||
"filter": Array [ | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"query_string": Object { | ||
"analyze_wildcard": true, | ||
"fields": Array [ | ||
"kibana.alert.instance.id", | ||
], | ||
"query": "*elastic*", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"kibana.alert.action_group": "test", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
"must": Array [], | ||
"must_not": Array [ | ||
Object { | ||
"match_phrase": Object { | ||
"_id": "assdasdasd", | ||
}, | ||
}, | ||
], | ||
"should": Array [], | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('should not inject analyze_wildcard if the query does not contain *', () => { | ||
const query = getQuery('test'); | ||
injectAnalyzeWildcard(query); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"filter": Array [ | ||
Object { | ||
"bool": Object { | ||
"filter": Array [ | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"query_string": Object { | ||
"fields": Array [ | ||
"kibana.alert.instance.id", | ||
], | ||
"query": "test", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"kibana.alert.action_group": "test", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
"must": Array [], | ||
"must_not": Array [ | ||
Object { | ||
"match_phrase": Object { | ||
"_id": "assdasdasd", | ||
}, | ||
}, | ||
], | ||
"should": Array [], | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
x-pack/plugins/alerting/server/alerts_client/lib/inject_analyze_wildcard.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,30 @@ | ||
/* | ||
* 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 { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
|
||
export const injectAnalyzeWildcard = (query: QueryDslQueryContainer): void => { | ||
if (!query) { | ||
return; | ||
} | ||
|
||
if (Array.isArray(query)) { | ||
return query.forEach((child) => injectAnalyzeWildcard(child)); | ||
} | ||
|
||
if (typeof query === 'object') { | ||
Object.entries(query).forEach(([key, value]) => { | ||
if (key !== 'query_string') { | ||
return injectAnalyzeWildcard(value); | ||
} | ||
|
||
if (typeof value.query === 'string' && value.query.includes('*')) { | ||
value.analyze_wildcard = true; | ||
} | ||
}); | ||
} | ||
}; |
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