Skip to content

Commit c9639b1

Browse files
committed
Change: update modify_agents and add modify_agent_controller_scan_config
1 parent ede60e9 commit c9639b1

File tree

5 files changed

+453
-39
lines changed

5 files changed

+453
-39
lines changed

gvm/protocols/gmp/_gmpnext.py

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
44

5-
from typing import Mapping, Optional, Sequence
5+
from typing import Any, Mapping, Optional, Sequence
66

77
from gvm.protocols.gmp.requests import EntityID
88

@@ -114,28 +114,42 @@ def modify_agents(
114114
agent_ids: list[EntityID],
115115
*,
116116
authorized: Optional[bool] = None,
117-
min_interval: Optional[int] = None,
118-
heartbeat_interval: Optional[int] = None,
119-
schedule: Optional[str] = None,
117+
config: Optional[Mapping[str, Any]] = None,
120118
comment: Optional[str] = None,
121119
) -> T:
122-
"""Modify multiple agents
120+
"""
121+
Modify multiple agents.
123122
124123
Args:
125-
agent_ids: List of agent UUIDs to modify
126-
authorized: Whether the agent is authorized
127-
min_interval: Minimum scan interval
128-
heartbeat_interval: Interval for sending heartbeats
129-
schedule: Cron-style schedule for agent
130-
comment: Comment for the agents
124+
agent_ids: List of agent UUIDs to modify.
125+
authorized: Whether the agent is authorized (writes <authorized>1/0</authorized>).
126+
config: Nested config matching the new schema, e.g.:
127+
{
128+
"agent_control": {
129+
"retry": {
130+
"attempts": 6,
131+
"delay_in_seconds": 60,
132+
"max_jitter_in_seconds": 10,
133+
}
134+
},
135+
"agent_script_executor": {
136+
"bulk_size": 2,
137+
"bulk_throttle_time_in_ms": 300,
138+
"indexer_dir_depth": 100,
139+
"scheduler_cron_time": ["0 */12 * * *"], # str or list[str]
140+
},
141+
"heartbeat": {
142+
"interval_in_seconds": 300,
143+
"miss_until_inactive": 1,
144+
},
145+
}
146+
comment: Optional comment for the change.
131147
"""
132148
return self._send_request_and_transform_response(
133149
Agents.modify_agents(
134150
agent_ids=agent_ids,
135151
authorized=authorized,
136-
min_interval=min_interval,
137-
heartbeat_interval=heartbeat_interval,
138-
schedule=schedule,
152+
config=config,
139153
comment=comment,
140154
)
141155
)
@@ -150,6 +164,46 @@ def delete_agents(self, agent_ids: list[EntityID]) -> T:
150164
Agents.delete_agents(agent_ids=agent_ids)
151165
)
152166

167+
def modify_agent_control_scan_config(
168+
self,
169+
agent_control_id: EntityID,
170+
config: Mapping[str, Any],
171+
) -> T:
172+
"""
173+
Modify agent control scan config.
174+
175+
Args:
176+
agent_control_id: The agent control UUID.
177+
config: Nested config, e.g.:
178+
{
179+
"agent_control": {
180+
"retry": {
181+
"attempts": 6,
182+
"delay_in_seconds": 60,
183+
"max_jitter_in_seconds": 10,
184+
}
185+
},
186+
"agent_script_executor": {
187+
"bulk_size": 2,
188+
"bulk_throttle_time_in_ms": 300,
189+
"indexer_dir_depth": 100,
190+
"scheduler_cron_time": ["0 */12 * * *"], # str or list[str]
191+
},
192+
"heartbeat": {
193+
"interval_in_seconds": 300,
194+
"miss_until_inactive": 1,
195+
},
196+
}
197+
Returns:
198+
Request: Prepared XML command.
199+
"""
200+
return self._send_request_and_transform_response(
201+
Agents.modify_agent_control_scan_config(
202+
agent_control_id=agent_control_id,
203+
config=config,
204+
)
205+
)
206+
153207
def get_agent_groups(
154208
self,
155209
*,

gvm/protocols/gmp/requests/next/_agents.py

Lines changed: 136 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
44

5-
from typing import Optional
5+
from typing import Any, Mapping, Optional
66

77
from gvm.errors import RequiredArgument
88
from gvm.protocols.core import Request
@@ -12,6 +12,57 @@
1212

1313

1414
class Agents:
15+
16+
@staticmethod
17+
def _add_el(parent, name: str, value) -> None:
18+
if value is not None:
19+
parent.add_element(name, str(value))
20+
21+
@classmethod
22+
def _append_agent_config(cls, parent, config: Mapping[str, Any]) -> None:
23+
xml_config = parent.add_element("config")
24+
25+
# agent_control.retry
26+
ac = config["agent_control"]
27+
retry = ac["retry"]
28+
xml_ac = xml_config.add_element("agent_control")
29+
xml_retry = xml_ac.add_element("retry")
30+
cls._add_el(xml_retry, "attempts", retry.get("attempts"))
31+
cls._add_el(
32+
xml_retry, "delay_in_seconds", retry.get("delay_in_seconds")
33+
)
34+
cls._add_el(
35+
xml_retry,
36+
"max_jitter_in_seconds",
37+
retry.get("max_jitter_in_seconds"),
38+
)
39+
40+
# agent_script_executor
41+
se = config["agent_script_executor"]
42+
xml_se = xml_config.add_element("agent_script_executor")
43+
cls._add_el(xml_se, "bulk_size", se.get("bulk_size"))
44+
cls._add_el(
45+
xml_se,
46+
"bulk_throttle_time_in_ms",
47+
se.get("bulk_throttle_time_in_ms"),
48+
)
49+
cls._add_el(xml_se, "indexer_dir_depth", se.get("indexer_dir_depth"))
50+
sched = se.get("scheduler_cron_time")
51+
if sched:
52+
xml_sched = xml_se.add_element("scheduler_cron_time")
53+
for item in sched:
54+
xml_sched.add_element("item", str(item))
55+
56+
# heartbeat
57+
hb = config["heartbeat"]
58+
xml_hb = xml_config.add_element("heartbeat")
59+
cls._add_el(
60+
xml_hb, "interval_in_seconds", hb.get("interval_in_seconds")
61+
)
62+
cls._add_el(
63+
xml_hb, "miss_until_inactive", hb.get("miss_until_inactive")
64+
)
65+
1566
@classmethod
1667
def get_agents(
1768
cls,
@@ -41,20 +92,36 @@ def modify_agents(
4192
agent_ids: list[EntityID],
4293
*,
4394
authorized: Optional[bool] = None,
44-
min_interval: Optional[int] = None,
45-
heartbeat_interval: Optional[int] = None,
46-
schedule: Optional[str] = None,
95+
config: Optional[Mapping[str, Any]] = None,
4796
comment: Optional[str] = None,
4897
) -> Request:
49-
"""Modify multiple agents
98+
"""
99+
Modify multiple agents.
50100
51101
Args:
52-
agent_ids: List of agent UUIDs to modify
53-
authorized: Whether the agent is authorized
54-
min_interval: Minimum scan interval
55-
heartbeat_interval: Interval for sending heartbeats
56-
schedule: Cron-style schedule for agent
57-
comment: Comment for the agents
102+
agent_ids: List of agent UUIDs to modify.
103+
authorized: Whether the agent is authorized.
104+
config: Nested config matching the new schema, e.g.:
105+
{
106+
"agent_control": {
107+
"retry": {
108+
"attempts": 6,
109+
"delay_in_seconds": 60,
110+
"max_jitter_in_seconds": 10,
111+
}
112+
},
113+
"agent_script_executor": {
114+
"bulk_size": 2,
115+
"bulk_throttle_time_in_ms": 300,
116+
"indexer_dir_depth": 100,
117+
"scheduler_cron_time": ["0 */12 * * *"], # list[str]
118+
},
119+
"heartbeat": {
120+
"interval_in_seconds": 300,
121+
"miss_until_inactive": 1,
122+
},
123+
}
124+
comment: Optional comment for the change.
58125
"""
59126
if not agent_ids:
60127
raise RequiredArgument(
@@ -69,12 +136,10 @@ def modify_agents(
69136

70137
if authorized is not None:
71138
cmd.add_element("authorized", to_bool(authorized))
72-
if min_interval is not None:
73-
cmd.add_element("min_interval", str(min_interval))
74-
if heartbeat_interval is not None:
75-
cmd.add_element("heartbeat_interval", str(heartbeat_interval))
76-
if schedule:
77-
cmd.add_element("schedule", schedule)
139+
140+
if config is not None:
141+
cls._append_agent_config(cmd, config)
142+
78143
if comment:
79144
cmd.add_element("comment", comment)
80145

@@ -99,3 +164,57 @@ def delete_agents(cls, agent_ids: list[EntityID]) -> Request:
99164
xml_agents.add_element("agent", attrs={"id": agent_id})
100165

101166
return cmd
167+
168+
@classmethod
169+
def modify_agent_control_scan_config(
170+
cls,
171+
agent_control_id: EntityID,
172+
config: Mapping[str, Any],
173+
) -> Request:
174+
"""
175+
Modify agent control scan config.
176+
177+
Args:
178+
agent_control_id: The agent control UUID.
179+
config: Nested config, e.g.:
180+
{
181+
"agent_control": {
182+
"retry": {
183+
"attempts": 6,
184+
"delay_in_seconds": 60,
185+
"max_jitter_in_seconds": 10,
186+
}
187+
},
188+
"agent_script_executor": {
189+
"bulk_size": 2,
190+
"bulk_throttle_time_in_ms": 300,
191+
"indexer_dir_depth": 100,
192+
"scheduler_cron_time": ["0 */12 * * *"], # str or list[str]
193+
},
194+
"heartbeat": {
195+
"interval_in_seconds": 300,
196+
"miss_until_inactive": 1,
197+
},
198+
}
199+
Returns:
200+
Request: Prepared XML command.
201+
"""
202+
if not agent_control_id:
203+
raise RequiredArgument(
204+
function=cls.modify_agent_control_scan_config.__name__,
205+
argument="agent_control_id",
206+
)
207+
if not config:
208+
raise RequiredArgument(
209+
function=cls.modify_agent_control_scan_config.__name__,
210+
argument="config",
211+
)
212+
213+
cmd = XmlCommand(
214+
"modify_agent_control_scan_config",
215+
)
216+
cmd.set_attribute("agent_control_id", str(agent_control_id))
217+
218+
cls._append_agent_config(cmd, config)
219+
220+
return cmd

0 commit comments

Comments
 (0)