-
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.
Browse files
Browse the repository at this point in the history
### Summary Fixing exceptions export format and adding integration tests for it. Co-authored-by: Yara Tercero <yctercero@users.noreply.github.com>
- Loading branch information
1 parent
eee9b07
commit 184592e
Showing
17 changed files
with
292 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
*/ | ||
|
||
export * from './add_remove_id_to_item'; | ||
export * from './transform_data_to_ndjson'; |
88 changes: 88 additions & 0 deletions
88
packages/kbn-securitysolution-utils/src/transform_data_to_ndjson/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,88 @@ | ||
/* | ||
* 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 { transformDataToNdjson } from './'; | ||
|
||
export const ANCHOR_DATE = '2020-02-20T03:57:54.037Z'; | ||
|
||
const getRulesSchemaMock = (anchorDate: string = ANCHOR_DATE) => ({ | ||
author: [], | ||
id: '7a7065d7-6e8b-4aae-8d20-c93613dec9f9', | ||
created_at: new Date(anchorDate).toISOString(), | ||
updated_at: new Date(anchorDate).toISOString(), | ||
created_by: 'elastic', | ||
description: 'some description', | ||
enabled: true, | ||
false_positives: ['false positive 1', 'false positive 2'], | ||
from: 'now-6m', | ||
immutable: false, | ||
name: 'Query with a rule id', | ||
query: 'user.name: root or user.name: admin', | ||
references: ['test 1', 'test 2'], | ||
severity: 'high', | ||
severity_mapping: [], | ||
updated_by: 'elastic_kibana', | ||
tags: ['some fake tag 1', 'some fake tag 2'], | ||
to: 'now', | ||
type: 'query', | ||
threat: [], | ||
version: 1, | ||
status: 'succeeded', | ||
status_date: '2020-02-22T16:47:50.047Z', | ||
last_success_at: '2020-02-22T16:47:50.047Z', | ||
last_success_message: 'succeeded', | ||
output_index: '.siem-signals-default', | ||
max_signals: 100, | ||
risk_score: 55, | ||
risk_score_mapping: [], | ||
language: 'kuery', | ||
rule_id: 'query-rule-id', | ||
interval: '5m', | ||
exceptions_list: [], | ||
}); | ||
|
||
describe('transformDataToNdjson', () => { | ||
test('if rules are empty it returns an empty string', () => { | ||
const ruleNdjson = transformDataToNdjson([]); | ||
expect(ruleNdjson).toEqual(''); | ||
}); | ||
|
||
test('single rule will transform with new line ending character for ndjson', () => { | ||
const rule = getRulesSchemaMock(); | ||
const ruleNdjson = transformDataToNdjson([rule]); | ||
expect(ruleNdjson.endsWith('\n')).toBe(true); | ||
}); | ||
|
||
test('multiple rules will transform with two new line ending characters for ndjson', () => { | ||
const result1 = getRulesSchemaMock(); | ||
const result2 = getRulesSchemaMock(); | ||
result2.id = 'some other id'; | ||
result2.rule_id = 'some other id'; | ||
result2.name = 'Some other rule'; | ||
|
||
const ruleNdjson = transformDataToNdjson([result1, result2]); | ||
// this is how we count characters in JavaScript :-) | ||
const count = ruleNdjson.split('\n').length - 1; | ||
expect(count).toBe(2); | ||
}); | ||
|
||
test('you can parse two rules back out without errors', () => { | ||
const result1 = getRulesSchemaMock(); | ||
const result2 = getRulesSchemaMock(); | ||
result2.id = 'some other id'; | ||
result2.rule_id = 'some other id'; | ||
result2.name = 'Some other rule'; | ||
|
||
const ruleNdjson = transformDataToNdjson([result1, result2]); | ||
const ruleStrings = ruleNdjson.split('\n'); | ||
const reParsed1 = JSON.parse(ruleStrings[0]); | ||
const reParsed2 = JSON.parse(ruleStrings[1]); | ||
expect(reParsed1).toEqual(result1); | ||
expect(reParsed2).toEqual(result2); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/kbn-securitysolution-utils/src/transform_data_to_ndjson/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,16 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export const transformDataToNdjson = (data: unknown[]): string => { | ||
if (data.length !== 0) { | ||
const dataString = data.map((item) => JSON.stringify(item)).join('\n'); | ||
return `${dataString}\n`; | ||
} else { | ||
return ''; | ||
} | ||
}; |
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
2 changes: 2 additions & 0 deletions
2
x-pack/plugins/security_solution/cypress/downloads/test_exception_list.ndjson
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,2 @@ | ||
{"_version":"WzQyNjA0LDFd","created_at":"2021-10-14T01:30:22.034Z","created_by":"elastic","description":"Test exception list description","id":"4c65a230-2c8e-11ec-be1c-2bbdec602f88","immutable":false,"list_id":"test_exception_list","name":"Test exception list","namespace_type":"single","os_types":[],"tags":[],"tie_breaker_id":"b04983b4-1617-441c-bb6c-c729281fa2e9","type":"detection","updated_at":"2021-10-14T01:30:22.036Z","updated_by":"elastic","version":1} | ||
{"exported_list_items_count":0} |
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
71 changes: 0 additions & 71 deletions
71
x-pack/plugins/security_solution/server/utils/read_stream/create_stream_from_ndjson.test.ts
This file was deleted.
Oops, something went wrong.
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.