-
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
[SIEM] Adds stable alerting ids and more scripting for product testing #48165
Closed
FrankHassanabad
wants to merge
26
commits into
elastic:master
from
FrankHassanabad:add-stable-id-option
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
8c9a5fc
Added API endpoints and more scripts
FrankHassanabad 3b56b4c
Merge branch 'master' into add-get-delete-api
FrankHassanabad 91953a1
Added the update endpoint and scripts for the support of it. Changed …
FrankHassanabad 84739d4
put back the require statement for the builds to work correctly
FrankHassanabad 91f2797
Added casting magic with a TODO block
FrankHassanabad ee7c2c3
cleaned up more types
FrankHassanabad d136c3b
Merge branch 'master' into add-get-delete-api
FrankHassanabad 5fe3a77
Updated per code review
FrankHassanabad 42fbb44
Changed per code review
FrankHassanabad a5f5448
Updated per code review
FrankHassanabad aa7108b
Added optional id parameter in the URL that can be sent
FrankHassanabad eadac5d
added some unit tests
FrankHassanabad 9a90e47
Merge branch 'master' into add-get-delete-api
FrankHassanabad 9a3c036
Does all crud through a request params called alert_id instead of the…
FrankHassanabad 5ab372a
Removed weird wording from a function
FrankHassanabad ee06ed0
Merge branch 'master' into add-get-delete-api
FrankHassanabad a236065
Merge branch 'add-get-delete-api' into add-stable-id-option
FrankHassanabad acd9ff0
Added scripts to convert from saved objects to signals for posting
FrankHassanabad 93e888e
Remove echo statement
FrankHassanabad e5cb35f
Merge branch 'master' into add-get-delete-api
FrankHassanabad a5eaab1
Merge branch 'master' into add-get-delete-api
FrankHassanabad 688da79
Merge branch 'add-get-delete-api' into add-stable-id-option
FrankHassanabad 31b8add
Merge branch 'master' into add-stable-id-option
FrankHassanabad 009010a
Merge branch 'master' into add-stable-id-option
FrankHassanabad 17394a2
Fix minor wording
FrankHassanabad 6cf8a42
Merge branch 'master' into add-stable-id-option
FrankHassanabad 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
131 changes: 131 additions & 0 deletions
131
x-pack/legacy/plugins/siem/scripts/convert_saved_search_to_signals.js
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,131 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
require('../../../../../src/setup_node_env'); | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
/* | ||
* This script is used to parse a set of saved searches on a file system | ||
* and output signal data compatible json files. | ||
* Example: | ||
* node saved_query_to_signals.js ${HOME}/saved_searches ${HOME}/saved_signals | ||
* | ||
* After editing any changes in the files of ${HOME}/saved_signals/*.json | ||
* you can then post the signals with a CURL post script such as: | ||
* | ||
* ./post_signal.sh ${HOME}/saved_signals/*.json | ||
* | ||
* Note: This script is recursive and but does not preserve folder structure | ||
* when it outputs the saved signals. | ||
*/ | ||
|
||
// Defaults of the outputted signals since the saved KQL searches do not have | ||
// this type of information. You usually will want to make any hand edits after | ||
// doing a search to KQL conversion before posting it as a signal or checking it | ||
// into another repository. | ||
const INTERVAL = '24h'; | ||
const SEVERITY = 1; | ||
const TYPE = 'kql'; | ||
const FROM = 'now-24h'; | ||
const TO = 'now'; | ||
const INDEX = ['auditbeat-*', 'filebeat-*', 'packetbeat-*', 'winlogbeat-*']; | ||
|
||
const walk = dir => { | ||
const list = fs.readdirSync(dir); | ||
return list.reduce((accum, file) => { | ||
const fileWithDir = dir + '/' + file; | ||
const stat = fs.statSync(fileWithDir); | ||
if (stat && stat.isDirectory()) { | ||
return [...accum, ...walk(fileWithDir)]; | ||
} else { | ||
return [...accum, fileWithDir]; | ||
} | ||
}, []); | ||
}; | ||
|
||
// Temporary hash function for converting string to numbers. | ||
// TODO: Once we move from numbers to pure strings for id's this can be removed | ||
// and the file name used as the id (or a GUID), etc... | ||
const hashFunc = str => { | ||
let chr; | ||
let hash = 0; | ||
if (str.length === 0) return 0; | ||
for (let i = 0; i < str.length; i++) { | ||
chr = str.charCodeAt(i); | ||
// eslint-disable-next-line no-bitwise | ||
hash = (hash << 5) - hash + chr; | ||
// eslint-disable-next-line no-bitwise | ||
hash |= 0; | ||
} | ||
return hash; | ||
}; | ||
|
||
//clean up the file system characters | ||
const cleanupFileName = file => { | ||
return path | ||
.basename(file, path.extname(file)) | ||
.replace(/\s+/g, '_') | ||
.replace(/,/g, '') | ||
.replace(/\+s/g, '') | ||
.replace(/-/g, '') | ||
.replace(/__/g, '_') | ||
.toLowerCase(); | ||
}; | ||
|
||
async function main() { | ||
if (process.argv.length !== 4) { | ||
throw new Error( | ||
'usage: saved_query_to_signals [input directory with saved searches] [output directory]' | ||
); | ||
} | ||
|
||
const files = process.argv[2]; | ||
const outputDir = process.argv[3]; | ||
|
||
const savedSearchesJson = walk(files).filter(file => file.endsWith('.ndjson')); | ||
|
||
const savedSearchesParsed = savedSearchesJson.reduce((accum, json) => { | ||
const jsonFile = fs.readFileSync(json, 'utf8'); | ||
try { | ||
const parsedFile = JSON.parse(jsonFile); | ||
parsedFile._file = json; | ||
parsedFile.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.parse( | ||
parsedFile.attributes.kibanaSavedObjectMeta.searchSourceJSON | ||
); | ||
return [...accum, parsedFile]; | ||
} catch (err) { | ||
return accum; | ||
} | ||
}, []); | ||
|
||
savedSearchesParsed.forEach(savedSearch => { | ||
const fileToWrite = cleanupFileName(savedSearch._file); | ||
|
||
const query = savedSearch.attributes.kibanaSavedObjectMeta.searchSourceJSON.query.query; | ||
if (query != null && query.trim() !== '') { | ||
const outputMessage = { | ||
id: `${hashFunc(fileToWrite)}`, // TODO: Remove this once we change id to a string | ||
description: savedSearch.attributes.description || savedSearch.attributes.title, | ||
index: INDEX, | ||
interval: INTERVAL, | ||
name: savedSearch.attributes.title, | ||
severity: SEVERITY, | ||
type: TYPE, | ||
from: FROM, | ||
to: TO, | ||
kql: savedSearch.attributes.kibanaSavedObjectMeta.searchSourceJSON.query.query, | ||
}; | ||
|
||
fs.writeFileSync(`${outputDir}/${fileToWrite}.json`, JSON.stringify(outputMessage, null, 2)); | ||
} | ||
}); | ||
} | ||
|
||
if (require.main === module) { | ||
main(); | ||
} | ||
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.
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.
Looking for a way to make files like this a TypeScript file. It is a command line script at the moment but we might end up having an endpoint which takes exported saved objects and just move most if not all of this logic into that endpoint eventually so will concentrate more on that instead and leave this as is.