Skip to content

Commit d7b8c58

Browse files
author
Juliya Smith
authored
INTEG 1001 - Risk Tag Error handling (#57)
1 parent 3b23b71 commit d7b8c58

File tree

9 files changed

+247
-42
lines changed

9 files changed

+247
-42
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
1616
- Additional information in the error log file:
1717
- The full command path for the command that errored.
1818
- User-facing error messages you see during adhoc sessions.
19+
- A custom error in the error log when you try adding unknown risk tags to user.
20+
- A custom error in the error log when you try adding a user to a detection list who is already added.
21+
22+
### Fixed
23+
24+
- Fixed bug in bulk commands where value-less fields in csv files were treated as empty strings instead of None.
1925

2026
### 0.5.3 - 2020-05-04
2127

src/code42cli/cmds/detectionlists/__init__.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from py42.exceptions import Py42BadRequestError
2+
13
from code42cli.compat import str
24
from code42cli.cmds.detectionlists.commands import DetectionListCommandFactory
35
from code42cli.bulk import generate_template, run_bulk_process, CSVReader, FlatFileReader
@@ -6,6 +8,7 @@
68
BulkCommandType,
79
DetectionLists,
810
DetectionListUserKeys,
11+
RiskTags,
912
)
1013

1114

@@ -15,7 +18,15 @@ def __init__(self, username, list_name):
1518
super(UserAlreadyAddedError, self).__init__(msg)
1619

1720

18-
def handle_bad_request_during_add(bad_request_err, username_tried_adding, list_name):
21+
class UnknownRiskTagError(Exception):
22+
def __init__(self, bad_tags):
23+
tags = u", ".join(bad_tags)
24+
super(UnknownRiskTagError, self).__init__(
25+
u"The following risk tags are unknown: '{}'.".format(tags)
26+
)
27+
28+
29+
def try_handle_user_already_added_error(bad_request_err, username_tried_adding, list_name):
1930
if _error_is_user_already_added(bad_request_err.response.text):
2031
raise UserAlreadyAddedError(username_tried_adding, list_name)
2132
return False
@@ -209,6 +220,7 @@ def update_user(sdk, user_id, cloud_alias=None, risk_tag=None, notes=None):
209220
"""Updates a detection list user.
210221
211222
Args:
223+
sdk (py42.sdk.SDKClient): py42 sdk.
212224
user_id (str or unicode): The ID of the user to update. This is their `userUid` found from
213225
`sdk.users.get_by_username()`.
214226
cloud_alias (str or unicode): A cloud alias to add to the user.
@@ -218,6 +230,29 @@ def update_user(sdk, user_id, cloud_alias=None, risk_tag=None, notes=None):
218230
if cloud_alias:
219231
sdk.detectionlists.add_user_cloud_alias(user_id, cloud_alias)
220232
if risk_tag:
221-
sdk.detectionlists.add_user_risk_tags(user_id, risk_tag)
233+
try_add_risk_tags(sdk, user_id, risk_tag)
222234
if notes:
223235
sdk.detectionlists.update_user_notes(user_id, notes)
236+
237+
238+
def try_add_risk_tags(sdk, user_id, risk_tag):
239+
_try_add_or_remove_risk_tags(user_id, risk_tag, sdk.detectionlists.add_user_risk_tags)
240+
241+
242+
def try_remove_risk_tags(sdk, user_id, risk_tag):
243+
_try_add_or_remove_risk_tags(user_id, risk_tag, sdk.detectionlists.remove_user_risk_tags)
244+
245+
246+
def _try_add_or_remove_risk_tags(user_id, risk_tag, func):
247+
try:
248+
func(user_id, risk_tag)
249+
except Py42BadRequestError:
250+
_try_handle_bad_risk_tag(risk_tag)
251+
raise
252+
253+
254+
def _try_handle_bad_risk_tag(tags):
255+
options = list(RiskTags())
256+
unknowns = [tag for tag in tags if tag not in options] if tags else None
257+
if unknowns:
258+
raise UnknownRiskTagError(unknowns)

src/code42cli/cmds/detectionlists/departing_employee.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
load_user_descriptions,
55
get_user_id,
66
update_user,
7-
handle_bad_request_during_add,
7+
try_handle_user_already_added_error,
88
)
99
from code42cli.cmds.detectionlists.enums import DetectionLists
1010

@@ -42,8 +42,9 @@ def add_departing_employee(
4242
sdk.detectionlists.departing_employee.add(user_id, departure_date)
4343
update_user(sdk, user_id, cloud_alias, notes=notes)
4444
except Py42BadRequestError as err:
45-
if not handle_bad_request_during_add(err, username, DetectionLists.DEPARTING_EMPLOYEE):
46-
raise
45+
list_name = DetectionLists.DEPARTING_EMPLOYEE
46+
try_handle_user_already_added_error(err, username, list_name)
47+
raise
4748

4849

4950
def remove_departing_employee(sdk, profile, username):

src/code42cli/cmds/detectionlists/enums.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,26 @@ class DetectionListUserKeys(object):
1616
USERNAME = u"username"
1717
NOTES = u"notes"
1818
RISK_TAG = u"risk_tag"
19+
20+
21+
class RiskTags(object):
22+
FLIGHT_RISK = u"FLIGHT_RISK"
23+
HIGH_IMPACT_EMPLOYEE = u"HIGH_IMPACT_EMPLOYEE"
24+
ELEVATED_ACCESS_PRIVILEGES = u"ELEVATED_ACCESS_PRIVILEGES"
25+
PERFORMANCE_CONCERNS = u"PERFORMANCE_CONCERNS"
26+
SUSPICIOUS_SYSTEM_ACTIVITY = u"SUSPICIOUS_SYSTEM_ACTIVITY"
27+
POOR_SECURITY_PRACTICES = u"POOR_SECURITY_PRACTICES"
28+
CONTRACT_EMPLOYEE = u"CONTRACT_EMPLOYEE"
29+
30+
def __iter__(self):
31+
return iter(
32+
[
33+
self.FLIGHT_RISK,
34+
self.HIGH_IMPACT_EMPLOYEE,
35+
self.ELEVATED_ACCESS_PRIVILEGES,
36+
self.PERFORMANCE_CONCERNS,
37+
self.SUSPICIOUS_SYSTEM_ACTIVITY,
38+
self.POOR_SECURITY_PRACTICES,
39+
self.CONTRACT_EMPLOYEE,
40+
]
41+
)

src/code42cli/cmds/detectionlists/high_risk_employee.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
load_username_description,
66
get_user_id,
77
update_user,
8-
handle_bad_request_during_add,
8+
try_handle_user_already_added_error,
9+
try_add_risk_tags,
10+
try_remove_risk_tags,
911
)
10-
from code42cli.cmds.detectionlists.enums import DetectionLists
11-
from code42cli.cmds.detectionlists.enums import DetectionListUserKeys
12+
from code42cli.cmds.detectionlists.enums import DetectionLists, DetectionListUserKeys, RiskTags
1213
from code42cli.commands import Command
1314

1415
from py42.exceptions import Py42BadRequestError
1516

1617

1718
def load_subcommands():
18-
1919
handlers = _create_handlers()
2020
detection_list = DetectionList.create_high_risk_employee_list(handlers)
2121
cmd_list = detection_list.load_subcommands()
@@ -47,13 +47,13 @@ def _create_handlers():
4747
def add_risk_tags(sdk, profile, username, risk_tag):
4848
risk_tag = _handle_list_args(risk_tag)
4949
user_id = get_user_id(sdk, username)
50-
sdk.detectionlists.add_user_risk_tags(user_id, risk_tag)
50+
try_add_risk_tags(sdk, user_id, risk_tag)
5151

5252

5353
def remove_risk_tags(sdk, profile, username, risk_tag):
5454
risk_tag = _handle_list_args(risk_tag)
5555
user_id = get_user_id(sdk, username)
56-
sdk.detectionlists.remove_user_risk_tags(user_id, risk_tag)
56+
try_remove_risk_tags(sdk, user_id, risk_tag)
5757

5858

5959
def add_high_risk_employee(sdk, profile, username, cloud_alias=None, risk_tag=None, notes=None):
@@ -74,8 +74,9 @@ def add_high_risk_employee(sdk, profile, username, cloud_alias=None, risk_tag=No
7474
sdk.detectionlists.high_risk_employee.add(user_id)
7575
update_user(sdk, user_id, cloud_alias, risk_tag, notes)
7676
except Py42BadRequestError as err:
77-
if not handle_bad_request_during_add(err, username, DetectionLists.HIGH_RISK_EMPLOYEE):
78-
raise
77+
list_name = DetectionLists.HIGH_RISK_EMPLOYEE
78+
try_handle_user_already_added_error(err, username, list_name)
79+
raise
7980

8081

8182
def remove_high_risk_employee(sdk, profile, username):
@@ -93,16 +94,9 @@ def remove_high_risk_employee(sdk, profile, username):
9394
def _load_risk_tag_description(argument_collection):
9495
risk_tag = argument_collection.arg_configs[DetectionListUserKeys.RISK_TAG]
9596
risk_tag.as_multi_val_param()
97+
tags = u", ".join(list(RiskTags()))
9698
risk_tag.set_help(
97-
u"Risk tags associated with the employee. "
98-
u"Options include "
99-
u"[HIGH_IMPACT_EMPLOYEE, "
100-
u"ELEVATED_ACCESS_PRIVILEGES, "
101-
u"PERFORMANCE_CONCERNS, "
102-
u"FLIGHT_RISK, "
103-
u"SUSPICIOUS_SYSTEM_ACTIVITY, "
104-
u"POOR_SECURITY_PRACTICES, "
105-
u"CONTRACT_EMPLOYEE]"
99+
u"Risk tags associated with the employee. Options include: [{}].".format(tags)
106100
)
107101

108102

tests/cmds/detectionlists/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def bad_request_for_user_already_added(mocker):
2727

2828

2929
@pytest.fixture
30-
def bad_request_for_other_reasons(mocker):
30+
def generic_bad_request(mocker):
3131
resp = mocker.MagicMock(spec=Response)
32-
resp.text = "Some other, non-supported reason for a bad request"
32+
resp.text = "TEST"
3333
return _create_bad_request_mock(resp)
3434

3535

tests/cmds/detectionlists/test_departing_employee.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ def test_add_departing_employee_when_user_already_added_raises_UserAlreadyAddedE
6060

6161

6262
def test_add_departing_employee_when_bad_request_but_not_user_already_added_raises_Py42BadRequestError(
63-
sdk_with_user, profile, bad_request_for_other_reasons, caplog
63+
sdk_with_user, profile, generic_bad_request, caplog
6464
):
65-
sdk_with_user.detectionlists.departing_employee.add.side_effect = bad_request_for_other_reasons
65+
sdk_with_user.detectionlists.departing_employee.add.side_effect = generic_bad_request
6666
with pytest.raises(Py42BadRequestError):
6767
add_departing_employee(sdk_with_user, profile, _EMPLOYEE)
6868

0 commit comments

Comments
 (0)