22#
33# SPDX-License-Identifier: GPL-3.0-or-later
44
5- from typing import Optional
5+ from typing import Any , Mapping , Optional
66
77from gvm .errors import RequiredArgument
88from gvm .protocols .core import Request
1212
1313
1414class 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