|
| 1 | +from code42cli.cmds.detectionlists.commands import DetectionListCommandFactory |
| 2 | +from code42cli.bulk import generate_template, run_bulk_process |
| 3 | +from code42cli.cmds.detectionlists.enums import ( |
| 4 | + BulkCommandType, |
| 5 | + DetectionLists, |
| 6 | + DetectionListUserKeys, |
| 7 | +) |
| 8 | +from code42cli.util import print_error |
| 9 | + |
| 10 | + |
| 11 | +class DetectionListHandlers(object): |
| 12 | + """Handlers DTO for passing in specific detection list functions. |
| 13 | + |
| 14 | + Args: |
| 15 | + add (callable): A function that adds an employee to the list. |
| 16 | + remove (callable): A function that removes an employee from the list. |
| 17 | + load_add (callable): A function that loads the add-related `ArgConfig`s. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, add=None, remove=None, load_add=None): |
| 21 | + self.add_employee = add |
| 22 | + self.remove_employee = remove |
| 23 | + self.load_add_description = load_add |
| 24 | + |
| 25 | + |
| 26 | +class UserDoesNotExistError(Exception): |
| 27 | + """An error to represent a username that is not in our system.""" |
| 28 | + |
| 29 | + def __init__(self, username): |
| 30 | + super(UserDoesNotExistError, self).__init__(u"User '{}' does not exist.".format(username)) |
| 31 | + |
| 32 | + |
| 33 | +class DetectionList(object): |
| 34 | + """An object representing a Code42 detection list. Use this class by passing in handlers for |
| 35 | + adding and removing employees. This class will handle the bulk-related commands and some |
| 36 | + shared help texts. |
| 37 | + |
| 38 | + Args: |
| 39 | + list_name (str): An option from the DetectionLists enum. For convenience, use one of the |
| 40 | + given `classmethods`. |
| 41 | + handlers (DetectionListHandlers): A DTO containing implementations for adding / removing |
| 42 | + users from specific lists. |
| 43 | + cmd_factory (DetectionListCommandFactory): A factory that creates detection list commands. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, list_name, handlers, cmd_factory=None): |
| 47 | + self.name = list_name |
| 48 | + self.handlers = handlers |
| 49 | + self.factory = cmd_factory or DetectionListCommandFactory(list_name) |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def create_high_risk_list(cls, handlers): |
| 53 | + """Creates a high risk detection list. |
| 54 | + |
| 55 | + Args: |
| 56 | + handlers (DetectionListHandlers): A DTO containing implementations for adding / |
| 57 | + removing users from specific lists. |
| 58 | + |
| 59 | + Returns: |
| 60 | + DetectionList: A high-risk employee detection list. |
| 61 | + """ |
| 62 | + return cls(DetectionLists.HIGH_RISK_EMPLOYEE, handlers) |
| 63 | + |
| 64 | + def load_subcommands(self): |
| 65 | + """Loads high risk employee related subcommands""" |
| 66 | + bulk = self.factory.create_bulk_command(lambda: self._load_bulk_subcommands()) |
| 67 | + add = self.factory.create_add_command( |
| 68 | + self.handlers.add_employee, self.handlers.load_add_description |
| 69 | + ) |
| 70 | + return [bulk, add] |
| 71 | + |
| 72 | + def _load_bulk_subcommands(self): |
| 73 | + generate_template_cmd = self.factory.create_bulk_generate_template_command( |
| 74 | + self.generate_csv_file |
| 75 | + ) |
| 76 | + add = self.factory.create_bulk_add_command(self.bulk_add_employees) |
| 77 | + return [generate_template_cmd, add] |
| 78 | + |
| 79 | + def generate_csv_file(self, cmd, path=None): |
| 80 | + """Generates a csv template a user would need to fill-in for bulk adding users to the |
| 81 | + detection list. |
| 82 | + |
| 83 | + Args: |
| 84 | + cmd (str): An option from the `BulkCommandType` enum specifying which type of csv to |
| 85 | + generate. |
| 86 | + path (str, optional): A path to put the file after it's generated. If None, will use |
| 87 | + the current working directory. Defaults to None. |
| 88 | + """ |
| 89 | + handler = None |
| 90 | + if cmd == BulkCommandType.ADD: |
| 91 | + handler = self.handlers.add_employee |
| 92 | + generate_template(handler, path) |
| 93 | + |
| 94 | + def bulk_add_employees(self, sdk, profile, csv_file): |
| 95 | + """Takes a csv file with each row representing an employee and adds them all to a |
| 96 | + detection list in a bulk fashion. |
| 97 | +
|
| 98 | + Args: |
| 99 | + sdk (py42.sdk.SDKClient): The py42 sdk. |
| 100 | + profile (Code42Profile): The profile under which to execute this command. |
| 101 | + csv_file (str): The path to the csv file containing rows of users. |
| 102 | + """ |
| 103 | + run_bulk_process(csv_file, lambda **kwargs: self._add_employee(sdk, profile, **kwargs)) |
| 104 | + |
| 105 | + def _add_employee(self, sdk, profile, **kwargs): |
| 106 | + if ( |
| 107 | + kwargs.has_key(DetectionListUserKeys.CLOUD_ALIAS) |
| 108 | + and type(kwargs[DetectionListUserKeys.CLOUD_ALIAS]) != list |
| 109 | + ): |
| 110 | + kwargs[DetectionListUserKeys.CLOUD_ALIAS] = kwargs[ |
| 111 | + DetectionListUserKeys.CLOUD_ALIAS |
| 112 | + ].split() |
| 113 | + |
| 114 | + self.handlers.add_employee(sdk, profile, **kwargs) |
| 115 | + |
| 116 | + |
| 117 | +def load_user_descriptions(argument_collection): |
| 118 | + """Loads the arg descriptions related to updating fields about a detection list user, such as |
| 119 | + notes or cloud aliases. |
| 120 | + |
| 121 | + Args: |
| 122 | + argument_collection (ArgConfigCollection): The arg configs off the command that needs its |
| 123 | + user descriptions loaded. |
| 124 | + """ |
| 125 | + username = argument_collection.arg_configs[DetectionListUserKeys.USERNAME] |
| 126 | + cloud_alias = argument_collection.arg_configs[DetectionListUserKeys.CLOUD_ALIAS] |
| 127 | + notes = argument_collection.arg_configs[DetectionListUserKeys.NOTES] |
| 128 | + |
| 129 | + username.set_help(u"The code42 username of the user you want to add.") |
| 130 | + cloud_alias.set_help(u"Alternative emails addresses for other cloud services.") |
| 131 | + cloud_alias.as_multi_val_param() |
| 132 | + notes.set_help(u"Notes about the employee.") |
| 133 | + |
| 134 | + |
| 135 | +def get_user_id(sdk, username): |
| 136 | + """Returns the user's UID (referred to by `user_id` in detection lists). If the user does not |
| 137 | + exist, it prints an error and exits. |
| 138 | + |
| 139 | + Args: |
| 140 | + sdk (py42.sdk.SDKClient): The py42 sdk. |
| 141 | + username (str or unicode): The username of the user to get an ID for. |
| 142 | + |
| 143 | + Returns: |
| 144 | + str: The user ID for the user with the given username. |
| 145 | + """ |
| 146 | + users = sdk.users.get_by_username(username)[u"users"] |
| 147 | + if not users: |
| 148 | + print_error(str(UserDoesNotExistError(username))) |
| 149 | + exit(1) |
| 150 | + return users[0][u"userUid"] |
| 151 | + |
| 152 | + |
| 153 | +def update_user(sdk, user_id, cloud_alias=None, risk_tag=None, notes=None): |
| 154 | + """Updates a detection list user. |
| 155 | + |
| 156 | + Args: |
| 157 | + user_id (str): The ID of the user to update. This is their `userUid` found from |
| 158 | + `sdk.users.get_by_username()`. |
| 159 | + cloud_alias (iter[str]): A list of cloud aliases to add to the user. |
| 160 | + risk_tag (iter[str]): A list of risk tags associated with user. |
| 161 | + notes (str): Notes about the user. |
| 162 | + """ |
| 163 | + if cloud_alias: |
| 164 | + sdk.detectionlists.add_user_cloud_aliases(user_id, cloud_alias) |
| 165 | + if risk_tag: |
| 166 | + sdk.detectionlists.add_user_risk_tags(user_id, risk_tag) |
| 167 | + if notes: |
| 168 | + sdk.detectionlists.update_user_notes(user_id, notes) |
0 commit comments