-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Conversation
@@ -22,19 +22,6 @@ import { | |||
} from '../../../common/constants'; | |||
import { CASE_REF_NAME, EXTERNAL_REFERENCE_REF_NAME } from '../../common/constants'; | |||
|
|||
export const createErrorSO = () => |
There was a problem hiding this comment.
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 })), |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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.
Pinging @elastic/response-ops (Team:ResponseOps) |
Pinging @elastic/response-ops-cases (Feature:Cases) |
There was a problem hiding this 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 👍
saved_objects: [caseSO], | ||
}); | ||
|
||
it('sets an ID if not provided', async () => { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
x-pack/plugins/cases/server/services/user_actions/operations/create.ts
Outdated
Show resolved
Hide resolved
} | ||
} 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>({ |
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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?
No, but I could not stand it anymore hahahaha. Sorry for the confusion. |
]); | ||
|
||
export const BulkCreateCasesRequestRt = rt.strict({ | ||
cases: rt.array(CaseCreateRequestWithOptionalId), |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@elasticmachine merge upstream |
💛 Build succeeded, but was flaky
Failed CI StepsMetrics [docs]Page load bundle
History
To update your PR or re-run it, just comment with: cc @cnasikas |
## 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]>
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