-
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
[Security Solution][Platform] - Fixing exceptions export format #114920
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this to package as it's used in multiple plugins and exactly the same.