From e18186b5e21be556aa7036d912b1d53b04a62a23 Mon Sep 17 00:00:00 2001 From: ozgen Date: Mon, 30 Nov 2026 10:10:56 +0100 Subject: [PATCH] add: support file search exclusions in agent GMP commands --- gvm/protocols/gmp/_gmpnext.py | 6 + gvm/protocols/gmp/requests/next/_agents.py | 42 ++++++ ...est_modify_agent_controller_scan_config.py | 136 ++++++++++++++++++ .../entities/agents/test_modify_agents.py | 122 ++++++++++++++++ 4 files changed, 306 insertions(+) diff --git a/gvm/protocols/gmp/_gmpnext.py b/gvm/protocols/gmp/_gmpnext.py index 362f0a4a..62a2b2d5 100644 --- a/gvm/protocols/gmp/_gmpnext.py +++ b/gvm/protocols/gmp/_gmpnext.py @@ -133,6 +133,9 @@ def modify_agents( authorized: Whether the agent is authorized. update_to_latest: Whether the agent is allowed to update to latest automatically. config: Nested config for Agent Controller. + Optional file_search.excluded_paths contains a sequence of + path strings. Use an empty sequence to clear excluded paths. + Paths are sent as provided. comment: Optional comment for the change. """ return self._send_request_and_transform_response( @@ -166,6 +169,9 @@ def modify_agent_control_scan_config( Args: agent_control_id: The agent control UUID. config: Nested config for Agent Controller. + Optional agent_defaults.file_search.excluded_paths contains + a sequence of path strings. Use an empty sequence to clear + excluded paths. Paths are sent as provided. """ return self._send_request_and_transform_response( Agents.modify_agent_control_scan_config( diff --git a/gvm/protocols/gmp/requests/next/_agents.py b/gvm/protocols/gmp/requests/next/_agents.py index 8d43d6a0..c6f52711 100644 --- a/gvm/protocols/gmp/requests/next/_agents.py +++ b/gvm/protocols/gmp/requests/next/_agents.py @@ -114,6 +114,26 @@ def valid_value(d: Mapping[str, Any], key: str, path: str) -> Any: valid_value(hb, "interval_in_seconds", "heartbeat.") valid_value(hb, "miss_until_inactive", "heartbeat.") + if "file_search" in config: + file_search = valid_map(config, "file_search", "") + if "excluded_paths" in file_search: + excluded_paths = file_search["excluded_paths"] + if not isinstance(excluded_paths, Sequence) or isinstance( + excluded_paths, (str, bytes) + ): + raise InvalidArgumentType( + function=caller, + argument="config.file_search.excluded_paths", + arg_type="Sequence[str]", + ) + for path in excluded_paths: + if not isinstance(path, str): + raise InvalidArgumentType( + function=caller, + argument="config.file_search.excluded_paths", + arg_type="str", + ) + @classmethod def _validate_config_defaults( cls, config_defaults: Mapping[str, Any], *, caller: str @@ -195,6 +215,9 @@ def _append_agent_config( "interval_in_seconds": 300, "miss_until_inactive": 1, }, + "file_search": { + "excluded_paths": ["/opt/private"], + }, } Args: @@ -202,6 +225,9 @@ def _append_agent_config( should be appended. config: Mapping containing the agent configuration fields to serialize. + The optional file_search.excluded_paths field accepts a + sequence of path strings. An empty sequence clears exclusions. + Path text is sent without extra backslash conversion. wrapper_tag: Optional wrapper element name. If None, fields are appended directly to parent. """ @@ -265,6 +291,16 @@ def _append_agent_config( xml_hb, "miss_until_inactive", hb.get("miss_until_inactive") ) + if "file_search" in config: + file_search = config["file_search"] + xml_file_search = xml_config.add_element("file_search") + if "excluded_paths" in file_search: + xml_excluded_paths = xml_file_search.add_element( + "excluded_paths", attrs={"is_list": "1"} + ) + for path in file_search["excluded_paths"]: + cls._add_element(xml_excluded_paths, "item", path) + @classmethod def _append_config_defaults( cls, parent, config_defaults: Mapping[str, Any] @@ -352,6 +388,9 @@ def modify_agents( "interval_in_seconds": 300, "miss_until_inactive": 1, }, + "file_search": { + "excluded_paths": ["/opt/private"], + }, } comment: Optional comment for the change. """ @@ -443,6 +482,9 @@ def modify_agent_control_scan_config( "interval_in_seconds": 300, "miss_until_inactive": 1, }, + "file_search": { + "excluded_paths": ["/opt/private"], + }, }, "agent_control_defaults": { "update_to_latest": False, diff --git a/tests/protocols/gmpnext/entities/agents/test_modify_agent_controller_scan_config.py b/tests/protocols/gmpnext/entities/agents/test_modify_agent_controller_scan_config.py index 93d7dd29..48110566 100644 --- a/tests/protocols/gmpnext/entities/agents/test_modify_agent_controller_scan_config.py +++ b/tests/protocols/gmpnext/entities/agents/test_modify_agent_controller_scan_config.py @@ -2,6 +2,8 @@ # # SPDX-License-Identifier: GPL-3.0-or-later +from copy import deepcopy + from gvm.errors import InvalidArgumentType, RequiredArgument @@ -933,3 +935,137 @@ def test_modify_agent_control_scan_config_wrong_update_to_latest_raises( self.gmp.modify_agent_control_scan_config( "fb6451bf-ec5a-45a8-8bab-5cf4b862e51b", config=cfg ) + + def test_modify_agent_control_scan_config_file_search_serialization(self): + agent_config = { + "agent_control": { + "retry": { + "attempts": 6, + "delay_in_seconds": 60, + "max_jitter_in_seconds": 10, + } + }, + "agent_script_executor": { + "bulk_size": 2, + "bulk_throttle_time_in_ms": 300, + "indexer_dir_depth": 100, + "scheduler_cron_time": [ + {"cron": "0 */12 * * *", "max_delay_seconds": 30} + ], + }, + "heartbeat": {"interval_in_seconds": 300, "miss_until_inactive": 1}, + } + + cases = ( + (None, b""), + ({}, b""), + ( + {"excluded_paths": []}, + b'', + ), + ( + { + "excluded_paths": [ + r"C:\ProgramData\Example\Cache", + r"C:\\ProgramData\\Example\\Cache", + r"\\server\share", + "/opt/ & cache", + ] + }, + b'' + rb"C:\ProgramData\Example\Cache" + rb"C:\\ProgramData\\Example\\Cache" + rb"\\server\share" + b"/opt/<private> & cache" + b"", + ), + ) + for file_search, expected_xml in cases: + with self.subTest(file_search=file_search): + config = deepcopy(agent_config) + if file_search is not None: + config["file_search"] = deepcopy(file_search) + unchanged = deepcopy(config) + self.gmp.modify_agent_control_scan_config( + "3b4be213-281f-49ee-b457-5a5f34f71510", + config={ + "agent_defaults": config, + "agent_control_defaults": {"update_to_latest": False}, + }, + ) + + self.connection.send.has_been_called_with( + b'' + b"" + b"" + b"6" + b"60" + b"10" + b"" + b"" + b"2" + b"300" + b"100" + b'' + b"0 */12 * * *" + b"30" + b"" + b"" + b"300" + b"1" + + expected_xml + + b"" + b"0" + b"" + b"" + ) + self.assertEqual(config, unchanged) + + def test_modify_agent_control_scan_config_invalid_file_search_raises(self): + agent_config = { + "agent_control": { + "retry": { + "attempts": 6, + "delay_in_seconds": 60, + "max_jitter_in_seconds": 10, + } + }, + "agent_script_executor": { + "bulk_size": 2, + "bulk_throttle_time_in_ms": 300, + "indexer_dir_depth": 100, + "scheduler_cron_time": [ + {"cron": "0 */12 * * *", "max_delay_seconds": 30} + ], + }, + "heartbeat": {"interval_in_seconds": 300, "miss_until_inactive": 1}, + } + + invalid_values = ( + (None, RequiredArgument), + ("not-a-mapping", RequiredArgument), + ([], RequiredArgument), + ({"excluded_paths": None}, InvalidArgumentType), + ({"excluded_paths": "/opt/private"}, InvalidArgumentType), + ({"excluded_paths": b"/opt/private"}, InvalidArgumentType), + ({"excluded_paths": 42}, InvalidArgumentType), + ({"excluded_paths": {"path": "/opt/private"}}, InvalidArgumentType), + ({"excluded_paths": [42]}, InvalidArgumentType), + ({"excluded_paths": [None]}, InvalidArgumentType), + ({"excluded_paths": ["/opt/private", False]}, InvalidArgumentType), + ) + for file_search, error in invalid_values: + with self.subTest(file_search=file_search): + config = deepcopy(agent_config) + config["file_search"] = file_search + with self.assertRaises(error): + self.gmp.modify_agent_control_scan_config( + "3b4be213-281f-49ee-b457-5a5f34f71510", + config={ + "agent_defaults": config, + "agent_control_defaults": { + "update_to_latest": False + }, + }, + ) + self.connection.send.has_not_been_called() diff --git a/tests/protocols/gmpnext/entities/agents/test_modify_agents.py b/tests/protocols/gmpnext/entities/agents/test_modify_agents.py index 0604f500..9e27fde9 100644 --- a/tests/protocols/gmpnext/entities/agents/test_modify_agents.py +++ b/tests/protocols/gmpnext/entities/agents/test_modify_agents.py @@ -2,6 +2,8 @@ # # SPDX-License-Identifier: GPL-3.0-or-later +from copy import deepcopy + from gvm.errors import InvalidArgumentType, RequiredArgument @@ -669,3 +671,123 @@ def test_modify_agents_scheduler_item_not_mapping_raises(self): with self.assertRaises(InvalidArgumentType): self.gmp.modify_agents(agent_ids=["agent-123"], config=cfg) + + def test_modify_agents_file_search_serialization(self): + agent_config = { + "agent_control": { + "retry": { + "attempts": 6, + "delay_in_seconds": 60, + "max_jitter_in_seconds": 10, + } + }, + "agent_script_executor": { + "bulk_size": 2, + "bulk_throttle_time_in_ms": 300, + "indexer_dir_depth": 100, + "scheduler_cron_time": [ + {"cron": "0 */12 * * *", "max_delay_seconds": 30} + ], + }, + "heartbeat": {"interval_in_seconds": 300, "miss_until_inactive": 1}, + } + + cases = ( + (None, b""), + ({}, b""), + ( + {"excluded_paths": []}, + b'', + ), + ( + { + "excluded_paths": [ + r"C:\ProgramData\Example\Cache", + r"C:\\ProgramData\\Example\\Cache", + r"\\server\share", + "/opt/ & cache", + ] + }, + b'' + rb"C:\ProgramData\Example\Cache" + rb"C:\\ProgramData\\Example\\Cache" + rb"\\server\share" + b"/opt/<private> & cache" + b"", + ), + ) + for file_search, expected_xml in cases: + with self.subTest(file_search=file_search): + config = deepcopy(agent_config) + if file_search is not None: + config["file_search"] = deepcopy(file_search) + unchanged = deepcopy(config) + self.gmp.modify_agents(agent_ids=["agent-123"], config=config) + + self.connection.send.has_been_called_with( + b"" + b'' + b"" + b"" + b"6" + b"60" + b"10" + b"" + b"" + b"2" + b"300" + b"100" + b'' + b"0 */12 * * *" + b"30" + b"" + b"" + b"300" + b"1" + + expected_xml + + b"" + ) + self.assertEqual(config, unchanged) + + def test_modify_agents_invalid_file_search_raises(self): + agent_config = { + "agent_control": { + "retry": { + "attempts": 6, + "delay_in_seconds": 60, + "max_jitter_in_seconds": 10, + } + }, + "agent_script_executor": { + "bulk_size": 2, + "bulk_throttle_time_in_ms": 300, + "indexer_dir_depth": 100, + "scheduler_cron_time": [ + {"cron": "0 */12 * * *", "max_delay_seconds": 30} + ], + }, + "heartbeat": {"interval_in_seconds": 300, "miss_until_inactive": 1}, + } + + invalid_values = ( + (None, RequiredArgument), + ("not-a-mapping", RequiredArgument), + ([], RequiredArgument), + ({"excluded_paths": None}, InvalidArgumentType), + ({"excluded_paths": "/opt/private"}, InvalidArgumentType), + ({"excluded_paths": b"/opt/private"}, InvalidArgumentType), + ({"excluded_paths": 42}, InvalidArgumentType), + ({"excluded_paths": {"path": "/opt/private"}}, InvalidArgumentType), + ({"excluded_paths": [42]}, InvalidArgumentType), + ({"excluded_paths": [None]}, InvalidArgumentType), + ({"excluded_paths": ["/opt/private", False]}, InvalidArgumentType), + ) + for file_search, error in invalid_values: + with self.subTest(file_search=file_search): + config = deepcopy(agent_config) + config["file_search"] = file_search + with self.assertRaises(error): + self.gmp.modify_agents( + agent_ids=["agent-123"], config=config + ) + self.connection.send.has_not_been_called()