Skip to content

risk tags cmds #41

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

Merged
merged 109 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
66bce0e
Do thing
Apr 15, 2020
6dccd93
Add missing word employee
Apr 15, 2020
b409154
Add missing word employee
Apr 15, 2020
5612b2f
Mega ultra reuse code
Apr 15, 2020
872c553
Fix username desc
Apr 15, 2020
2ba8392
Merge branch 'master' into chore/refactor-detetctionlists
Apr 15, 2020
af781fd
Upgrade py42 and extractor
Apr 16, 2020
390bbd2
Merge branch 'chore/refactor-detetctionlists' of https://github.com/c…
Apr 16, 2020
c7ac5d3
Undo last commit
Apr 16, 2020
557330c
Upgrade py42 and extractor
Apr 16, 2020
7f2baf8
Impl add
Apr 16, 2020
0cdc409
Add tests
Apr 16, 2020
d13fef7
Format
Apr 16, 2020
406dd19
Fix search args
Apr 16, 2020
1b4c279
Fix underscore to hyphen issue
Apr 16, 2020
4be3302
Fix bulk
Apr 16, 2020
b6c4590
update py42
Apr 16, 2020
d690005
Fix tests
Apr 16, 2020
7ac0e40
Fix everything
Apr 16, 2020
ca2e168
More tests
Apr 16, 2020
966ab82
get handlers
Apr 16, 2020
0c02883
Save
Apr 16, 2020
63db05b
Remove bulk, had to massage it for that
Apr 16, 2020
713f68d
Revise
Apr 16, 2020
76cd42f
Fix tests
Apr 17, 2020
1f99191
Use generators
Apr 17, 2020
61c817f
Add more tests
Apr 17, 2020
742b56a
Update changelog
Apr 17, 2020
2adf39a
Remove print
Apr 17, 2020
59c125c
Remove command
Apr 17, 2020
6e78d22
REturn
Apr 17, 2020
516a6c0
Check for no user
Apr 17, 2020
f90c229
Simplify
Apr 17, 2020
41eca3b
Update tests
Apr 17, 2020
8c1d863
More tests
Apr 17, 2020
70e8d30
Update upstream
Apr 17, 2020
01a2922
Remove unused import
Apr 17, 2020
50e357e
Bumps
Apr 17, 2020
ea9e074
Merge branch 'chore/refactor-detetctionlists' into feature/rm-hr
Apr 17, 2020
1bc6d58
Bumps correctly
Apr 17, 2020
20d14a8
Tests
Apr 17, 2020
afe670b
Reader arg
Apr 17, 2020
9462bcb
More tests
Apr 17, 2020
ec2ed73
Rename methods
Apr 17, 2020
c9cec92
Docstr+ format
Apr 20, 2020
5db1983
Genericize the username desc
Apr 20, 2020
51ff8fb
Pull out command names
Apr 20, 2020
d449473
Add readme
Apr 20, 2020
312793f
Errorsgit sgit s
Apr 20, 2020
57c30bb
Error handling
Apr 20, 2020
5a9a125
Handle template for remove
Apr 20, 2020
97c778d
Print message for remove generation
Apr 20, 2020
b9cde61
Update CL
Apr 20, 2020
cf2aa52
Revise
Apr 20, 2020
769f599
Try fix tests
Apr 20, 2020
4b85169
Add missing periods
Apr 20, 2020
7e1f295
Revise README
Apr 20, 2020
13ce049
Revise Bulk docstr
Apr 20, 2020
5c226bf
Clarify method names
Apr 20, 2020
db13630
Doc
Apr 20, 2020
61af99b
Docs revise
Apr 20, 2020
a2c81fb
Fix critical bug
Apr 20, 2020
9c379a4
Fix more critical bugs
Apr 20, 2020
746ac24
Remove spaces
Apr 20, 2020
a418100
Formatting
Apr 20, 2020
db3754a
Fix readme issues
Apr 20, 2020
f61cef6
Do all
Apr 20, 2020
4a65322
Missing s
Apr 20, 2020
e1ae368
README command replace
Apr 20, 2020
a72f45f
Say delimited one line per user in readme
Apr 20, 2020
c7fd31b
Generic type for documentation purposes
Apr 20, 2020
2fcb5eb
Some pr fb
Apr 20, 2020
951c4aa
Merge branch 'master' into feature/rm-hr
Apr 20, 2020
9e97e70
Determine which handler to use based on num args
Apr 20, 2020
13aacdd
Print message during py42forbidden
Apr 20, 2020
9f12ba4
Handle flat files better
Apr 20, 2020
81eb8c1
Update readme
Apr 20, 2020
a241279
Remove pointless base class
Apr 21, 2020
3219f64
Update words
Apr 21, 2020
2746127
Make test for error log loc printing simpler
Apr 21, 2020
d833b3d
Remove redundancy and fix docstr
Apr 21, 2020
7f76cb4
<=
Apr 21, 2020
d3c48d9
Docs
Apr 21, 2020
95bbdb3
Profile and sdk names reuse
Apr 21, 2020
b4a5ca5
Make only accpet single cloud alias
Apr 21, 2020
27a206b
Format
Apr 21, 2020
37b0888
Better help
Apr 21, 2020
4a58374
elif
Apr 21, 2020
ffd8bd8
Str or unicode, makes pycharm happier
Apr 21, 2020
86ed52a
cloud alias
Apr 21, 2020
2f37256
cloud alias for real
Apr 21, 2020
e6be3b2
Bumps
Apr 21, 2020
7da5346
Remove pinning
Apr 21, 2020
28f5aee
Fix tests
Apr 21, 2020
3c210ac
Ignore none
Apr 21, 2020
72788b9
Fix edge case with blank rows
Apr 21, 2020
0de86ad
Put back something that got deleted accidentally
Apr 21, 2020
c0a9910
Improve teset
Apr 21, 2020
8d8e10c
Actually use custom ex
Apr 21, 2020
9acba6e
Simplify
Apr 21, 2020
1037039
Updates from upstream
Apr 21, 2020
a326f1b
Safety first
Apr 21, 2020
036ab07
Merge branch 'feature/rm-hr' into feature/update-tags
Apr 21, 2020
a4bba04
Resolve flicts
Apr 22, 2020
1e1deb3
Update cl and fix bug
Apr 22, 2020
993fdcb
Add comment
Apr 22, 2020
3f6e776
Resolve flicts
Apr 22, 2020
e77a91f
Bump v
Apr 22, 2020
6413adc
add v in cl
Apr 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.

## Unreleased
## 0.5.0 - Unreleased

### Changed

Expand Down Expand Up @@ -36,6 +36,8 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
- `remove`: that takes a list of users in a file.
- `add` that takes parameters: `--username`, `--cloud-alias`, `--risk-factor`, and `--notes`.
- `remove` that takes a username.
- `add-risk-tags` that takes a username and risk tags.
- `remove-risk-tags` that takes a username and risk tags.
- `code42 departing-employee` commands:
- `bulk` with subcommands:
- `add`: that takes a csv file of users.
Expand Down
2 changes: 1 addition & 1 deletion src/code42cli/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.4"
__version__ = "0.5.0"
8 changes: 5 additions & 3 deletions src/code42cli/cmds/detectionlists/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def load_subcommands(self):
self.handlers.add_employee, self.handlers.load_add_description
)
remove = self.factory.create_remove_command(
self.handlers.remove_employee, _load_username_description
self.handlers.remove_employee, load_username_description
)
return [bulk, add, remove]

Expand Down Expand Up @@ -148,7 +148,9 @@ def _remove_employee(self, sdk, profile, *args, **kwargs):
self.handlers.remove_employee(sdk, profile, *args, **kwargs)


def _load_username_description(argument_collection):

def load_username_description(argument_collection):
"""Loads the arg descriptions for the `username` CLI parameter."""
username = argument_collection.arg_configs[DetectionListUserKeys.USERNAME]
username.set_help(u"A code42 username for an employee.")

Expand All @@ -161,7 +163,7 @@ def load_user_descriptions(argument_collection):
argument_collection (ArgConfigCollection): The arg configs off the command that needs its
user descriptions loaded.
"""
_load_username_description(argument_collection)
load_username_description(argument_collection)
cloud_alias = argument_collection.arg_configs[DetectionListUserKeys.CLOUD_ALIAS]
notes = argument_collection.arg_configs[DetectionListUserKeys.NOTES]
cloud_alias.set_help(u"An alternative email address for another cloud service.")
Expand Down
58 changes: 52 additions & 6 deletions src/code42cli/cmds/detectionlists/high_risk_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@
DetectionList,
DetectionListHandlers,
load_user_descriptions,
load_username_description,
get_user_id,
update_user,
)
from code42cli.cmds.detectionlists.enums import DetectionListUserKeys
from code42cli.commands import Command


def load_subcommands():

handlers = _create_handlers()
detection_list = DetectionList.create_high_risk_employee_list(handlers)
return detection_list.load_subcommands()
cmd_list = detection_list.load_subcommands()
cmd_list.extend(
[
Command(
u"add-risk-tags",
u"Associates risk tags with a user.",
handler=add_risk_tags,
arg_customizer=_load_risk_tag_mgmt_descriptions,
),
Command(
u"remove-risk-tags",
u"Disassociates risk tags from a user.",
handler=remove_risk_tags,
arg_customizer=_load_risk_tag_mgmt_descriptions,
),
]
)
return cmd_list


def _create_handlers():
Expand All @@ -20,6 +40,17 @@ def _create_handlers():
)


def add_risk_tags(sdk, profile, username, risk_tag):
risk_tag = _handle_list_args(risk_tag)
user_id = get_user_id(sdk, username)
sdk.detectionlists.add_user_risk_tags(user_id, risk_tag)

def remove_risk_tags(sdk, profile, username, risk_tag):
risk_tag = _handle_list_args(risk_tag)
user_id = get_user_id(sdk, username)
sdk.detectionlists.remove_user_risk_tags(user_id, risk_tag)


def add_high_risk_employee(sdk, profile, username, cloud_alias=None, risk_tag=None, notes=None):
"""Adds an employee to the high risk employee detection list.

Expand All @@ -31,9 +62,7 @@ def add_high_risk_employee(sdk, profile, username, cloud_alias=None, risk_tag=No
risk_tag (iter[str]): Risk tags associated with the employee.
notes: (str): Notes about the employee.
"""
if risk_tag and type(risk_tag) != list:
risk_tag = risk_tag.split()

risk_tag = _handle_list_args(risk_tag)
user_id = get_user_id(sdk, username)
update_user(sdk, user_id, cloud_alias, risk_tag, notes)
sdk.detectionlists.high_risk_employee.add(user_id)
Expand All @@ -51,8 +80,7 @@ def remove_high_risk_employee(sdk, profile, username):
sdk.detectionlists.high_risk_employee.remove(user_id)


def _load_add_description(argument_collection):
load_user_descriptions(argument_collection)
def _load_risk_tag_description(argument_collection):
risk_tag = argument_collection.arg_configs[DetectionListUserKeys.RISK_TAG]
risk_tag.as_multi_val_param()
risk_tag.set_help(
Expand All @@ -66,3 +94,21 @@ def _load_add_description(argument_collection):
u"POOR_SECURITY_PRACTICES, "
u"CONTRACT_EMPLOYEE]"
)


def _load_add_description(argument_collection):
load_user_descriptions(argument_collection)
_load_risk_tag_description(argument_collection)


def _load_risk_tag_mgmt_descriptions(argument_collection):
load_username_description(argument_collection)
_load_risk_tag_description(argument_collection)


def _handle_list_args(list_arg):
"""Converts str args to a list. Useful for `bulk` commands which don't use `argparse` but
instead pass in values from files, such as in the form "item1 item2"."""
if list_arg and type(list_arg) != list:
return list_arg.split()
Copy link
Contributor

@alanag13 alanag13 Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment explaining why this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

return list_arg
56 changes: 56 additions & 0 deletions tests/cmds/detectionlists/test_high_risk_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from code42cli.cmds.detectionlists.high_risk_employee import (
add_high_risk_employee,
remove_high_risk_employee,
add_risk_tags,
remove_risk_tags,
)
from .conftest import TEST_ID

Expand Down Expand Up @@ -74,3 +76,57 @@ def test_remove_high_risk_employee_when_user_does_not_exist_prints_error(
except UserDoesNotExistError:
capture = capsys.readouterr()
assert str(UserDoesNotExistError(_EMPLOYEE)) in capture.out


def test_add_risk_tags_adds_tags(sdk_with_user, profile):
add_risk_tags(sdk_with_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
sdk_with_user.detectionlists.add_user_risk_tags.assert_called_once_with(
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
)


def test_add_risk_tags_when_given_space_delimited_str_adds_expected_tags(sdk_with_user, profile):
add_risk_tags(sdk_with_user, profile, _EMPLOYEE, "TAG_YOU_ARE_IT GROUND_IS_LAVA")
sdk_with_user.detectionlists.add_user_risk_tags.assert_called_once_with(
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
)


def test_add_risk_tags_when_user_does_not_exist_exits(sdk_without_user, profile):
with pytest.raises(UserDoesNotExistError):
add_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])


def test_add_risk_tags_when_user_does_not_exist_prints_error(sdk_without_user, profile, capsys):
try:
add_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
except UserDoesNotExistError:
capture = capsys.readouterr()
assert str(UserDoesNotExistError(_EMPLOYEE)) in capture.out


def test_remove_risk_tags_adds_tags(sdk_with_user, profile):
remove_risk_tags(sdk_with_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
sdk_with_user.detectionlists.remove_user_risk_tags.assert_called_once_with(
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
)


def test_remove_risk_tags_when_given_space_delimited_str_adds_expected_tags(sdk_with_user, profile):
remove_risk_tags(sdk_with_user, profile, _EMPLOYEE, "TAG_YOU_ARE_IT GROUND_IS_LAVA")
sdk_with_user.detectionlists.remove_user_risk_tags.assert_called_once_with(
TEST_ID, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"]
)


def test_remove_risk_tags_when_user_does_not_exist_exits(sdk_without_user, profile):
with pytest.raises(UserDoesNotExistError):
remove_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])


def test_remove_risk_tags_when_user_does_not_exist_prints_error(sdk_without_user, profile, capsys):
try:
remove_risk_tags(sdk_without_user, profile, _EMPLOYEE, ["TAG_YOU_ARE_IT", "GROUND_IS_LAVA"])
except UserDoesNotExistError:
capture = capsys.readouterr()
assert str(UserDoesNotExistError(_EMPLOYEE)) in capture.out