Skip to content

Commit 62b5263

Browse files
author
Juliya Smith
authored
Bugfix/put back cef (#138)
1 parent 1af10f6 commit 62b5263

26 files changed

Lines changed: 845 additions & 298 deletions

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,6 @@ Document all notable consumer-affecting changes in CHANGELOG.md per principles a
183183

184184
## Opening a PR
185185

186-
When you're satisified with your changes, open a PR and fill out the pull request template file. We recommend prefixing the name of your branch and/or PR title with `bugfix`, `chore`, or `feature` to help quickly categorize your change. Your unit tests and other checks will run against all supported python versions when you do this.
186+
When you're satisfied with your changes, open a PR and fill out the pull request template file. We recommend prefixing the name of your branch and/or PR title with `bugfix`, `chore`, or `feature` to help quickly categorize your change. Your unit tests and other checks will run against all supported python versions when you do this.
187187

188188
A team member should get in contact with you shortly to help merge your PR to completion and get it ready for a release!

integration/test_alerts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def _validate_field_value(field, value, response):
2424
("{} --state OPEN".format(ALERT_COMMAND), "state", "OPEN"),
2525
("{} --state RESOLVED".format(ALERT_COMMAND), "state", "RESOLVED"),
2626
(
27-
"{} --actor spatel@code42.com".format(ALERT_COMMAND),
27+
"{} --actor user@code42.com".format(ALERT_COMMAND),
2828
"actor",
29-
"spatel@code42.com",
29+
"user@code42.com",
3030
),
3131
(
3232
"{} --rule-name 'File Upload Alert'".format(ALERT_COMMAND),

src/code42cli/cmds/alert_rules.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from code42cli.errors import Code42CLIError
1313
from code42cli.errors import InvalidRuleTypeError
1414
from code42cli.file_readers import read_csv_arg
15+
from code42cli.options import format_option
1516
from code42cli.options import OrderedGroup
1617
from code42cli.options import sdk_options
17-
from code42cli.output_formats import format_option
18+
from code42cli.output_formats import get_output_format_func
1819

1920

2021
class AlertRuleTypes:
@@ -77,9 +78,10 @@ def remove_user(state, rule_id, username):
7778
@sdk_options()
7879
def list_alert_rules(state, format=None):
7980
"""Fetch existing alert rules."""
81+
format_func = get_output_format_func(format)
8082
selected_rules = _get_all_rules_metadata(state.sdk)
8183
if selected_rules:
82-
formatted_output = format(selected_rules, _HEADER_KEYS_MAP)
84+
formatted_output = format_func(selected_rules, _HEADER_KEYS_MAP)
8385
echo(formatted_output)
8486

8587

src/code42cli/cmds/alerts.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import code42cli.errors as errors
1212
import code42cli.options as opt
1313
from code42cli.cmds.search.cursor_store import AlertCursorStore
14-
from code42cli.output_formats import extraction_format_option as format_option
15-
14+
from code42cli.options import format_option
15+
from code42cli.output_formats import get_output_format_func
1616

1717
SEARCH_DEFAULT_HEADER = OrderedDict()
1818
SEARCH_DEFAULT_HEADER["name"] = "RuleName"
@@ -182,15 +182,18 @@ def search(
182182
**kwargs
183183
):
184184
"""Search for alerts."""
185+
output_header = ext.try_get_default_header(
186+
include_all, SEARCH_DEFAULT_HEADER, format
187+
)
188+
format_func = get_output_format_func(format)
185189
cursor = _get_alert_cursor_store(cli_state.profile.name) if use_checkpoint else None
186190
handlers = ext.create_handlers(
187191
cli_state.sdk,
188192
AlertExtractor,
189193
cursor,
190194
use_checkpoint,
191-
include_all=include_all,
192-
output_format=format,
193-
output_header=SEARCH_DEFAULT_HEADER,
195+
format_func=format_func,
196+
output_header=output_header,
194197
)
195198
extractor = _get_alert_extractor(cli_state.sdk, handlers)
196199
extractor.use_or_query = or_query

src/code42cli/cmds/enums.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/code42cli/cmds/legal_hold.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
from code42cli.errors import UserAlreadyAddedError
1616
from code42cli.errors import UserNotInLegalHoldError
1717
from code42cli.file_readers import read_csv_arg
18+
from code42cli.options import format_option
1819
from code42cli.options import OrderedGroup
1920
from code42cli.options import sdk_options
20-
from code42cli.output_formats import format_option
21+
from code42cli.output_formats import get_output_format_func
2122
from code42cli.util import format_string_list_to_columns
2223

2324

@@ -75,9 +76,10 @@ def remove_user(state, matter_id, username):
7576
@sdk_options()
7677
def _list(state, format=None):
7778
"""Fetch existing legal hold matters."""
79+
format_func = get_output_format_func(format)
7880
matters = _get_all_active_matters(state.sdk)
7981
if matters:
80-
output = format(matters, _MATTER_KEYS_MAP)
82+
output = format_func(matters, _MATTER_KEYS_MAP)
8183
echo(output)
8284

8385

@@ -98,6 +100,7 @@ def _list(state, format=None):
98100
@sdk_options()
99101
def show(state, matter_id, include_inactive=False, include_policy=False, format=None):
100102
"""Display details of a given legal hold matter."""
103+
format_func = get_output_format_func(format)
101104
matter = _check_matter_is_accessible(state.sdk, matter_id)
102105
matter["creator_username"] = matter["creator"]["username"]
103106

@@ -114,7 +117,7 @@ def show(state, matter_id, include_inactive=False, include_policy=False, format=
114117
member["user"]["username"] for member in memberships if not member["active"]
115118
]
116119

117-
output = format([matter], _MATTER_KEYS_MAP)
120+
output = format_func([matter], _MATTER_KEYS_MAP)
118121
echo(output)
119122

120123
_print_matter_members(active_usernames, member_type="active")

src/code42cli/cmds/search/enums.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1+
from code42cli.output_formats import OutputFormat
2+
13
IS_CHECKPOINT_KEY = "use_checkpoint"
24

35

4-
class OutputFormat:
6+
class FileEventsOutputFormat(OutputFormat):
57
CEF = "CEF"
6-
JSON = "JSON"
7-
RAW = "RAW-JSON"
8-
9-
def __iter__(self):
10-
return iter([self.CEF, self.JSON, self.RAW])
11-
12-
13-
class AlertOutputFormat:
14-
JSON = "JSON"
15-
RAW = "RAW-JSON"
168

179
def __iter__(self):
18-
return iter([self.JSON, self.RAW])
10+
return iter([self.TABLE, self.CSV, self.JSON, self.RAW, self.CEF])
1911

2012

2113
class AlertSeverity:

src/code42cli/cmds/search/extraction.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,28 @@
88
import code42cli.errors as errors
99
from code42cli.date_helper import verify_timestamp_order
1010
from code42cli.logger import get_main_cli_logger
11-
from code42cli.output_formats import get_dynamic_header
11+
from code42cli.output_formats import OutputFormat
1212
from code42cli.util import warn_interrupt
1313

1414
logger = get_main_cli_logger()
1515

1616
_ALERT_DETAIL_BATCH_SIZE = 100
1717

1818

19+
def try_get_default_header(include_all, default_header, output_format):
20+
"""Returns appropriate header based on include-all and output format. If returns None,
21+
the CLI format option will figure out the header based on the data keys."""
22+
output_header = None
23+
24+
if output_format == OutputFormat.TABLE and not include_all:
25+
output_header = default_header
26+
elif output_format != OutputFormat.TABLE and include_all:
27+
err_text = "--include-all only allowed for non-Table output formats."
28+
logger.log_error(err_text)
29+
raise errors.Code42CLIError(err_text)
30+
return output_header
31+
32+
1933
def _get_alert_details(sdk, alert_summary_list):
2034
alert_ids = [alert["id"] for alert in alert_summary_list]
2135
batches = [
@@ -31,13 +45,7 @@ def _get_alert_details(sdk, alert_summary_list):
3145

3246

3347
def create_handlers(
34-
sdk,
35-
extractor_class,
36-
cursor_store,
37-
checkpoint_name,
38-
include_all,
39-
output_format,
40-
output_header,
48+
sdk, extractor_class, cursor_store, checkpoint_name, format_func, output_header,
4149
):
4250
extractor = extractor_class(sdk, ExtractionHandlers())
4351
handlers = ExtractionHandlers()
@@ -76,7 +84,7 @@ def handle_response(response):
7684
handlers.TOTAL_EVENTS += total_events
7785

7886
def paginate():
79-
yield _process_events(output_format, include_all, events, output_header)
87+
yield format_func(events, output_header)
8088

8189
if len(events) > 10:
8290
click.echo_via_pager(paginate)
@@ -116,9 +124,3 @@ def create_time_range_filter(filter_cls, begin_date=None, end_date=None):
116124

117125
elif end_date and not begin_date:
118126
return filter_cls.on_or_before(end_date)
119-
120-
121-
def _process_events(output_format, include_all, events, output_header):
122-
if include_all:
123-
output_header = get_dynamic_header(events[0])
124-
return output_format(events, output_header)

src/code42cli/cmds/search/logger_factory.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/code42cli/cmds/securitydata.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import code42cli.cmds.search.options as searchopt
1212
import code42cli.errors as errors
1313
from code42cli.cmds.search.cursor_store import FileEventCursorStore
14+
from code42cli.cmds.securitydata_output_formats import (
15+
get_file_events_output_format_func,
16+
)
1417
from code42cli.logger import get_main_cli_logger
18+
from code42cli.options import format_option
1519
from code42cli.options import incompatible_with
1620
from code42cli.options import OrderedGroup
1721
from code42cli.options import sdk_options
18-
from code42cli.output_formats import extraction_format_option
19-
from code42cli.output_formats import format_option
20-
22+
from code42cli.output_formats import get_output_format_func
2123

2224
logger = get_main_cli_logger()
2325

@@ -37,6 +39,15 @@
3739
SEARCH_DEFAULT_HEADER["sha256Checksum"] = "SHA256Checksum"
3840

3941

42+
file_events_format_option = click.option(
43+
"-f",
44+
"--format",
45+
type=click.Choice(enum.FileEventsOutputFormat(), case_sensitive=False),
46+
help="The output format of the result. Defaults to table format.",
47+
default=enum.FileEventsOutputFormat.TABLE,
48+
)
49+
50+
4051
search_options = searchopt.create_search_options("file events")
4152

4253

@@ -149,7 +160,7 @@ def file_event_options(f):
149160
f = process_owner_option(f)
150161
f = tab_url_option(f)
151162
f = include_non_exposure_option(f)
152-
f = extraction_format_option(f)
163+
f = file_events_format_option(f)
153164
f = saved_search_option(f)
154165
return f
155166

@@ -196,18 +207,20 @@ def search(
196207
**kwargs
197208
):
198209
"""Search for file events."""
210+
output_header = ext.try_get_default_header(
211+
include_all, SEARCH_DEFAULT_HEADER, format
212+
)
213+
format_func = get_file_events_output_format_func(format)
199214
cursor = (
200215
_get_file_event_cursor_store(state.profile.name) if use_checkpoint else None
201216
)
202-
203217
handlers = ext.create_handlers(
204218
state.sdk,
205219
FileEventExtractor,
206220
cursor,
207221
use_checkpoint,
208-
include_all,
209-
output_format=format,
210-
output_header=SEARCH_DEFAULT_HEADER,
222+
format_func=format_func,
223+
output_header=output_header,
211224
)
212225
extractor = _get_file_event_extractor(state.sdk, handlers)
213226
extractor.use_or_query = or_query
@@ -237,10 +250,11 @@ def saved_search(state):
237250
@sdk_options()
238251
def _list(state, format=None):
239252
"""List available saved searches."""
253+
format_func = get_output_format_func(format)
240254
response = state.sdk.securitydata.savedsearches.get()
241255
result = response["searches"]
242256
if result:
243-
output = format(result, _HEADER_KEYS_MAP)
257+
output = format_func(result, _HEADER_KEYS_MAP)
244258
echo(output)
245259

246260

0 commit comments

Comments
 (0)