Skip to content

Commit ddb86a7

Browse files
authored
Bugfix/securitydata search empty filter list (#378)
* fix empty filter list bug * changelog and version bump * style * fix test in CI * fix test in CI for real * try again * mock the profile name * actually use the mock state 🤦 * style
1 parent f606b30 commit ddb86a7

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
99
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.
1010

11+
## 1.14.2 - 2022-06-17
12+
13+
### Fixed
14+
15+
- Bug where the `code42 security-data search` command using a checkpoint and only the `--include-non-exposure` filter constructed an invalid search query.
16+
1117
## 1.14.1 - 2022-06-13
1218

1319
### Fixed

src/code42cli/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.14.1"
1+
__version__ = "1.14.2"

src/code42cli/cmds/securitydata.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from pprint import pformat
23

34
import click
@@ -474,6 +475,16 @@ def _construct_query(state, begin, end, saved_search, advanced_query, or_query):
474475
)
475476
if or_query:
476477
state.search_filters = convert_to_or_query(state.search_filters)
478+
479+
if not state.search_filters:
480+
# if a checkpoint and _only_ --include-non-exposure is passed, the filter list will be empty, which isn't a
481+
# valid query, so in that case we want to fallback to a 90 day (max event age) date range. The checkpoint will
482+
# still cause the query results to only contain events after the checkpointed event.
483+
_90_days = datetime.datetime.utcnow() - datetime.timedelta(days=90)
484+
timestamp = convert_datetime_to_timestamp(_90_days)
485+
state.search_filters.append(
486+
create_time_range_filter(f.EventTimestamp, timestamp, None)
487+
)
477488
query = FileEventQuery(*state.search_filters)
478489
query.page_size = MAX_EVENT_PAGE_SIZE
479490
query.sort_direction = "asc"

tests/cmds/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from tests.conftest import create_mock_response
1414
from tests.conftest import TEST_ID
1515

16+
from code42cli.cmds.search.cursor_store import FileEventCursorStore
1617
from code42cli.logger import CliLogger
1718

1819

@@ -61,6 +62,16 @@ def cli_state_without_user(sdk_without_user, cli_state):
6162
return cli_state
6263

6364

65+
@pytest.fixture
66+
def mock_file_event_checkpoint(mocker):
67+
mock_file_event_checkpointer = mocker.MagicMock(spec=FileEventCursorStore)
68+
mocker.patch(
69+
"code42cli.cmds.securitydata._get_file_event_cursor_store",
70+
return_value=mock_file_event_checkpointer,
71+
)
72+
return mock_file_event_checkpointer
73+
74+
6475
@pytest.fixture
6576
def custom_error(mocker):
6677
err = mocker.MagicMock(spec=HTTPError)

tests/cmds/test_securitydata.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33

4+
import pandas
45
import py42.sdk.queries.fileevents.filters as f
56
import pytest
67
from py42.sdk.queries.fileevents.file_event_query import FileEventQuery
@@ -1388,3 +1389,24 @@ def test_saved_search_list_with_format_option_does_not_return_when_response_is_e
13881389
cli, ["security-data", "saved-search", "list", "-f", "csv"], obj=cli_state
13891390
)
13901391
assert "Name,Id" not in result.output
1392+
1393+
1394+
def test_non_exposure_only_query_with_checkpoint_does_not_send_empty_filter_list(
1395+
runner, cli_state, mock_file_event_checkpoint, mocker
1396+
):
1397+
mock_file_event_checkpoint.get.return_value = "event_1234"
1398+
mock_get_all_file_events = mocker.patch(
1399+
"code42cli.cmds.securitydata._get_all_file_events"
1400+
)
1401+
1402+
def generator():
1403+
yield pandas.DataFrame()
1404+
1405+
mock_get_all_file_events.return_value = generator()
1406+
result = runner.invoke(
1407+
cli,
1408+
["security-data", "search", "--include-non-exposure", "-c", "checkpoint"],
1409+
obj=cli_state,
1410+
)
1411+
assert result.exit_code == 0
1412+
assert len(mock_get_all_file_events.call_args[0][1]._filter_group_list) > 0

0 commit comments

Comments
 (0)