Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gvm/protocols/gmp/_gmpnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
42 changes: 42 additions & 0 deletions gvm/protocols/gmp/requests/next/_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -195,13 +215,19 @@ def _append_agent_config(
"interval_in_seconds": 300,
"miss_until_inactive": 1,
},
"file_search": {
"excluded_paths": ["/opt/private"],
},
}

Args:
parent: The XML parent element to which the wrapper element
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.
"""
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from copy import deepcopy

from gvm.errors import InvalidArgumentType, RequiredArgument


Expand Down Expand Up @@ -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"<file_search/>"),
(
{"excluded_paths": []},
b'<file_search><excluded_paths is_list="1"/></file_search>',
),
(
{
"excluded_paths": [
r"C:\ProgramData\Example\Cache",
r"C:\\ProgramData\\Example\\Cache",
r"\\server\share",
"/opt/<private> & cache",
]
},
b'<file_search><excluded_paths is_list="1">'
rb"<item>C:\ProgramData\Example\Cache</item>"
rb"<item>C:\\ProgramData\\Example\\Cache</item>"
rb"<item>\\server\share</item>"
b"<item>/opt/&lt;private&gt; &amp; cache</item>"
b"</excluded_paths></file_search>",
),
)
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'<modify_agent_control_scan_config agent_control_id="3b4be213-281f-49ee-b457-5a5f34f71510">'
b"<config_defaults><agent_defaults>"
b"<agent_control><retry>"
b"<attempts>6</attempts>"
b"<delay_in_seconds>60</delay_in_seconds>"
b"<max_jitter_in_seconds>10</max_jitter_in_seconds>"
b"</retry></agent_control>"
b"<agent_script_executor>"
b"<bulk_size>2</bulk_size>"
b"<bulk_throttle_time_in_ms>300</bulk_throttle_time_in_ms>"
b"<indexer_dir_depth>100</indexer_dir_depth>"
b'<scheduler_cron_time is_list="1"><item>'
b"<cron>0 */12 * * *</cron>"
b"<max_delay_seconds>30</max_delay_seconds>"
b"</item></scheduler_cron_time>"
b"</agent_script_executor>"
b"<heartbeat><interval_in_seconds>300</interval_in_seconds>"
b"<miss_until_inactive>1</miss_until_inactive></heartbeat>"
+ expected_xml
+ b"</agent_defaults><agent_control_defaults>"
b"<update_to_latest>0</update_to_latest>"
b"</agent_control_defaults></config_defaults>"
b"</modify_agent_control_scan_config>"
)
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()
122 changes: 122 additions & 0 deletions tests/protocols/gmpnext/entities/agents/test_modify_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from copy import deepcopy

from gvm.errors import InvalidArgumentType, RequiredArgument


Expand Down Expand Up @@ -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"<file_search/>"),
(
{"excluded_paths": []},
b'<file_search><excluded_paths is_list="1"/></file_search>',
),
(
{
"excluded_paths": [
r"C:\ProgramData\Example\Cache",
r"C:\\ProgramData\\Example\\Cache",
r"\\server\share",
"/opt/<private> & cache",
]
},
b'<file_search><excluded_paths is_list="1">'
rb"<item>C:\ProgramData\Example\Cache</item>"
rb"<item>C:\\ProgramData\\Example\\Cache</item>"
rb"<item>\\server\share</item>"
b"<item>/opt/&lt;private&gt; &amp; cache</item>"
b"</excluded_paths></file_search>",
),
)
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"<modify_agent>"
b'<agents><agent id="agent-123"/></agents>'
b"<config>"
b"<agent_control><retry>"
b"<attempts>6</attempts>"
b"<delay_in_seconds>60</delay_in_seconds>"
b"<max_jitter_in_seconds>10</max_jitter_in_seconds>"
b"</retry></agent_control>"
b"<agent_script_executor>"
b"<bulk_size>2</bulk_size>"
b"<bulk_throttle_time_in_ms>300</bulk_throttle_time_in_ms>"
b"<indexer_dir_depth>100</indexer_dir_depth>"
b'<scheduler_cron_time is_list="1"><item>'
b"<cron>0 */12 * * *</cron>"
b"<max_delay_seconds>30</max_delay_seconds>"
b"</item></scheduler_cron_time>"
b"</agent_script_executor>"
b"<heartbeat><interval_in_seconds>300</interval_in_seconds>"
b"<miss_until_inactive>1</miss_until_inactive></heartbeat>"
+ expected_xml
+ b"</config></modify_agent>"
)
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()