Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CreateHash, DeduplicateValuesbyKey, JsonUnescape [Common Scripts Util…
…ity Consolidations] (#23672) * Moved CreateHash script to CommonScripts * Standardized CreateHash code * Added createHash UT * Moved DeduplicateValuesbyKey to CommonScripts * Added DeduplicateValuesbyKey UT * Updated release notes, Moved JsonUnescape * Added JsonUnescape UT * fixed DeduplicateValuesbyKey UT * Updated CommonScripts RN * Removed nosec from hashlib functions * Added to known_words.txt * Fixed linting issue, Updated RN * added noseq to ignore hash Bandit error * Updated RN * Updated RN * Updated RN * Updated RN * Updated RN * Updated RN * Updated RN * Updated docker images & RN Co-authored-by: Shmuel Fainberg <sfainberg@paloaltonetworks.com>
- Loading branch information
1 parent
aaa9c11
commit 5ce9355
Showing
29 changed files
with
240 additions
and
575 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
#### Scripts | ||
##### New: JsonUnescape | ||
- Recursively un-escapes JSON data if escaped JSON is found (Available from Cortex XSOAR 6.0.0). | ||
- Updated the Docker image to: *demisto/python3-deb:3.10.9.43863*. | ||
##### New: DeduplicateValuesbyKey | ||
- Given a list of objects and a key found in each of those objects, return a unique list of values associated with that key. Returns error if the objects provided do not contain the key of interest. (Available from Cortex XSOAR 6.0.0). | ||
- Updated the Docker image to: *demisto/python3:3.10.9.43882*. | ||
##### New: CreateHash | ||
- Creating a hash of a given input, support sha1, sha256, sha512, md5 and blake. Wrapper for https://docs.python.org/3/library/hashlib.html. | ||
(Available from Cortex XSOAR 6.0.0). | ||
- Updated the Docker image to: *demisto/python3:3.10.9.43882*. |
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,52 @@ | ||
|
||
import hashlib | ||
from hashlib import blake2b | ||
|
||
import demistomock as demisto # noqa: F401 | ||
from CommonServerPython import * # noqa: F401 | ||
|
||
|
||
def create_hash(text, hashtype): | ||
"""Create a hash from a given input and return it as a context outputs | ||
Args: | ||
text (str): input to hash | ||
hashtype (str): hash type | ||
Returns: | ||
Dict[str,str]: Dictionary representing the command results context | ||
""" | ||
if hashtype == "sha512": | ||
h = hashlib.sha512() | ||
h.update(text.encode('utf-8')) | ||
elif hashtype == "sha256": | ||
h = hashlib.sha256() | ||
h.update(text.encode('utf-8')) | ||
elif hashtype == 'sha1': | ||
h = hashlib.sha1() # nosec | ||
h.update(text.encode('utf-8')) | ||
elif hashtype == 'md5': | ||
h = hashlib.md5() # nosec | ||
h.update(text.encode('utf-8')) | ||
else: | ||
h = blake2b() | ||
h.update(text.encode('utf-8')) | ||
|
||
context = { | ||
"CreateHash": str(h.hexdigest()) | ||
} | ||
|
||
return context | ||
|
||
|
||
def main(): # pragma: no cover | ||
args = demisto.args() | ||
text = args.get('text') | ||
hashtype = args.get('type') | ||
|
||
context = create_hash(text, hashtype) | ||
return_results(CommandResults(outputs=context)) | ||
|
||
|
||
if __name__ in ('__builtin__', 'builtins', '__main__'): | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("hash_method", ['sha512', 'sha256', 'sha1', 'md5', '']) | ||
def test_create_hash(hash_method): | ||
""" | ||
Given: | ||
- A string | ||
When: | ||
- Running the script | ||
Then: | ||
- Ensure the expected hash is returned | ||
""" | ||
from CreateHash import create_hash | ||
context = create_hash('test', hash_method) | ||
|
||
assert isinstance(context, dict) | ||
assert isinstance(context.get('CreateHash'), str) |
File renamed without changes.
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: 71 additions & 0 deletions
71
Packs/CommonScripts/Scripts/DeduplicateValuesbyKey/DeduplicateValuesbyKey_test.py
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,71 @@ | ||
|
||
import pytest | ||
|
||
|
||
def test_generate_unique_values_from_objects(): | ||
from DeduplicateValuesbyKey import generate_unique_values_from_objects | ||
objects = [ | ||
{ | ||
"key": "value1", | ||
"value": "value1" | ||
}, | ||
{ | ||
"key": "value1", | ||
"value": "value2" | ||
}, | ||
{ | ||
"key": "value2", | ||
"value": "value3" | ||
}, | ||
{ | ||
"key": "value2", | ||
"value": "value4" | ||
}, | ||
{ | ||
"key": "value3", | ||
"value": "value5" | ||
}, | ||
{ | ||
"key": "value3", | ||
"value": "value6" | ||
}] | ||
values = generate_unique_values_from_objects(objects, "key", False) | ||
assert set(values) == set(["value1", "value2", "value3"]) | ||
|
||
|
||
def test_generate_unique_values_from_objects_with_none(): | ||
from DeduplicateValuesbyKey import generate_unique_values_from_objects | ||
objects = [ | ||
{ | ||
"key": "value1", | ||
"value": "value1" | ||
}, | ||
{ | ||
"key": "value1", | ||
"value": "value2" | ||
}, | ||
{ | ||
"key": "value2", | ||
"value": "value3" | ||
}, | ||
{ | ||
"key": "value2", | ||
"value": "value4" | ||
}, | ||
{ | ||
"key": "value3", | ||
"value": "value5" | ||
}, | ||
{ | ||
"key": "None_value", | ||
"value": None | ||
}] | ||
values = generate_unique_values_from_objects(objects, "key", True) | ||
assert set(values) == set(["None_value", "value1", "value2", "value3"]) | ||
|
||
|
||
def test_generate_unique_values_from_objects_fail(): | ||
from DeduplicateValuesbyKey import generate_unique_values_from_objects | ||
|
||
with pytest.raises(SystemExit): | ||
generate_unique_values_from_objects([], "key", True) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
26 changes: 26 additions & 0 deletions
26
Packs/CommonScripts/Scripts/JsonUnescape/JsonUnescape_test.py
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,26 @@ | ||
def test_hook(): | ||
""" | ||
Given | ||
- A string value representing a json | ||
When | ||
- Running the hook function | ||
Then | ||
- Ensure the string value is converted to json | ||
""" | ||
from JsonUnescape import hook | ||
assert hook({"key": "value"}) == {"key": "value"} | ||
assert hook({"key": "{'key': 'value'}"}) == {'key': "{'key': 'value'}"} | ||
|
||
|
||
def test_unescape(): | ||
""" | ||
Given | ||
- A dictionary with a string value | ||
When | ||
- Running the unescape function | ||
Then | ||
- Ensure the string value is converted to json | ||
""" | ||
from JsonUnescape import unescape | ||
assert unescape({"value": "value"}) == 'value' | ||
assert unescape({"value": "{'key': 'value'}"}) == "{'key': 'value'}" |
File renamed without changes.
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 @@ | ||
{"breakingChanges":true,"breakingChangesNotes":"The script: CreateHash moved to *CommonScripts* pack. Make sure to update the *CommonScripts* pack to the latest version in order to use this script."} |
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 @@ | ||
##### CreateHash | ||
- Deprecated. **CreateHash** script moved to *Common Scripts* pack. |
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
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 @@ | ||
{"breakingChanges":true,"breakingChangesNotes":"The script: DeduplicateValuesbyKey moved to *CommonScripts* pack. Make sure to update the *CommonScripts* pack to the latest version in order to use this script."} |
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 @@ | ||
##### DeduplicateValuesbyKey | ||
- Deprecated. **DeduplicateValuesbyKey** script moved to *Common Scripts* pack. |
Oops, something went wrong.