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] Support bulk creating cases though the cases client #170326

Merged
merged 15 commits into from
Nov 7, 2023

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Nov 1, 2023

Summary

This PR extends the cases client to support bulk-creating cases. It is needed by the case action.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@cnasikas cnasikas added Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.12.0 labels Nov 1, 2023
@cnasikas cnasikas self-assigned this Nov 1, 2023
@cnasikas cnasikas mentioned this pull request Nov 1, 2023
3 tasks
@cnasikas cnasikas added the release_note:skip Skip the PR/issue when compiling release notes label Nov 1, 2023
@@ -22,19 +22,6 @@ import {
} from '../../../common/constants';
import { CASE_REF_NAME, EXTERNAL_REFERENCE_REF_NAME } from '../../common/constants';

export const createErrorSO = () =>
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to the upper level.


export const CaseCreateRequestWithOptionalId = rt.intersection([
CasePostRequestRt,
rt.exact(rt.partial({ id: rt.string })),
Copy link
Member Author

Choose a reason for hiding this comment

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

The case action needs to provide the ids of the cases.

payload,
connectorId,
attachmentId,
userAction,
Copy link
Member Author

Choose a reason for hiding this comment

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

Small refactor to separate the data from the options.

Copy link
Contributor

Choose a reason for hiding this comment

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

This was a bit confusing in the PR 😅 Does it actually affect the changes you will need for the subactions?


router.post(
{
path: '/api/cases_fixture/cases:bulkCreate',
Copy link
Member Author

Choose a reason for hiding this comment

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

Testing route to test the bulk create cases method.

@cnasikas cnasikas marked this pull request as ready for review November 6, 2023 09:16
@cnasikas cnasikas requested a review from a team as a code owner November 6, 2023 09:16
@elasticmachine
Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

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

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.

lgtm, left a few comments 👍

x-pack/plugins/cases/server/client/cases/bulk_create.ts Outdated Show resolved Hide resolved
saved_objects: [caseSO],
});

it('sets an ID if not provided', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I find this test name a bit confusing. Is this what we wanna test under authorization? Or rather that ensureAuth is called?

Copy link
Member Author

Choose a reason for hiding this comment

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

With this test we want to test this line id: theCase.id ?? SavedObjectsUtils.generateId(),. Any suggestions for the name?

Copy link
Contributor

Choose a reason for hiding this comment

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

With this test we want to test this line id: theCase.id ?? SavedObjectsUtils.generateId(),.

But that is basically 'sets an ID if not provided' on line 199.

I was asking because this is inside describe('authorization', () => { so I was thinking something like ensureAuthorized is called with the right params.

But not a big deal, ill leave it up to you

Copy link
Member Author

@cnasikas cnasikas Nov 6, 2023

Choose a reason for hiding this comment

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

sets a custom ID correctly tests the theCase.id and sets an ID if not provided test the SavedObjectsUtils.generateId() part. I will rename it to something like it generates an ID if not provided in the request. Does it make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I just noticed what is going on. Sorry my bad. Inside describe('authorization' the title is wrong. Inside describe('execution' the titles are correct. In describe('authorization' I am testing a different think. I am going to fix the title.

}

for (const theCase of casesSOs) {
const userActionPayload: CasePostRequest = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this now be CaseCreateRequestWithOptionalId?

Copy link
Member Author

Choose a reason for hiding this comment

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

The user action does not accept the ID of the case in the payload. This is why I used the CasePostRequest.

payload,
connectorId,
attachmentId,
userAction,
Copy link
Contributor

Choose a reason for hiding this comment

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

This was a bit confusing in the PR 😅 Does it actually affect the changes you will need for the subactions?

}
} catch (error) {
this.context.log.error(`Error on creating user action of type: ${type}. Error: ${error}`);
throw error;
}
}

public async bulkCreateUserAction<T extends keyof BuilderParameters>({
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 add some tests for this function? (x-pack/plugins/cases/server/services/user_actions/operations/create.test.ts)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I totally missed that.

*
* The test route is configured here x-pack/test/cases_api_integration/common/plugins/cases/server/routes.ts
*/
describe('bulk_create_cases', () => {
Copy link
Contributor

@adcoelho adcoelho Nov 6, 2023

Choose a reason for hiding this comment

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

Maybe we could include a test for the assignees/platinum license logic. I also didn't see any(for existing similar logic) in x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/post_case.ts.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought the unit test would be sufficient. Do you prefer an integration test?

@cnasikas
Copy link
Member Author

cnasikas commented Nov 6, 2023

This was a bit confusing in the PR 😅 Does it actually affect the changes you will need for the subactions?

No, but I could not stand it anymore hahahaha. Sorry for the confusion.

]);

export const BulkCreateCasesRequestRt = rt.strict({
cases: rt.array(CaseCreateRequestWithOptionalId),
Copy link
Contributor

Choose a reason for hiding this comment

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

I really like the API contract as by default I would have thought about just an array of cases.

};
});

const bulkCreateResponse =
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm missing a test to check the right behaviour when the user wants to create two cases but the second one fails

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a test in x-pack/plugins/cases/server/services/cases/index.test.ts called return cases with the SO errors correctly that tests this behavior. Do you have something else in mind?

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry, missed it, that was it

x-pack/plugins/cases/server/client/cases/bulk_create.ts Outdated Show resolved Hide resolved
@cnasikas cnasikas enabled auto-merge (squash) November 7, 2023 17:15
@cnasikas
Copy link
Member Author

cnasikas commented Nov 7, 2023

@elasticmachine merge upstream

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
cases 155.1KB 155.2KB +118.0B

History

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

cc @cnasikas

@cnasikas cnasikas merged commit 30e6677 into elastic:main Nov 7, 2023
@cnasikas cnasikas deleted the ca_bulk_create branch November 7, 2023 18:32
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Nov 7, 2023
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
backport:skip This commit does not require backporting 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.

6 participants