-
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.
[Security Solution][Lists] - Fix exception list with comments import …
…bug (#124909) (#125808) ### Summary Addresses #124742 #### Issue TLDR Import of rules that reference exception items with comments fail. Failure message states that comments cannot include `created_at`, `created_by`, `id`. (cherry picked from commit f894d86) Co-authored-by: Yara Tercero <yctercero@users.noreply.github.com>
- Loading branch information
1 parent
fe9b14c
commit 91ee460
Showing
16 changed files
with
666 additions
and
17 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...-securitysolution-io-ts-list-types/src/common/default_import_comments_array/index.test.ts
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,87 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { left } from 'fp-ts/lib/Either'; | ||
import { foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils'; | ||
import { ImportCommentsArray } from '../import_comment'; | ||
import { DefaultImportCommentsArray } from '../default_import_comments_array'; | ||
import { getCommentsArrayMock } from '../comment/index.mock'; | ||
import { getCreateCommentsArrayMock } from '../create_comment/index.mock'; | ||
|
||
describe('default_import_comments_array', () => { | ||
test('it should pass validation when supplied an empty array', () => { | ||
const payload: ImportCommentsArray = []; | ||
const decoded = DefaultImportCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it should pass validation when supplied an array of comments', () => { | ||
const payload: ImportCommentsArray = getCommentsArrayMock(); | ||
const decoded = DefaultImportCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it should pass validation when supplied an array of new comments', () => { | ||
const payload: ImportCommentsArray = getCreateCommentsArrayMock(); | ||
const decoded = DefaultImportCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it should pass validation when supplied an array of new and existing comments', () => { | ||
const payload: ImportCommentsArray = [ | ||
...getCommentsArrayMock(), | ||
...getCreateCommentsArrayMock(), | ||
]; | ||
const decoded = DefaultImportCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it should fail validation when supplied an array of numbers', () => { | ||
const payload = [1]; | ||
const decoded = DefaultImportCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([ | ||
'Invalid value "1" supplied to "DefaultImportComments"', | ||
]); | ||
expect(message.schema).toEqual({}); | ||
}); | ||
|
||
test('it should fail validation when supplied an array of strings', () => { | ||
const payload = ['some string']; | ||
const decoded = DefaultImportCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([ | ||
'Invalid value "some string" supplied to "DefaultImportComments"', | ||
]); | ||
expect(message.schema).toEqual({}); | ||
}); | ||
|
||
test('it should return a default array entry', () => { | ||
const payload = null; | ||
const decoded = DefaultImportCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual([]); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
...s/kbn-securitysolution-io-ts-list-types/src/common/default_import_comments_array/index.ts
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,27 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
import { Either } from 'fp-ts/lib/Either'; | ||
import { importComment, ImportCommentsArray } from '../import_comment'; | ||
|
||
/** | ||
* Types the DefaultImportCommentsArray as: | ||
* - If null or undefined, then a default array of type ImportCommentsArray will be set | ||
*/ | ||
export const DefaultImportCommentsArray = new t.Type< | ||
ImportCommentsArray, | ||
ImportCommentsArray, | ||
unknown | ||
>( | ||
'DefaultImportComments', | ||
t.array(importComment).is, | ||
(input, context): Either<t.Errors, ImportCommentsArray> => | ||
input == null ? t.success([]) : t.array(importComment).validate(input, context), | ||
t.identity | ||
); |
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
135 changes: 135 additions & 0 deletions
135
packages/kbn-securitysolution-io-ts-list-types/src/common/import_comment/index.test.ts
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,135 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { left } from 'fp-ts/lib/Either'; | ||
import { getCommentsArrayMock, getCommentsMock } from '../comment/index.mock'; | ||
import { getCreateCommentsArrayMock } from '../create_comment/index.mock'; | ||
import { | ||
importComment, | ||
ImportCommentsArray, | ||
importCommentsArray, | ||
ImportCommentsArrayOrUndefined, | ||
importCommentsArrayOrUndefined, | ||
} from '.'; | ||
import { foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils'; | ||
|
||
describe('ImportComment', () => { | ||
describe('importComment', () => { | ||
test('it passes validation with a typical comment', () => { | ||
const payload = getCommentsMock(); | ||
const decoded = importComment.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it passes validation with a new comment', () => { | ||
const payload = { comment: 'new comment' }; | ||
const decoded = importComment.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it fails validation when undefined', () => { | ||
const payload = undefined; | ||
const decoded = importComment.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([ | ||
'Invalid value "undefined" supplied to "(({| comment: NonEmptyString, created_at: string, created_by: string, id: NonEmptyString |} & Partial<{| updated_at: string, updated_by: string |}>) | {| comment: NonEmptyString |})"', | ||
]); | ||
expect(message.schema).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('importCommentsArray', () => { | ||
test('it passes validation an array of Comment', () => { | ||
const payload = getCommentsArrayMock(); | ||
const decoded = importCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it passes validation an array of CreateComment', () => { | ||
const payload = getCreateCommentsArrayMock(); | ||
const decoded = importCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it passes validation an array of Comment and CreateComment', () => { | ||
const payload = [...getCommentsArrayMock(), ...getCreateCommentsArrayMock()]; | ||
const decoded = importCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it fails validation when undefined', () => { | ||
const payload = undefined; | ||
const decoded = importCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([ | ||
'Invalid value "undefined" supplied to "Array<(({| comment: NonEmptyString, created_at: string, created_by: string, id: NonEmptyString |} & Partial<{| updated_at: string, updated_by: string |}>) | {| comment: NonEmptyString |})>"', | ||
]); | ||
expect(message.schema).toEqual({}); | ||
}); | ||
|
||
test('it fails validation when array includes non ImportComment types', () => { | ||
const payload = [1] as unknown as ImportCommentsArray; | ||
const decoded = importCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([ | ||
'Invalid value "1" supplied to "Array<(({| comment: NonEmptyString, created_at: string, created_by: string, id: NonEmptyString |} & Partial<{| updated_at: string, updated_by: string |}>) | {| comment: NonEmptyString |})>"', | ||
]); | ||
expect(message.schema).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('importCommentsArrayOrUndefined', () => { | ||
test('it passes validation an array of ImportComment', () => { | ||
const payload = [...getCommentsArrayMock(), ...getCreateCommentsArrayMock()]; | ||
const decoded = importCommentsArrayOrUndefined.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it passes validation when undefined', () => { | ||
const payload = undefined; | ||
const decoded = importCommentsArrayOrUndefined.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(payload); | ||
}); | ||
|
||
test('it fails validation when array includes non ImportComment types', () => { | ||
const payload = [1] as unknown as ImportCommentsArrayOrUndefined; | ||
const decoded = importCommentsArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([ | ||
'Invalid value "1" supplied to "Array<(({| comment: NonEmptyString, created_at: string, created_by: string, id: NonEmptyString |} & Partial<{| updated_at: string, updated_by: string |}>) | {| comment: NonEmptyString |})>"', | ||
]); | ||
expect(message.schema).toEqual({}); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/kbn-securitysolution-io-ts-list-types/src/common/import_comment/index.ts
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,19 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
import { createComment } from '../create_comment'; | ||
import { comment } from '../comment'; | ||
|
||
export const importComment = t.union([comment, createComment]); | ||
|
||
export type ImportComment = t.TypeOf<typeof importComment>; | ||
export const importCommentsArray = t.array(importComment); | ||
export type ImportCommentsArray = t.TypeOf<typeof importCommentsArray>; | ||
export const importCommentsArrayOrUndefined = t.union([importCommentsArray, t.undefined]); | ||
export type ImportCommentsArrayOrUndefined = t.TypeOf<typeof importCommentsArrayOrUndefined>; |
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
Oops, something went wrong.