|
| 1 | +import json |
| 2 | +from _collections import OrderedDict |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +from code42cli.click_ext.groups import OrderedGroup |
| 7 | +from code42cli.date_helper import parse_max_timestamp |
| 8 | +from code42cli.date_helper import parse_min_timestamp |
| 9 | +from code42cli.logger import get_logger_for_server |
| 10 | +from code42cli.options import begin_option |
| 11 | +from code42cli.options import end_option |
| 12 | +from code42cli.options import format_option |
| 13 | +from code42cli.options import sdk_options |
| 14 | +from code42cli.options import send_to_format_options |
| 15 | +from code42cli.options import server_options |
| 16 | +from code42cli.output_formats import OutputFormatter |
| 17 | +from code42cli.util import warn_interrupt |
| 18 | + |
| 19 | +EVENT_KEY = "events" |
| 20 | +AUDIT_LOGS_KEYWORD = "audit-logs" |
| 21 | + |
| 22 | +AUDIT_LOGS_DEFAULT_HEADER = OrderedDict() |
| 23 | +AUDIT_LOGS_DEFAULT_HEADER["timestamp"] = "Timestamp" |
| 24 | +AUDIT_LOGS_DEFAULT_HEADER["type$"] = "Type" |
| 25 | +AUDIT_LOGS_DEFAULT_HEADER["actorName"] = "ActorName" |
| 26 | +AUDIT_LOGS_DEFAULT_HEADER["actorIpAddress"] = "ActorIpAddress" |
| 27 | +AUDIT_LOGS_DEFAULT_HEADER["userName"] = "AffectedUser" |
| 28 | +AUDIT_LOGS_DEFAULT_HEADER["userId"] = "AffectedUserUID" |
| 29 | +# AUDIT_LOGS_DEFAULT_HEADER["success"] = "Success" |
| 30 | +# AUDIT_LOGS_DEFAULT_HEADER["resultCount"] = "ResultCount" |
| 31 | + |
| 32 | +filter_option_usernames = click.option( |
| 33 | + "--username", required=False, help="Filter results by usernames.", multiple=True, |
| 34 | +) |
| 35 | +filter_option_user_ids = click.option( |
| 36 | + "--user-id", required=False, help="Filter results by user ids.", multiple=True, |
| 37 | +) |
| 38 | + |
| 39 | +filter_option_user_ip_addresses = click.option( |
| 40 | + "--user-ip", |
| 41 | + required=False, |
| 42 | + help="Filter results by user ip addresses.", |
| 43 | + multiple=True, |
| 44 | +) |
| 45 | +filter_option_affected_user_ids = click.option( |
| 46 | + "--affected-user-id", |
| 47 | + required=False, |
| 48 | + help="Filter results by affected user ids.", |
| 49 | + multiple=True, |
| 50 | +) |
| 51 | +filter_option_affected_usernames = click.option( |
| 52 | + "--affected-username", |
| 53 | + required=False, |
| 54 | + help="Filter results by affected usernames.", |
| 55 | + multiple=True, |
| 56 | +) |
| 57 | +filter_option_event_types = click.option( |
| 58 | + "--event-type", |
| 59 | + required=False, |
| 60 | + help="Filter results by event types.", |
| 61 | + multiple=True, |
| 62 | +) |
| 63 | + |
| 64 | + |
| 65 | +def filter_options(f): |
| 66 | + f = begin_option( |
| 67 | + f, |
| 68 | + AUDIT_LOGS_KEYWORD, |
| 69 | + callback=lambda ctx, param, arg: parse_min_timestamp(arg), |
| 70 | + required=True, |
| 71 | + ) |
| 72 | + f = end_option( |
| 73 | + f, AUDIT_LOGS_KEYWORD, callback=lambda ctx, param, arg: parse_max_timestamp(arg) |
| 74 | + ) |
| 75 | + f = filter_option_event_types(f) |
| 76 | + f = filter_option_usernames(f) |
| 77 | + f = filter_option_user_ids(f) |
| 78 | + f = filter_option_user_ip_addresses(f) |
| 79 | + f = filter_option_affected_user_ids(f) |
| 80 | + f = filter_option_affected_usernames(f) |
| 81 | + return f |
| 82 | + |
| 83 | + |
| 84 | +@click.group(cls=OrderedGroup) |
| 85 | +@sdk_options(hidden=True) |
| 86 | +def audit_logs(state): |
| 87 | + """Retrieve audit logs.""" |
| 88 | + pass |
| 89 | + |
| 90 | + |
| 91 | +@audit_logs.command() |
| 92 | +@filter_options |
| 93 | +@format_option |
| 94 | +@sdk_options() |
| 95 | +def search( |
| 96 | + state, |
| 97 | + begin, |
| 98 | + end, |
| 99 | + event_type, |
| 100 | + username, |
| 101 | + user_id, |
| 102 | + user_ip, |
| 103 | + affected_user_id, |
| 104 | + affected_username, |
| 105 | + format, |
| 106 | +): |
| 107 | + """Search audit logs.""" |
| 108 | + _search( |
| 109 | + state.sdk, |
| 110 | + format, |
| 111 | + begin_time=begin, |
| 112 | + end_time=end, |
| 113 | + event_types=event_type, |
| 114 | + usernames=username, |
| 115 | + user_ids=user_id, |
| 116 | + user_ip_addresses=user_ip, |
| 117 | + affected_user_ids=affected_user_id, |
| 118 | + affected_usernames=affected_username, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +@audit_logs.command() |
| 123 | +@filter_options |
| 124 | +@server_options |
| 125 | +@send_to_format_options |
| 126 | +@sdk_options() |
| 127 | +def send_to( |
| 128 | + state, |
| 129 | + hostname, |
| 130 | + protocol, |
| 131 | + format, |
| 132 | + begin, |
| 133 | + end, |
| 134 | + event_type, |
| 135 | + username, |
| 136 | + user_id, |
| 137 | + user_ip, |
| 138 | + affected_user_id, |
| 139 | + affected_username, |
| 140 | +): |
| 141 | + """Send audit logs to the given server address.""" |
| 142 | + _send_to( |
| 143 | + state.sdk, |
| 144 | + hostname, |
| 145 | + protocol, |
| 146 | + format, |
| 147 | + begin_time=begin, |
| 148 | + end_time=end, |
| 149 | + event_types=event_type, |
| 150 | + usernames=username, |
| 151 | + user_ids=user_id, |
| 152 | + user_ip_addresses=user_ip, |
| 153 | + affected_user_ids=affected_user_id, |
| 154 | + affected_usernames=affected_username, |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | +def _search(sdk, format, **filter_args): |
| 159 | + |
| 160 | + formatter = OutputFormatter(format, AUDIT_LOGS_DEFAULT_HEADER) |
| 161 | + response_gen = sdk.auditlogs.get_all(**filter_args) |
| 162 | + |
| 163 | + events = [] |
| 164 | + try: |
| 165 | + for response in response_gen: |
| 166 | + response_dict = json.loads(response.text) |
| 167 | + if EVENT_KEY in response_dict: |
| 168 | + events.extend(response_dict.get(EVENT_KEY)) |
| 169 | + except KeyError: |
| 170 | + # API endpoint (get_page) returns a response without events key when no records are found |
| 171 | + # e.g {"paginationRangeStartIndex": 10000, "paginationRangeEndIndex": 10000, "totalResultCount": 1593} |
| 172 | + pass |
| 173 | + |
| 174 | + event_count = len(events) |
| 175 | + if not event_count: |
| 176 | + click.echo("No results found.") |
| 177 | + elif event_count > 10: |
| 178 | + click.echo_via_pager(formatter.get_formatted_output(events)) |
| 179 | + else: |
| 180 | + formatter.echo_formatted_list(events) |
| 181 | + |
| 182 | + |
| 183 | +def _send_to(sdk, hostname, protocol, format, **filter_args): |
| 184 | + logger = get_logger_for_server(hostname, protocol, format) |
| 185 | + with warn_interrupt(): |
| 186 | + response_gen = sdk.auditlogs.get_all(**filter_args) |
| 187 | + try: |
| 188 | + for response in response_gen: |
| 189 | + if EVENT_KEY in response: |
| 190 | + for event in response[EVENT_KEY]: |
| 191 | + logger.info(event) |
| 192 | + else: |
| 193 | + logger.info("No results found.") |
| 194 | + except KeyError: |
| 195 | + pass |
0 commit comments