-
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] Delete Cases API Guardrails #160846
Changes from 1 commit
429dd71
cc5688a
6d34670
27cdcf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { MAX_FILES_PER_CASE } from '../../../common/constants'; | ||
import { MAX_DELETE_IDS_LENGTH, MAX_FILES_PER_CASE } from '../../../common/constants'; | ||
import type { FindFileArgs } from '@kbn/files-plugin/server'; | ||
import { createFileServiceMock } from '@kbn/files-plugin/server/mocks'; | ||
import type { FileJSON } from '@kbn/shared-ux-file-types'; | ||
|
@@ -95,6 +95,22 @@ describe('delete', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('errors', () => { | ||
it(`throws 400 when trying to delete more than ${MAX_DELETE_IDS_LENGTH} files at a time`, async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be testing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, do you mean the test name? I left There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for some reason I thought the |
||
const caseIds = new Array(MAX_DELETE_IDS_LENGTH + 1).fill('id'); | ||
|
||
await expect(deleteCases(caseIds, clientArgs)).rejects.toThrowError( | ||
'Error: The length of the field ids is too long. Array must be of length <= 100.' | ||
); | ||
}); | ||
|
||
it('throws 400 when no id is passed to delete', async () => { | ||
await expect(deleteCases([], clientArgs)).rejects.toThrowError( | ||
'Error: The length of the field ids is too short. Array must be of length >= 1.' | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ import pMap from 'p-map'; | |
import { chunk } from 'lodash'; | ||
import type { SavedObjectsBulkDeleteObject } from '@kbn/core/server'; | ||
import type { FileServiceStart } from '@kbn/files-plugin/server'; | ||
import type { CasesDeleteRequest } from '../../../common/api'; | ||
import { CasesDeleteRequestRt, decodeWithExcessOrThrow } from '../../../common/api'; | ||
import { | ||
CASE_COMMENT_SAVED_OBJECT, | ||
CASE_SAVED_OBJECT, | ||
|
@@ -26,7 +28,10 @@ import { createFileEntities, deleteFiles } from '../files'; | |
/** | ||
* Deletes the specified cases and their attachments. | ||
*/ | ||
export async function deleteCases(ids: string[], clientArgs: CasesClientArgs): Promise<void> { | ||
export async function deleteCases( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed we also define a params schema in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What do you mean with
? I am not familiar with this I guess I can just replace this with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discuss it offline and we decided that we are going to remove the schema defined in all cases routes and rely only on the validation inside the cases client. |
||
ids: CasesDeleteRequest, | ||
clientArgs: CasesClientArgs | ||
): Promise<void> { | ||
const { | ||
services: { caseService, attachmentService, userActionService, alertsService }, | ||
logger, | ||
|
@@ -35,7 +40,8 @@ export async function deleteCases(ids: string[], clientArgs: CasesClientArgs): P | |
} = clientArgs; | ||
|
||
try { | ||
const cases = await caseService.getCases({ caseIds: ids }); | ||
const caseIds = decodeWithExcessOrThrow(CasesDeleteRequestRt)(ids); | ||
const cases = await caseService.getCases({ caseIds }); | ||
const entities = new Map<string, OwnerEntity>(); | ||
|
||
for (const theCase of cases.saved_objects) { | ||
|
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.
super nit: I think we should move it to
Validation
section below.