Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cases] Case action: Time window #171754

Merged
merged 56 commits into from
Dec 8, 2023
Merged

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Nov 22, 2023

Summary

This implements the time window logic. Specifically, if a case is older than the defined time window (default is 7d) then the case action will create a new case. This is achieved by increasing the counter in the oracle record responsible for the specific grouping.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@cnasikas cnasikas added v8.12.0 and removed skip-ci labels Nov 29, 2023
@cnasikas cnasikas marked this pull request as ready for review November 29, 2023 12:58
@cnasikas cnasikas requested a review from a team as a code owner November 29, 2023 12:58
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

@cnasikas cnasikas mentioned this pull request Nov 29, 2023
3 tasks
@@ -5,6 +5,8 @@
* 2.0.
*/

import dateMath from '@kbn/datemath';
import moment from 'moment';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we already use moment in cases? Is there no alternative? It's a deprecated 72.1kb (gzipped) library :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Good point. Unfortunately, the @kbn/datemath library which is developed and maintained by Kibana uses moment behind the scenes. I need @kbn/datemath to allow users to set the time window as 7d etc. This is a test file. I wanted to mock the datemath to make the test predicable. Do you have in mind any alternatives?

*
* Example: 20d, 2w, 1M, etc
*/
const timeWindowRegex = new RegExp(/[1-9][0-9]*[d,w,M,y]/, 'g');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you add the start and end of string anchors? ^[1-9][0-9]*[d,w,M,y]$

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for explaining the issue offline. Good question! I tested and I did not see any issues. I will put more tests in my next PR to cover more scenarios.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We talked about it again offline. I was wrong. @adcoelho found an issue that the user could bypass the Regex. Thank you so much! I fixed it.

const bulkCreateReq: BulkCreateOracleRecordRequest = [];

const ids = groupedAlertsWithOracleKey.map(({ oracleKey }) => oracleKey);

const bulkGetRes = await this.casesOracleService.bulkGetRecords(ids);
const [bulkGetValidRecords, bulkGetRecordsErrors] = partitionRecords(bulkGetRes);
const [bulkGetValidRecords, bulkGetRecordsErrors] = partitionRecordsByError(bulkGetRes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remind me in which scenarios records can end up with errors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly 404 errors or 5xx transient errors.

groupedAlertsWithOracleKey: GroupedAlertsWithOracleKey[]
): Promise<OracleRecord[]> {
const { timeWindow } = params;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: why not spread above in the params declaration?

  private async upsertOracleRecords(
    { timeWindow }: CasesConnectorRunParams,
    groupedAlertsWithOracleKey: GroupedAlertsWithOracleKey[]
  ): ...

Copy link
Contributor

@adcoelho adcoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good 👍

@cnasikas cnasikas requested a review from a team as a code owner December 8, 2023 10:36
@cnasikas cnasikas merged commit 7a2c5d9 into elastic:case_action Dec 8, 2023
18 of 37 checks passed
@cnasikas cnasikas deleted the ca_time_window branch December 8, 2023 11:09
@kibana-ci
Copy link
Collaborator

kibana-ci commented Dec 8, 2023

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #81 / discover/group2 discover data grid field tokens should render field tokens correctly for ES|QL
  • [job] [logs] FTR Configs #81 / discover/group2 discover data grid field tokens should render field tokens correctly for ES|QL
  • [job] [logs] FTR Configs #48 / Serverless Common UI - Console console app with folded/unfolded lines in request body doesn't fail if a fold fails

Metrics [docs]

‼️ ERROR: no builds found for mergeBase sha [76fd124]

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @cnasikas

cnasikas added a commit that referenced this pull request Dec 20, 2023
## Summary

Depends on: #171754

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <[email protected]>
cnasikas added a commit that referenced this pull request Apr 12, 2024
## Summary

Depends on: #166267,
#170326,
#169484,
#173740,
#173763,
#178068,
#178307,
#178600,
#180437

PRs:
- #168370
- #169229
- #171754
- #172709
- #173012
- #175107
- #175452
- #175505
- #177033
- #178277
- #177139
- #179796

Fixes: #153837

## Testing

Run Kibana with `--run-examples` if you want to use the "Always firing"
rule.

Create a rule with a case action in observability and the stack. The
security solution is not supported. You should not be able to assign a
case action in a security solution rule.

1. Test the "Reopen closed cases" configuration.
2. Test the "Grouping by" configuration. Only one field is allowed. Not
all fields are persisted in alerts. If you select a field not part of
the alert the case action will create a case where the grouping value is
set to `unknow`.
3. Test the "Time window" feature. You can comment out the validation to
test for shorter times.
4. Verify that the case action is experimental.
5. Verify that based on the rule type the case is created in the correct
solution.
6. Verify that you cannot create a rule with the case action on the
basic license.
7. Verify that the execution of the case action fails if you do not have
permission for cases. Pending work on the system actions framework level
to not allow users to create rules with system actions where they do not
have permission.
8. Stress test the case action by creating multiple rules.

### Checklist

Delete any items that are not applicable to this PR.

- [x]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [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

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

## Release notes

Automatically create cases when an alert is triggered.

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: adcoelho <[email protected]>
Co-authored-by: Janki Salvi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Cases Cases feature release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants