Skip to content

Commit f686aa9

Browse files
committed
add: add web application target support to python-gvm
Add request builders and client methods for web application targets. This includes create, modify, clone, delete, get, and list support, together with the related GMP request tests.
1 parent 90f68a3 commit f686aa9

11 files changed

Lines changed: 763 additions & 0 deletions

gvm/protocols/gmp/_gmpnext.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
ReportTlsCertificates,
3131
ReportVulnerabilities,
3232
Tasks,
33+
WebApplicationTargets,
3334
)
3435
from .requests.v224 import HostsOrdering
3536

@@ -1245,3 +1246,139 @@ def get_report_vulnerabilities(
12451246
details=details,
12461247
)
12471248
)
1249+
1250+
def create_web_application_target(
1251+
self,
1252+
name: str,
1253+
urls: list[str],
1254+
*,
1255+
comment: str | None = None,
1256+
exclude_urls: list[str] | None = None,
1257+
credential_id: EntityID | None = None,
1258+
) -> T:
1259+
"""Create a new web application target.
1260+
1261+
Args:
1262+
name: Name of the web application target.
1263+
urls: List of URLs to scan.
1264+
comment: Comment for the target.
1265+
exclude_urls: List of URLs to exclude from the scan.
1266+
credential_id: UUID of a credential to use on target.
1267+
"""
1268+
return self._send_request_and_transform_response(
1269+
WebApplicationTargets.create_web_application_target(
1270+
name=name,
1271+
urls=urls,
1272+
comment=comment,
1273+
exclude_urls=exclude_urls,
1274+
credential_id=credential_id,
1275+
)
1276+
)
1277+
1278+
def modify_web_application_target(
1279+
self,
1280+
web_application_target_id: EntityID,
1281+
*,
1282+
name: str | None = None,
1283+
comment: str | None = None,
1284+
urls: list[str] | None = None,
1285+
exclude_urls: list[str] | None = None,
1286+
credential_id: EntityID | None = None,
1287+
) -> T:
1288+
"""Modify an existing web application target.
1289+
1290+
Args:
1291+
web_application_target_id: UUID of target to modify.
1292+
name: Name of target.
1293+
comment: Comment on target.
1294+
urls: List of URLs to scan.
1295+
exclude_urls: List of URLs to exclude from the scan.
1296+
credential_id: UUID of credential to use on target.
1297+
"""
1298+
return self._send_request_and_transform_response(
1299+
WebApplicationTargets.modify_web_application_target(
1300+
web_application_target_id,
1301+
name=name,
1302+
comment=comment,
1303+
urls=urls,
1304+
exclude_urls=exclude_urls,
1305+
credential_id=credential_id,
1306+
)
1307+
)
1308+
1309+
def clone_web_application_target(
1310+
self, web_application_target_id: EntityID
1311+
) -> T:
1312+
"""Clone an existing web application target.
1313+
1314+
Args:
1315+
web_application_target_id: UUID of an existing web application target to clone.
1316+
"""
1317+
return self._send_request_and_transform_response(
1318+
WebApplicationTargets.clone_web_application_target(
1319+
web_application_target_id
1320+
)
1321+
)
1322+
1323+
def delete_web_application_target(
1324+
self,
1325+
web_application_target_id: EntityID,
1326+
*,
1327+
ultimate: bool | None = False,
1328+
) -> T:
1329+
"""Delete an existing web application target.
1330+
1331+
Args:
1332+
web_application_target_id: UUID of an existing web application target to delete.
1333+
ultimate: Whether to remove entirely or to the trashcan.
1334+
"""
1335+
return self._send_request_and_transform_response(
1336+
WebApplicationTargets.delete_web_application_target(
1337+
web_application_target_id,
1338+
ultimate=ultimate,
1339+
)
1340+
)
1341+
1342+
def get_web_application_target(
1343+
self,
1344+
web_application_target_id: EntityID,
1345+
*,
1346+
tasks: bool | None = None,
1347+
) -> T:
1348+
"""Request a single web application target.
1349+
1350+
Args:
1351+
web_application_target_id: UUID of the web application target to request.
1352+
tasks: Whether to include list of tasks that use the target.
1353+
"""
1354+
return self._send_request_and_transform_response(
1355+
WebApplicationTargets.get_web_application_target(
1356+
web_application_target_id,
1357+
tasks=tasks,
1358+
)
1359+
)
1360+
1361+
def get_web_application_targets(
1362+
self,
1363+
*,
1364+
filter_string: str | None = None,
1365+
filter_id: EntityID | None = None,
1366+
trash: bool | None = None,
1367+
tasks: bool | None = None,
1368+
) -> T:
1369+
"""Request a list of web application targets.
1370+
1371+
Args:
1372+
filter_string: Filter term to use for the query.
1373+
filter_id: UUID of an existing filter to use for the query.
1374+
trash: Whether to include targets in the trashcan.
1375+
tasks: Whether to include list of tasks that use the target.
1376+
"""
1377+
return self._send_request_and_transform_response(
1378+
WebApplicationTargets.get_web_application_targets(
1379+
filter_string=filter_string,
1380+
filter_id=filter_id,
1381+
trash=trash,
1382+
tasks=tasks,
1383+
)
1384+
)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
ReportVulnerabilities,
4646
)
4747
from gvm.protocols.gmp.requests.next._tasks import Tasks
48+
from gvm.protocols.gmp.requests.next._web_application_targets import (
49+
WebApplicationTargets,
50+
)
4851

4952
from .._entity_id import EntityID
5053
from .._version import Version
@@ -203,4 +206,5 @@
203206
"Users",
204207
"Version",
205208
"Vulnerabilities",
209+
"WebApplicationTargets",
206210
)
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# SPDX-FileCopyrightText: 2026 Greenbone AG
2+
#
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
#
5+
6+
from gvm.errors import RequiredArgument
7+
from gvm.protocols.core import Request
8+
from gvm.protocols.gmp.requests._entity_id import EntityID
9+
from gvm.utils import to_bool, to_comma_list
10+
from gvm.xml import XmlCommand
11+
12+
13+
class WebApplicationTargets:
14+
@classmethod
15+
def create_web_application_target(
16+
cls,
17+
name: str,
18+
urls: list[str],
19+
*,
20+
comment: str | None = None,
21+
exclude_urls: list[str] | None = None,
22+
credential_id: EntityID | None = None,
23+
) -> Request:
24+
"""Create a new web application target.
25+
26+
Args:
27+
name: Name of the target.
28+
urls: List of URLs to scan.
29+
comment: Comment for the target.
30+
exclude_urls: List of URLs to exclude from the scan.
31+
credential_id: UUID of a credential to use on target.
32+
"""
33+
if not name:
34+
raise RequiredArgument(
35+
function=cls.create_web_application_target.__name__,
36+
argument="name",
37+
)
38+
39+
if not urls:
40+
raise RequiredArgument(
41+
function=cls.create_web_application_target.__name__,
42+
argument="urls",
43+
)
44+
45+
cmd = XmlCommand("create_web_application_target")
46+
cmd.add_element("name", name)
47+
cmd.add_element("urls", to_comma_list(urls))
48+
49+
if comment:
50+
cmd.add_element("comment", comment)
51+
52+
if exclude_urls:
53+
cmd.add_element("exclude_urls", to_comma_list(exclude_urls))
54+
55+
if credential_id:
56+
cmd.add_element("credential", attrs={"id": str(credential_id)})
57+
58+
return cmd
59+
60+
@classmethod
61+
def modify_web_application_target(
62+
cls,
63+
web_application_target_id: EntityID,
64+
*,
65+
name: str | None = None,
66+
comment: str | None = None,
67+
urls: list[str] | None = None,
68+
exclude_urls: list[str] | None = None,
69+
credential_id: EntityID | None = None,
70+
) -> Request:
71+
"""Modify an existing web application target.
72+
73+
Args:
74+
web_application_target_id: UUID of target to modify.
75+
name: Name of target.
76+
comment: Comment on target.
77+
urls: List of URLs to scan.
78+
exclude_urls: List of URLs to exclude from the scan.
79+
credential_id: UUID of credential to use on target.
80+
"""
81+
if not web_application_target_id:
82+
raise RequiredArgument(
83+
function=cls.modify_web_application_target.__name__,
84+
argument="web_application_target_id",
85+
)
86+
87+
cmd = XmlCommand("modify_web_application_target")
88+
cmd.set_attribute(
89+
"web_application_target_id", str(web_application_target_id)
90+
)
91+
92+
if comment:
93+
cmd.add_element("comment", comment)
94+
95+
if name:
96+
cmd.add_element("name", name)
97+
98+
if urls:
99+
cmd.add_element("urls", to_comma_list(urls))
100+
101+
if exclude_urls:
102+
cmd.add_element("exclude_urls", to_comma_list(exclude_urls))
103+
104+
if credential_id:
105+
cmd.add_element("credential", attrs={"id": str(credential_id)})
106+
107+
return cmd
108+
109+
@classmethod
110+
def clone_web_application_target(
111+
cls, web_application_target_id: EntityID
112+
) -> Request:
113+
"""Clone an existing web application target.
114+
115+
Args:
116+
web_application_target_id: UUID of an existing target to clone.
117+
"""
118+
if not web_application_target_id:
119+
raise RequiredArgument(
120+
function=cls.clone_web_application_target.__name__,
121+
argument="web_application_target_id",
122+
)
123+
124+
cmd = XmlCommand("create_web_application_target")
125+
cmd.add_element("copy", str(web_application_target_id))
126+
return cmd
127+
128+
@classmethod
129+
def delete_web_application_target(
130+
cls,
131+
web_application_target_id: EntityID,
132+
*,
133+
ultimate: bool | None = False,
134+
) -> Request:
135+
"""Delete an existing web application target.
136+
137+
Args:
138+
web_application_target_id: UUID of an existing target to delete.
139+
ultimate: Whether to remove entirely or move to the trashcan.
140+
"""
141+
if not web_application_target_id:
142+
raise RequiredArgument(
143+
function=cls.delete_web_application_target.__name__,
144+
argument="web_application_target_id",
145+
)
146+
147+
cmd = XmlCommand("delete_web_application_target")
148+
cmd.set_attribute(
149+
"web_application_target_id", str(web_application_target_id)
150+
)
151+
cmd.set_attribute("ultimate", to_bool(ultimate))
152+
return cmd
153+
154+
@classmethod
155+
def get_web_application_target(
156+
cls,
157+
web_application_target_id: EntityID,
158+
*,
159+
tasks: bool | None = None,
160+
) -> Request:
161+
"""Request a single web application target.
162+
163+
Args:
164+
web_application_target_id: UUID of the target to request.
165+
tasks: Whether to include list of tasks that use the target.
166+
"""
167+
if not web_application_target_id:
168+
raise RequiredArgument(
169+
function=cls.get_web_application_target.__name__,
170+
argument="web_application_target_id",
171+
)
172+
173+
cmd = XmlCommand("get_web_application_targets")
174+
cmd.set_attribute(
175+
"web_application_target_id", str(web_application_target_id)
176+
)
177+
178+
if tasks is not None:
179+
cmd.set_attribute("tasks", to_bool(tasks))
180+
181+
return cmd
182+
183+
@classmethod
184+
def get_web_application_targets(
185+
cls,
186+
*,
187+
filter_string: str | None = None,
188+
filter_id: EntityID | None = None,
189+
trash: bool | None = None,
190+
tasks: bool | None = None,
191+
) -> Request:
192+
"""Request a list of web application targets.
193+
194+
Args:
195+
filter_string: Filter term to use for the query.
196+
filter_id: UUID of an existing filter to use for the query.
197+
trash: Whether to include targets in the trashcan.
198+
tasks: Whether to include list of tasks that use the target.
199+
"""
200+
cmd = XmlCommand("get_web_application_targets")
201+
cmd.add_filter(filter_string, filter_id)
202+
203+
if trash is not None:
204+
cmd.set_attribute("trash", to_bool(trash))
205+
206+
if tasks is not None:
207+
cmd.set_attribute("tasks", to_bool(tasks))
208+
209+
return cmd

0 commit comments

Comments
 (0)