Skip to content

Commit 0058956

Browse files
author
Juliya Smith
authored
Merge pull request #35 from code42/handle-lower-case-pdf
2 parents 9fe63db + dc824fe commit 0058956

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

Packs/Code42/Integrations/Code42/Code42.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,6 @@
8888
"osHostName": "Hostname",
8989
}
9090

91-
CODE42_FILE_CATEGORY_MAPPER = {
92-
"SourceCode": FileCategory.SOURCE_CODE,
93-
"Audio": FileCategory.AUDIO,
94-
"Executable": FileCategory.EXECUTABLE,
95-
"Document": FileCategory.DOCUMENT,
96-
"Image": FileCategory.IMAGE,
97-
"PDF": FileCategory.PDF,
98-
"Presentation": FileCategory.PRESENTATION,
99-
"Script": FileCategory.SCRIPT,
100-
"Spreadsheet": FileCategory.SPREADSHEET,
101-
"Video": FileCategory.VIDEO,
102-
"VirtualDiskImage": FileCategory.VIRTUAL_DISK_IMAGE,
103-
"Archive": FileCategory.ZIP,
104-
}
105-
10691
SECURITY_EVENT_HEADERS = [
10792
"EventType",
10893
"FileName",
@@ -540,8 +525,24 @@ def _create_exposure_filter(exposure_arg):
540525
return ExposureType.is_in(exposure_arg)
541526

542527

543-
def _get_file_category_value(key):
544-
return CODE42_FILE_CATEGORY_MAPPER.get(key, "UNCATEGORIZED")
528+
def get_file_category_value(key):
529+
# Meant to handle all possible cases
530+
key = key.lower().replace("-", "").replace("_", "")
531+
category_map = {
532+
"sourcecode": FileCategory.SOURCE_CODE,
533+
"audio": FileCategory.AUDIO,
534+
"executable": FileCategory.EXECUTABLE,
535+
"document": FileCategory.DOCUMENT,
536+
"image": FileCategory.IMAGE,
537+
"pdf": FileCategory.PDF,
538+
"presentation": FileCategory.PRESENTATION,
539+
"script": FileCategory.SCRIPT,
540+
"spreadsheet": FileCategory.SPREADSHEET,
541+
"video": FileCategory.VIDEO,
542+
"virtualdiskimage": FileCategory.VIRTUAL_DISK_IMAGE,
543+
"archive": FileCategory.ZIP,
544+
}
545+
return category_map.get(key, "UNCATEGORIZED")
545546

546547

547548
class ObservationToSecurityQueryMapper(object):
@@ -642,7 +643,7 @@ def _create_file_category_filters(self):
642643
observed_file_categories = self._observation_data.get("fileCategories")
643644
if observed_file_categories:
644645
categories = [
645-
_get_file_category_value(c.get("category"))
646+
get_file_category_value(c.get("category"))
646647
for c in observed_file_categories
647648
if c.get("isSignificant") and c.get("category")
648649
]

Packs/Code42/Integrations/Code42/Code42_test.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import json
22
import pytest
3+
from py42.sdk.queries.fileevents.filters import FileCategory
34
from requests import Response
45
from py42.sdk import SDKClient
56
from py42.response import Py42Response
7+
from py42.sdk.queries.alerts.filters import Severity
68
from Code42 import (
79
Code42Client,
810
Code42LegalHoldMatterNotFoundError,
911
Code42InvalidLegalHoldMembershipError,
12+
get_file_category_value,
1013
build_query_payload,
1114
map_observation_to_security_query,
1215
map_to_code42_event_context,
@@ -476,6 +479,13 @@
476479
"fileCount": 3,
477480
"totalFileSize": 533846,
478481
"isSignificant": true
482+
},
483+
{
484+
"type$": "OBSERVED_FILE_CATEGORY",
485+
"category": "Pdf",
486+
"fileCount": 3,
487+
"totalFileSize": 533846,
488+
"isSignificant": true
479489
}
480490
],
481491
"files": [
@@ -723,9 +733,12 @@
723733
"filters": [{"operator": "IS", "term": "exposure", "value": "ApplicationRead"}],
724734
},
725735
{
726-
"filterClause": "AND",
727-
"filters": [{"operator": "IS", "term": "fileCategory", "value": "SOURCE_CODE"}],
728-
},
736+
"filterClause": "OR",
737+
"filters": [
738+
{"operator": "IS", "term": "fileCategory", "value": "PDF"},
739+
{"operator": "IS", "term": "fileCategory", "value": "SOURCE_CODE"}
740+
]
741+
}
729742
],
730743
"pgNum": 1,
731744
"pgSize": 10000,
@@ -1347,6 +1360,48 @@ def assert_detection_list_outputs_match_response_items(outputs_list, response_it
13471360
"""TESTS"""
13481361

13491362

1363+
def test_get_file_category_value_handles_screaming_snake_case():
1364+
actual = get_file_category_value("SOURCE_CODE")
1365+
expected = FileCategory.SOURCE_CODE
1366+
assert actual == expected
1367+
1368+
1369+
def test_get_file_category_value_handles_capitalized_case():
1370+
actual = get_file_category_value("Pdf")
1371+
expected = FileCategory.PDF
1372+
assert actual == expected
1373+
1374+
1375+
def test_get_file_category_value_handles_lower_case():
1376+
actual = get_file_category_value("pdf")
1377+
expected = FileCategory.PDF
1378+
assert actual == expected
1379+
1380+
1381+
def test_get_file_category_value_handles_upper_case():
1382+
actual = get_file_category_value("PDF")
1383+
expected = FileCategory.PDF
1384+
assert actual == expected
1385+
1386+
1387+
def test_get_file_category_value_handles_pascal_case():
1388+
actual = get_file_category_value("SourceCode")
1389+
expected = FileCategory.SOURCE_CODE
1390+
assert actual == expected
1391+
1392+
1393+
def test_get_file_category_value_handles_hungarian_case():
1394+
actual = get_file_category_value("sourceCode")
1395+
expected = FileCategory.SOURCE_CODE
1396+
assert actual == expected
1397+
1398+
1399+
def test_get_file_category_value_handles_hyphenated_case():
1400+
actual = get_file_category_value("source-code")
1401+
expected = FileCategory.SOURCE_CODE
1402+
assert actual == expected
1403+
1404+
13501405
def test_client_lazily_inits_sdk(mocker, code42_sdk_mock):
13511406
sdk_factory_mock = mocker.patch("py42.sdk.from_local_account")
13521407
response_json_mock = """{"total": 1, "users": [{"username": "Test"}]}"""
@@ -1992,8 +2047,8 @@ def test_fetch_incidents_handles_multi_severity(code42_fetch_incidents_mock):
19922047
integration_context=None,
19932048
)
19942049
call_args = str(code42_fetch_incidents_mock.alerts.search.call_args[0][0])
1995-
assert "HIGH" in call_args
1996-
assert "LOW" in call_args
2050+
assert Severity.HIGH in call_args
2051+
assert Severity.LOW in call_args
19972052

19982053

19992054
def test_fetch_when_include_files_includes_files(code42_fetch_incidents_mock):

Packs/Code42/ReleaseNotes/2_0_4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#### Integrations
33
##### Code42
4+
- Fix bug where capitalized Alert file-observation file categories would not map to file event query values.
45
- Upgrade py42 dependency and internal code improvements.
56

67
#### Playbooks

0 commit comments

Comments
 (0)