|
| 1 | +from code42cli.args import ArgConfig |
| 2 | +from code42cli.commands import Command |
| 3 | +from code42cli.cmds.alerts.extraction import extract |
| 4 | +from code42cli.cmds.search_shared import args, logger_factory |
| 5 | +from code42cli.cmds.search_shared.enums import ( |
| 6 | + AlertFilterArguments, |
| 7 | + AlertState, |
| 8 | + AlertSeverity, |
| 9 | + ServerProtocol, |
| 10 | + RuleType, |
| 11 | +) |
| 12 | +from code42cli.cmds.search_shared.cursor_store import AlertCursorStore |
| 13 | + |
| 14 | + |
| 15 | +def load_subcommands(): |
| 16 | + """Sets up the `alerts` subcommand with all of its subcommands.""" |
| 17 | + usage_prefix = u"code42 alerts" |
| 18 | + |
| 19 | + print_func = Command( |
| 20 | + u"print", |
| 21 | + u"Print alerts to stdout", |
| 22 | + u"{} {}".format(usage_prefix, u"print <optional-args>"), |
| 23 | + handler=print_out, |
| 24 | + arg_customizer=_load_search_args, |
| 25 | + use_single_arg_obj=True, |
| 26 | + ) |
| 27 | + |
| 28 | + write = Command( |
| 29 | + u"write-to", |
| 30 | + u"Write alerts to the file with the given name.", |
| 31 | + u"{} {}".format(usage_prefix, u"write-to <filename> <optional-args>"), |
| 32 | + handler=write_to, |
| 33 | + arg_customizer=_load_write_to_args, |
| 34 | + use_single_arg_obj=True, |
| 35 | + ) |
| 36 | + |
| 37 | + send = Command( |
| 38 | + u"send-to", |
| 39 | + u"Send alerts to the given server address.", |
| 40 | + u"{} {}".format(usage_prefix, u"send-to <server-address> <optional-args>"), |
| 41 | + handler=send_to, |
| 42 | + arg_customizer=_load_send_to_args, |
| 43 | + use_single_arg_obj=True, |
| 44 | + ) |
| 45 | + |
| 46 | + clear = Command( |
| 47 | + u"clear-checkpoint", |
| 48 | + u"Remove the saved alert checkpoint from 'incremental' (-i) mode.", |
| 49 | + u"{} {}".format(usage_prefix, u"clear-checkpoint <optional-args>"), |
| 50 | + handler=clear_checkpoint, |
| 51 | + ) |
| 52 | + |
| 53 | + return [print_func, write, send, clear] |
| 54 | + |
| 55 | + |
| 56 | +def clear_checkpoint(sdk, profile): |
| 57 | + """Removes the stored checkpoint that keeps track of the last alert retrieved for the given profile.. |
| 58 | + To use, run `code42 alerts clear-checkpoint`. |
| 59 | + This affects `incremental` mode by causing it to behave like it has never been run before. |
| 60 | + """ |
| 61 | + AlertCursorStore(profile.name).replace_stored_cursor_timestamp(None) |
| 62 | + |
| 63 | + |
| 64 | +def print_out(sdk, profile, args): |
| 65 | + """Activates 'print' command. It gets alerts and prints them to stdout.""" |
| 66 | + logger = logger_factory.get_logger_for_stdout(args.format) |
| 67 | + extract(sdk, profile, logger, args) |
| 68 | + |
| 69 | + |
| 70 | +def write_to(sdk, profile, args): |
| 71 | + """Activates 'write-to' command. It gets alerts and writes them to the given file.""" |
| 72 | + logger = logger_factory.get_logger_for_file(args.output_file, args.format) |
| 73 | + extract(sdk, profile, logger, args) |
| 74 | + |
| 75 | + |
| 76 | +def send_to(sdk, profile, args): |
| 77 | + """Activates 'send-to' command. It getsalerts and logs them to the given server.""" |
| 78 | + logger = logger_factory.get_logger_for_server(args.server, args.protocol, args.format) |
| 79 | + extract(sdk, profile, logger, args) |
| 80 | + |
| 81 | + |
| 82 | +def _load_write_to_args(arg_collection): |
| 83 | + output_file = ArgConfig(u"output_file", help=u"The name of the local file to send output to.") |
| 84 | + arg_collection.append(u"output_file", output_file) |
| 85 | + _load_search_args(arg_collection) |
| 86 | + |
| 87 | + |
| 88 | +def _load_send_to_args(arg_collection): |
| 89 | + send_to_args = { |
| 90 | + u"server": ArgConfig(u"server", help=u"The server address to send output to."), |
| 91 | + u"protocol": ArgConfig( |
| 92 | + u"-p", |
| 93 | + u"--protocol", |
| 94 | + choices=ServerProtocol(), |
| 95 | + default=ServerProtocol.UDP, |
| 96 | + help=u"Protocol used to send logs to server.", |
| 97 | + ), |
| 98 | + } |
| 99 | + |
| 100 | + arg_collection.extend(send_to_args) |
| 101 | + _load_search_args(arg_collection) |
| 102 | + |
| 103 | + |
| 104 | +def _load_search_args(arg_collection): |
| 105 | + filter_args = { |
| 106 | + AlertFilterArguments.SEVERITY: ArgConfig( |
| 107 | + u"--{}".format(AlertFilterArguments.SEVERITY), |
| 108 | + nargs=u"+", |
| 109 | + help=u"Filter alerts by severity. Defaults to returning all severities. Available choices={0}".format( |
| 110 | + list(AlertSeverity()) |
| 111 | + ), |
| 112 | + ), |
| 113 | + AlertFilterArguments.STATE: ArgConfig( |
| 114 | + u"--{}".format(AlertFilterArguments.STATE), |
| 115 | + help=u"Filter alerts by state. Defaults to returning all states. Available choices={0}".format( |
| 116 | + list(AlertState()) |
| 117 | + ), |
| 118 | + ), |
| 119 | + AlertFilterArguments.ACTOR: ArgConfig( |
| 120 | + u"--{}".format(AlertFilterArguments.ACTOR.replace("_", "-")), |
| 121 | + metavar=u"ACTOR", |
| 122 | + help=u"Filter alerts by including the given actor(s) who triggered the alert. Args must match actor username exactly.", |
| 123 | + nargs=u"+", |
| 124 | + ), |
| 125 | + AlertFilterArguments.ACTOR_CONTAINS: ArgConfig( |
| 126 | + u"--{}".format(AlertFilterArguments.ACTOR_CONTAINS.replace("_", "-")), |
| 127 | + metavar=u"ACTOR", |
| 128 | + help=u"Filter alerts by including actor(s) whose username contains the given string.", |
| 129 | + nargs=u"+", |
| 130 | + ), |
| 131 | + AlertFilterArguments.EXCLUDE_ACTOR: ArgConfig( |
| 132 | + u"--{}".format(AlertFilterArguments.EXCLUDE_ACTOR.replace("_", "-")), |
| 133 | + metavar=u"ACTOR", |
| 134 | + help=u"Filter alerts by excluding the given actor(s) who triggered the alert. Args must match actor username exactly.", |
| 135 | + nargs=u"+", |
| 136 | + ), |
| 137 | + AlertFilterArguments.EXCLUDE_ACTOR_CONTAINS: ArgConfig( |
| 138 | + u"--{}".format(AlertFilterArguments.EXCLUDE_ACTOR_CONTAINS.replace("_", "-")), |
| 139 | + metavar=u"ACTOR", |
| 140 | + help=u"Filter alerts by excluding actor(s) whose username contains the given string.", |
| 141 | + nargs=u"+", |
| 142 | + ), |
| 143 | + AlertFilterArguments.RULE_NAME: ArgConfig( |
| 144 | + u"--{}".format(AlertFilterArguments.RULE_NAME.replace("_", "-")), |
| 145 | + metavar=u"RULE_NAME", |
| 146 | + help=u"Filter alerts by including the given rule name(s).", |
| 147 | + nargs=u"+", |
| 148 | + ), |
| 149 | + AlertFilterArguments.EXCLUDE_RULE_NAME: ArgConfig( |
| 150 | + u"--{}".format(AlertFilterArguments.EXCLUDE_RULE_NAME.replace("_", "-")), |
| 151 | + metavar=u"RULE_NAME", |
| 152 | + help=u"Filter alerts by excluding the given rule name(s).", |
| 153 | + nargs=u"+", |
| 154 | + ), |
| 155 | + AlertFilterArguments.RULE_ID: ArgConfig( |
| 156 | + u"--{}".format(AlertFilterArguments.RULE_ID.replace("_", "-")), |
| 157 | + metavar=u"RULE_ID", |
| 158 | + help=u"Filter alerts by including the given rule id(s).", |
| 159 | + nargs=u"+", |
| 160 | + ), |
| 161 | + AlertFilterArguments.EXCLUDE_RULE_ID: ArgConfig( |
| 162 | + u"--{}".format(AlertFilterArguments.EXCLUDE_RULE_ID.replace("_", "-")), |
| 163 | + metavar=u"RULE_ID", |
| 164 | + help=u"Filter alerts by excluding the given rule id(s).", |
| 165 | + nargs=u"+", |
| 166 | + ), |
| 167 | + AlertFilterArguments.RULE_TYPE: ArgConfig( |
| 168 | + u"--{}".format(AlertFilterArguments.RULE_TYPE.replace("_", "-")), |
| 169 | + metavar=u"RULE_TYPE", |
| 170 | + help=u"Filter alerts by including the given rule type(s). Available choices={0}".format( |
| 171 | + list(RuleType()) |
| 172 | + ), |
| 173 | + nargs=u"+", |
| 174 | + ), |
| 175 | + AlertFilterArguments.EXCLUDE_RULE_TYPE: ArgConfig( |
| 176 | + u"--{}".format(AlertFilterArguments.EXCLUDE_RULE_TYPE.replace("_", "-")), |
| 177 | + metavar=u"RULE_TYPE", |
| 178 | + help=u"Filter alerts by excluding the given rule type(s). Available choices={0}".format( |
| 179 | + list(RuleType()) |
| 180 | + ), |
| 181 | + nargs=u"+", |
| 182 | + ), |
| 183 | + AlertFilterArguments.DESCRIPTION: ArgConfig( |
| 184 | + u"--{}".format(AlertFilterArguments.DESCRIPTION), |
| 185 | + help=u"Filter alerts by description. Does fuzzy search by default.", |
| 186 | + ), |
| 187 | + } |
| 188 | + search_args = args.create_search_args(search_for=u"alerts", filter_args=filter_args) |
| 189 | + arg_collection.extend(search_args) |
0 commit comments