-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cases delete files case deletion (#153979)
This PR extends the case deletion functionality to delete any files that are attached to the case when a case is deleted. To avoid attempting to delete too many files at once I chunk case ids into 50 at a time such that we're at most only deleting 5000 (50 case * 100 files per case) at once. That way we don't exceed the 10k find api limit. ## Testing Run the python script from here: https://github.com/elastic/cases-files-generator to generate some files for a case Deleting the case should remove all files
- Loading branch information
1 parent
c98fa82
commit b6a113c
Showing
12 changed files
with
568 additions
and
46 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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 { 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'; | ||
import type { CaseFileMetadataForDeletion } from '../../../common/files'; | ||
import { constructFileKindIdByOwner } from '../../../common/files'; | ||
import { getFileEntities } from './delete'; | ||
|
||
const getCaseIds = (numIds: number) => { | ||
return Array.from(Array(numIds).keys()).map((key) => key.toString()); | ||
}; | ||
describe('delete', () => { | ||
describe('getFileEntities', () => { | ||
const numCaseIds = 1000; | ||
const caseIds = getCaseIds(numCaseIds); | ||
const mockFileService = createFileServiceMock(); | ||
mockFileService.find.mockImplementation(async (args: FindFileArgs) => { | ||
const caseMeta = args.meta as unknown as CaseFileMetadataForDeletion; | ||
const numFilesToGen = caseMeta.caseIds.length * MAX_FILES_PER_CASE; | ||
const files = Array.from(Array(numFilesToGen).keys()).map(() => createMockFileJSON()); | ||
|
||
return { | ||
files, | ||
total: files.length, | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('only provides 50 case ids in a single call to the find api', async () => { | ||
await getFileEntities(caseIds, mockFileService); | ||
|
||
for (const call of mockFileService.find.mock.calls) { | ||
const callMeta = call[0].meta as unknown as CaseFileMetadataForDeletion; | ||
expect(callMeta.caseIds.length).toEqual(50); | ||
} | ||
}); | ||
|
||
it('calls the find function the number of case ids divided by the chunk size', async () => { | ||
await getFileEntities(caseIds, mockFileService); | ||
|
||
const chunkSize = 50; | ||
|
||
expect(mockFileService.find).toHaveBeenCalledTimes(numCaseIds / chunkSize); | ||
}); | ||
|
||
it('returns the number of entities equal to the case ids times the max files per case limit', async () => { | ||
const expectedEntities = Array.from(Array(numCaseIds * MAX_FILES_PER_CASE).keys()).map( | ||
() => ({ | ||
id: '123', | ||
owner: 'securitySolution', | ||
}) | ||
); | ||
|
||
const entities = await getFileEntities(caseIds, mockFileService); | ||
|
||
expect(entities.length).toEqual(numCaseIds * MAX_FILES_PER_CASE); | ||
expect(entities).toEqual(expectedEntities); | ||
}); | ||
}); | ||
}); | ||
|
||
const createMockFileJSON = (): FileJSON => { | ||
return { | ||
id: '123', | ||
fileKind: constructFileKindIdByOwner('securitySolution'), | ||
meta: { | ||
owner: ['securitySolution'], | ||
}, | ||
} as unknown as FileJSON; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 { createFileServiceMock } from '@kbn/files-plugin/server/mocks'; | ||
import { OBSERVABILITY_OWNER, SECURITY_SOLUTION_OWNER } from '../../../common'; | ||
import { constructFileKindIdByOwner } from '../../../common/files'; | ||
import { createFileEntities, deleteFiles } from '.'; | ||
|
||
describe('server files', () => { | ||
describe('createFileEntities', () => { | ||
it('returns an empty array when passed no files', () => { | ||
expect(createFileEntities([])).toEqual([]); | ||
}); | ||
|
||
it('throws an error when the file kind is not valid', () => { | ||
expect.assertions(1); | ||
|
||
expect(() => | ||
createFileEntities([{ fileKind: 'abc', id: '1' }]) | ||
).toThrowErrorMatchingInlineSnapshot(`"File id 1 has invalid file kind abc"`); | ||
}); | ||
|
||
it('throws an error when one of the file entities does not have a valid file kind', () => { | ||
expect.assertions(1); | ||
|
||
expect(() => | ||
createFileEntities([ | ||
{ fileKind: constructFileKindIdByOwner(SECURITY_SOLUTION_OWNER), id: '1' }, | ||
{ fileKind: 'abc', id: '2' }, | ||
]) | ||
).toThrowErrorMatchingInlineSnapshot(`"File id 2 has invalid file kind abc"`); | ||
}); | ||
|
||
it('returns an array of entities when the file kind is valid', () => { | ||
expect.assertions(1); | ||
|
||
expect( | ||
createFileEntities([ | ||
{ fileKind: constructFileKindIdByOwner(SECURITY_SOLUTION_OWNER), id: '1' }, | ||
{ fileKind: constructFileKindIdByOwner(OBSERVABILITY_OWNER), id: '2' }, | ||
]) | ||
).toEqual([ | ||
{ id: '1', owner: 'securitySolution' }, | ||
{ id: '2', owner: 'observability' }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('deleteFiles', () => { | ||
it('calls delete twice with the ids passed in', async () => { | ||
const fileServiceMock = createFileServiceMock(); | ||
|
||
expect.assertions(2); | ||
await deleteFiles(['1', '2'], fileServiceMock); | ||
|
||
expect(fileServiceMock.delete).toBeCalledTimes(2); | ||
expect(fileServiceMock.delete.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
Object { | ||
"id": "1", | ||
}, | ||
], | ||
Array [ | ||
Object { | ||
"id": "2", | ||
}, | ||
], | ||
] | ||
`); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.