From 23f59d86b25ca233d125f2da5436f11f16773346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kira=20No=C3=ABl?= <129797218+kenoel@users.noreply.github.com> Date: Fri, 26 May 2023 09:39:27 -0400 Subject: [PATCH] [BUGFIX] Remove online state enrichment in describe_devices() by default (#100) * Default describe_devices() online state enrichment to false * bump version to 0.3.1 --- caracara/modules/hosts/hosts.py | 15 ++++++++++----- pyproject.toml | 2 +- tests/unit_tests/test_hosts.py | 8 ++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/caracara/modules/hosts/hosts.py b/caracara/modules/hosts/hosts.py index 4c47aad..8a3d4d2 100644 --- a/caracara/modules/hosts/hosts.py +++ b/caracara/modules/hosts/hosts.py @@ -132,6 +132,7 @@ def describe_devices( self, filters: FalconFilter or str = None, online_state: Optional[Union[OnlineState, str]] = None, + enrich_with_online_state: Optional[bool] = False, ) -> Dict[str, Dict]: """Return a dictionary containing details for every device matching the provided filter. @@ -149,11 +150,14 @@ def describe_devices( self.logger.info("Describing devices according to the filter string %s", filters) device_ids = self.get_device_ids(filters) - # Collect state data - device_state_data = self.get_online_state(device_ids) + if enrich_with_online_state: + # Collect state data + device_state_data = self.get_online_state(device_ids) # Filter by online state, if applicable. if online_state is not None: + if not enrich_with_online_state: + device_state_data = self.get_online_state(device_ids) self.validate_online_state(online_state) device_ids = list(filter( lambda key: device_state_data[key]["state"] == online_state, @@ -162,9 +166,10 @@ def describe_devices( device_data = self.get_device_data(device_ids) - # Enrich the results with the online state field - for device_id, data in device_data.items(): - data["state"] = device_state_data[device_id]["state"] + if enrich_with_online_state: + # Enrich the results with the online state field + for device_id, data in device_data.items(): + data["state"] = device_state_data[device_id]["state"] return device_data diff --git a/pyproject.toml b/pyproject.toml index c57b709..25b6060 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "caracara" -version = "0.3.0" +version = "0.3.1" description = "The CrowdStrike Falcon Developer Toolkit" authors = [ "CrowdStrike " ] readme = "README.md" diff --git a/tests/unit_tests/test_hosts.py b/tests/unit_tests/test_hosts.py index b8f6fc8..db87141 100644 --- a/tests/unit_tests/test_hosts.py +++ b/tests/unit_tests/test_hosts.py @@ -151,8 +151,7 @@ def test_describe_devices(auth: Client, **_): visible_devices = dict( (id_, dev) for id_, dev in mock_devices.items() if dev.get("host_hidden_status") != "hidden" ) - for device_id, data in visible_devices.items(): - data["state"] = mock_device_online_states[device_id]["state"] + assert auth.hosts.describe_devices() == visible_devices @@ -170,8 +169,6 @@ def test_describe_devices__online_only(auth: Client, **_): lambda item: item[0] in list(set(visible_ids) & set(online_ids)), mock_devices.items(), )) - for _, dev in online_visible_devices.items(): - dev["state"] = "online" assert auth.hosts.describe_devices(online_state="online") == online_visible_devices @@ -190,8 +187,7 @@ def test_describe_devices__enum_online_state(auth: Client, **_): lambda item: item[0] in list(set(visible_ids) & set(offline_ids)), mock_devices.items(), )) - for _, dev in offline_visible_devices.items(): - dev["state"] = "offline" + assert auth.hosts.describe_devices(online_state=OnlineState.OFFLINE) == offline_visible_devices