| Total Complexity | 785 |
| Total Lines | 5872 |
| Duplicated Lines | 16.14 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like gvm.protocols.gmpv7 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | # Copyright (C) 2018 Greenbone Networks GmbH |
||
| 3 | # |
||
| 4 | # SPDX-License-Identifier: GPL-3.0-or-later |
||
| 5 | # |
||
| 6 | # This program is free software: you can redistribute it and/or modify |
||
| 7 | # it under the terms of the GNU General Public License as published by |
||
| 8 | # the Free Software Foundation, either version 3 of the License, or |
||
| 9 | # (at your option) any later version. |
||
| 10 | # |
||
| 11 | # This program is distributed in the hope that it will be useful, |
||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 14 | # GNU General Public License for more details. |
||
| 15 | # |
||
| 16 | # You should have received a copy of the GNU General Public License |
||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 18 | |||
| 19 | # pylint: disable=too-many-lines,redefined-builtin |
||
| 20 | """ |
||
| 21 | Module for communication with gvmd in `Greenbone Management Protocol version 7`_ |
||
| 22 | |||
| 23 | .. _Greenbone Management Protocol version 7: |
||
| 24 | https://docs.greenbone.net/API/GMP/gmp-7.0.html |
||
| 25 | """ |
||
| 26 | import base64 |
||
| 27 | import collections |
||
| 28 | import logging |
||
| 29 | import numbers |
||
| 30 | |||
| 31 | from lxml import etree |
||
| 32 | |||
| 33 | from gvm.errors import InvalidArgument, RequiredArgument |
||
| 34 | from gvm.utils import get_version_string, deprecation |
||
| 35 | from gvm.xml import XmlCommand, create_parser |
||
| 36 | |||
| 37 | from .base import GvmProtocol |
||
| 38 | |||
| 39 | logger = logging.getLogger(__name__) |
||
| 40 | |||
| 41 | PROTOCOL_VERSION = (7,) |
||
| 42 | |||
| 43 | FILTER_TYPES = ( |
||
| 44 | "agent", |
||
| 45 | "alert", |
||
| 46 | "asset", |
||
| 47 | "config", |
||
| 48 | "credential", |
||
| 49 | "filter", |
||
| 50 | "group", |
||
| 51 | "note", |
||
| 52 | "override", |
||
| 53 | "permission", |
||
| 54 | "port_list", |
||
| 55 | "report", |
||
| 56 | "report_format", |
||
| 57 | "result", |
||
| 58 | "role", |
||
| 59 | "schedule", |
||
| 60 | "secinfo", |
||
| 61 | "tag", |
||
| 62 | "target", |
||
| 63 | "task", |
||
| 64 | "user", |
||
| 65 | ) |
||
| 66 | |||
| 67 | TIME_UNITS = ( |
||
| 68 | "second", |
||
| 69 | "minute", |
||
| 70 | "hour", |
||
| 71 | "day", |
||
| 72 | "week", |
||
| 73 | "month", |
||
| 74 | "year", |
||
| 75 | "decade", |
||
| 76 | ) |
||
| 77 | |||
| 78 | ALIVE_TESTS = ( |
||
| 79 | "Consider Alive", |
||
| 80 | "ICMP, TCP-ACK Service & ARP Ping", |
||
| 81 | "TCP-ACK Service & ARP Ping", |
||
| 82 | "ICMP & ARP Ping", |
||
| 83 | "ICMP & TCP-ACK Service Ping", |
||
| 84 | "ARP Ping", |
||
| 85 | "TCP-ACK Service Ping", |
||
| 86 | "TCP-SYN Service Ping", |
||
| 87 | "ICMP Ping", |
||
| 88 | "Scan Config Default", |
||
| 89 | ) |
||
| 90 | |||
| 91 | CREDENTIAL_TYPES = ("cc", "snmp", "up", "usk") |
||
| 92 | |||
| 93 | OSP_SCANNER_TYPE = "1" |
||
| 94 | OPENVAS_SCANNER_TYPE = "2" |
||
| 95 | CVE_SCANNER_TYPE = "3" |
||
| 96 | GMP_SCANNER_TYPE = "4" # formerly slave scanner |
||
| 97 | |||
| 98 | SCANNER_TYPES = ( |
||
| 99 | OSP_SCANNER_TYPE, |
||
| 100 | OPENVAS_SCANNER_TYPE, |
||
| 101 | CVE_SCANNER_TYPE, |
||
| 102 | GMP_SCANNER_TYPE, |
||
| 103 | ) |
||
| 104 | |||
| 105 | ALERT_EVENTS = ("Task run status changed",) |
||
| 106 | |||
| 107 | ALERT_EVENTS_SECINFO = ("Updated SecInfo arrived", "New SecInfo arrived") |
||
| 108 | |||
| 109 | ALERT_CONDITIONS = ( |
||
| 110 | "Always", |
||
| 111 | "Severity at least", |
||
| 112 | "Filter count changed", |
||
| 113 | "Filter count at least", |
||
| 114 | ) |
||
| 115 | |||
| 116 | ALERT_CONDITIONS_SECINFO = ("Always",) |
||
| 117 | |||
| 118 | ALERT_METHODS = ( |
||
| 119 | "SCP", |
||
| 120 | "Send", |
||
| 121 | "SMB", |
||
| 122 | "SNMP", |
||
| 123 | "Syslog", |
||
| 124 | "Email", |
||
| 125 | "Start Task", |
||
| 126 | "HTTP Get", |
||
| 127 | "Sourcefire Connector", |
||
| 128 | "verinice Connector", |
||
| 129 | ) |
||
| 130 | |||
| 131 | ALERT_METHODS_SECINFO = ("SCP", "Send", "SMB", "SNMP", "Syslog", "Email") |
||
| 132 | |||
| 133 | ASSET_TYPES = ("host", "os") |
||
| 134 | |||
| 135 | INFO_TYPES = ( |
||
| 136 | "CERT_BUND_ADV", |
||
| 137 | "CPE", |
||
| 138 | "CVE", |
||
| 139 | "DFN_CERT_ADV", |
||
| 140 | "OVALDEF", |
||
| 141 | "NVT", |
||
| 142 | "ALLINFO", |
||
| 143 | ) |
||
| 144 | |||
| 145 | THREAD_TYPES = ("High", "Medium", "Low", "Alarm", "Log", "Debug") |
||
| 146 | |||
| 147 | SUBJECT_TYPES = ("user", "group", "role") |
||
| 148 | |||
| 149 | RESOURCE_TYPES = ( |
||
| 150 | "alert", |
||
| 151 | "allinfo", |
||
| 152 | "cert_bund_adv", |
||
| 153 | "cpe", |
||
| 154 | "cve", |
||
| 155 | "dfn_cert_adv", |
||
| 156 | "host", |
||
| 157 | "note", |
||
| 158 | "nvt", |
||
| 159 | "os", |
||
| 160 | "ovaldef", |
||
| 161 | "override", |
||
| 162 | "report", |
||
| 163 | "result", |
||
| 164 | "task", |
||
| 165 | "vuln", |
||
| 166 | ) |
||
| 167 | |||
| 168 | |||
| 169 | def _check_command_status(xml): |
||
| 170 | """Check gmp response |
||
| 171 | |||
| 172 | Look into the gmp response and check for the status in the root element |
||
| 173 | |||
| 174 | Arguments: |
||
| 175 | xml {string} -- XML-Source |
||
| 176 | |||
| 177 | Returns: |
||
| 178 | bool -- True if valid, otherwise False |
||
| 179 | """ |
||
| 180 | |||
| 181 | if xml is 0 or xml is None: |
||
|
|
|||
| 182 | logger.error("XML Command is empty") |
||
| 183 | return False |
||
| 184 | |||
| 185 | try: |
||
| 186 | root = etree.XML(xml, parser=create_parser()) |
||
| 187 | status = root.attrib["status"] |
||
| 188 | return status is not None and status[0] == "2" |
||
| 189 | |||
| 190 | except etree.Error as e: |
||
| 191 | logger.error("etree.XML(xml): %s", e) |
||
| 192 | return False |
||
| 193 | |||
| 194 | |||
| 195 | def _to_bool(value): |
||
| 196 | return "1" if value else "0" |
||
| 197 | |||
| 198 | |||
| 199 | def _to_base64(value): |
||
| 200 | return base64.b64encode(value.encode("utf-8")) |
||
| 201 | |||
| 202 | |||
| 203 | def _to_comma_list(value): |
||
| 204 | return ",".join(value) |
||
| 205 | |||
| 206 | |||
| 207 | def _add_filter(cmd, filter, filter_id): |
||
| 208 | if filter: |
||
| 209 | cmd.set_attribute("filter", filter) |
||
| 210 | |||
| 211 | if filter_id: |
||
| 212 | cmd.set_attribute("filt_id", filter_id) |
||
| 213 | |||
| 214 | |||
| 215 | def _check_event(event, condition, method): |
||
| 216 | if event in ALERT_EVENTS: |
||
| 217 | if condition not in ALERT_CONDITIONS: |
||
| 218 | raise InvalidArgument("Invalid condition for event") |
||
| 219 | if method not in ALERT_METHODS: |
||
| 220 | raise InvalidArgument("Invalid method for event") |
||
| 221 | elif event in ALERT_EVENTS_SECINFO: |
||
| 222 | if condition not in ALERT_CONDITIONS_SECINFO: |
||
| 223 | raise InvalidArgument("Invalid condition for event") |
||
| 224 | if method not in ALERT_METHODS_SECINFO: |
||
| 225 | raise InvalidArgument("Invalid method for event") |
||
| 226 | elif event is not None: |
||
| 227 | raise InvalidArgument('Invalid event "{0}"'.format(event)) |
||
| 228 | |||
| 229 | |||
| 230 | def _is_list_like(value): |
||
| 231 | return isinstance(value, (list, tuple)) |
||
| 232 | |||
| 233 | |||
| 234 | class Gmp(GvmProtocol): |
||
| 235 | """Python interface for Greenbone Management Protocol |
||
| 236 | |||
| 237 | This class implements the `Greenbone Management Protocol version 7`_ |
||
| 238 | |||
| 239 | Attributes: |
||
| 240 | connection (:class:`gvm.connections.GvmConnection`): Connection to use |
||
| 241 | to talk with the gvmd daemon. See :mod:`gvm.connections` for |
||
| 242 | possible connection types. |
||
| 243 | transform (`callable`_, optional): Optional transform callable to |
||
| 244 | convert response data. After each request the callable gets passed |
||
| 245 | the plain response data which can be used to check the data and/or |
||
| 246 | conversion into different representations like a xml dom. |
||
| 247 | |||
| 248 | See :mod:`gvm.transforms` for existing transforms. |
||
| 249 | |||
| 250 | .. _Greenbone Management Protocol version 7: |
||
| 251 | https://docs.greenbone.net/API/GMP/gmp-7.0.html |
||
| 252 | .. _callable: |
||
| 253 | https://docs.python.org/3/library/functions.html#callable |
||
| 254 | """ |
||
| 255 | |||
| 256 | def __init__(self, connection, *, transform=None): |
||
| 257 | super().__init__(connection, transform=transform) |
||
| 258 | |||
| 259 | # Is authenticated on gvmd |
||
| 260 | self._authenticated = False |
||
| 261 | |||
| 262 | @staticmethod |
||
| 263 | def get_protocol_version(): |
||
| 264 | """Allow to determine the Greenbone Management Protocol version. |
||
| 265 | |||
| 266 | Returns: |
||
| 267 | str: Implemented version of the Greenbone Management Protocol |
||
| 268 | """ |
||
| 269 | return get_version_string(PROTOCOL_VERSION) |
||
| 270 | |||
| 271 | def is_authenticated(self): |
||
| 272 | """Checks if the user is authenticated |
||
| 273 | |||
| 274 | If the user is authenticated privileged GMP commands like get_tasks |
||
| 275 | may be send to gvmd. |
||
| 276 | |||
| 277 | Returns: |
||
| 278 | bool: True if an authenticated connection to gvmd has been |
||
| 279 | established. |
||
| 280 | """ |
||
| 281 | return self._authenticated |
||
| 282 | |||
| 283 | def authenticate(self, username, password): |
||
| 284 | """Authenticate to gvmd. |
||
| 285 | |||
| 286 | The generated authenticate command will be send to server. |
||
| 287 | Afterwards the response is read, transformed and returned. |
||
| 288 | |||
| 289 | Arguments: |
||
| 290 | username (str): Username |
||
| 291 | password (str): Password |
||
| 292 | |||
| 293 | Returns: |
||
| 294 | any, str by default: Transformed response from server. |
||
| 295 | """ |
||
| 296 | cmd = XmlCommand("authenticate") |
||
| 297 | |||
| 298 | if not username: |
||
| 299 | raise RequiredArgument("authenticate requires username") |
||
| 300 | |||
| 301 | if not password: |
||
| 302 | raise RequiredArgument("authenticate requires password") |
||
| 303 | |||
| 304 | credentials = cmd.add_element("credentials") |
||
| 305 | credentials.add_element("username", username) |
||
| 306 | credentials.add_element("password", password) |
||
| 307 | |||
| 308 | self._send(cmd.to_string()) |
||
| 309 | response = self._read() |
||
| 310 | |||
| 311 | if _check_command_status(response): |
||
| 312 | self._authenticated = True |
||
| 313 | |||
| 314 | return self._transform(response) |
||
| 315 | |||
| 316 | def create_agent( |
||
| 317 | self, |
||
| 318 | installer, |
||
| 319 | signature, |
||
| 320 | name, |
||
| 321 | *, |
||
| 322 | comment=None, |
||
| 323 | howto_install=None, |
||
| 324 | howto_use=None |
||
| 325 | ): |
||
| 326 | """Create a new agent |
||
| 327 | |||
| 328 | Arguments: |
||
| 329 | installer (str): A base64 encoded file that installs the agent on a |
||
| 330 | target machine |
||
| 331 | signature: (str): A detached OpenPGP signature of the installer |
||
| 332 | name (str): A name for the agent |
||
| 333 | comment (str, optional): A comment for the agent |
||
| 334 | howto_install (str, optional): A file that describes how to install |
||
| 335 | the agent |
||
| 336 | howto_use (str, optional): A file that describes how to use the |
||
| 337 | agent |
||
| 338 | |||
| 339 | Returns: |
||
| 340 | The response. See :py:meth:`send_command` for details. |
||
| 341 | """ |
||
| 342 | if not name: |
||
| 343 | raise RequiredArgument("create_agent requires name argument") |
||
| 344 | |||
| 345 | if not installer: |
||
| 346 | raise RequiredArgument("create_agent requires installer argument") |
||
| 347 | |||
| 348 | if not signature: |
||
| 349 | raise RequiredArgument("create_agent requires signature argument") |
||
| 350 | |||
| 351 | cmd = XmlCommand("create_agent") |
||
| 352 | cmd.add_element("installer", installer) |
||
| 353 | cmd.add_element("signature", signature) |
||
| 354 | cmd.add_element("name", name) |
||
| 355 | |||
| 356 | if comment: |
||
| 357 | cmd.add_element("comment", comment) |
||
| 358 | |||
| 359 | if howto_install: |
||
| 360 | cmd.add_element("howto_install", howto_install) |
||
| 361 | |||
| 362 | if howto_use: |
||
| 363 | cmd.add_element("howto_use", howto_use) |
||
| 364 | |||
| 365 | return self._send_xml_command(cmd) |
||
| 366 | |||
| 367 | def clone_agent(self, agent_id): |
||
| 368 | """Clone an existing agent |
||
| 369 | |||
| 370 | Arguments: |
||
| 371 | copy (str): UUID of an existing agent to clone from |
||
| 372 | |||
| 373 | Returns: |
||
| 374 | The response. See :py:meth:`send_command` for details. |
||
| 375 | """ |
||
| 376 | if not agent_id: |
||
| 377 | raise RequiredArgument("clone_agent requires a agent_id argument") |
||
| 378 | |||
| 379 | cmd = XmlCommand("create_agent") |
||
| 380 | cmd.add_element("copy", agent_id) |
||
| 381 | return self._send_xml_command(cmd) |
||
| 382 | |||
| 383 | def create_alert( |
||
| 384 | self, |
||
| 385 | name, |
||
| 386 | condition, |
||
| 387 | event, |
||
| 388 | method, |
||
| 389 | *, |
||
| 390 | method_data=None, |
||
| 391 | event_data=None, |
||
| 392 | condition_data=None, |
||
| 393 | filter_id=None, |
||
| 394 | comment=None |
||
| 395 | ): |
||
| 396 | """Create a new alert |
||
| 397 | |||
| 398 | Arguments: |
||
| 399 | name (str): Name of the new Alert |
||
| 400 | condition (str): The condition that must be satisfied for the alert |
||
| 401 | to occur; if the event is either 'Updated SecInfo arrived' or |
||
| 402 | 'New SecInfo arrived', condition must be 'Always'. Otherwise, |
||
| 403 | condition can also be on of 'Severity at least', 'Filter count |
||
| 404 | changed' or 'Filter count at least'. |
||
| 405 | event (str): The event that must happen for the alert to occur, one |
||
| 406 | of 'Task run status changed', 'Updated SecInfo arrived' or 'New |
||
| 407 | SecInfo arrived' |
||
| 408 | method (str): The method by which the user is alerted, one of 'SCP', |
||
| 409 | 'Send', 'SMB', 'SNMP', 'Syslog' or 'Email'; if the event is |
||
| 410 | neither 'Updated SecInfo arrived' nor 'New SecInfo arrived', |
||
| 411 | method can also be one of 'Start Task', 'HTTP Get', 'Sourcefire |
||
| 412 | Connector' or 'verinice Connector'. |
||
| 413 | condition_data (dict, optional): Data that defines the condition |
||
| 414 | event_data (dict, optional): Data that defines the event |
||
| 415 | method_data (dict, optional): Data that defines the method |
||
| 416 | filter_id (str, optional): Filter to apply when executing alert |
||
| 417 | comment (str, optional): Comment for the alert |
||
| 418 | |||
| 419 | Returns: |
||
| 420 | The response. See :py:meth:`send_command` for details. |
||
| 421 | """ |
||
| 422 | if not name: |
||
| 423 | raise RequiredArgument("create_alert requires name argument") |
||
| 424 | |||
| 425 | if not condition: |
||
| 426 | raise RequiredArgument("create_alert requires condition argument") |
||
| 427 | |||
| 428 | if not event: |
||
| 429 | raise RequiredArgument("create_alert requires event argument") |
||
| 430 | |||
| 431 | if not method: |
||
| 432 | raise RequiredArgument("create_alert requires method argument") |
||
| 433 | |||
| 434 | _check_event(event, condition, method) |
||
| 435 | |||
| 436 | cmd = XmlCommand("create_alert") |
||
| 437 | cmd.add_element("name", name) |
||
| 438 | |||
| 439 | conditions = cmd.add_element("condition", condition) |
||
| 440 | |||
| 441 | if not condition_data is None: |
||
| 442 | for key, value in condition_data.items(): |
||
| 443 | _data = conditions.add_element("data", value) |
||
| 444 | _data.add_element("name", key) |
||
| 445 | |||
| 446 | events = cmd.add_element("event", event) |
||
| 447 | |||
| 448 | if not event_data is None: |
||
| 449 | for key, value in event_data.items(): |
||
| 450 | _data = events.add_element("data", value) |
||
| 451 | _data.add_element("name", key) |
||
| 452 | |||
| 453 | methods = cmd.add_element("method", method) |
||
| 454 | |||
| 455 | if not method_data is None: |
||
| 456 | for key, value in method_data.items(): |
||
| 457 | _data = methods.add_element("data", value) |
||
| 458 | _data.add_element("name", key) |
||
| 459 | |||
| 460 | if filter_id: |
||
| 461 | cmd.add_element("filter", attrs={"id": filter_id}) |
||
| 462 | |||
| 463 | if comment: |
||
| 464 | cmd.add_element("comment", comment) |
||
| 465 | |||
| 466 | return self._send_xml_command(cmd) |
||
| 467 | |||
| 468 | def clone_alert(self, alert_id): |
||
| 469 | """Clone an existing alert |
||
| 470 | |||
| 471 | Arguments: |
||
| 472 | copy (str): UUID of an existing alert to clone from |
||
| 473 | |||
| 474 | Returns: |
||
| 475 | The response. See :py:meth:`send_command` for details. |
||
| 476 | """ |
||
| 477 | if not alert_id: |
||
| 478 | raise RequiredArgument("clone_alert requires a alert_id argument") |
||
| 479 | |||
| 480 | cmd = XmlCommand("create_alert") |
||
| 481 | cmd.add_element("copy", alert_id) |
||
| 482 | return self._send_xml_command(cmd) |
||
| 483 | |||
| 484 | def create_config(self, config_id, name): |
||
| 485 | """Create a new scan config from an existing one |
||
| 486 | |||
| 487 | Arguments: |
||
| 488 | config_id (str): UUID of the existing scan config |
||
| 489 | name (str): Name of the new scan config |
||
| 490 | |||
| 491 | Returns: |
||
| 492 | The response. See :py:meth:`send_command` for details. |
||
| 493 | """ |
||
| 494 | if not name: |
||
| 495 | raise RequiredArgument("create_config requires name argument") |
||
| 496 | |||
| 497 | if not config_id: |
||
| 498 | raise RequiredArgument("create_config requires config_id argument") |
||
| 499 | |||
| 500 | cmd = XmlCommand("create_config") |
||
| 501 | cmd.add_element("copy", config_id) |
||
| 502 | cmd.add_element("name", name) |
||
| 503 | return self._send_xml_command(cmd) |
||
| 504 | |||
| 505 | def clone_config(self, config_id): |
||
| 506 | """Clone a scan config from an existing one |
||
| 507 | |||
| 508 | Arguments: |
||
| 509 | config_id (str): UUID of the existing scan config |
||
| 510 | |||
| 511 | Returns: |
||
| 512 | The response. See :py:meth:`send_command` for details. |
||
| 513 | """ |
||
| 514 | if not config_id: |
||
| 515 | raise RequiredArgument("clone_config requires config_id argument") |
||
| 516 | |||
| 517 | cmd = XmlCommand("create_config") |
||
| 518 | cmd.add_element("copy", config_id) |
||
| 519 | return self._send_xml_command(cmd) |
||
| 520 | |||
| 521 | def import_config(self, config): |
||
| 522 | """Import a scan config from XML |
||
| 523 | |||
| 524 | Arguments: |
||
| 525 | config (str): Scan Config XML as string to import. This XML must |
||
| 526 | contain a :code:`<get_configs_response>` root element. |
||
| 527 | |||
| 528 | Returns: |
||
| 529 | The response. See :py:meth:`send_command` for details. |
||
| 530 | """ |
||
| 531 | if not config: |
||
| 532 | raise RequiredArgument("import_config requires config argument") |
||
| 533 | |||
| 534 | cmd = XmlCommand("create_config") |
||
| 535 | |||
| 536 | try: |
||
| 537 | cmd.append_xml_str(config) |
||
| 538 | except etree.XMLSyntaxError as e: |
||
| 539 | raise InvalidArgument( |
||
| 540 | "Invalid xml passed as config to import_config", e |
||
| 541 | ) |
||
| 542 | |||
| 543 | return self._send_xml_command(cmd) |
||
| 544 | |||
| 545 | def create_credential( |
||
| 546 | self, |
||
| 547 | name, |
||
| 548 | credential_type, |
||
| 549 | *, |
||
| 550 | comment=None, |
||
| 551 | allow_insecure=None, |
||
| 552 | certificate=None, |
||
| 553 | key_phrase=None, |
||
| 554 | private_key=None, |
||
| 555 | login=None, |
||
| 556 | password=None, |
||
| 557 | auth_algorithm=None, |
||
| 558 | community=None, |
||
| 559 | privacy_algorithm=None, |
||
| 560 | privacy_password=None |
||
| 561 | ): |
||
| 562 | """Create a new credential |
||
| 563 | |||
| 564 | Create a new credential e.g. to be used in the method of an alert. |
||
| 565 | |||
| 566 | Currently the following credential types are supported: |
||
| 567 | |||
| 568 | - 'up' - Username + Password |
||
| 569 | - 'usk' - Username + private SSH-Key |
||
| 570 | - 'cc' - Client Certificates |
||
| 571 | - 'snmp' - SNMPv1 or SNMPv2c protocol |
||
| 572 | |||
| 573 | Arguments: |
||
| 574 | name (str): Name of the new credential |
||
| 575 | credential_type (str): The credential type. One of 'cc', 'snmp', |
||
| 576 | 'up', 'usk' |
||
| 577 | comment (str, optional): Comment for the credential |
||
| 578 | allow_insecure (boolean, optional): Whether to allow insecure use of |
||
| 579 | the credential |
||
| 580 | certificate (str, optional): Certificate for the credential. |
||
| 581 | Required for cc credential type. |
||
| 582 | key_phrase (str, optional): Key passphrase for the private key. |
||
| 583 | Used for the usk credential type. |
||
| 584 | private_key (str, optional): Private key to use for login. Required |
||
| 585 | for usk credential type. Also used for the cc credential type. |
||
| 586 | The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM, |
||
| 587 | PKC#12, OpenSSL, ...) depend on your installed GnuTLS version. |
||
| 588 | login (str, optional): Username for the credential. Required for |
||
| 589 | up, usk and snmp credential type. |
||
| 590 | password (str, optional): Password for the credential. Used for |
||
| 591 | up and snmp credential types. |
||
| 592 | community (str, optional): The SNMP community. |
||
| 593 | auth_algorithm (str, optional): The SNMP authentication algorithm. |
||
| 594 | Either 'md5' or 'sha1'. Required for snmp credential type. |
||
| 595 | privacy_algorithm (str, optional): The SNMP privacy algorithm, |
||
| 596 | either aes or des. |
||
| 597 | privacy_password (str, optional): The SNMP privacy password |
||
| 598 | |||
| 599 | Examples: |
||
| 600 | Creating a Username + Password credential |
||
| 601 | |||
| 602 | .. code-block:: python |
||
| 603 | |||
| 604 | gmp.create_credential( |
||
| 605 | name='UP Credential', |
||
| 606 | credential_type='up', |
||
| 607 | login='foo', |
||
| 608 | password='bar', |
||
| 609 | ); |
||
| 610 | |||
| 611 | Creating a Username + SSH Key credential |
||
| 612 | |||
| 613 | .. code-block:: python |
||
| 614 | |||
| 615 | with open('path/to/private-ssh-key') as f: |
||
| 616 | key = f.read() |
||
| 617 | |||
| 618 | gmp.create_credential( |
||
| 619 | name='USK Credential', |
||
| 620 | credential_type='usk', |
||
| 621 | login='foo', |
||
| 622 | key_phrase='foobar', |
||
| 623 | private_key=key, |
||
| 624 | ) |
||
| 625 | |||
| 626 | Returns: |
||
| 627 | The response. See :py:meth:`send_command` for details. |
||
| 628 | """ |
||
| 629 | if not name: |
||
| 630 | raise RequiredArgument("create_credential requires name argument") |
||
| 631 | |||
| 632 | if credential_type not in CREDENTIAL_TYPES: |
||
| 633 | raise InvalidArgument( |
||
| 634 | "create_credential requires type to be either cc, snmp, up " |
||
| 635 | " or usk" |
||
| 636 | ) |
||
| 637 | |||
| 638 | cmd = XmlCommand("create_credential") |
||
| 639 | cmd.add_element("name", name) |
||
| 640 | |||
| 641 | cmd.add_element("type", credential_type) |
||
| 642 | |||
| 643 | if comment: |
||
| 644 | cmd.add_element("comment", comment) |
||
| 645 | |||
| 646 | if allow_insecure is not None: |
||
| 647 | cmd.add_element("allow_insecure", _to_bool(allow_insecure)) |
||
| 648 | |||
| 649 | if credential_type == "cc": |
||
| 650 | if not certificate: |
||
| 651 | raise RequiredArgument( |
||
| 652 | "create_credential requires certificate argument for " |
||
| 653 | "credential_type {0}".format(credential_type) |
||
| 654 | ) |
||
| 655 | |||
| 656 | cmd.add_element("certificate", certificate) |
||
| 657 | |||
| 658 | View Code Duplication | if ( |
|
| 659 | credential_type == "up" |
||
| 660 | or credential_type == "usk" |
||
| 661 | or credential_type == "snmp" |
||
| 662 | ): |
||
| 663 | if not login: |
||
| 664 | raise RequiredArgument( |
||
| 665 | "create_credential requires login argument for " |
||
| 666 | "credential_type {0}".format(credential_type) |
||
| 667 | ) |
||
| 668 | |||
| 669 | cmd.add_element("login", login) |
||
| 670 | |||
| 671 | if (credential_type == "up" or credential_type == "snmp") and password: |
||
| 672 | cmd.add_element("password", password) |
||
| 673 | |||
| 674 | View Code Duplication | if credential_type == "usk": |
|
| 675 | if not private_key: |
||
| 676 | raise RequiredArgument( |
||
| 677 | "create_credential requires certificate argument for " |
||
| 678 | "credential_type usk" |
||
| 679 | ) |
||
| 680 | |||
| 681 | _xmlkey = cmd.add_element("key") |
||
| 682 | _xmlkey.add_element("private", private_key) |
||
| 683 | |||
| 684 | if key_phrase: |
||
| 685 | _xmlkey.add_element("phrase", key_phrase) |
||
| 686 | |||
| 687 | if credential_type == "cc" and private_key: |
||
| 688 | _xmlkey = cmd.add_element("key") |
||
| 689 | _xmlkey.add_element("private", private_key) |
||
| 690 | |||
| 691 | View Code Duplication | if credential_type == "snmp": |
|
| 692 | if auth_algorithm not in ("md5", "sha1"): |
||
| 693 | raise InvalidArgument( |
||
| 694 | "create_credential requires auth_algorithm to be either " |
||
| 695 | "md5 or sha1" |
||
| 696 | ) |
||
| 697 | |||
| 698 | cmd.add_element("auth_algorithm", auth_algorithm) |
||
| 699 | |||
| 700 | if community: |
||
| 701 | cmd.add_element("community", community) |
||
| 702 | |||
| 703 | if privacy_algorithm is not None or privacy_password: |
||
| 704 | _xmlprivacy = cmd.add_element("privacy") |
||
| 705 | |||
| 706 | if privacy_algorithm is not None: |
||
| 707 | if privacy_algorithm not in ("aes", "des"): |
||
| 708 | raise InvalidArgument( |
||
| 709 | "create_credential requires algorithm to be either " |
||
| 710 | "aes or des" |
||
| 711 | ) |
||
| 712 | |||
| 713 | _xmlprivacy.add_element("algorithm", privacy_algorithm) |
||
| 714 | |||
| 715 | if privacy_password: |
||
| 716 | _xmlprivacy.add_element("password", privacy_password) |
||
| 717 | |||
| 718 | return self._send_xml_command(cmd) |
||
| 719 | |||
| 720 | def clone_credential(self, credential_id): |
||
| 721 | """Clone an existing credential |
||
| 722 | |||
| 723 | Arguments: |
||
| 724 | copy (str): UUID of an existing credential to clone from |
||
| 725 | |||
| 726 | Returns: |
||
| 727 | The response. See :py:meth:`send_command` for details. |
||
| 728 | """ |
||
| 729 | if not credential_id: |
||
| 730 | raise RequiredArgument( |
||
| 731 | "clone_credential requires a credential_id argument" |
||
| 732 | ) |
||
| 733 | |||
| 734 | cmd = XmlCommand("create_credential") |
||
| 735 | cmd.add_element("copy", credential_id) |
||
| 736 | return self._send_xml_command(cmd) |
||
| 737 | |||
| 738 | def create_filter( |
||
| 739 | self, |
||
| 740 | name, |
||
| 741 | *, |
||
| 742 | make_unique=False, |
||
| 743 | filter_type=None, |
||
| 744 | comment=None, |
||
| 745 | term=None |
||
| 746 | ): |
||
| 747 | """Create a new filter |
||
| 748 | |||
| 749 | Arguments: |
||
| 750 | name (str): Name of the new filter |
||
| 751 | make_unique (boolean, optional): |
||
| 752 | filter_type (str, optional): Filter for entity type |
||
| 753 | comment (str, optional): Comment for the filter |
||
| 754 | term (str, optional): Filter term e.g. 'name=foo' |
||
| 755 | |||
| 756 | Returns: |
||
| 757 | The response. See :py:meth:`send_command` for details. |
||
| 758 | """ |
||
| 759 | if not name: |
||
| 760 | raise RequiredArgument("create_filter requires a name argument") |
||
| 761 | |||
| 762 | cmd = XmlCommand("create_filter") |
||
| 763 | _xmlname = cmd.add_element("name", name) |
||
| 764 | if make_unique: |
||
| 765 | _xmlname.add_element("make_unique", "1") |
||
| 766 | |||
| 767 | if comment: |
||
| 768 | cmd.add_element("comment", comment) |
||
| 769 | |||
| 770 | if term: |
||
| 771 | cmd.add_element("term", term) |
||
| 772 | |||
| 773 | if filter_type: |
||
| 774 | filter_type = filter_type.lower() |
||
| 775 | if filter_type not in FILTER_TYPES: |
||
| 776 | raise InvalidArgument( |
||
| 777 | "create_filter requires type to be one of {0} but " |
||
| 778 | "was {1}".format(", ".join(FILTER_TYPES), filter_type) |
||
| 779 | ) |
||
| 780 | cmd.add_element("type", filter_type) |
||
| 781 | |||
| 782 | return self._send_xml_command(cmd) |
||
| 783 | |||
| 784 | def clone_filter(self, filter_id): |
||
| 785 | """Clone an existing filter |
||
| 786 | |||
| 787 | Arguments: |
||
| 788 | copy (str): UUID of an existing filter to clone from |
||
| 789 | |||
| 790 | Returns: |
||
| 791 | The response. See :py:meth:`send_command` for details. |
||
| 792 | """ |
||
| 793 | if not filter_id: |
||
| 794 | raise RequiredArgument("clone_filter requires a filter_id argument") |
||
| 795 | |||
| 796 | cmd = XmlCommand("create_filter") |
||
| 797 | cmd.add_element("copy", filter_id) |
||
| 798 | return self._send_xml_command(cmd) |
||
| 799 | |||
| 800 | def create_group(self, name, *, comment=None, special=False, users=None): |
||
| 801 | """Create a new group |
||
| 802 | |||
| 803 | Arguments: |
||
| 804 | name (str): Name of the new group |
||
| 805 | comment (str, optional): Comment for the group |
||
| 806 | special (boolean, optional): Create permission giving members full |
||
| 807 | access to each other's entities |
||
| 808 | users (list, optional): List of user names to be in the group |
||
| 809 | |||
| 810 | Returns: |
||
| 811 | The response. See :py:meth:`send_command` for details. |
||
| 812 | """ |
||
| 813 | if not name: |
||
| 814 | raise RequiredArgument("create_group requires a name argument") |
||
| 815 | |||
| 816 | cmd = XmlCommand("create_group") |
||
| 817 | cmd.add_element("name", name) |
||
| 818 | |||
| 819 | if comment: |
||
| 820 | cmd.add_element("comment", comment) |
||
| 821 | |||
| 822 | if special: |
||
| 823 | _xmlspecial = cmd.add_element("specials") |
||
| 824 | _xmlspecial.add_element("full") |
||
| 825 | |||
| 826 | if users: |
||
| 827 | cmd.add_element("users", _to_comma_list(users)) |
||
| 828 | |||
| 829 | return self._send_xml_command(cmd) |
||
| 830 | |||
| 831 | def clone_group(self, group_id): |
||
| 832 | """Clone an existing group |
||
| 833 | |||
| 834 | Arguments: |
||
| 835 | copy (str): UUID of an existing group to clone from |
||
| 836 | |||
| 837 | Returns: |
||
| 838 | The response. See :py:meth:`send_command` for details. |
||
| 839 | """ |
||
| 840 | if not group_id: |
||
| 841 | raise RequiredArgument("clone_group requires a group_id argument") |
||
| 842 | |||
| 843 | cmd = XmlCommand("create_group") |
||
| 844 | cmd.add_element("copy", group_id) |
||
| 845 | return self._send_xml_command(cmd) |
||
| 846 | |||
| 847 | def create_host(self, name, *, comment=None): |
||
| 848 | """Create a new host asset |
||
| 849 | |||
| 850 | Arguments: |
||
| 851 | name (str): Name for the new host asset |
||
| 852 | comment (str, optional): Comment for the new host asset |
||
| 853 | |||
| 854 | Returns: |
||
| 855 | The response. See :py:meth:`send_command` for details. |
||
| 856 | """ |
||
| 857 | if not name: |
||
| 858 | raise RequiredArgument("create_host requires name argument") |
||
| 859 | |||
| 860 | cmd = XmlCommand("create_asset") |
||
| 861 | asset = cmd.add_element("asset") |
||
| 862 | asset.add_element("type", "host") # ignored for gmp7, required for gmp8 |
||
| 863 | asset.add_element("name", name) |
||
| 864 | |||
| 865 | if comment: |
||
| 866 | asset.add_element("comment", comment) |
||
| 867 | |||
| 868 | return self._send_xml_command(cmd) |
||
| 869 | |||
| 870 | View Code Duplication | def create_note( |
|
| 871 | self, |
||
| 872 | text, |
||
| 873 | nvt_oid, |
||
| 874 | *, |
||
| 875 | seconds_active=None, |
||
| 876 | hosts=None, |
||
| 877 | result_id=None, |
||
| 878 | severity=None, |
||
| 879 | task_id=None, |
||
| 880 | threat=None, |
||
| 881 | port=None |
||
| 882 | ): |
||
| 883 | """Create a new note |
||
| 884 | |||
| 885 | Arguments: |
||
| 886 | text (str): Text of the new note |
||
| 887 | nvt_id (str): OID of the nvt to which note applies |
||
| 888 | seconds_active (int, optional): Seconds note will be active. -1 on |
||
| 889 | always, 0 off |
||
| 890 | hosts (list, optional): A list of hosts addresses |
||
| 891 | port (int, optional): Port to which the note applies |
||
| 892 | result_id (str, optional): UUID of a result to which note applies |
||
| 893 | severity (decimal, optional): Severity to which note applies |
||
| 894 | task_id (str, optional): UUID of task to which note applies |
||
| 895 | threat (str, optional): Threat level to which note applies. One of |
||
| 896 | High, Medium, Low, Alarm, Log or Debug. Will be converted to |
||
| 897 | severity. |
||
| 898 | |||
| 899 | Returns: |
||
| 900 | The response. See :py:meth:`send_command` for details. |
||
| 901 | """ |
||
| 902 | if not text: |
||
| 903 | raise RequiredArgument("create_note requires a text argument") |
||
| 904 | |||
| 905 | if not nvt_oid: |
||
| 906 | raise RequiredArgument("create_note requires a nvt_oid argument") |
||
| 907 | |||
| 908 | cmd = XmlCommand("create_note") |
||
| 909 | cmd.add_element("text", text) |
||
| 910 | cmd.add_element("nvt", attrs={"oid": nvt_oid}) |
||
| 911 | |||
| 912 | if not seconds_active is None: |
||
| 913 | cmd.add_element("active", str(seconds_active)) |
||
| 914 | |||
| 915 | if hosts: |
||
| 916 | cmd.add_element("hosts", _to_comma_list(hosts)) |
||
| 917 | |||
| 918 | if port: |
||
| 919 | cmd.add_element("port", str(port)) |
||
| 920 | |||
| 921 | if result_id: |
||
| 922 | cmd.add_element("result", attrs={"id": result_id}) |
||
| 923 | |||
| 924 | if severity: |
||
| 925 | cmd.add_element("severity", str(severity)) |
||
| 926 | |||
| 927 | if task_id: |
||
| 928 | cmd.add_element("task", attrs={"id": task_id}) |
||
| 929 | |||
| 930 | if threat is not None: |
||
| 931 | if threat not in THREAD_TYPES: |
||
| 932 | raise InvalidArgument( |
||
| 933 | "create_note threat argument {0} is invalid. threat must " |
||
| 934 | "be one of {1}".format(threat, ", ".join(THREAD_TYPES)) |
||
| 935 | ) |
||
| 936 | |||
| 937 | cmd.add_element("threat", threat) |
||
| 938 | |||
| 939 | return self._send_xml_command(cmd) |
||
| 940 | |||
| 941 | def clone_note(self, note_id): |
||
| 942 | """Clone an existing note |
||
| 943 | |||
| 944 | Arguments: |
||
| 945 | copy (str): UUID of an existing note to clone from |
||
| 946 | |||
| 947 | Returns: |
||
| 948 | The response. See :py:meth:`send_command` for details. |
||
| 949 | """ |
||
| 950 | if not note_id: |
||
| 951 | raise RequiredArgument("clone_note requires a note_id argument") |
||
| 952 | |||
| 953 | cmd = XmlCommand("create_note") |
||
| 954 | cmd.add_element("copy", note_id) |
||
| 955 | return self._send_xml_command(cmd) |
||
| 956 | |||
| 957 | View Code Duplication | def create_override( |
|
| 958 | self, |
||
| 959 | text, |
||
| 960 | nvt_oid, |
||
| 961 | *, |
||
| 962 | seconds_active=None, |
||
| 963 | hosts=None, |
||
| 964 | port=None, |
||
| 965 | result_id=None, |
||
| 966 | severity=None, |
||
| 967 | new_severity=None, |
||
| 968 | task_id=None, |
||
| 969 | threat=None, |
||
| 970 | new_threat=None |
||
| 971 | ): |
||
| 972 | """Create a new override |
||
| 973 | |||
| 974 | Arguments: |
||
| 975 | text (str): Text of the new override |
||
| 976 | nvt_id (str): OID of the nvt to which override applies |
||
| 977 | seconds_active (int, optional): Seconds override will be active. |
||
| 978 | -1 on always, 0 off |
||
| 979 | hosts (list, optional): A list of host addresses |
||
| 980 | port (int, optional): Port to which the override applies |
||
| 981 | result_id (str, optional): UUID of a result to which override |
||
| 982 | applies |
||
| 983 | severity (decimal, optional): Severity to which override applies |
||
| 984 | new_severity (decimal, optional): New severity for result |
||
| 985 | task_id (str, optional): UUID of task to which override applies |
||
| 986 | threat (str, optional): Threat level to which override applies. One |
||
| 987 | of High, Medium, Low, Alarm, Log or Debug. Will be converted to |
||
| 988 | severity. |
||
| 989 | new_threat (str, optional): New threat level for results. One |
||
| 990 | of High, Medium, Low, Alarm, Log or Debug. Will be converted to |
||
| 991 | new_severity. |
||
| 992 | |||
| 993 | Returns: |
||
| 994 | The response. See :py:meth:`send_command` for details. |
||
| 995 | """ |
||
| 996 | if not text: |
||
| 997 | raise RequiredArgument("create_override requires a text argument") |
||
| 998 | |||
| 999 | if not nvt_oid: |
||
| 1000 | raise RequiredArgument( |
||
| 1001 | "create_override requires a nvt_oid " "argument" |
||
| 1002 | ) |
||
| 1003 | |||
| 1004 | cmd = XmlCommand("create_override") |
||
| 1005 | cmd.add_element("text", text) |
||
| 1006 | cmd.add_element("nvt", attrs={"oid": nvt_oid}) |
||
| 1007 | |||
| 1008 | if not seconds_active is None: |
||
| 1009 | cmd.add_element("active", str(seconds_active)) |
||
| 1010 | |||
| 1011 | if hosts: |
||
| 1012 | cmd.add_element("hosts", _to_comma_list(hosts)) |
||
| 1013 | |||
| 1014 | if port: |
||
| 1015 | cmd.add_element("port", str(port)) |
||
| 1016 | |||
| 1017 | if result_id: |
||
| 1018 | cmd.add_element("result", attrs={"id": result_id}) |
||
| 1019 | |||
| 1020 | if severity: |
||
| 1021 | cmd.add_element("severity", str(severity)) |
||
| 1022 | |||
| 1023 | if new_severity: |
||
| 1024 | cmd.add_element("new_severity", str(new_severity)) |
||
| 1025 | |||
| 1026 | if task_id: |
||
| 1027 | cmd.add_element("task", attrs={"id": task_id}) |
||
| 1028 | |||
| 1029 | if threat is not None: |
||
| 1030 | if threat not in THREAD_TYPES: |
||
| 1031 | raise InvalidArgument( |
||
| 1032 | "create_override threat argument {0} is invalid. threat" |
||
| 1033 | "must be one of {1}".format(threat, ", ".join(THREAD_TYPES)) |
||
| 1034 | ) |
||
| 1035 | |||
| 1036 | cmd.add_element("threat", threat) |
||
| 1037 | |||
| 1038 | if new_threat is not None: |
||
| 1039 | if new_threat not in THREAD_TYPES: |
||
| 1040 | raise InvalidArgument( |
||
| 1041 | "create_override new_threat argument {0} is invalid. " |
||
| 1042 | "new_threat must be one of {1}".format( |
||
| 1043 | new_threat, ", ".join(THREAD_TYPES) |
||
| 1044 | ) |
||
| 1045 | ) |
||
| 1046 | |||
| 1047 | cmd.add_element("new_threat", new_threat) |
||
| 1048 | |||
| 1049 | return self._send_xml_command(cmd) |
||
| 1050 | |||
| 1051 | def clone_override(self, override_id): |
||
| 1052 | """Clone an existing override |
||
| 1053 | |||
| 1054 | Arguments: |
||
| 1055 | copy (str): UUID of an existing override to clone from |
||
| 1056 | |||
| 1057 | Returns: |
||
| 1058 | The response. See :py:meth:`send_command` for details. |
||
| 1059 | """ |
||
| 1060 | if not override_id: |
||
| 1061 | raise RequiredArgument( |
||
| 1062 | "clone_override requires a override_id argument" |
||
| 1063 | ) |
||
| 1064 | |||
| 1065 | cmd = XmlCommand("create_override") |
||
| 1066 | cmd.add_element("copy", override_id) |
||
| 1067 | return self._send_xml_command(cmd) |
||
| 1068 | |||
| 1069 | def create_permission( |
||
| 1070 | self, |
||
| 1071 | name, |
||
| 1072 | subject_id, |
||
| 1073 | subject_type, |
||
| 1074 | *, |
||
| 1075 | resource_id=None, |
||
| 1076 | resource_type=None, |
||
| 1077 | comment=None |
||
| 1078 | ): |
||
| 1079 | """Create a new permission |
||
| 1080 | |||
| 1081 | Arguments: |
||
| 1082 | name (str): Name of the new permission |
||
| 1083 | subject_id (str): UUID of subject to whom the permission is granted |
||
| 1084 | subject_type (str): Type of the subject user, group or role |
||
| 1085 | comment (str, optional): Comment for the permission |
||
| 1086 | resource_id (str, optional): UUID of entity to which the permission |
||
| 1087 | applies |
||
| 1088 | resource_type (str, optional): Type of the resource. For Super |
||
| 1089 | permissions user, group or role |
||
| 1090 | |||
| 1091 | Returns: |
||
| 1092 | The response. See :py:meth:`send_command` for details. |
||
| 1093 | """ |
||
| 1094 | if not name: |
||
| 1095 | raise RequiredArgument("create_permission requires a name argument") |
||
| 1096 | |||
| 1097 | if not subject_id: |
||
| 1098 | raise RequiredArgument( |
||
| 1099 | "create_permission requires a subject_id argument" |
||
| 1100 | ) |
||
| 1101 | |||
| 1102 | if subject_type not in SUBJECT_TYPES: |
||
| 1103 | raise InvalidArgument( |
||
| 1104 | "create_permission requires subject_type to be either user, " |
||
| 1105 | "group or role" |
||
| 1106 | ) |
||
| 1107 | |||
| 1108 | cmd = XmlCommand("create_permission") |
||
| 1109 | cmd.add_element("name", name) |
||
| 1110 | |||
| 1111 | _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
||
| 1112 | _xmlsubject.add_element("type", subject_type) |
||
| 1113 | |||
| 1114 | if comment: |
||
| 1115 | cmd.add_element("comment", comment) |
||
| 1116 | |||
| 1117 | if resource_id or resource_type: |
||
| 1118 | if not resource_id: |
||
| 1119 | raise RequiredArgument( |
||
| 1120 | "create_permission requires resource_id for resource_type" |
||
| 1121 | ) |
||
| 1122 | |||
| 1123 | if not resource_type: |
||
| 1124 | raise RequiredArgument( |
||
| 1125 | "create_permission requires resource_type for resource_id" |
||
| 1126 | ) |
||
| 1127 | |||
| 1128 | _xmlresource = cmd.add_element( |
||
| 1129 | "resource", attrs={"id": resource_id} |
||
| 1130 | ) |
||
| 1131 | _xmlresource.add_element("type", resource_type) |
||
| 1132 | |||
| 1133 | return self._send_xml_command(cmd) |
||
| 1134 | |||
| 1135 | def clone_permission(self, permission_id): |
||
| 1136 | """Clone an existing permission |
||
| 1137 | |||
| 1138 | Arguments: |
||
| 1139 | copy (str): UUID of an existing permission to clone from |
||
| 1140 | |||
| 1141 | Returns: |
||
| 1142 | The response. See :py:meth:`send_command` for details. |
||
| 1143 | """ |
||
| 1144 | if not permission_id: |
||
| 1145 | raise RequiredArgument( |
||
| 1146 | "clone_permission requires a permission_id argument" |
||
| 1147 | ) |
||
| 1148 | |||
| 1149 | cmd = XmlCommand("create_permission") |
||
| 1150 | cmd.add_element("copy", permission_id) |
||
| 1151 | return self._send_xml_command(cmd) |
||
| 1152 | |||
| 1153 | def create_port_list(self, name, port_range, *, comment=None): |
||
| 1154 | """Create a new port list |
||
| 1155 | |||
| 1156 | Arguments: |
||
| 1157 | name (str): Name of the new port list |
||
| 1158 | port_range (str): Port list ranges e.g. `"T: 1-1234"` for tcp port |
||
| 1159 | 1 - 1234 |
||
| 1160 | comment (str, optional): Comment for the port list |
||
| 1161 | |||
| 1162 | Returns: |
||
| 1163 | The response. See :py:meth:`send_command` for details. |
||
| 1164 | """ |
||
| 1165 | if not name: |
||
| 1166 | raise RequiredArgument("create_port_list requires a name argument") |
||
| 1167 | |||
| 1168 | if not port_range: |
||
| 1169 | raise RequiredArgument( |
||
| 1170 | "create_port_list requires a port_range argument" |
||
| 1171 | ) |
||
| 1172 | |||
| 1173 | cmd = XmlCommand("create_port_list") |
||
| 1174 | cmd.add_element("name", name) |
||
| 1175 | cmd.add_element("port_range", port_range) |
||
| 1176 | |||
| 1177 | if comment: |
||
| 1178 | cmd.add_element("comment", comment) |
||
| 1179 | |||
| 1180 | return self._send_xml_command(cmd) |
||
| 1181 | |||
| 1182 | def clone_port_list(self, port_list_id): |
||
| 1183 | """Clone an existing port list |
||
| 1184 | |||
| 1185 | Arguments: |
||
| 1186 | copy (str): UUID of an existing port list to clone from |
||
| 1187 | |||
| 1188 | Returns: |
||
| 1189 | The response. See :py:meth:`send_command` for details. |
||
| 1190 | """ |
||
| 1191 | if not port_list_id: |
||
| 1192 | raise RequiredArgument( |
||
| 1193 | "clone_port_list requires a port_list_id argument" |
||
| 1194 | ) |
||
| 1195 | |||
| 1196 | cmd = XmlCommand("create_port_list") |
||
| 1197 | cmd.add_element("copy", port_list_id) |
||
| 1198 | return self._send_xml_command(cmd) |
||
| 1199 | |||
| 1200 | def create_port_range( |
||
| 1201 | self, port_list_id, start, end, port_range_type, *, comment=None |
||
| 1202 | ): |
||
| 1203 | """Create new port range |
||
| 1204 | |||
| 1205 | Arguments: |
||
| 1206 | port_list_id (str): UUID of the port list to which to add the range |
||
| 1207 | start (int): The first port in the range |
||
| 1208 | end (int): The last port in the range |
||
| 1209 | port_range_type (str): The type of the ports: TCP, UDP, ... |
||
| 1210 | comment (str, optional): Comment for the port range |
||
| 1211 | |||
| 1212 | Returns: |
||
| 1213 | The response. See :py:meth:`send_command` for details. |
||
| 1214 | """ |
||
| 1215 | if not port_list_id: |
||
| 1216 | raise RequiredArgument( |
||
| 1217 | "create_port_range requires " "a port_list_id argument" |
||
| 1218 | ) |
||
| 1219 | |||
| 1220 | if not port_range_type: |
||
| 1221 | raise RequiredArgument( |
||
| 1222 | "create_port_range requires a port_range_type argument" |
||
| 1223 | ) |
||
| 1224 | |||
| 1225 | if not start: |
||
| 1226 | raise RequiredArgument( |
||
| 1227 | "create_port_range requires a start argument" |
||
| 1228 | ) |
||
| 1229 | |||
| 1230 | if not end: |
||
| 1231 | raise RequiredArgument("create_port_range requires a end argument") |
||
| 1232 | |||
| 1233 | cmd = XmlCommand("create_port_range") |
||
| 1234 | cmd.add_element("port_list", attrs={"id": port_list_id}) |
||
| 1235 | cmd.add_element("start", str(start)) |
||
| 1236 | cmd.add_element("end", str(end)) |
||
| 1237 | cmd.add_element("type", port_range_type) |
||
| 1238 | |||
| 1239 | if comment: |
||
| 1240 | cmd.add_element("comment", comment) |
||
| 1241 | |||
| 1242 | return self._send_xml_command(cmd) |
||
| 1243 | |||
| 1244 | def import_report( |
||
| 1245 | self, |
||
| 1246 | report, |
||
| 1247 | *, |
||
| 1248 | task_id=None, |
||
| 1249 | task_name=None, |
||
| 1250 | task_comment=None, |
||
| 1251 | in_assets=None |
||
| 1252 | ): |
||
| 1253 | """Import a Report from XML |
||
| 1254 | |||
| 1255 | Arguments: |
||
| 1256 | report (str): Report XML as string to import. This XML must contain |
||
| 1257 | a :code:`<report>` root element. |
||
| 1258 | task_id (str, optional): UUID of task to import report to |
||
| 1259 | task_name (str, optional): Name of task to be created if task_id is |
||
| 1260 | not present. Either task_id or task_name must be passed |
||
| 1261 | task_comment (str, optional): Comment for task to be created if |
||
| 1262 | task_id is not present |
||
| 1263 | in_asset (boolean, optional): Whether to create or update assets |
||
| 1264 | using the report |
||
| 1265 | |||
| 1266 | Returns: |
||
| 1267 | The response. See :py:meth:`send_command` for details. |
||
| 1268 | """ |
||
| 1269 | if not report: |
||
| 1270 | raise RequiredArgument("import_report requires a report argument") |
||
| 1271 | |||
| 1272 | cmd = XmlCommand("create_report") |
||
| 1273 | |||
| 1274 | if task_id: |
||
| 1275 | cmd.add_element("task", attrs={"id": task_id}) |
||
| 1276 | elif task_name: |
||
| 1277 | _xmltask = cmd.add_element("task") |
||
| 1278 | _xmltask.add_element("name", task_name) |
||
| 1279 | |||
| 1280 | if task_comment: |
||
| 1281 | _xmltask.add_element("comment", task_comment) |
||
| 1282 | else: |
||
| 1283 | raise RequiredArgument( |
||
| 1284 | "import_report requires a task_id or task_name argument" |
||
| 1285 | ) |
||
| 1286 | |||
| 1287 | if not in_assets is None: |
||
| 1288 | cmd.add_element("in_assets", _to_bool(in_assets)) |
||
| 1289 | |||
| 1290 | try: |
||
| 1291 | cmd.append_xml_str(report) |
||
| 1292 | except etree.XMLSyntaxError as e: |
||
| 1293 | raise InvalidArgument( |
||
| 1294 | "Invalid xml passed as report to import_report", e |
||
| 1295 | ) |
||
| 1296 | |||
| 1297 | return self._send_xml_command(cmd) |
||
| 1298 | |||
| 1299 | def create_role(self, name, *, comment=None, users=None): |
||
| 1300 | """Create a new role |
||
| 1301 | |||
| 1302 | Arguments: |
||
| 1303 | name (str): Name of the role |
||
| 1304 | comment (str, optional): Comment for the role |
||
| 1305 | users (list, optional): List of user names to add to the role |
||
| 1306 | |||
| 1307 | Returns: |
||
| 1308 | The response. See :py:meth:`send_command` for details. |
||
| 1309 | """ |
||
| 1310 | |||
| 1311 | if not name: |
||
| 1312 | raise RequiredArgument("create_role requires a name argument") |
||
| 1313 | |||
| 1314 | cmd = XmlCommand("create_role") |
||
| 1315 | cmd.add_element("name", name) |
||
| 1316 | |||
| 1317 | if comment: |
||
| 1318 | cmd.add_element("comment", comment) |
||
| 1319 | |||
| 1320 | if users: |
||
| 1321 | cmd.add_element("users", _to_comma_list(users)) |
||
| 1322 | |||
| 1323 | return self._send_xml_command(cmd) |
||
| 1324 | |||
| 1325 | def clone_role(self, role_id): |
||
| 1326 | """Clone an existing role |
||
| 1327 | |||
| 1328 | Arguments: |
||
| 1329 | copy (str): UUID of an existing role to clone from |
||
| 1330 | |||
| 1331 | Returns: |
||
| 1332 | The response. See :py:meth:`send_command` for details. |
||
| 1333 | """ |
||
| 1334 | if not role_id: |
||
| 1335 | raise RequiredArgument("clone_role requires a role_id argument") |
||
| 1336 | |||
| 1337 | cmd = XmlCommand("create_role") |
||
| 1338 | cmd.add_element("copy", role_id) |
||
| 1339 | return self._send_xml_command(cmd) |
||
| 1340 | |||
| 1341 | def create_scanner( |
||
| 1342 | self, |
||
| 1343 | name, |
||
| 1344 | host, |
||
| 1345 | port, |
||
| 1346 | scanner_type, |
||
| 1347 | credential_id, |
||
| 1348 | *, |
||
| 1349 | ca_pub=None, |
||
| 1350 | comment=None |
||
| 1351 | ): |
||
| 1352 | """Create a new scanner |
||
| 1353 | |||
| 1354 | Arguments: |
||
| 1355 | name (str): Name of the scanner |
||
| 1356 | host (str): The host of the scanner |
||
| 1357 | port (int): The port of the scanner |
||
| 1358 | scanner_type (str): Type of the scanner. |
||
| 1359 | '1' for OSP, '2' for OpenVAS (classic) Scanner. |
||
| 1360 | credential_id (str): UUID of client certificate credential for the |
||
| 1361 | scanner |
||
| 1362 | ca_pub (str, optional): Certificate of CA to verify scanner |
||
| 1363 | certificate |
||
| 1364 | comment (str, optional): Comment for the scanner |
||
| 1365 | |||
| 1366 | Returns: |
||
| 1367 | The response. See :py:meth:`send_command` for details. |
||
| 1368 | """ |
||
| 1369 | if not name: |
||
| 1370 | raise RequiredArgument("create_scanner requires a name argument") |
||
| 1371 | |||
| 1372 | if not host: |
||
| 1373 | raise RequiredArgument("create_scanner requires a host argument") |
||
| 1374 | |||
| 1375 | if not port: |
||
| 1376 | raise RequiredArgument("create_scanner requires a port argument") |
||
| 1377 | |||
| 1378 | if not scanner_type: |
||
| 1379 | raise RequiredArgument( |
||
| 1380 | "create_scanner requires a scanner_type " "argument" |
||
| 1381 | ) |
||
| 1382 | |||
| 1383 | if not credential_id: |
||
| 1384 | raise RequiredArgument( |
||
| 1385 | "create_scanner requires a credential_id " "argument" |
||
| 1386 | ) |
||
| 1387 | |||
| 1388 | if scanner_type not in SCANNER_TYPES: |
||
| 1389 | raise InvalidArgument( |
||
| 1390 | "create_scanner requires a scanner_type " |
||
| 1391 | 'argument which must be either "1" for OSP, ' |
||
| 1392 | '"2" for OpenVAS (Classic), "3" for CVE or ' |
||
| 1393 | '"4" for GMP Scanner.' |
||
| 1394 | ) |
||
| 1395 | |||
| 1396 | cmd = XmlCommand("create_scanner") |
||
| 1397 | cmd.add_element("name", name) |
||
| 1398 | cmd.add_element("host", host) |
||
| 1399 | cmd.add_element("port", str(port)) |
||
| 1400 | cmd.add_element("type", scanner_type) |
||
| 1401 | |||
| 1402 | if ca_pub: |
||
| 1403 | cmd.add_element("ca_pub", ca_pub) |
||
| 1404 | |||
| 1405 | cmd.add_element("credential", attrs={"id": str(credential_id)}) |
||
| 1406 | |||
| 1407 | if comment: |
||
| 1408 | cmd.add_element("comment", comment) |
||
| 1409 | |||
| 1410 | return self._send_xml_command(cmd) |
||
| 1411 | |||
| 1412 | def clone_scanner(self, scanner_id): |
||
| 1413 | """Clone an existing scanner |
||
| 1414 | |||
| 1415 | Arguments: |
||
| 1416 | copy (str): UUID of an existing scanner to clone from |
||
| 1417 | |||
| 1418 | Returns: |
||
| 1419 | The response. See :py:meth:`send_command` for details. |
||
| 1420 | """ |
||
| 1421 | if not scanner_id: |
||
| 1422 | raise RequiredArgument( |
||
| 1423 | "clone_scanner requires a scanner_id argument" |
||
| 1424 | ) |
||
| 1425 | |||
| 1426 | cmd = XmlCommand("create_scanner") |
||
| 1427 | cmd.add_element("copy", scanner_id) |
||
| 1428 | return self._send_xml_command(cmd) |
||
| 1429 | |||
| 1430 | View Code Duplication | def create_schedule( |
|
| 1431 | self, |
||
| 1432 | name, |
||
| 1433 | *, |
||
| 1434 | comment=None, |
||
| 1435 | first_time_minute=None, |
||
| 1436 | first_time_hour=None, |
||
| 1437 | first_time_day_of_month=None, |
||
| 1438 | first_time_month=None, |
||
| 1439 | first_time_year=None, |
||
| 1440 | duration=None, |
||
| 1441 | duration_unit=None, |
||
| 1442 | period=None, |
||
| 1443 | period_unit=None, |
||
| 1444 | timezone=None |
||
| 1445 | ): |
||
| 1446 | """Create a new schedule |
||
| 1447 | |||
| 1448 | Arguments: |
||
| 1449 | name (str): Name of the schedule |
||
| 1450 | comment (str, optional): Comment for the schedule |
||
| 1451 | first_time_minute (int, optional): First time minute the schedule |
||
| 1452 | will run. Must be an integer >= 0. |
||
| 1453 | first_time_hour (int, optional): First time hour the schedule |
||
| 1454 | will run. Must be an integer >= 0. |
||
| 1455 | first_time_day_of_month (int, optional): First time day of month the |
||
| 1456 | schedule will run. Must be an integer > 0 <= 31. |
||
| 1457 | first_time_month (int, optional): First time month the schedule |
||
| 1458 | will run. Must be an integer >= 1 <= 12. |
||
| 1459 | first_time_year (int, optional): First time year the schedule |
||
| 1460 | will run. Must be an integer >= 1970. |
||
| 1461 | duration (int, optional): How long the Manager will run the |
||
| 1462 | scheduled task for until it gets paused if not finished yet. |
||
| 1463 | Must be an integer > 0. |
||
| 1464 | duration_unit (str, optional): Unit of the duration. One of second, |
||
| 1465 | minute, hour, day, week, month, year, decade. Required if |
||
| 1466 | duration is set. |
||
| 1467 | period (int, optional): How often the Manager will repeat the |
||
| 1468 | scheduled task. Must be an integer > 0. |
||
| 1469 | period_unit (str, optional): Unit of the period. One of second, |
||
| 1470 | minute, hour, day, week, month, year, decade. Required if |
||
| 1471 | period is set. |
||
| 1472 | timezone (str, optional): The timezone the schedule will follow |
||
| 1473 | |||
| 1474 | Returns: |
||
| 1475 | The response. See :py:meth:`send_command` for details. |
||
| 1476 | """ |
||
| 1477 | if not name: |
||
| 1478 | raise RequiredArgument("create_schedule requires a name argument") |
||
| 1479 | |||
| 1480 | cmd = XmlCommand("create_schedule") |
||
| 1481 | cmd.add_element("name", name) |
||
| 1482 | |||
| 1483 | if comment: |
||
| 1484 | cmd.add_element("comment", comment) |
||
| 1485 | |||
| 1486 | if ( |
||
| 1487 | first_time_minute is not None |
||
| 1488 | or first_time_hour is not None |
||
| 1489 | or first_time_day_of_month is not None |
||
| 1490 | or first_time_month is not None |
||
| 1491 | or first_time_year is not None |
||
| 1492 | ): |
||
| 1493 | |||
| 1494 | if first_time_minute is None: |
||
| 1495 | raise RequiredArgument( |
||
| 1496 | "Setting first_time requires first_time_minute argument" |
||
| 1497 | ) |
||
| 1498 | elif ( |
||
| 1499 | not isinstance(first_time_minute, numbers.Integral) |
||
| 1500 | or first_time_minute < 0 |
||
| 1501 | ): |
||
| 1502 | raise InvalidArgument( |
||
| 1503 | "first_time_minute argument of create_schedule needs to be " |
||
| 1504 | "an integer greater or equal 0" |
||
| 1505 | ) |
||
| 1506 | |||
| 1507 | if first_time_hour is None: |
||
| 1508 | raise RequiredArgument( |
||
| 1509 | "Setting first_time requires first_time_hour argument" |
||
| 1510 | ) |
||
| 1511 | elif ( |
||
| 1512 | not isinstance(first_time_hour, numbers.Integral) |
||
| 1513 | or first_time_hour < 0 |
||
| 1514 | ): |
||
| 1515 | raise InvalidArgument( |
||
| 1516 | "first_time_hour argument of create_schedule needs to be " |
||
| 1517 | "an integer greater or equal 0" |
||
| 1518 | ) |
||
| 1519 | |||
| 1520 | if first_time_day_of_month is None: |
||
| 1521 | raise RequiredArgument( |
||
| 1522 | "Setting first_time requires first_time_day_of_month " |
||
| 1523 | "argument" |
||
| 1524 | ) |
||
| 1525 | elif ( |
||
| 1526 | not isinstance(first_time_day_of_month, numbers.Integral) |
||
| 1527 | or first_time_day_of_month < 1 |
||
| 1528 | or first_time_day_of_month > 31 |
||
| 1529 | ): |
||
| 1530 | raise InvalidArgument( |
||
| 1531 | "first_time_day_of_month argument of create_schedule needs " |
||
| 1532 | "to be an integer between 1 and 31" |
||
| 1533 | ) |
||
| 1534 | |||
| 1535 | if first_time_month is None: |
||
| 1536 | raise RequiredArgument( |
||
| 1537 | "Setting first_time requires first_time_month argument" |
||
| 1538 | ) |
||
| 1539 | elif ( |
||
| 1540 | not isinstance(first_time_month, numbers.Integral) |
||
| 1541 | or first_time_month < 1 |
||
| 1542 | or first_time_month > 12 |
||
| 1543 | ): |
||
| 1544 | raise InvalidArgument( |
||
| 1545 | "first_time_month argument of create_schedule needs " |
||
| 1546 | "to be an integer between 1 and 12" |
||
| 1547 | ) |
||
| 1548 | |||
| 1549 | if first_time_year is None: |
||
| 1550 | raise RequiredArgument( |
||
| 1551 | "Setting first_time requires first_time_year argument" |
||
| 1552 | ) |
||
| 1553 | elif ( |
||
| 1554 | not isinstance(first_time_year, numbers.Integral) |
||
| 1555 | or first_time_year < 1970 |
||
| 1556 | ): |
||
| 1557 | raise InvalidArgument( |
||
| 1558 | "first_time_year argument of create_schedule needs " |
||
| 1559 | "to be an integer greater or equal 1970" |
||
| 1560 | ) |
||
| 1561 | |||
| 1562 | _xmlftime = cmd.add_element("first_time") |
||
| 1563 | _xmlftime.add_element("minute", str(first_time_minute)) |
||
| 1564 | _xmlftime.add_element("hour", str(first_time_hour)) |
||
| 1565 | _xmlftime.add_element("day_of_month", str(first_time_day_of_month)) |
||
| 1566 | _xmlftime.add_element("month", str(first_time_month)) |
||
| 1567 | _xmlftime.add_element("year", str(first_time_year)) |
||
| 1568 | |||
| 1569 | if duration is not None: |
||
| 1570 | if not duration_unit: |
||
| 1571 | raise RequiredArgument( |
||
| 1572 | "Setting duration requires duration_unit argument" |
||
| 1573 | ) |
||
| 1574 | |||
| 1575 | if not duration_unit in TIME_UNITS: |
||
| 1576 | raise InvalidArgument( |
||
| 1577 | "duration_unit must be one of {units}. But {actual} has " |
||
| 1578 | "been passed".format( |
||
| 1579 | units=", ".join(TIME_UNITS), actual=duration_unit |
||
| 1580 | ) |
||
| 1581 | ) |
||
| 1582 | |||
| 1583 | if not isinstance(duration, numbers.Integral) or duration < 1: |
||
| 1584 | raise InvalidArgument( |
||
| 1585 | "duration argument must be an integer greater than 0" |
||
| 1586 | ) |
||
| 1587 | |||
| 1588 | _xmlduration = cmd.add_element("duration", str(duration)) |
||
| 1589 | _xmlduration.add_element("unit", duration_unit) |
||
| 1590 | |||
| 1591 | if period is not None: |
||
| 1592 | if not period_unit: |
||
| 1593 | raise RequiredArgument( |
||
| 1594 | "Setting period requires period_unit argument" |
||
| 1595 | ) |
||
| 1596 | |||
| 1597 | if not period_unit in TIME_UNITS: |
||
| 1598 | raise InvalidArgument( |
||
| 1599 | "period_unit must be one of {units} but {actual} has " |
||
| 1600 | "been passed".format( |
||
| 1601 | units=", ".join(TIME_UNITS), actual=period_unit |
||
| 1602 | ) |
||
| 1603 | ) |
||
| 1604 | |||
| 1605 | if not isinstance(period, numbers.Integral) or period < 1: |
||
| 1606 | raise InvalidArgument( |
||
| 1607 | "period argument must be an integer greater than 0" |
||
| 1608 | ) |
||
| 1609 | |||
| 1610 | _xmlperiod = cmd.add_element("period", str(period)) |
||
| 1611 | _xmlperiod.add_element("unit", period_unit) |
||
| 1612 | |||
| 1613 | if timezone: |
||
| 1614 | cmd.add_element("timezone", timezone) |
||
| 1615 | |||
| 1616 | return self._send_xml_command(cmd) |
||
| 1617 | |||
| 1618 | def clone_schedule(self, schedule_id): |
||
| 1619 | """Clone an existing schedule |
||
| 1620 | |||
| 1621 | Arguments: |
||
| 1622 | copy (str): UUID of an existing schedule to clone from |
||
| 1623 | |||
| 1624 | Returns: |
||
| 1625 | The response. See :py:meth:`send_command` for details. |
||
| 1626 | """ |
||
| 1627 | if not schedule_id: |
||
| 1628 | raise RequiredArgument( |
||
| 1629 | "clone_schedule requires a schedule_id argument" |
||
| 1630 | ) |
||
| 1631 | |||
| 1632 | cmd = XmlCommand("create_schedule") |
||
| 1633 | cmd.add_element("copy", schedule_id) |
||
| 1634 | return self._send_xml_command(cmd) |
||
| 1635 | |||
| 1636 | View Code Duplication | def create_tag( |
|
| 1637 | self, |
||
| 1638 | name, |
||
| 1639 | resource_id, |
||
| 1640 | resource_type, |
||
| 1641 | *, |
||
| 1642 | value=None, |
||
| 1643 | comment=None, |
||
| 1644 | active=None |
||
| 1645 | ): |
||
| 1646 | """Create a new tag |
||
| 1647 | |||
| 1648 | Arguments: |
||
| 1649 | name (str): Name of the tag. A full tag name consisting of namespace |
||
| 1650 | and predicate e.g. `foo:bar`. |
||
| 1651 | resource_id (str): ID of the resource the tag is to be attached to. |
||
| 1652 | resource_type (str): Entity type the tag is to be attached to |
||
| 1653 | value (str, optional): Value associated with the tag |
||
| 1654 | comment (str, optional): Comment for the tag |
||
| 1655 | active (boolean, optional): Whether the tag should be active |
||
| 1656 | |||
| 1657 | Returns: |
||
| 1658 | The response. See :py:meth:`send_command` for details. |
||
| 1659 | """ |
||
| 1660 | if not name: |
||
| 1661 | raise RequiredArgument("create_tag requires name argument") |
||
| 1662 | |||
| 1663 | if not resource_id: |
||
| 1664 | raise RequiredArgument("create_tag requires resource_id argument") |
||
| 1665 | |||
| 1666 | if not resource_type: |
||
| 1667 | raise RequiredArgument("create_tag requires resource_type argument") |
||
| 1668 | |||
| 1669 | cmd = XmlCommand("create_tag") |
||
| 1670 | cmd.add_element("name", name) |
||
| 1671 | _xmlresource = cmd.add_element( |
||
| 1672 | "resource", attrs={"id": str(resource_id)} |
||
| 1673 | ) |
||
| 1674 | _xmlresource.add_element("type", resource_type) |
||
| 1675 | |||
| 1676 | if comment: |
||
| 1677 | cmd.add_element("comment", comment) |
||
| 1678 | |||
| 1679 | if value: |
||
| 1680 | cmd.add_element("value", value) |
||
| 1681 | |||
| 1682 | if not active is None: |
||
| 1683 | if active: |
||
| 1684 | cmd.add_element("active", "1") |
||
| 1685 | else: |
||
| 1686 | cmd.add_element("active", "0") |
||
| 1687 | |||
| 1688 | return self._send_xml_command(cmd) |
||
| 1689 | |||
| 1690 | def clone_tag(self, tag_id): |
||
| 1691 | """Clone an existing tag |
||
| 1692 | |||
| 1693 | Arguments: |
||
| 1694 | copy (str): UUID of an existing tag to clone from |
||
| 1695 | |||
| 1696 | Returns: |
||
| 1697 | The response. See :py:meth:`send_command` for details. |
||
| 1698 | """ |
||
| 1699 | if not tag_id: |
||
| 1700 | raise RequiredArgument("clone_tag requires a tag_id argument") |
||
| 1701 | |||
| 1702 | cmd = XmlCommand("create_tag") |
||
| 1703 | cmd.add_element("copy", tag_id) |
||
| 1704 | return self._send_xml_command(cmd) |
||
| 1705 | |||
| 1706 | def create_target( |
||
| 1707 | self, |
||
| 1708 | name, |
||
| 1709 | *, |
||
| 1710 | make_unique=None, |
||
| 1711 | asset_hosts_filter=None, |
||
| 1712 | hosts=None, |
||
| 1713 | comment=None, |
||
| 1714 | exclude_hosts=None, |
||
| 1715 | ssh_credential_id=None, |
||
| 1716 | ssh_credential_port=None, |
||
| 1717 | smb_credential_id=None, |
||
| 1718 | esxi_credential_id=None, |
||
| 1719 | snmp_credential_id=None, |
||
| 1720 | alive_tests=None, |
||
| 1721 | reverse_lookup_only=None, |
||
| 1722 | reverse_lookup_unify=None, |
||
| 1723 | port_range=None, |
||
| 1724 | port_list_id=None |
||
| 1725 | ): |
||
| 1726 | """Create a new target |
||
| 1727 | |||
| 1728 | Arguments: |
||
| 1729 | name (str): Name of the target |
||
| 1730 | make_unique (boolean, optional): Append a unique suffix if the name |
||
| 1731 | already exists |
||
| 1732 | asset_hosts_filter (str, optional): Filter to select target host |
||
| 1733 | from assets hosts |
||
| 1734 | hosts (list, optional): List of hosts addresses to scan |
||
| 1735 | exclude_hosts (list, optional): List of hosts addresses to exclude |
||
| 1736 | from scan |
||
| 1737 | comment (str, optional): Comment for the target |
||
| 1738 | ssh_credential_id (str, optional): UUID of a ssh credential to use |
||
| 1739 | on target |
||
| 1740 | ssh_credential_port (int, optional): The port to use for ssh |
||
| 1741 | credential |
||
| 1742 | smb_credential_id (str, optional): UUID of a smb credential to use |
||
| 1743 | on target |
||
| 1744 | snmp_credential_id (str, optional): UUID of a snmp credential to use |
||
| 1745 | on target |
||
| 1746 | esxi_credential_id (str, optional): UUID of a esxi credential to use |
||
| 1747 | on target |
||
| 1748 | alive_tests (str, optional): Which alive tests to use |
||
| 1749 | reverse_lookup_only (boolean, optional): Whether to scan only hosts |
||
| 1750 | that have names |
||
| 1751 | reverse_lookup_unify (boolean, optional): Whether to scan only one |
||
| 1752 | IP when multiple IPs have the same name. |
||
| 1753 | port_range (str, optional): Port range for the target |
||
| 1754 | port_list_id (str, optional): UUID of the port list to use on target |
||
| 1755 | |||
| 1756 | Returns: |
||
| 1757 | The response. See :py:meth:`send_command` for details. |
||
| 1758 | """ |
||
| 1759 | if not name: |
||
| 1760 | raise RequiredArgument("create_target requires a name argument") |
||
| 1761 | |||
| 1762 | cmd = XmlCommand("create_target") |
||
| 1763 | _xmlname = cmd.add_element("name", name) |
||
| 1764 | |||
| 1765 | if make_unique is not None: |
||
| 1766 | _xmlname.add_element("make_unique", _to_bool(make_unique)) |
||
| 1767 | |||
| 1768 | if asset_hosts_filter: |
||
| 1769 | cmd.add_element( |
||
| 1770 | "asset_hosts", attrs={"filter": str(asset_hosts_filter)} |
||
| 1771 | ) |
||
| 1772 | elif hosts: |
||
| 1773 | cmd.add_element("hosts", _to_comma_list(hosts)) |
||
| 1774 | else: |
||
| 1775 | raise RequiredArgument( |
||
| 1776 | "create_target requires either a hosts or " |
||
| 1777 | "an asset_hosts_filter argument" |
||
| 1778 | ) |
||
| 1779 | |||
| 1780 | if comment: |
||
| 1781 | cmd.add_element("comment", comment) |
||
| 1782 | |||
| 1783 | if exclude_hosts: |
||
| 1784 | cmd.add_element("exclude_hosts", _to_comma_list(exclude_hosts)) |
||
| 1785 | |||
| 1786 | if ssh_credential_id: |
||
| 1787 | _xmlssh = cmd.add_element( |
||
| 1788 | "ssh_credential", attrs={"id": ssh_credential_id} |
||
| 1789 | ) |
||
| 1790 | if ssh_credential_port: |
||
| 1791 | _xmlssh.add_element("port", str(ssh_credential_port)) |
||
| 1792 | |||
| 1793 | if smb_credential_id: |
||
| 1794 | cmd.add_element("smb_credential", attrs={"id": smb_credential_id}) |
||
| 1795 | |||
| 1796 | if esxi_credential_id: |
||
| 1797 | cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id}) |
||
| 1798 | |||
| 1799 | if snmp_credential_id: |
||
| 1800 | cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id}) |
||
| 1801 | |||
| 1802 | if alive_tests: |
||
| 1803 | if not alive_tests in ALIVE_TESTS: |
||
| 1804 | raise InvalidArgument( |
||
| 1805 | "alive_tests must be one of {tests} but " |
||
| 1806 | "{actual} has been passed".format( |
||
| 1807 | tests="|".join(ALIVE_TESTS), actual=alive_tests |
||
| 1808 | ) |
||
| 1809 | ) |
||
| 1810 | |||
| 1811 | cmd.add_element("alive_tests", alive_tests) |
||
| 1812 | |||
| 1813 | if not reverse_lookup_only is None: |
||
| 1814 | cmd.add_element( |
||
| 1815 | "reverse_lookup_only", _to_bool(reverse_lookup_only) |
||
| 1816 | ) |
||
| 1817 | |||
| 1818 | if not reverse_lookup_unify is None: |
||
| 1819 | cmd.add_element( |
||
| 1820 | "reverse_lookup_unify", _to_bool(reverse_lookup_unify) |
||
| 1821 | ) |
||
| 1822 | |||
| 1823 | if port_range: |
||
| 1824 | cmd.add_element("port_range", port_range) |
||
| 1825 | |||
| 1826 | if port_list_id: |
||
| 1827 | cmd.add_element("port_list", attrs={"id": port_list_id}) |
||
| 1828 | |||
| 1829 | return self._send_xml_command(cmd) |
||
| 1830 | |||
| 1831 | def clone_target(self, target_id): |
||
| 1832 | """Clone an existing target |
||
| 1833 | |||
| 1834 | Arguments: |
||
| 1835 | copy (str): UUID of an existing target to clone from |
||
| 1836 | |||
| 1837 | Returns: |
||
| 1838 | The response. See :py:meth:`send_command` for details. |
||
| 1839 | """ |
||
| 1840 | if not target_id: |
||
| 1841 | raise RequiredArgument("clone_target requires a target_id argument") |
||
| 1842 | |||
| 1843 | cmd = XmlCommand("create_target") |
||
| 1844 | cmd.add_element("copy", target_id) |
||
| 1845 | return self._send_xml_command(cmd) |
||
| 1846 | |||
| 1847 | def create_task( |
||
| 1848 | self, |
||
| 1849 | name, |
||
| 1850 | config_id, |
||
| 1851 | target_id, |
||
| 1852 | scanner_id, |
||
| 1853 | *, |
||
| 1854 | alterable=None, |
||
| 1855 | hosts_ordering=None, |
||
| 1856 | schedule_id=None, |
||
| 1857 | alert_ids=None, |
||
| 1858 | comment=None, |
||
| 1859 | schedule_periods=None, |
||
| 1860 | observers=None, |
||
| 1861 | preferences=None |
||
| 1862 | ): |
||
| 1863 | """Create a new task |
||
| 1864 | |||
| 1865 | Arguments: |
||
| 1866 | name (str): Name of the task |
||
| 1867 | config_id (str): UUID of scan config to use by the task |
||
| 1868 | target_id (str): UUID of target to be scanned |
||
| 1869 | scanner_id (str): UUID of scanner to use for scanning the target |
||
| 1870 | comment (str, optional): Comment for the task |
||
| 1871 | alterable (boolean, optional): Whether the task should be alterable |
||
| 1872 | alert_ids (list, optional): List of UUIDs for alerts to be applied |
||
| 1873 | to the task |
||
| 1874 | hosts_ordering (str, optional): The order hosts are scanned in |
||
| 1875 | schedule_id (str, optional): UUID of a schedule when the task should |
||
| 1876 | be run. |
||
| 1877 | schedule_periods (int, optional): A limit to the number of times the |
||
| 1878 | task will be scheduled, or 0 for no limit |
||
| 1879 | observers (list, optional): List of names or ids of users which |
||
| 1880 | should be allowed to observe this task |
||
| 1881 | preferences (dict, optional): Name/Value pairs of scanner |
||
| 1882 | preferences. |
||
| 1883 | |||
| 1884 | Returns: |
||
| 1885 | The response. See :py:meth:`send_command` for details. |
||
| 1886 | """ |
||
| 1887 | if not name: |
||
| 1888 | raise RequiredArgument("create_task requires a name argument") |
||
| 1889 | |||
| 1890 | if not config_id: |
||
| 1891 | raise RequiredArgument("create_task requires a config_id argument") |
||
| 1892 | |||
| 1893 | if not target_id: |
||
| 1894 | raise RequiredArgument("create_task requires a target_id argument") |
||
| 1895 | |||
| 1896 | if not scanner_id: |
||
| 1897 | raise RequiredArgument("create_task requires a scanner_id argument") |
||
| 1898 | |||
| 1899 | # don't allow to create a container task with create_task |
||
| 1900 | if target_id == '0': |
||
| 1901 | raise InvalidArgument( |
||
| 1902 | 'Invalid argument {} for target_id'.format(target_id) |
||
| 1903 | ) |
||
| 1904 | |||
| 1905 | cmd = XmlCommand("create_task") |
||
| 1906 | cmd.add_element("name", name) |
||
| 1907 | cmd.add_element("config", attrs={"id": config_id}) |
||
| 1908 | cmd.add_element("target", attrs={"id": target_id}) |
||
| 1909 | cmd.add_element("scanner", attrs={"id": scanner_id}) |
||
| 1910 | |||
| 1911 | if comment: |
||
| 1912 | cmd.add_element("comment", comment) |
||
| 1913 | |||
| 1914 | if not alterable is None: |
||
| 1915 | cmd.add_element("alterable", _to_bool(alterable)) |
||
| 1916 | |||
| 1917 | if hosts_ordering: |
||
| 1918 | # not sure about the possible values for hosts_orderning |
||
| 1919 | # it seems gvmd does not check the param |
||
| 1920 | # gsa allows to select 'sequential', 'random' or 'reverse' |
||
| 1921 | cmd.add_element("hosts_ordering", hosts_ordering) |
||
| 1922 | |||
| 1923 | if alert_ids: |
||
| 1924 | if isinstance(alert_ids, str): |
||
| 1925 | deprecation( |
||
| 1926 | "Please pass a list as alert_ids parameter to create_task. " |
||
| 1927 | "Passing a string is deprecated and will be removed in " |
||
| 1928 | "future." |
||
| 1929 | ) |
||
| 1930 | |||
| 1931 | # if a single id is given as a string wrap it into a list |
||
| 1932 | alert_ids = [alert_ids] |
||
| 1933 | if _is_list_like(alert_ids): |
||
| 1934 | # parse all given alert id's |
||
| 1935 | for alert in alert_ids: |
||
| 1936 | cmd.add_element("alert", attrs={"id": str(alert)}) |
||
| 1937 | |||
| 1938 | if schedule_id: |
||
| 1939 | cmd.add_element("schedule", attrs={"id": schedule_id}) |
||
| 1940 | |||
| 1941 | View Code Duplication | if schedule_periods is not None: |
|
| 1942 | if ( |
||
| 1943 | not isinstance(schedule_periods, numbers.Integral) |
||
| 1944 | or schedule_periods < 0 |
||
| 1945 | ): |
||
| 1946 | raise InvalidArgument( |
||
| 1947 | "schedule_periods must be an integer greater or equal " |
||
| 1948 | "than 0" |
||
| 1949 | ) |
||
| 1950 | cmd.add_element("schedule_periods", str(schedule_periods)) |
||
| 1951 | |||
| 1952 | if observers is not None: |
||
| 1953 | if not _is_list_like(observers): |
||
| 1954 | raise InvalidArgument("obeservers argument must be a list") |
||
| 1955 | |||
| 1956 | # gvmd splits by comma and space |
||
| 1957 | # gvmd tries to lookup each value as user name and afterwards as |
||
| 1958 | # user id. So both user name and user id are possible |
||
| 1959 | cmd.add_element("observers", _to_comma_list(observers)) |
||
| 1960 | |||
| 1961 | if preferences is not None: |
||
| 1962 | if not isinstance(preferences, collections.abc.Mapping): |
||
| 1963 | raise InvalidArgument('preferences argument must be a dict') |
||
| 1964 | |||
| 1965 | _xmlprefs = cmd.add_element("preferences") |
||
| 1966 | for pref_name, pref_value in preferences.items(): |
||
| 1967 | _xmlpref = _xmlprefs.add_element("preference") |
||
| 1968 | _xmlpref.add_element("scanner_name", pref_name) |
||
| 1969 | _xmlpref.add_element("value", str(pref_value)) |
||
| 1970 | |||
| 1971 | return self._send_xml_command(cmd) |
||
| 1972 | |||
| 1973 | def create_container_task(self, name, *, comment=None): |
||
| 1974 | """Create a new container task |
||
| 1975 | |||
| 1976 | A container task is a "meta" task to import and view reports from other |
||
| 1977 | systems. |
||
| 1978 | |||
| 1979 | Arguments: |
||
| 1980 | name (str): Name of the task |
||
| 1981 | comment (str, optional): Comment for the task |
||
| 1982 | |||
| 1983 | Returns: |
||
| 1984 | The response. See :py:meth:`send_command` for details. |
||
| 1985 | """ |
||
| 1986 | if not name: |
||
| 1987 | raise RequiredArgument("create_task requires a name argument") |
||
| 1988 | |||
| 1989 | cmd = XmlCommand("create_task") |
||
| 1990 | cmd.add_element("name", name) |
||
| 1991 | cmd.add_element("target", attrs={"id": "0"}) |
||
| 1992 | |||
| 1993 | if comment: |
||
| 1994 | cmd.add_element("comment", comment) |
||
| 1995 | |||
| 1996 | return self._send_xml_command(cmd) |
||
| 1997 | |||
| 1998 | def clone_task(self, task_id): |
||
| 1999 | """Clone an existing task |
||
| 2000 | |||
| 2001 | Arguments: |
||
| 2002 | task_id (str): UUID of existing task to clone from |
||
| 2003 | |||
| 2004 | Returns: |
||
| 2005 | The response. See :py:meth:`send_command` for details. |
||
| 2006 | """ |
||
| 2007 | if not task_id: |
||
| 2008 | raise RequiredArgument("clone_task requires a task_id argument") |
||
| 2009 | |||
| 2010 | cmd = XmlCommand("create_task") |
||
| 2011 | cmd.add_element("copy", task_id) |
||
| 2012 | return self._send_xml_command(cmd) |
||
| 2013 | |||
| 2014 | def create_user( |
||
| 2015 | self, |
||
| 2016 | name, |
||
| 2017 | *, |
||
| 2018 | password=None, |
||
| 2019 | hosts=None, |
||
| 2020 | hosts_allow=False, |
||
| 2021 | ifaces=None, |
||
| 2022 | ifaces_allow=False, |
||
| 2023 | role_ids=None |
||
| 2024 | ): |
||
| 2025 | """Create a new user |
||
| 2026 | |||
| 2027 | Arguments: |
||
| 2028 | name (str): Name of the user |
||
| 2029 | password (str, optional): Password of the user |
||
| 2030 | hosts (list, optional): A list of host addresses (IPs, DNS names) |
||
| 2031 | hosts_allow (boolean, optional): If True allow only access to passed |
||
| 2032 | hosts otherwise deny access. Default is False for deny hosts. |
||
| 2033 | ifaces (list, optional): A list of interface names |
||
| 2034 | ifaces_allow (boolean, optional): If True allow only access to |
||
| 2035 | passed interfaces otherwise deny access. Default is False for |
||
| 2036 | deny interfaces. |
||
| 2037 | role_ids (list, optional): A list of role UUIDs for the user |
||
| 2038 | |||
| 2039 | Returns: |
||
| 2040 | The response. See :py:meth:`send_command` for details. |
||
| 2041 | """ |
||
| 2042 | if not name: |
||
| 2043 | raise RequiredArgument("create_user requires a name argument") |
||
| 2044 | |||
| 2045 | cmd = XmlCommand("create_user") |
||
| 2046 | cmd.add_element("name", name) |
||
| 2047 | |||
| 2048 | if password: |
||
| 2049 | cmd.add_element("password", password) |
||
| 2050 | |||
| 2051 | if hosts: |
||
| 2052 | cmd.add_element( |
||
| 2053 | "hosts", |
||
| 2054 | _to_comma_list(hosts), |
||
| 2055 | attrs={"allow": _to_bool(hosts_allow)}, |
||
| 2056 | ) |
||
| 2057 | |||
| 2058 | if ifaces: |
||
| 2059 | cmd.add_element( |
||
| 2060 | "ifaces", |
||
| 2061 | _to_comma_list(ifaces), |
||
| 2062 | attrs={"allow": _to_bool(ifaces_allow)}, |
||
| 2063 | ) |
||
| 2064 | |||
| 2065 | if role_ids: |
||
| 2066 | for role in role_ids: |
||
| 2067 | cmd.add_element("role", attrs={"id": role}) |
||
| 2068 | |||
| 2069 | return self._send_xml_command(cmd) |
||
| 2070 | |||
| 2071 | def clone_user(self, user_id): |
||
| 2072 | """Clone an existing user |
||
| 2073 | |||
| 2074 | Arguments: |
||
| 2075 | user_id (str): UUID of existing user to clone from |
||
| 2076 | |||
| 2077 | Returns: |
||
| 2078 | The response. See :py:meth:`send_command` for details. |
||
| 2079 | """ |
||
| 2080 | if not user_id: |
||
| 2081 | raise RequiredArgument("clone_user requires a user_id argument") |
||
| 2082 | |||
| 2083 | cmd = XmlCommand("create_user") |
||
| 2084 | cmd.add_element("copy", user_id) |
||
| 2085 | return self._send_xml_command(cmd) |
||
| 2086 | |||
| 2087 | def delete_agent(self, agent_id, *, ultimate=False): |
||
| 2088 | """Deletes an existing agent |
||
| 2089 | |||
| 2090 | Arguments: |
||
| 2091 | agent_id (str) UUID of the agent to be deleted. |
||
| 2092 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2093 | or to the trashcan. |
||
| 2094 | """ |
||
| 2095 | if not agent_id: |
||
| 2096 | raise RequiredArgument("delete_agent requires an agent_id argument") |
||
| 2097 | |||
| 2098 | cmd = XmlCommand("delete_agent") |
||
| 2099 | cmd.set_attribute("agent_id", agent_id) |
||
| 2100 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2101 | |||
| 2102 | return self._send_xml_command(cmd) |
||
| 2103 | |||
| 2104 | def delete_alert(self, alert_id, *, ultimate=False): |
||
| 2105 | """Deletes an existing alert |
||
| 2106 | |||
| 2107 | Arguments: |
||
| 2108 | alert_id (str) UUID of the alert to be deleted. |
||
| 2109 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2110 | or to the trashcan. |
||
| 2111 | """ |
||
| 2112 | if not alert_id: |
||
| 2113 | raise RequiredArgument("delete_alert requires an alert_id argument") |
||
| 2114 | |||
| 2115 | cmd = XmlCommand("delete_alert") |
||
| 2116 | cmd.set_attribute("alert_id", alert_id) |
||
| 2117 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2118 | |||
| 2119 | return self._send_xml_command(cmd) |
||
| 2120 | |||
| 2121 | def delete_asset(self, *, asset_id=None, report_id=None): |
||
| 2122 | """Deletes an existing asset |
||
| 2123 | |||
| 2124 | Arguments: |
||
| 2125 | asset_id (str, optional): UUID of the single asset to delete. |
||
| 2126 | report_id (str,optional): UUID of report from which to get all |
||
| 2127 | assets to delete. |
||
| 2128 | """ |
||
| 2129 | if not asset_id and not report_id: |
||
| 2130 | raise RequiredArgument( |
||
| 2131 | "delete_asset requires an asset_id or " "a report_id argument" |
||
| 2132 | ) |
||
| 2133 | |||
| 2134 | cmd = XmlCommand("delete_asset") |
||
| 2135 | if asset_id: |
||
| 2136 | cmd.set_attribute("asset_id", asset_id) |
||
| 2137 | else: |
||
| 2138 | cmd.set_attribute("report_id", report_id) |
||
| 2139 | |||
| 2140 | return self._send_xml_command(cmd) |
||
| 2141 | |||
| 2142 | def delete_config(self, config_id, *, ultimate=False): |
||
| 2143 | """Deletes an existing config |
||
| 2144 | |||
| 2145 | Arguments: |
||
| 2146 | config_id (str) UUID of the config to be deleted. |
||
| 2147 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2148 | or to the trashcan. |
||
| 2149 | """ |
||
| 2150 | if not config_id: |
||
| 2151 | raise RequiredArgument( |
||
| 2152 | "delete_config requires a " "config_id argument" |
||
| 2153 | ) |
||
| 2154 | |||
| 2155 | cmd = XmlCommand("delete_config") |
||
| 2156 | cmd.set_attribute("config_id", config_id) |
||
| 2157 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2158 | |||
| 2159 | return self._send_xml_command(cmd) |
||
| 2160 | |||
| 2161 | def delete_credential(self, credential_id, *, ultimate=False): |
||
| 2162 | """Deletes an existing credential |
||
| 2163 | |||
| 2164 | Arguments: |
||
| 2165 | credential_id (str) UUID of the credential to be deleted. |
||
| 2166 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2167 | or to the trashcan. |
||
| 2168 | """ |
||
| 2169 | if not credential_id: |
||
| 2170 | raise RequiredArgument( |
||
| 2171 | "delete_credential requires a " "credential_id argument" |
||
| 2172 | ) |
||
| 2173 | |||
| 2174 | cmd = XmlCommand("delete_credential") |
||
| 2175 | cmd.set_attribute("credential_id", credential_id) |
||
| 2176 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2177 | |||
| 2178 | return self._send_xml_command(cmd) |
||
| 2179 | |||
| 2180 | def delete_filter(self, filter_id, *, ultimate=False): |
||
| 2181 | """Deletes an existing filter |
||
| 2182 | |||
| 2183 | Arguments: |
||
| 2184 | filter_id (str) UUID of the filter to be deleted. |
||
| 2185 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2186 | or to the trashcan. |
||
| 2187 | """ |
||
| 2188 | if not filter_id: |
||
| 2189 | raise RequiredArgument( |
||
| 2190 | "delete_filter requires a " "filter_id argument" |
||
| 2191 | ) |
||
| 2192 | |||
| 2193 | cmd = XmlCommand("delete_filter") |
||
| 2194 | cmd.set_attribute("filter_id", filter_id) |
||
| 2195 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2196 | |||
| 2197 | return self._send_xml_command(cmd) |
||
| 2198 | |||
| 2199 | def delete_group(self, group_id, *, ultimate=False): |
||
| 2200 | """Deletes an existing group |
||
| 2201 | |||
| 2202 | Arguments: |
||
| 2203 | group_id (str) UUID of the group to be deleted. |
||
| 2204 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2205 | or to the trashcan. |
||
| 2206 | """ |
||
| 2207 | if not group_id: |
||
| 2208 | raise RequiredArgument( |
||
| 2209 | "delete_group requires a " "group_id argument" |
||
| 2210 | ) |
||
| 2211 | |||
| 2212 | cmd = XmlCommand("delete_group") |
||
| 2213 | cmd.set_attribute("group_id", group_id) |
||
| 2214 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2215 | |||
| 2216 | return self._send_xml_command(cmd) |
||
| 2217 | |||
| 2218 | def delete_note(self, note_id, *, ultimate=False): |
||
| 2219 | """Deletes an existing note |
||
| 2220 | |||
| 2221 | Arguments: |
||
| 2222 | note_id (str) UUID of the note to be deleted. |
||
| 2223 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2224 | or to the trashcan. |
||
| 2225 | """ |
||
| 2226 | if not note_id: |
||
| 2227 | raise RequiredArgument("delete_note requires a " "note_id argument") |
||
| 2228 | |||
| 2229 | cmd = XmlCommand("delete_note") |
||
| 2230 | cmd.set_attribute("note_id", note_id) |
||
| 2231 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2232 | |||
| 2233 | return self._send_xml_command(cmd) |
||
| 2234 | |||
| 2235 | def delete_override(self, override_id, *, ultimate=False): |
||
| 2236 | """Deletes an existing override |
||
| 2237 | |||
| 2238 | Arguments: |
||
| 2239 | override_id (str) UUID of the override to be deleted. |
||
| 2240 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2241 | or to the trashcan. |
||
| 2242 | """ |
||
| 2243 | if not override_id: |
||
| 2244 | raise RequiredArgument( |
||
| 2245 | "delete_override requires a " "override_id argument" |
||
| 2246 | ) |
||
| 2247 | |||
| 2248 | cmd = XmlCommand("delete_override") |
||
| 2249 | cmd.set_attribute("override_id", override_id) |
||
| 2250 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2251 | |||
| 2252 | return self._send_xml_command(cmd) |
||
| 2253 | |||
| 2254 | def delete_permission(self, permission_id, *, ultimate=False): |
||
| 2255 | """Deletes an existing permission |
||
| 2256 | |||
| 2257 | Arguments: |
||
| 2258 | permission_id (str) UUID of the permission to be deleted. |
||
| 2259 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2260 | or to the trashcan. |
||
| 2261 | """ |
||
| 2262 | if not permission_id: |
||
| 2263 | raise RequiredArgument( |
||
| 2264 | "delete_permission requires a " "permission_id argument" |
||
| 2265 | ) |
||
| 2266 | |||
| 2267 | cmd = XmlCommand("delete_permission") |
||
| 2268 | cmd.set_attribute("permission_id", permission_id) |
||
| 2269 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2270 | |||
| 2271 | return self._send_xml_command(cmd) |
||
| 2272 | |||
| 2273 | def delete_port_list(self, port_list_id, *, ultimate=False): |
||
| 2274 | """Deletes an existing port list |
||
| 2275 | |||
| 2276 | Arguments: |
||
| 2277 | port_list_id (str) UUID of the port list to be deleted. |
||
| 2278 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2279 | or to the trashcan. |
||
| 2280 | """ |
||
| 2281 | if not port_list_id: |
||
| 2282 | raise RequiredArgument( |
||
| 2283 | "delete_port_list requires a " "port_list_id argument" |
||
| 2284 | ) |
||
| 2285 | |||
| 2286 | cmd = XmlCommand("delete_port_list") |
||
| 2287 | cmd.set_attribute("port_list_id", port_list_id) |
||
| 2288 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2289 | |||
| 2290 | return self._send_xml_command(cmd) |
||
| 2291 | |||
| 2292 | def delete_port_range(self, port_range_id): |
||
| 2293 | """Deletes an existing port range |
||
| 2294 | |||
| 2295 | Arguments: |
||
| 2296 | port_range_id (str) UUID of the port range to be deleted. |
||
| 2297 | """ |
||
| 2298 | if not port_range_id: |
||
| 2299 | raise RequiredArgument( |
||
| 2300 | "delete_port_range requires a " "port_range_id argument" |
||
| 2301 | ) |
||
| 2302 | |||
| 2303 | cmd = XmlCommand("delete_port_range") |
||
| 2304 | cmd.set_attribute("port_range_id", port_range_id) |
||
| 2305 | |||
| 2306 | return self._send_xml_command(cmd) |
||
| 2307 | |||
| 2308 | def delete_report(self, report_id): |
||
| 2309 | """Deletes an existing report |
||
| 2310 | |||
| 2311 | Arguments: |
||
| 2312 | report_id (str) UUID of the report to be deleted. |
||
| 2313 | """ |
||
| 2314 | if not report_id: |
||
| 2315 | raise RequiredArgument( |
||
| 2316 | "delete_report requires a " "report_id argument" |
||
| 2317 | ) |
||
| 2318 | |||
| 2319 | cmd = XmlCommand("delete_report") |
||
| 2320 | cmd.set_attribute("report_id", report_id) |
||
| 2321 | |||
| 2322 | return self._send_xml_command(cmd) |
||
| 2323 | |||
| 2324 | def delete_report_format(self, report_format_id, *, ultimate=False): |
||
| 2325 | """Deletes an existing report format |
||
| 2326 | |||
| 2327 | Arguments: |
||
| 2328 | report_format_id (str) UUID of the report format to be deleted. |
||
| 2329 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2330 | or to the trashcan. |
||
| 2331 | """ |
||
| 2332 | if not report_format_id: |
||
| 2333 | raise RequiredArgument( |
||
| 2334 | "delete_report_format requires a " "report_format_id argument" |
||
| 2335 | ) |
||
| 2336 | |||
| 2337 | cmd = XmlCommand("delete_report_format") |
||
| 2338 | cmd.set_attribute("report_format_id", report_format_id) |
||
| 2339 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2340 | |||
| 2341 | return self._send_xml_command(cmd) |
||
| 2342 | |||
| 2343 | def delete_role(self, role_id, *, ultimate=False): |
||
| 2344 | """Deletes an existing role |
||
| 2345 | |||
| 2346 | Arguments: |
||
| 2347 | role_id (str) UUID of the role to be deleted. |
||
| 2348 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2349 | or to the trashcan. |
||
| 2350 | """ |
||
| 2351 | if not role_id: |
||
| 2352 | raise RequiredArgument("delete_role requires a " "role_id argument") |
||
| 2353 | |||
| 2354 | cmd = XmlCommand("delete_role") |
||
| 2355 | cmd.set_attribute("role_id", role_id) |
||
| 2356 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2357 | |||
| 2358 | return self._send_xml_command(cmd) |
||
| 2359 | |||
| 2360 | def delete_scanner(self, scanner_id, *, ultimate=False): |
||
| 2361 | """Deletes an existing scanner |
||
| 2362 | |||
| 2363 | Arguments: |
||
| 2364 | scanner_id (str) UUID of the scanner to be deleted. |
||
| 2365 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2366 | or to the trashcan. |
||
| 2367 | """ |
||
| 2368 | if not scanner_id: |
||
| 2369 | raise RequiredArgument( |
||
| 2370 | "delete_scanner requires a " "scanner_id argument" |
||
| 2371 | ) |
||
| 2372 | |||
| 2373 | cmd = XmlCommand("delete_scanner") |
||
| 2374 | cmd.set_attribute("scanner_id", scanner_id) |
||
| 2375 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2376 | |||
| 2377 | return self._send_xml_command(cmd) |
||
| 2378 | |||
| 2379 | def delete_schedule(self, schedule_id, *, ultimate=False): |
||
| 2380 | """Deletes an existing schedule |
||
| 2381 | |||
| 2382 | Arguments: |
||
| 2383 | schedule_id (str) UUID of the schedule to be deleted. |
||
| 2384 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2385 | or to the trashcan. |
||
| 2386 | """ |
||
| 2387 | if not schedule_id: |
||
| 2388 | raise RequiredArgument( |
||
| 2389 | "delete_schedule requires a " "schedule_id argument" |
||
| 2390 | ) |
||
| 2391 | |||
| 2392 | cmd = XmlCommand("delete_schedule") |
||
| 2393 | cmd.set_attribute("schedule_id", schedule_id) |
||
| 2394 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2395 | |||
| 2396 | return self._send_xml_command(cmd) |
||
| 2397 | |||
| 2398 | def delete_tag(self, tag_id, *, ultimate=False): |
||
| 2399 | """Deletes an existing tag |
||
| 2400 | |||
| 2401 | Arguments: |
||
| 2402 | tag_id (str) UUID of the tag to be deleted. |
||
| 2403 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2404 | or to the trashcan. |
||
| 2405 | """ |
||
| 2406 | if not tag_id: |
||
| 2407 | raise RequiredArgument("delete_tag requires a " "tag_id argument") |
||
| 2408 | |||
| 2409 | cmd = XmlCommand("delete_tag") |
||
| 2410 | cmd.set_attribute("tag_id", tag_id) |
||
| 2411 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2412 | |||
| 2413 | return self._send_xml_command(cmd) |
||
| 2414 | |||
| 2415 | def delete_target(self, target_id, *, ultimate=False): |
||
| 2416 | """Deletes an existing target |
||
| 2417 | |||
| 2418 | Arguments: |
||
| 2419 | target_id (str) UUID of the target to be deleted. |
||
| 2420 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2421 | or to the trashcan. |
||
| 2422 | """ |
||
| 2423 | if not target_id: |
||
| 2424 | raise RequiredArgument( |
||
| 2425 | "delete_target requires a " "target_id argument" |
||
| 2426 | ) |
||
| 2427 | |||
| 2428 | cmd = XmlCommand("delete_target") |
||
| 2429 | cmd.set_attribute("target_id", target_id) |
||
| 2430 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2431 | |||
| 2432 | return self._send_xml_command(cmd) |
||
| 2433 | |||
| 2434 | def delete_task(self, task_id, *, ultimate=False): |
||
| 2435 | """Deletes an existing task |
||
| 2436 | |||
| 2437 | Arguments: |
||
| 2438 | task_id (str) UUID of the task to be deleted. |
||
| 2439 | ultimate (boolean, optional): Whether to remove entirely, |
||
| 2440 | or to the trashcan. |
||
| 2441 | """ |
||
| 2442 | if not task_id: |
||
| 2443 | raise RequiredArgument("delete_task requires a " "task_id argument") |
||
| 2444 | |||
| 2445 | cmd = XmlCommand("delete_task") |
||
| 2446 | cmd.set_attribute("task_id", task_id) |
||
| 2447 | cmd.set_attribute("ultimate", _to_bool(ultimate)) |
||
| 2448 | |||
| 2449 | return self._send_xml_command(cmd) |
||
| 2450 | |||
| 2451 | def delete_user( |
||
| 2452 | self, user_id=None, *, name=None, inheritor_id=None, inheritor_name=None |
||
| 2453 | ): |
||
| 2454 | """Deletes an existing user |
||
| 2455 | |||
| 2456 | Either user_id or name must be passed. |
||
| 2457 | |||
| 2458 | Arguments: |
||
| 2459 | user_id (str, optional): UUID of the task to be deleted. |
||
| 2460 | name (str, optional): The name of the user to be deleted. |
||
| 2461 | inheritor_id (str, optional): The ID of the inheriting user |
||
| 2462 | or "self". Overrides inheritor_name. |
||
| 2463 | inheritor_name (str, optional): The name of the inheriting user. |
||
| 2464 | |||
| 2465 | """ |
||
| 2466 | if not user_id and not name: |
||
| 2467 | raise RequiredArgument( |
||
| 2468 | "delete_user requires a user_id or name argument" |
||
| 2469 | ) |
||
| 2470 | |||
| 2471 | cmd = XmlCommand("delete_user") |
||
| 2472 | |||
| 2473 | if user_id: |
||
| 2474 | cmd.set_attribute("user_id", user_id) |
||
| 2475 | |||
| 2476 | if name: |
||
| 2477 | cmd.set_attribute("name", name) |
||
| 2478 | |||
| 2479 | if inheritor_id: |
||
| 2480 | cmd.set_attribute("inheritor_id", inheritor_id) |
||
| 2481 | |||
| 2482 | if inheritor_name: |
||
| 2483 | cmd.set_attribute("inheritor_name", inheritor_name) |
||
| 2484 | |||
| 2485 | return self._send_xml_command(cmd) |
||
| 2486 | |||
| 2487 | def describe_auth(self): |
||
| 2488 | """Describe authentication methods |
||
| 2489 | |||
| 2490 | Returns a list of all used authentication methods if such a list is |
||
| 2491 | available. |
||
| 2492 | |||
| 2493 | Returns: |
||
| 2494 | The response. See :py:meth:`send_command` for details. |
||
| 2495 | """ |
||
| 2496 | return self._send_xml_command(XmlCommand("describe_auth")) |
||
| 2497 | |||
| 2498 | def empty_trashcan(self): |
||
| 2499 | """Empty the trashcan |
||
| 2500 | |||
| 2501 | Remove all entities from the trashcan. **Attention:** this command can |
||
| 2502 | not be reverted |
||
| 2503 | |||
| 2504 | Returns: |
||
| 2505 | The response. See :py:meth:`send_command` for details. |
||
| 2506 | """ |
||
| 2507 | return self._send_xml_command(XmlCommand("empty_trashcan")) |
||
| 2508 | |||
| 2509 | def get_agents( |
||
| 2510 | self, |
||
| 2511 | *, |
||
| 2512 | filter=None, |
||
| 2513 | filter_id=None, |
||
| 2514 | trash=None, |
||
| 2515 | details=None, |
||
| 2516 | format=None |
||
| 2517 | ): |
||
| 2518 | """Request a list of agents |
||
| 2519 | |||
| 2520 | Arguments: |
||
| 2521 | filter (str, optional): Filter term to use for the query |
||
| 2522 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2523 | the query |
||
| 2524 | trash (boolean, optional): True to request the agents in the |
||
| 2525 | trashcan |
||
| 2526 | details (boolean, optional): Whether to include agents package |
||
| 2527 | information when no format was provided |
||
| 2528 | format (str, optional): One of "installer", "howto_install" or |
||
| 2529 | "howto_use" |
||
| 2530 | |||
| 2531 | Returns: |
||
| 2532 | The response. See :py:meth:`send_command` for details. |
||
| 2533 | """ |
||
| 2534 | cmd = XmlCommand("get_agents") |
||
| 2535 | |||
| 2536 | _add_filter(cmd, filter, filter_id) |
||
| 2537 | |||
| 2538 | if not trash is None: |
||
| 2539 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 2540 | |||
| 2541 | if not details is None: |
||
| 2542 | cmd.set_attribute("details", _to_bool(details)) |
||
| 2543 | |||
| 2544 | if format: |
||
| 2545 | if not format in ("installer", "howto_install", "howto_use"): |
||
| 2546 | raise InvalidArgument( |
||
| 2547 | "installer argument needs to be one of installer, " |
||
| 2548 | "howto_install or howto_use" |
||
| 2549 | ) |
||
| 2550 | |||
| 2551 | cmd.set_attribute("format", format) |
||
| 2552 | |||
| 2553 | return self._send_xml_command(cmd) |
||
| 2554 | |||
| 2555 | def get_agent(self, agent_id): |
||
| 2556 | """Request a single agent |
||
| 2557 | |||
| 2558 | Arguments: |
||
| 2559 | agent_id (str): UUID of an existing agent |
||
| 2560 | |||
| 2561 | Returns: |
||
| 2562 | The response. See :py:meth:`send_command` for details. |
||
| 2563 | """ |
||
| 2564 | if not agent_id: |
||
| 2565 | raise RequiredArgument("get_agent requires an agent_id argument") |
||
| 2566 | |||
| 2567 | cmd = XmlCommand("get_agents") |
||
| 2568 | cmd.set_attribute("agent_id", agent_id) |
||
| 2569 | |||
| 2570 | # for single entity always request all details |
||
| 2571 | cmd.set_attribute("details", "1") |
||
| 2572 | return self._send_xml_command(cmd) |
||
| 2573 | |||
| 2574 | def get_aggregates(self, resource_type, **kwargs): |
||
| 2575 | """Request aggregated information on a resource type |
||
| 2576 | |||
| 2577 | Additional arguments can be set via the **kwargs parameter, but are not |
||
| 2578 | yet validated. |
||
| 2579 | |||
| 2580 | Arguments: |
||
| 2581 | resource_type (str): The GMP resource type to gather data from |
||
| 2582 | |||
| 2583 | Returns: |
||
| 2584 | The response. See :py:meth:`send_command` for details. |
||
| 2585 | """ |
||
| 2586 | if not resource_type: |
||
| 2587 | raise RequiredArgument( |
||
| 2588 | "get_aggregates requires resource_type argument" |
||
| 2589 | ) |
||
| 2590 | |||
| 2591 | if resource_type not in RESOURCE_TYPES: |
||
| 2592 | raise InvalidArgument( |
||
| 2593 | "get_aggregates requires a valid resource_type argument" |
||
| 2594 | ) |
||
| 2595 | |||
| 2596 | cmd = XmlCommand("get_aggregates") |
||
| 2597 | |||
| 2598 | cmd.set_attribute("type", resource_type) |
||
| 2599 | |||
| 2600 | cmd.set_attributes(kwargs) |
||
| 2601 | return self._send_xml_command(cmd) |
||
| 2602 | |||
| 2603 | View Code Duplication | def get_alerts( |
|
| 2604 | self, *, filter=None, filter_id=None, trash=None, tasks=None |
||
| 2605 | ): |
||
| 2606 | """Request a list of alerts |
||
| 2607 | |||
| 2608 | Arguments: |
||
| 2609 | filter (str, optional): Filter term to use for the query |
||
| 2610 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2611 | the query |
||
| 2612 | trash (boolean, optional): True to request the alerts in the |
||
| 2613 | trashcan |
||
| 2614 | tasks (boolean, optional): Whether to include the tasks using the |
||
| 2615 | alerts |
||
| 2616 | Returns: |
||
| 2617 | The response. See :py:meth:`send_command` for details. |
||
| 2618 | """ |
||
| 2619 | cmd = XmlCommand("get_alerts") |
||
| 2620 | |||
| 2621 | _add_filter(cmd, filter, filter_id) |
||
| 2622 | |||
| 2623 | if not trash is None: |
||
| 2624 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 2625 | |||
| 2626 | if not tasks is None: |
||
| 2627 | cmd.set_attribute("tasks", _to_bool(tasks)) |
||
| 2628 | |||
| 2629 | return self._send_xml_command(cmd) |
||
| 2630 | |||
| 2631 | def get_alert(self, alert_id): |
||
| 2632 | """Request a single alert |
||
| 2633 | |||
| 2634 | Arguments: |
||
| 2635 | alert_id (str): UUID of an existing alert |
||
| 2636 | |||
| 2637 | Returns: |
||
| 2638 | The response. See :py:meth:`send_command` for details. |
||
| 2639 | """ |
||
| 2640 | if not alert_id: |
||
| 2641 | raise RequiredArgument("get_alert requires an alert_id argument") |
||
| 2642 | |||
| 2643 | cmd = XmlCommand("get_alerts") |
||
| 2644 | cmd.set_attribute("alert_id", alert_id) |
||
| 2645 | return self._send_xml_command(cmd) |
||
| 2646 | |||
| 2647 | def get_assets(self, asset_type, *, filter=None, filter_id=None): |
||
| 2648 | """Request a list of assets |
||
| 2649 | |||
| 2650 | Arguments: |
||
| 2651 | asset_type (str): Either 'os' or 'host' |
||
| 2652 | filter (str, optional): Filter term to use for the query |
||
| 2653 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2654 | the query |
||
| 2655 | |||
| 2656 | Returns: |
||
| 2657 | The response. See :py:meth:`send_command` for details. |
||
| 2658 | """ |
||
| 2659 | if not asset_type in ASSET_TYPES: |
||
| 2660 | raise InvalidArgument("asset_type must be either os or host") |
||
| 2661 | |||
| 2662 | cmd = XmlCommand("get_assets") |
||
| 2663 | |||
| 2664 | cmd.set_attribute("type", asset_type) |
||
| 2665 | |||
| 2666 | _add_filter(cmd, filter, filter_id) |
||
| 2667 | |||
| 2668 | return self._send_xml_command(cmd) |
||
| 2669 | |||
| 2670 | def get_asset(self, asset_id, asset_type): |
||
| 2671 | """Request a single asset |
||
| 2672 | |||
| 2673 | Arguments: |
||
| 2674 | asset_type (str): Either 'os' or 'host' |
||
| 2675 | asset_id (str): UUID of an existing asset |
||
| 2676 | |||
| 2677 | Returns: |
||
| 2678 | The response. See :py:meth:`send_command` for details. |
||
| 2679 | """ |
||
| 2680 | if not asset_type in ASSET_TYPES: |
||
| 2681 | raise InvalidArgument("asset_type must be either os or host") |
||
| 2682 | |||
| 2683 | if not asset_id: |
||
| 2684 | raise RequiredArgument("get_asset requires an asset_type argument") |
||
| 2685 | |||
| 2686 | cmd = XmlCommand("get_assets") |
||
| 2687 | cmd.set_attribute("asset_id", asset_id) |
||
| 2688 | cmd.set_attribute("type", asset_type) |
||
| 2689 | |||
| 2690 | return self._send_xml_command(cmd) |
||
| 2691 | |||
| 2692 | def get_credentials( |
||
| 2693 | self, |
||
| 2694 | *, |
||
| 2695 | filter=None, |
||
| 2696 | filter_id=None, |
||
| 2697 | scanners=None, |
||
| 2698 | trash=None, |
||
| 2699 | targets=None |
||
| 2700 | ): |
||
| 2701 | """Request a list of credentials |
||
| 2702 | |||
| 2703 | Arguments: |
||
| 2704 | filter (str, optional): Filter term to use for the query |
||
| 2705 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2706 | the query |
||
| 2707 | scanners (boolean, optional): Whether to include a list of scanners |
||
| 2708 | using the credentials |
||
| 2709 | trash (boolean, optional): Whether to get the trashcan credentials |
||
| 2710 | instead |
||
| 2711 | targets (boolean, optional): Whether to include a list of targets |
||
| 2712 | using the credentials |
||
| 2713 | |||
| 2714 | Returns: |
||
| 2715 | The response. See :py:meth:`send_command` for details. |
||
| 2716 | """ |
||
| 2717 | cmd = XmlCommand("get_credentials") |
||
| 2718 | |||
| 2719 | _add_filter(cmd, filter, filter_id) |
||
| 2720 | |||
| 2721 | if not scanners is None: |
||
| 2722 | cmd.set_attribute("scanners", _to_bool(scanners)) |
||
| 2723 | |||
| 2724 | if not trash is None: |
||
| 2725 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 2726 | |||
| 2727 | if not targets is None: |
||
| 2728 | cmd.set_attribute("targets", _to_bool(targets)) |
||
| 2729 | |||
| 2730 | return self._send_xml_command(cmd) |
||
| 2731 | |||
| 2732 | def get_credential(self, credential_id, *, credential_format=None): |
||
| 2733 | """Request a single credential |
||
| 2734 | |||
| 2735 | Arguments: |
||
| 2736 | credential_id (str): UUID of an existing credential |
||
| 2737 | credential_format (str, optional): One of "key", "rpm", "deb", |
||
| 2738 | "exe" or "pem" |
||
| 2739 | |||
| 2740 | Returns: |
||
| 2741 | The response. See :py:meth:`send_command` for details. |
||
| 2742 | """ |
||
| 2743 | if not credential_id: |
||
| 2744 | raise RequiredArgument( |
||
| 2745 | "get_credential requires credential_id argument" |
||
| 2746 | ) |
||
| 2747 | |||
| 2748 | cmd = XmlCommand("get_credentials") |
||
| 2749 | cmd.set_attribute("credential_id", credential_id) |
||
| 2750 | if credential_format: |
||
| 2751 | if not credential_format in ("key", "rpm", "deb", "exe", "pem"): |
||
| 2752 | raise InvalidArgument( |
||
| 2753 | "credential_format argument needs to be one of " |
||
| 2754 | "key, rpm, deb, exe or pem" |
||
| 2755 | ) |
||
| 2756 | |||
| 2757 | cmd.set_attribute("format", credential_format) |
||
| 2758 | return self._send_xml_command(cmd) |
||
| 2759 | |||
| 2760 | def get_configs( |
||
| 2761 | self, |
||
| 2762 | *, |
||
| 2763 | filter=None, |
||
| 2764 | filter_id=None, |
||
| 2765 | trash=None, |
||
| 2766 | details=None, |
||
| 2767 | families=None, |
||
| 2768 | preferences=None, |
||
| 2769 | tasks=None |
||
| 2770 | ): |
||
| 2771 | """Request a list of scan configs |
||
| 2772 | |||
| 2773 | Arguments: |
||
| 2774 | filter (str, optional): Filter term to use for the query |
||
| 2775 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2776 | the query |
||
| 2777 | trash (boolean, optional): Whether to get the trashcan scan configs |
||
| 2778 | instead |
||
| 2779 | details (boolean, optional): Whether to get config families, |
||
| 2780 | preferences, nvt selectors and tasks. |
||
| 2781 | families (boolean, optional): Whether to include the families if no |
||
| 2782 | details are requested |
||
| 2783 | preferences (boolean, optional): Whether to include the preferences |
||
| 2784 | if no details are requested |
||
| 2785 | tasks (boolean, optional): Whether to get tasks using this config |
||
| 2786 | |||
| 2787 | Returns: |
||
| 2788 | The response. See :py:meth:`send_command` for details. |
||
| 2789 | """ |
||
| 2790 | cmd = XmlCommand("get_configs") |
||
| 2791 | |||
| 2792 | _add_filter(cmd, filter, filter_id) |
||
| 2793 | |||
| 2794 | if not trash is None: |
||
| 2795 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 2796 | |||
| 2797 | if not details is None: |
||
| 2798 | cmd.set_attribute("details", _to_bool(details)) |
||
| 2799 | |||
| 2800 | if not families is None: |
||
| 2801 | cmd.set_attribute("families", _to_bool(families)) |
||
| 2802 | |||
| 2803 | if not preferences is None: |
||
| 2804 | cmd.set_attribute("preferences", _to_bool(preferences)) |
||
| 2805 | |||
| 2806 | if not tasks is None: |
||
| 2807 | cmd.set_attribute("tasks", _to_bool(tasks)) |
||
| 2808 | |||
| 2809 | return self._send_xml_command(cmd) |
||
| 2810 | |||
| 2811 | def get_config(self, config_id): |
||
| 2812 | """Request a single scan config |
||
| 2813 | |||
| 2814 | Arguments: |
||
| 2815 | config_id (str): UUID of an existing scan config |
||
| 2816 | |||
| 2817 | Returns: |
||
| 2818 | The response. See :py:meth:`send_command` for details. |
||
| 2819 | """ |
||
| 2820 | if not config_id: |
||
| 2821 | raise RequiredArgument("get_config requires config_id argument") |
||
| 2822 | |||
| 2823 | cmd = XmlCommand("get_configs") |
||
| 2824 | cmd.set_attribute("config_id", config_id) |
||
| 2825 | |||
| 2826 | # for single entity always request all details |
||
| 2827 | cmd.set_attribute("details", "1") |
||
| 2828 | return self._send_xml_command(cmd) |
||
| 2829 | |||
| 2830 | def get_feeds(self): |
||
| 2831 | """Request the list of feeds |
||
| 2832 | |||
| 2833 | Returns: |
||
| 2834 | The response. See :py:meth:`send_command` for details. |
||
| 2835 | """ |
||
| 2836 | return self._send_xml_command(XmlCommand("get_feeds")) |
||
| 2837 | |||
| 2838 | def get_feed(self, feed_type): |
||
| 2839 | """Request a single feed |
||
| 2840 | |||
| 2841 | Arguments: |
||
| 2842 | feed_type (str): Type of single feed to get: NVT, CERT or SCAP |
||
| 2843 | |||
| 2844 | Returns: |
||
| 2845 | The response. See :py:meth:`send_command` for details. |
||
| 2846 | """ |
||
| 2847 | if not feed_type: |
||
| 2848 | raise RequiredArgument("get_feed requires a feed_type argument") |
||
| 2849 | |||
| 2850 | feed_type = feed_type.upper() |
||
| 2851 | |||
| 2852 | if not feed_type in ("NVT", "CERT", "SCAP"): |
||
| 2853 | raise InvalidArgument( |
||
| 2854 | "get_feed type arguments must be one of NVT, CERT or SCAP" |
||
| 2855 | ) |
||
| 2856 | |||
| 2857 | cmd = XmlCommand("get_feeds") |
||
| 2858 | cmd.set_attribute("type", feed_type) |
||
| 2859 | |||
| 2860 | return self._send_xml_command(cmd) |
||
| 2861 | |||
| 2862 | def get_filters( |
||
| 2863 | self, *, filter=None, filter_id=None, trash=None, alerts=None |
||
| 2864 | ): |
||
| 2865 | """Request a list of filters |
||
| 2866 | |||
| 2867 | Arguments: |
||
| 2868 | filter (str, optional): Filter term to use for the query |
||
| 2869 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2870 | the query |
||
| 2871 | trash (boolean, optional): Whether to get the trashcan filters |
||
| 2872 | instead |
||
| 2873 | alerts (boolean, optional): Whether to include list of alerts that |
||
| 2874 | use the filter. |
||
| 2875 | |||
| 2876 | Returns: |
||
| 2877 | The response. See :py:meth:`send_command` for details. |
||
| 2878 | """ |
||
| 2879 | cmd = XmlCommand("get_filters") |
||
| 2880 | |||
| 2881 | _add_filter(cmd, filter, filter_id) |
||
| 2882 | |||
| 2883 | if not trash is None: |
||
| 2884 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 2885 | |||
| 2886 | if not alerts is None: |
||
| 2887 | cmd.set_attribute("alerts", _to_bool(alerts)) |
||
| 2888 | |||
| 2889 | return self._send_xml_command(cmd) |
||
| 2890 | |||
| 2891 | def get_filter(self, filter_id): |
||
| 2892 | """Request a single filter |
||
| 2893 | |||
| 2894 | Arguments: |
||
| 2895 | filter_id (str): UUID of an existing filter |
||
| 2896 | |||
| 2897 | Returns: |
||
| 2898 | The response. See :py:meth:`send_command` for details. |
||
| 2899 | """ |
||
| 2900 | if not filter_id: |
||
| 2901 | raise RequiredArgument("get_filter requires a filter_id argument") |
||
| 2902 | |||
| 2903 | cmd = XmlCommand("get_filters") |
||
| 2904 | cmd.set_attribute("filter_id", filter_id) |
||
| 2905 | return self._send_xml_command(cmd) |
||
| 2906 | |||
| 2907 | def get_groups(self, *, filter=None, filter_id=None, trash=None): |
||
| 2908 | """Request a list of groups |
||
| 2909 | |||
| 2910 | Arguments: |
||
| 2911 | filter (str, optional): Filter term to use for the query |
||
| 2912 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2913 | the query |
||
| 2914 | trash (boolean, optional): Whether to get the trashcan groups |
||
| 2915 | instead |
||
| 2916 | |||
| 2917 | Returns: |
||
| 2918 | The response. See :py:meth:`send_command` for details. |
||
| 2919 | """ |
||
| 2920 | cmd = XmlCommand("get_groups") |
||
| 2921 | |||
| 2922 | _add_filter(cmd, filter, filter_id) |
||
| 2923 | |||
| 2924 | if not trash is None: |
||
| 2925 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 2926 | |||
| 2927 | return self._send_xml_command(cmd) |
||
| 2928 | |||
| 2929 | def get_group(self, group_id): |
||
| 2930 | """Request a single group |
||
| 2931 | |||
| 2932 | Arguments: |
||
| 2933 | group_id (str): UUID of an existing group |
||
| 2934 | |||
| 2935 | Returns: |
||
| 2936 | The response. See :py:meth:`send_command` for details. |
||
| 2937 | """ |
||
| 2938 | if not group_id: |
||
| 2939 | raise RequiredArgument("get_group requires a group_id argument") |
||
| 2940 | |||
| 2941 | cmd = XmlCommand("get_groups") |
||
| 2942 | cmd.set_attribute("group_id", group_id) |
||
| 2943 | return self._send_xml_command(cmd) |
||
| 2944 | |||
| 2945 | def get_info_list( |
||
| 2946 | self, info_type, *, filter=None, filter_id=None, name=None, details=None |
||
| 2947 | ): |
||
| 2948 | """Request a list of security information |
||
| 2949 | |||
| 2950 | Arguments: |
||
| 2951 | info_type (str): Type must be either CERT_BUND_ADV, CPE, CVE, |
||
| 2952 | DFN_CERT_ADV, OVALDEF, NVT or ALLINFO |
||
| 2953 | filter (str, optional): Filter term to use for the query |
||
| 2954 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 2955 | the query |
||
| 2956 | name (str, optional): Name or identifier of the requested |
||
| 2957 | information |
||
| 2958 | details (boolean, optional): Whether to include information about |
||
| 2959 | references to this information |
||
| 2960 | |||
| 2961 | Returns: |
||
| 2962 | The response. See :py:meth:`send_command` for details. |
||
| 2963 | """ |
||
| 2964 | if not info_type: |
||
| 2965 | raise RequiredArgument( |
||
| 2966 | "get_info_list requires an info_type argument" |
||
| 2967 | ) |
||
| 2968 | |||
| 2969 | info_type = info_type.upper() |
||
| 2970 | |||
| 2971 | if not info_type in INFO_TYPES: |
||
| 2972 | raise InvalidArgument( |
||
| 2973 | "get_info_list info_type argument must be one of CERT_BUND_ADV" |
||
| 2974 | ", CPE, CVE, DFN_CERT_ADV, OVALDEF, NVT or ALLINFO" |
||
| 2975 | ) |
||
| 2976 | |||
| 2977 | cmd = XmlCommand("get_info") |
||
| 2978 | |||
| 2979 | cmd.set_attribute("type", info_type) |
||
| 2980 | |||
| 2981 | _add_filter(cmd, filter, filter_id) |
||
| 2982 | |||
| 2983 | if name: |
||
| 2984 | cmd.set_attribute("name", name) |
||
| 2985 | |||
| 2986 | if not details is None: |
||
| 2987 | cmd.set_attribute("details", _to_bool(details)) |
||
| 2988 | |||
| 2989 | return self._send_xml_command(cmd) |
||
| 2990 | |||
| 2991 | def get_info(self, info_id, info_type): |
||
| 2992 | """Request a single secinfo |
||
| 2993 | |||
| 2994 | Arguments: |
||
| 2995 | info_id (str): UUID of an existing secinfo |
||
| 2996 | info_type (str): Type must be either CERT_BUND_ADV, CPE, CVE, |
||
| 2997 | DFN_CERT_ADV, OVALDEF, NVT or ALLINFO |
||
| 2998 | |||
| 2999 | Returns: |
||
| 3000 | The response. See :py:meth:`send_command` for details. |
||
| 3001 | """ |
||
| 3002 | if not info_type: |
||
| 3003 | raise RequiredArgument("get_info requires an info_type argument") |
||
| 3004 | |||
| 3005 | info_type = info_type.upper() |
||
| 3006 | |||
| 3007 | if not info_type in INFO_TYPES: |
||
| 3008 | raise InvalidArgument( |
||
| 3009 | "get_info info_type argument must be one of CERT_BUND_ADV" |
||
| 3010 | ", CPE, CVE, DFN_CERT_ADV, OVALDEF, NVT or ALLINFO" |
||
| 3011 | ) |
||
| 3012 | |||
| 3013 | if not info_id: |
||
| 3014 | raise RequiredArgument("get_info requires an info_id argument") |
||
| 3015 | |||
| 3016 | cmd = XmlCommand("get_info") |
||
| 3017 | cmd.set_attribute("info_id", info_id) |
||
| 3018 | |||
| 3019 | cmd.set_attribute("type", info_type) |
||
| 3020 | |||
| 3021 | # for single entity always request all details |
||
| 3022 | cmd.set_attribute("details", "1") |
||
| 3023 | return self._send_xml_command(cmd) |
||
| 3024 | |||
| 3025 | View Code Duplication | def get_notes( |
|
| 3026 | self, *, filter=None, filter_id=None, details=None, result=None |
||
| 3027 | ): |
||
| 3028 | """Request a list of notes |
||
| 3029 | |||
| 3030 | Arguments: |
||
| 3031 | filter (str, optional): Filter term to use for the query |
||
| 3032 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3033 | the query |
||
| 3034 | details (boolean, optional): Add info about connected results and |
||
| 3035 | tasks |
||
| 3036 | result (boolean, optional): Return the details of possible connected |
||
| 3037 | results. |
||
| 3038 | |||
| 3039 | Returns: |
||
| 3040 | The response. See :py:meth:`send_command` for details. |
||
| 3041 | """ |
||
| 3042 | cmd = XmlCommand("get_notes") |
||
| 3043 | |||
| 3044 | _add_filter(cmd, filter, filter_id) |
||
| 3045 | |||
| 3046 | if not details is None: |
||
| 3047 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3048 | |||
| 3049 | if not result is None: |
||
| 3050 | cmd.set_attribute("result", _to_bool(result)) |
||
| 3051 | |||
| 3052 | return self._send_xml_command(cmd) |
||
| 3053 | |||
| 3054 | def get_note(self, note_id): |
||
| 3055 | """Request a single note |
||
| 3056 | |||
| 3057 | Arguments: |
||
| 3058 | note_id (str): UUID of an existing note |
||
| 3059 | |||
| 3060 | Returns: |
||
| 3061 | The response. See :py:meth:`send_command` for details. |
||
| 3062 | """ |
||
| 3063 | if not note_id: |
||
| 3064 | raise RequiredArgument("get_note requires a note_id argument") |
||
| 3065 | |||
| 3066 | cmd = XmlCommand("get_notes") |
||
| 3067 | cmd.set_attribute("note_id", note_id) |
||
| 3068 | |||
| 3069 | # for single entity always request all details |
||
| 3070 | cmd.set_attribute("details", "1") |
||
| 3071 | return self._send_xml_command(cmd) |
||
| 3072 | |||
| 3073 | def get_nvts( |
||
| 3074 | self, |
||
| 3075 | *, |
||
| 3076 | details=None, |
||
| 3077 | preferences=None, |
||
| 3078 | preference_count=None, |
||
| 3079 | timeout=None, |
||
| 3080 | config_id=None, |
||
| 3081 | preferences_config_id=None, |
||
| 3082 | family=None, |
||
| 3083 | sort_order=None, |
||
| 3084 | sort_field=None |
||
| 3085 | ): |
||
| 3086 | """Request a list of nvts |
||
| 3087 | |||
| 3088 | Arguments: |
||
| 3089 | details (boolean, optional): Whether to include full details |
||
| 3090 | preferences (boolean, optional): Whether to include nvt preferences |
||
| 3091 | preference_count (boolean, optional): Whether to include preference |
||
| 3092 | count |
||
| 3093 | timeout (boolean, optional): Whether to include the special timeout |
||
| 3094 | preference |
||
| 3095 | config_id (str, optional): UUID of scan config to which to limit the |
||
| 3096 | NVT listing |
||
| 3097 | preferences_config_id (str, optional): UUID of scan config to use |
||
| 3098 | for preference values |
||
| 3099 | family (str, optional): Family to which to limit NVT listing |
||
| 3100 | sort_order (str, optional): Sort order |
||
| 3101 | sort_field (str, optional): Sort field |
||
| 3102 | |||
| 3103 | Returns: |
||
| 3104 | The response. See :py:meth:`send_command` for details. |
||
| 3105 | """ |
||
| 3106 | cmd = XmlCommand("get_nvts") |
||
| 3107 | |||
| 3108 | if not details is None: |
||
| 3109 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3110 | |||
| 3111 | if not preferences is None: |
||
| 3112 | cmd.set_attribute("preferences", _to_bool(preferences)) |
||
| 3113 | |||
| 3114 | if not preference_count is None: |
||
| 3115 | cmd.set_attribute("preference_count", _to_bool(preference_count)) |
||
| 3116 | |||
| 3117 | if not timeout is None: |
||
| 3118 | cmd.set_attribute("timeout", _to_bool(timeout)) |
||
| 3119 | |||
| 3120 | if config_id: |
||
| 3121 | cmd.set_attribute("config_id", config_id) |
||
| 3122 | |||
| 3123 | if preferences_config_id: |
||
| 3124 | cmd.set_attribute("preferences_config_id", preferences_config_id) |
||
| 3125 | |||
| 3126 | if family: |
||
| 3127 | cmd.set_attribute("family", family) |
||
| 3128 | |||
| 3129 | if sort_order: |
||
| 3130 | cmd.set_attribute("sort_order", sort_order) |
||
| 3131 | |||
| 3132 | if sort_field: |
||
| 3133 | cmd.set_attribute("sort_field", sort_field) |
||
| 3134 | |||
| 3135 | return self._send_xml_command(cmd) |
||
| 3136 | |||
| 3137 | def get_nvt(self, nvt_oid): |
||
| 3138 | """Request a single nvt |
||
| 3139 | |||
| 3140 | Arguments: |
||
| 3141 | nvt_oid (str): OID of an existing nvt |
||
| 3142 | |||
| 3143 | Returns: |
||
| 3144 | The response. See :py:meth:`send_command` for details. |
||
| 3145 | """ |
||
| 3146 | if not nvt_oid: |
||
| 3147 | raise RequiredArgument("get_nvt requires nvt_oid argument") |
||
| 3148 | |||
| 3149 | cmd = XmlCommand("get_nvts") |
||
| 3150 | cmd.set_attribute("nvt_oid", nvt_oid) |
||
| 3151 | |||
| 3152 | # for single entity always request all details |
||
| 3153 | cmd.set_attribute("details", "1") |
||
| 3154 | return self._send_xml_command(cmd) |
||
| 3155 | |||
| 3156 | def get_nvt_families(self, *, sort_order=None): |
||
| 3157 | """Request a list of nvt families |
||
| 3158 | |||
| 3159 | Arguments: |
||
| 3160 | sort_order (str, optional): Sort order |
||
| 3161 | |||
| 3162 | Returns: |
||
| 3163 | The response. See :py:meth:`send_command` for details. |
||
| 3164 | """ |
||
| 3165 | cmd = XmlCommand("get_nvt_families") |
||
| 3166 | |||
| 3167 | if sort_order: |
||
| 3168 | cmd.set_attribute("sort_order", sort_order) |
||
| 3169 | |||
| 3170 | return self._send_xml_command(cmd) |
||
| 3171 | |||
| 3172 | View Code Duplication | def get_overrides( |
|
| 3173 | self, *, filter=None, filter_id=None, details=None, result=None |
||
| 3174 | ): |
||
| 3175 | """Request a list of overrides |
||
| 3176 | |||
| 3177 | Arguments: |
||
| 3178 | filter (str, optional): Filter term to use for the query |
||
| 3179 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3180 | the query |
||
| 3181 | details (boolean, optional): |
||
| 3182 | result (boolean, optional): |
||
| 3183 | |||
| 3184 | Returns: |
||
| 3185 | The response. See :py:meth:`send_command` for details. |
||
| 3186 | """ |
||
| 3187 | cmd = XmlCommand("get_overrides") |
||
| 3188 | |||
| 3189 | _add_filter(cmd, filter, filter_id) |
||
| 3190 | |||
| 3191 | if not details is None: |
||
| 3192 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3193 | |||
| 3194 | if not result is None: |
||
| 3195 | cmd.set_attribute("result", _to_bool(result)) |
||
| 3196 | |||
| 3197 | return self._send_xml_command(cmd) |
||
| 3198 | |||
| 3199 | def get_override(self, override_id): |
||
| 3200 | """Request a single override |
||
| 3201 | |||
| 3202 | Arguments: |
||
| 3203 | override_id (str): UUID of an existing override |
||
| 3204 | |||
| 3205 | Returns: |
||
| 3206 | The response. See :py:meth:`send_command` for details. |
||
| 3207 | """ |
||
| 3208 | if not override_id: |
||
| 3209 | raise RequiredArgument( |
||
| 3210 | "get_override requires an override_id argument" |
||
| 3211 | ) |
||
| 3212 | |||
| 3213 | cmd = XmlCommand("get_overrides") |
||
| 3214 | cmd.set_attribute("override_id", override_id) |
||
| 3215 | |||
| 3216 | # for single entity always request all details |
||
| 3217 | cmd.set_attribute("details", "1") |
||
| 3218 | return self._send_xml_command(cmd) |
||
| 3219 | |||
| 3220 | def get_permissions(self, *, filter=None, filter_id=None, trash=None): |
||
| 3221 | """Request a list of permissions |
||
| 3222 | |||
| 3223 | Arguments: |
||
| 3224 | filter (str, optional): Filter term to use for the query |
||
| 3225 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3226 | the query |
||
| 3227 | trash (boolean, optional): Whether to get permissions in the |
||
| 3228 | trashcan instead |
||
| 3229 | |||
| 3230 | Returns: |
||
| 3231 | The response. See :py:meth:`send_command` for details. |
||
| 3232 | """ |
||
| 3233 | cmd = XmlCommand("get_permissions") |
||
| 3234 | |||
| 3235 | _add_filter(cmd, filter, filter_id) |
||
| 3236 | |||
| 3237 | if not trash is None: |
||
| 3238 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3239 | |||
| 3240 | return self._send_xml_command(cmd) |
||
| 3241 | |||
| 3242 | def get_permission(self, permission_id): |
||
| 3243 | """Request a single permission |
||
| 3244 | |||
| 3245 | Arguments: |
||
| 3246 | permission_id (str): UUID of an existing permission |
||
| 3247 | |||
| 3248 | Returns: |
||
| 3249 | The response. See :py:meth:`send_command` for details. |
||
| 3250 | """ |
||
| 3251 | if not permission_id: |
||
| 3252 | raise RequiredArgument( |
||
| 3253 | "get_permission requires a permission_id argument" |
||
| 3254 | ) |
||
| 3255 | |||
| 3256 | cmd = XmlCommand("get_permissions") |
||
| 3257 | cmd.set_attribute("permission_id", permission_id) |
||
| 3258 | return self._send_xml_command(cmd) |
||
| 3259 | |||
| 3260 | View Code Duplication | def get_port_lists( |
|
| 3261 | self, |
||
| 3262 | *, |
||
| 3263 | filter=None, |
||
| 3264 | filter_id=None, |
||
| 3265 | details=None, |
||
| 3266 | targets=None, |
||
| 3267 | trash=None |
||
| 3268 | ): |
||
| 3269 | """Request a list of port lists |
||
| 3270 | |||
| 3271 | Arguments: |
||
| 3272 | filter (str, optional): Filter term to use for the query |
||
| 3273 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3274 | the query |
||
| 3275 | details (boolean, optional): Whether to include full port list |
||
| 3276 | details |
||
| 3277 | targets (boolean, optional): Whether to include targets using this |
||
| 3278 | port list |
||
| 3279 | trash (boolean, optional): Whether to get port lists in the |
||
| 3280 | trashcan instead |
||
| 3281 | |||
| 3282 | Returns: |
||
| 3283 | The response. See :py:meth:`send_command` for details. |
||
| 3284 | """ |
||
| 3285 | cmd = XmlCommand("get_port_lists") |
||
| 3286 | |||
| 3287 | _add_filter(cmd, filter, filter_id) |
||
| 3288 | |||
| 3289 | if not details is None: |
||
| 3290 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3291 | |||
| 3292 | if not targets is None: |
||
| 3293 | cmd.set_attribute("targets", _to_bool(targets)) |
||
| 3294 | |||
| 3295 | if not trash is None: |
||
| 3296 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3297 | |||
| 3298 | return self._send_xml_command(cmd) |
||
| 3299 | |||
| 3300 | def get_port_list(self, port_list_id): |
||
| 3301 | """Request a single port list |
||
| 3302 | |||
| 3303 | Arguments: |
||
| 3304 | port_list_id (str): UUID of an existing port list |
||
| 3305 | |||
| 3306 | Returns: |
||
| 3307 | The response. See :py:meth:`send_command` for details. |
||
| 3308 | """ |
||
| 3309 | if not port_list_id: |
||
| 3310 | raise RequiredArgument( |
||
| 3311 | "get_port_list requires a port_list_id argument" |
||
| 3312 | ) |
||
| 3313 | |||
| 3314 | cmd = XmlCommand("get_port_lists") |
||
| 3315 | cmd.set_attribute("port_list_id", port_list_id) |
||
| 3316 | |||
| 3317 | # for single entity always request all details |
||
| 3318 | cmd.set_attribute("details", "1") |
||
| 3319 | return self._send_xml_command(cmd) |
||
| 3320 | |||
| 3321 | def get_preferences(self, *, nvt_oid=None, config_id=None): |
||
| 3322 | """Request a list of preferences |
||
| 3323 | |||
| 3324 | When the command includes a config_id attribute, the preference element |
||
| 3325 | includes the preference name, type and value, and the NVT to which the |
||
| 3326 | preference applies. Otherwise, the preference element includes just the |
||
| 3327 | name and value, with the NVT and type built into the name. |
||
| 3328 | |||
| 3329 | Arguments: |
||
| 3330 | nvt_oid (str, optional): OID of nvt |
||
| 3331 | config_id (str, optional): UUID of scan config of which to show |
||
| 3332 | preference values |
||
| 3333 | preference (str, optional): name of a particular preference to get |
||
| 3334 | |||
| 3335 | Returns: |
||
| 3336 | The response. See :py:meth:`send_command` for details. |
||
| 3337 | """ |
||
| 3338 | cmd = XmlCommand("get_preferences") |
||
| 3339 | |||
| 3340 | if nvt_oid: |
||
| 3341 | cmd.set_attribute("nvt_oid", nvt_oid) |
||
| 3342 | |||
| 3343 | if config_id: |
||
| 3344 | cmd.set_attribute("config_id", config_id) |
||
| 3345 | |||
| 3346 | return self._send_xml_command(cmd) |
||
| 3347 | |||
| 3348 | def get_preference(self, name): |
||
| 3349 | """Request a nvt preference |
||
| 3350 | |||
| 3351 | |||
| 3352 | Arguments: |
||
| 3353 | preference (str): name of a particular preference |
||
| 3354 | |||
| 3355 | Returns: |
||
| 3356 | The response. See :py:meth:`send_command` for details. |
||
| 3357 | """ |
||
| 3358 | if not name: |
||
| 3359 | raise RequiredArgument("get_preference requires a name argument") |
||
| 3360 | |||
| 3361 | cmd = XmlCommand("get_preferences") |
||
| 3362 | |||
| 3363 | cmd.set_attribute("preference", name) |
||
| 3364 | |||
| 3365 | return self._send_xml_command(cmd) |
||
| 3366 | |||
| 3367 | def get_reports( |
||
| 3368 | self, |
||
| 3369 | *, |
||
| 3370 | filter=None, |
||
| 3371 | filter_id=None, |
||
| 3372 | note_details=None, |
||
| 3373 | override_details=None |
||
| 3374 | ): |
||
| 3375 | """Request a list of reports |
||
| 3376 | |||
| 3377 | Arguments: |
||
| 3378 | filter (str, optional): Filter term to use for the query |
||
| 3379 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3380 | the query |
||
| 3381 | note_details (boolean, optional): If notes are included, whether to |
||
| 3382 | include note details |
||
| 3383 | override_details (boolean, optional): If overrides are included, |
||
| 3384 | whether to include override details |
||
| 3385 | |||
| 3386 | Returns: |
||
| 3387 | The response. See :py:meth:`send_command` for details. |
||
| 3388 | """ |
||
| 3389 | cmd = XmlCommand("get_reports") |
||
| 3390 | |||
| 3391 | if filter: |
||
| 3392 | cmd.set_attribute("report_filter", filter) |
||
| 3393 | |||
| 3394 | if filter_id: |
||
| 3395 | cmd.set_attribute("report_filt_id", filter_id) |
||
| 3396 | |||
| 3397 | if not note_details is None: |
||
| 3398 | cmd.set_attribute("note_details", _to_bool(note_details)) |
||
| 3399 | |||
| 3400 | if not override_details is None: |
||
| 3401 | cmd.set_attribute("override_details", _to_bool(override_details)) |
||
| 3402 | |||
| 3403 | cmd.set_attribute("ignore_pagination", "1") |
||
| 3404 | |||
| 3405 | return self._send_xml_command(cmd) |
||
| 3406 | |||
| 3407 | def get_report( |
||
| 3408 | self, |
||
| 3409 | report_id, |
||
| 3410 | *, |
||
| 3411 | filter=None, |
||
| 3412 | filter_id=None, |
||
| 3413 | delta_report_id=None, |
||
| 3414 | report_format_id=None |
||
| 3415 | ): |
||
| 3416 | """Request a single report |
||
| 3417 | |||
| 3418 | Arguments: |
||
| 3419 | report_id (str): UUID of an existing report |
||
| 3420 | filter (str, optional): Filter term to use to filter results in the |
||
| 3421 | report |
||
| 3422 | filter_id (str, optional): UUID of filter to use to filter results |
||
| 3423 | in the report |
||
| 3424 | delta_report_id (str, optional): UUID of an existing report to |
||
| 3425 | compare report to. |
||
| 3426 | report_format_id (str, optional): UUID of report format to use |
||
| 3427 | |||
| 3428 | Returns: |
||
| 3429 | The response. See :py:meth:`send_command` for details. |
||
| 3430 | """ |
||
| 3431 | if not report_id: |
||
| 3432 | raise RequiredArgument("get_report requires a report_id argument") |
||
| 3433 | |||
| 3434 | cmd = XmlCommand("get_reports") |
||
| 3435 | cmd.set_attribute("report_id", report_id) |
||
| 3436 | |||
| 3437 | _add_filter(cmd, filter, filter_id) |
||
| 3438 | |||
| 3439 | if delta_report_id: |
||
| 3440 | cmd.set_attribute("delta_report_id", delta_report_id) |
||
| 3441 | |||
| 3442 | if report_format_id: |
||
| 3443 | cmd.set_attribute("format_id", report_format_id) |
||
| 3444 | |||
| 3445 | return self._send_xml_command(cmd) |
||
| 3446 | |||
| 3447 | def get_report_formats( |
||
| 3448 | self, |
||
| 3449 | *, |
||
| 3450 | filter=None, |
||
| 3451 | filter_id=None, |
||
| 3452 | trash=None, |
||
| 3453 | alerts=None, |
||
| 3454 | params=None, |
||
| 3455 | details=None |
||
| 3456 | ): |
||
| 3457 | """Request a list of report formats |
||
| 3458 | |||
| 3459 | Arguments: |
||
| 3460 | filter (str, optional): Filter term to use for the query |
||
| 3461 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3462 | the query |
||
| 3463 | trash (boolean, optional): Whether to get the trashcan report |
||
| 3464 | formats instead |
||
| 3465 | alerts (boolean, optional): Whether to include alerts that use the |
||
| 3466 | report format |
||
| 3467 | params (boolean, optional): Whether to include report format |
||
| 3468 | parameters |
||
| 3469 | details (boolean, optional): Include report format file, signature |
||
| 3470 | and parameters |
||
| 3471 | |||
| 3472 | Returns: |
||
| 3473 | The response. See :py:meth:`send_command` for details. |
||
| 3474 | """ |
||
| 3475 | cmd = XmlCommand("get_report_formats") |
||
| 3476 | |||
| 3477 | _add_filter(cmd, filter, filter_id) |
||
| 3478 | |||
| 3479 | if not details is None: |
||
| 3480 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3481 | |||
| 3482 | if not alerts is None: |
||
| 3483 | cmd.set_attribute("alerts", _to_bool(alerts)) |
||
| 3484 | |||
| 3485 | if not params is None: |
||
| 3486 | cmd.set_attribute("params", _to_bool(params)) |
||
| 3487 | |||
| 3488 | if not trash is None: |
||
| 3489 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3490 | |||
| 3491 | return self._send_xml_command(cmd) |
||
| 3492 | |||
| 3493 | def get_report_format(self, report_format_id): |
||
| 3494 | """Request a single report format |
||
| 3495 | |||
| 3496 | Arguments: |
||
| 3497 | report_format_id (str): UUID of an existing report format |
||
| 3498 | |||
| 3499 | Returns: |
||
| 3500 | The response. See :py:meth:`send_command` for details. |
||
| 3501 | """ |
||
| 3502 | if not report_format_id: |
||
| 3503 | raise RequiredArgument( |
||
| 3504 | "get_report_format requires report_format_id argument" |
||
| 3505 | ) |
||
| 3506 | |||
| 3507 | cmd = XmlCommand("get_report_formats") |
||
| 3508 | cmd.set_attribute("report_format_id", report_format_id) |
||
| 3509 | |||
| 3510 | # for single entity always request all details |
||
| 3511 | cmd.set_attribute("details", "1") |
||
| 3512 | return self._send_xml_command(cmd) |
||
| 3513 | |||
| 3514 | def get_results( |
||
| 3515 | self, |
||
| 3516 | *, |
||
| 3517 | filter=None, |
||
| 3518 | filter_id=None, |
||
| 3519 | task_id=None, |
||
| 3520 | note_details=None, |
||
| 3521 | override_details=None, |
||
| 3522 | details=None |
||
| 3523 | ): |
||
| 3524 | """Request a list of results |
||
| 3525 | |||
| 3526 | Arguments: |
||
| 3527 | filter (str, optional): Filter term to use for the query |
||
| 3528 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3529 | the query |
||
| 3530 | task_id (str, optional): UUID of task for note and override handling |
||
| 3531 | note_details (boolean, optional): If notes are included, whether to |
||
| 3532 | include note details |
||
| 3533 | override_details (boolean, optional): If overrides are included, |
||
| 3534 | whether to include override details |
||
| 3535 | details (boolean, optional): Whether to include additional details |
||
| 3536 | of the results |
||
| 3537 | |||
| 3538 | Returns: |
||
| 3539 | The response. See :py:meth:`send_command` for details. |
||
| 3540 | """ |
||
| 3541 | cmd = XmlCommand("get_results") |
||
| 3542 | |||
| 3543 | _add_filter(cmd, filter, filter_id) |
||
| 3544 | |||
| 3545 | if task_id: |
||
| 3546 | cmd.set_attribute("task_id", task_id) |
||
| 3547 | |||
| 3548 | if not details is None: |
||
| 3549 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3550 | |||
| 3551 | if not note_details is None: |
||
| 3552 | cmd.set_attribute("note_details", _to_bool(note_details)) |
||
| 3553 | |||
| 3554 | if not override_details is None: |
||
| 3555 | cmd.set_attribute("override_details", _to_bool(override_details)) |
||
| 3556 | |||
| 3557 | return self._send_xml_command(cmd) |
||
| 3558 | |||
| 3559 | def get_result(self, result_id): |
||
| 3560 | """Request a single result |
||
| 3561 | |||
| 3562 | Arguments: |
||
| 3563 | result_id (str): UUID of an existing result |
||
| 3564 | |||
| 3565 | Returns: |
||
| 3566 | The response. See :py:meth:`send_command` for details. |
||
| 3567 | """ |
||
| 3568 | if not result_id: |
||
| 3569 | raise RequiredArgument("get_result requires a result_id argument") |
||
| 3570 | |||
| 3571 | cmd = XmlCommand("get_results") |
||
| 3572 | cmd.set_attribute("result_id", result_id) |
||
| 3573 | |||
| 3574 | # for single entity always request all details |
||
| 3575 | cmd.set_attribute("details", "1") |
||
| 3576 | return self._send_xml_command(cmd) |
||
| 3577 | |||
| 3578 | def get_roles(self, *, filter=None, filter_id=None, trash=None): |
||
| 3579 | """Request a list of roles |
||
| 3580 | |||
| 3581 | Arguments: |
||
| 3582 | filter (str, optional): Filter term to use for the query |
||
| 3583 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3584 | the query |
||
| 3585 | trash (boolean, optional): Whether to get the trashcan roles instead |
||
| 3586 | |||
| 3587 | Returns: |
||
| 3588 | The response. See :py:meth:`send_command` for details. |
||
| 3589 | """ |
||
| 3590 | cmd = XmlCommand("get_roles") |
||
| 3591 | |||
| 3592 | _add_filter(cmd, filter, filter_id) |
||
| 3593 | |||
| 3594 | if not trash is None: |
||
| 3595 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3596 | |||
| 3597 | return self._send_xml_command(cmd) |
||
| 3598 | |||
| 3599 | def get_role(self, role_id): |
||
| 3600 | """Request a single role |
||
| 3601 | |||
| 3602 | Arguments: |
||
| 3603 | role_id (str): UUID of an existing role |
||
| 3604 | |||
| 3605 | Returns: |
||
| 3606 | The response. See :py:meth:`send_command` for details. |
||
| 3607 | """ |
||
| 3608 | if not role_id: |
||
| 3609 | raise RequiredArgument("get_role requires a role_id argument") |
||
| 3610 | |||
| 3611 | cmd = XmlCommand("get_roles") |
||
| 3612 | cmd.set_attribute("role_id", role_id) |
||
| 3613 | return self._send_xml_command(cmd) |
||
| 3614 | |||
| 3615 | def get_scanners( |
||
| 3616 | self, *, filter=None, filter_id=None, trash=None, details=None |
||
| 3617 | ): |
||
| 3618 | """Request a list of scanners |
||
| 3619 | |||
| 3620 | Arguments: |
||
| 3621 | filter (str, optional): Filter term to use for the query |
||
| 3622 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3623 | the query |
||
| 3624 | trash (boolean, optional): Whether to get the trashcan scanners |
||
| 3625 | instead |
||
| 3626 | details (boolean, optional): Whether to include extra details like |
||
| 3627 | tasks using this scanner |
||
| 3628 | |||
| 3629 | Returns: |
||
| 3630 | The response. See :py:meth:`send_command` for details. |
||
| 3631 | """ |
||
| 3632 | cmd = XmlCommand("get_scanners") |
||
| 3633 | |||
| 3634 | _add_filter(cmd, filter, filter_id) |
||
| 3635 | |||
| 3636 | if not trash is None: |
||
| 3637 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3638 | |||
| 3639 | if not details is None: |
||
| 3640 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3641 | |||
| 3642 | return self._send_xml_command(cmd) |
||
| 3643 | |||
| 3644 | def get_scanner(self, scanner_id): |
||
| 3645 | """Request a single scanner |
||
| 3646 | |||
| 3647 | Arguments: |
||
| 3648 | scanner_id (str): UUID of an existing scanner |
||
| 3649 | |||
| 3650 | Returns: |
||
| 3651 | The response. See :py:meth:`send_command` for details. |
||
| 3652 | """ |
||
| 3653 | if not scanner_id: |
||
| 3654 | raise RequiredArgument("get_scanner requires a scanner_id argument") |
||
| 3655 | |||
| 3656 | cmd = XmlCommand("get_scanners") |
||
| 3657 | cmd.set_attribute("scanner_id", scanner_id) |
||
| 3658 | |||
| 3659 | # for single entity always request all details |
||
| 3660 | cmd.set_attribute("details", "1") |
||
| 3661 | return self._send_xml_command(cmd) |
||
| 3662 | |||
| 3663 | View Code Duplication | def get_schedules( |
|
| 3664 | self, *, filter=None, filter_id=None, trash=None, tasks=None |
||
| 3665 | ): |
||
| 3666 | """Request a list of schedules |
||
| 3667 | |||
| 3668 | Arguments: |
||
| 3669 | filter (str, optional): Filter term to use for the query |
||
| 3670 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3671 | the query |
||
| 3672 | trash (boolean, optional): Whether to get the trashcan schedules |
||
| 3673 | instead |
||
| 3674 | tasks (boolean, optional): Whether to include tasks using the |
||
| 3675 | schedules |
||
| 3676 | |||
| 3677 | Returns: |
||
| 3678 | The response. See :py:meth:`send_command` for details. |
||
| 3679 | """ |
||
| 3680 | cmd = XmlCommand("get_schedules") |
||
| 3681 | |||
| 3682 | _add_filter(cmd, filter, filter_id) |
||
| 3683 | |||
| 3684 | if not trash is None: |
||
| 3685 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3686 | |||
| 3687 | if not tasks is None: |
||
| 3688 | cmd.set_attribute("tasks", _to_bool(tasks)) |
||
| 3689 | |||
| 3690 | return self._send_xml_command(cmd) |
||
| 3691 | |||
| 3692 | def get_schedule(self, schedule_id): |
||
| 3693 | """Request a single schedule |
||
| 3694 | |||
| 3695 | Arguments: |
||
| 3696 | schedule_id (str): UUID of an existing schedule |
||
| 3697 | |||
| 3698 | Returns: |
||
| 3699 | The response. See :py:meth:`send_command` for details. |
||
| 3700 | """ |
||
| 3701 | if not schedule_id: |
||
| 3702 | raise RequiredArgument( |
||
| 3703 | "get_schedule requires a schedule_id argument" |
||
| 3704 | ) |
||
| 3705 | |||
| 3706 | cmd = XmlCommand("get_schedules") |
||
| 3707 | cmd.set_attribute("schedule_id", schedule_id) |
||
| 3708 | return self._send_xml_command(cmd) |
||
| 3709 | |||
| 3710 | def get_settings(self, *, filter=None): |
||
| 3711 | """Request a list of user settings |
||
| 3712 | |||
| 3713 | Arguments: |
||
| 3714 | filter (str, optional): Filter term to use for the query |
||
| 3715 | |||
| 3716 | Returns: |
||
| 3717 | The response. See :py:meth:`send_command` for details. |
||
| 3718 | """ |
||
| 3719 | cmd = XmlCommand("get_settings") |
||
| 3720 | |||
| 3721 | if filter: |
||
| 3722 | cmd.set_attribute("filter", filter) |
||
| 3723 | |||
| 3724 | return self._send_xml_command(cmd) |
||
| 3725 | |||
| 3726 | def get_setting(self, setting_id): |
||
| 3727 | """Request a single setting |
||
| 3728 | |||
| 3729 | Arguments: |
||
| 3730 | setting_id (str): UUID of an existing setting |
||
| 3731 | |||
| 3732 | Returns: |
||
| 3733 | The response. See :py:meth:`send_command` for details. |
||
| 3734 | """ |
||
| 3735 | if not setting_id: |
||
| 3736 | raise RequiredArgument("get_setting requires a setting_id argument") |
||
| 3737 | |||
| 3738 | cmd = XmlCommand("get_settings") |
||
| 3739 | cmd.set_attribute("setting_id", setting_id) |
||
| 3740 | return self._send_xml_command(cmd) |
||
| 3741 | |||
| 3742 | def get_system_reports( |
||
| 3743 | self, |
||
| 3744 | *, |
||
| 3745 | name=None, |
||
| 3746 | duration=None, |
||
| 3747 | start_time=None, |
||
| 3748 | end_time=None, |
||
| 3749 | brief=None, |
||
| 3750 | slave_id=None |
||
| 3751 | ): |
||
| 3752 | """Request a list of system reports |
||
| 3753 | |||
| 3754 | Arguments: |
||
| 3755 | name (str, optional): A string describing the required system report |
||
| 3756 | duration (int, optional): The number of seconds into the past that |
||
| 3757 | the system report should include |
||
| 3758 | start_time (str, optional): The start of the time interval the |
||
| 3759 | system report should include in ISO time format |
||
| 3760 | end_time (str, optional): The end of the time interval the system |
||
| 3761 | report should include in ISO time format |
||
| 3762 | brief (boolean, optional): Whether to include the actual system |
||
| 3763 | reports |
||
| 3764 | slave_id (str, optional): UUID of GMP scanner from which to get the |
||
| 3765 | system reports |
||
| 3766 | |||
| 3767 | Returns: |
||
| 3768 | The response. See :py:meth:`send_command` for details. |
||
| 3769 | """ |
||
| 3770 | cmd = XmlCommand("get_system_reports") |
||
| 3771 | |||
| 3772 | if name: |
||
| 3773 | cmd.set_attribute("name", name) |
||
| 3774 | |||
| 3775 | if not duration is None: |
||
| 3776 | if not isinstance(duration, numbers.Integral): |
||
| 3777 | raise InvalidArgument("duration needs to be an integer number") |
||
| 3778 | |||
| 3779 | cmd.set_attribute("duration", str(duration)) |
||
| 3780 | |||
| 3781 | if start_time: |
||
| 3782 | cmd.set_attribute("start_time", str(start_time)) |
||
| 3783 | |||
| 3784 | if end_time: |
||
| 3785 | cmd.set_attribute("end_time", str(end_time)) |
||
| 3786 | |||
| 3787 | if not brief is None: |
||
| 3788 | cmd.set_attribute("brief", _to_bool(brief)) |
||
| 3789 | |||
| 3790 | if slave_id: |
||
| 3791 | cmd.set_attribute("slave_id", slave_id) |
||
| 3792 | |||
| 3793 | return self._send_xml_command(cmd) |
||
| 3794 | |||
| 3795 | def get_tags( |
||
| 3796 | self, *, filter=None, filter_id=None, trash=None, names_only=None |
||
| 3797 | ): |
||
| 3798 | """Request a list of tags |
||
| 3799 | |||
| 3800 | Arguments: |
||
| 3801 | filter (str, optional): Filter term to use for the query |
||
| 3802 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3803 | the query |
||
| 3804 | trash (boolean, optional): Whether to get tags from the trashcan |
||
| 3805 | instead |
||
| 3806 | names_only (boolean, optional): Whether to get only distinct tag |
||
| 3807 | names |
||
| 3808 | |||
| 3809 | Returns: |
||
| 3810 | The response. See :py:meth:`send_command` for details. |
||
| 3811 | """ |
||
| 3812 | cmd = XmlCommand("get_tags") |
||
| 3813 | |||
| 3814 | _add_filter(cmd, filter, filter_id) |
||
| 3815 | |||
| 3816 | if not trash is None: |
||
| 3817 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3818 | |||
| 3819 | if not names_only is None: |
||
| 3820 | cmd.set_attribute("names_only", _to_bool(names_only)) |
||
| 3821 | |||
| 3822 | return self._send_xml_command(cmd) |
||
| 3823 | |||
| 3824 | def get_tag(self, tag_id): |
||
| 3825 | """Request a single tag |
||
| 3826 | |||
| 3827 | Arguments: |
||
| 3828 | tag_id (str): UUID of an existing tag |
||
| 3829 | |||
| 3830 | Returns: |
||
| 3831 | The response. See :py:meth:`send_command` for details. |
||
| 3832 | """ |
||
| 3833 | if not tag_id: |
||
| 3834 | raise RequiredArgument("get_tag requires a tag_id argument") |
||
| 3835 | |||
| 3836 | cmd = XmlCommand("get_tags") |
||
| 3837 | cmd.set_attribute("tag_id", tag_id) |
||
| 3838 | return self._send_xml_command(cmd) |
||
| 3839 | |||
| 3840 | View Code Duplication | def get_targets( |
|
| 3841 | self, *, filter=None, filter_id=None, trash=None, tasks=None |
||
| 3842 | ): |
||
| 3843 | """Request a list of targets |
||
| 3844 | |||
| 3845 | Arguments: |
||
| 3846 | filter (str, optional): Filter term to use for the query |
||
| 3847 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3848 | the query |
||
| 3849 | trash (boolean, optional): Whether to get the trashcan targets |
||
| 3850 | instead |
||
| 3851 | tasks (boolean, optional): Whether to include list of tasks that |
||
| 3852 | use the target |
||
| 3853 | |||
| 3854 | Returns: |
||
| 3855 | The response. See :py:meth:`send_command` for details. |
||
| 3856 | """ |
||
| 3857 | cmd = XmlCommand("get_targets") |
||
| 3858 | |||
| 3859 | _add_filter(cmd, filter, filter_id) |
||
| 3860 | |||
| 3861 | if not trash is None: |
||
| 3862 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3863 | |||
| 3864 | if not tasks is None: |
||
| 3865 | cmd.set_attribute("tasks", _to_bool(tasks)) |
||
| 3866 | |||
| 3867 | return self._send_xml_command(cmd) |
||
| 3868 | |||
| 3869 | def get_target(self, target_id): |
||
| 3870 | """Request a single target |
||
| 3871 | |||
| 3872 | Arguments: |
||
| 3873 | target_id (str): UUID of an existing target |
||
| 3874 | |||
| 3875 | Returns: |
||
| 3876 | The response. See :py:meth:`send_command` for details. |
||
| 3877 | """ |
||
| 3878 | if not target_id: |
||
| 3879 | raise RequiredArgument("get_target requires a target_id argument") |
||
| 3880 | |||
| 3881 | cmd = XmlCommand("get_targets") |
||
| 3882 | cmd.set_attribute("target_id", target_id) |
||
| 3883 | return self._send_xml_command(cmd) |
||
| 3884 | |||
| 3885 | View Code Duplication | def get_tasks( |
|
| 3886 | self, |
||
| 3887 | *, |
||
| 3888 | filter=None, |
||
| 3889 | filter_id=None, |
||
| 3890 | trash=None, |
||
| 3891 | details=None, |
||
| 3892 | schedules_only=None |
||
| 3893 | ): |
||
| 3894 | """Request a list of tasks |
||
| 3895 | |||
| 3896 | Arguments: |
||
| 3897 | filter (str, optional): Filter term to use for the query |
||
| 3898 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3899 | the query |
||
| 3900 | trash (boolean, optional): Whether to get the trashcan tasks instead |
||
| 3901 | details (boolean, optional): Whether to include full task details |
||
| 3902 | schedules_only (boolean, optional): Whether to only include id, name |
||
| 3903 | and schedule details |
||
| 3904 | |||
| 3905 | Returns: |
||
| 3906 | The response. See :py:meth:`send_command` for details. |
||
| 3907 | """ |
||
| 3908 | cmd = XmlCommand("get_tasks") |
||
| 3909 | |||
| 3910 | _add_filter(cmd, filter, filter_id) |
||
| 3911 | |||
| 3912 | if not trash is None: |
||
| 3913 | cmd.set_attribute("trash", _to_bool(trash)) |
||
| 3914 | |||
| 3915 | if not details is None: |
||
| 3916 | cmd.set_attribute("details", _to_bool(details)) |
||
| 3917 | |||
| 3918 | if not schedules_only is None: |
||
| 3919 | cmd.set_attribute("schedules_only", _to_bool(schedules_only)) |
||
| 3920 | |||
| 3921 | return self._send_xml_command(cmd) |
||
| 3922 | |||
| 3923 | def get_task(self, task_id): |
||
| 3924 | """Request a single task |
||
| 3925 | |||
| 3926 | Arguments: |
||
| 3927 | task_id (str): UUID of an existing task |
||
| 3928 | |||
| 3929 | Returns: |
||
| 3930 | The response. See :py:meth:`send_command` for details. |
||
| 3931 | """ |
||
| 3932 | if not task_id: |
||
| 3933 | raise RequiredArgument("get_task requires task_id argument") |
||
| 3934 | |||
| 3935 | cmd = XmlCommand("get_tasks") |
||
| 3936 | cmd.set_attribute("task_id", task_id) |
||
| 3937 | |||
| 3938 | # for single entity always request all details |
||
| 3939 | cmd.set_attribute("details", "1") |
||
| 3940 | return self._send_xml_command(cmd) |
||
| 3941 | |||
| 3942 | def get_users(self, *, filter=None, filter_id=None): |
||
| 3943 | """Request a list of users |
||
| 3944 | |||
| 3945 | Arguments: |
||
| 3946 | filter (str, optional): Filter term to use for the query |
||
| 3947 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 3948 | the query |
||
| 3949 | |||
| 3950 | Returns: |
||
| 3951 | The response. See :py:meth:`send_command` for details. |
||
| 3952 | """ |
||
| 3953 | cmd = XmlCommand("get_users") |
||
| 3954 | |||
| 3955 | _add_filter(cmd, filter, filter_id) |
||
| 3956 | |||
| 3957 | return self._send_xml_command(cmd) |
||
| 3958 | |||
| 3959 | def get_user(self, user_id): |
||
| 3960 | """Request a single user |
||
| 3961 | |||
| 3962 | Arguments: |
||
| 3963 | user_id (str): UUID of an existing user |
||
| 3964 | |||
| 3965 | Returns: |
||
| 3966 | The response. See :py:meth:`send_command` for details. |
||
| 3967 | """ |
||
| 3968 | if not user_id: |
||
| 3969 | raise RequiredArgument("get_user requires a user_id argument") |
||
| 3970 | |||
| 3971 | cmd = XmlCommand("get_users") |
||
| 3972 | cmd.set_attribute("user_id", user_id) |
||
| 3973 | return self._send_xml_command(cmd) |
||
| 3974 | |||
| 3975 | def get_version(self): |
||
| 3976 | """Get the Greenbone Manager Protocol version used by the remote gvmd |
||
| 3977 | |||
| 3978 | Returns: |
||
| 3979 | The response. See :py:meth:`send_command` for details. |
||
| 3980 | """ |
||
| 3981 | return self._send_xml_command(XmlCommand("get_version")) |
||
| 3982 | |||
| 3983 | def help(self, *, format=None, help_type=""): |
||
| 3984 | """Get the help text |
||
| 3985 | |||
| 3986 | Arguments: |
||
| 3987 | format (str, optional): One of "html", "rnc", "text" or "xml |
||
| 3988 | type (str, optional): One of "brief" or "". Default "" |
||
| 3989 | |||
| 3990 | Returns: |
||
| 3991 | The response. See :py:meth:`send_command` for details. |
||
| 3992 | """ |
||
| 3993 | cmd = XmlCommand("help") |
||
| 3994 | |||
| 3995 | if help_type not in ("", "brief"): |
||
| 3996 | raise InvalidArgument( |
||
| 3997 | 'help_type argument must be an empty string or "brief"' |
||
| 3998 | ) |
||
| 3999 | |||
| 4000 | cmd.set_attribute("type", help_type) |
||
| 4001 | |||
| 4002 | if format: |
||
| 4003 | if not format.lower() in ("html", "rnc", "text", "xml"): |
||
| 4004 | raise InvalidArgument( |
||
| 4005 | "help format Argument must be one of html, rnc, text or " |
||
| 4006 | "xml" |
||
| 4007 | ) |
||
| 4008 | |||
| 4009 | cmd.set_attribute("format", format) |
||
| 4010 | |||
| 4011 | return self._send_xml_command(cmd) |
||
| 4012 | |||
| 4013 | def modify_agent(self, agent_id, *, name=None, comment=None): |
||
| 4014 | """Modifies an existing agent |
||
| 4015 | |||
| 4016 | Arguments: |
||
| 4017 | agent_id (str) UUID of the agent to be modified. |
||
| 4018 | name (str, optional): Name of the new credential |
||
| 4019 | comment (str, optional): Comment for the credential |
||
| 4020 | |||
| 4021 | Returns: |
||
| 4022 | The response. See :py:meth:`send_command` for details. |
||
| 4023 | """ |
||
| 4024 | if not agent_id: |
||
| 4025 | raise RequiredArgument("modify_agent requires agent_id argument") |
||
| 4026 | |||
| 4027 | cmd = XmlCommand("modify_agent") |
||
| 4028 | cmd.set_attribute("agent_id", str(agent_id)) |
||
| 4029 | |||
| 4030 | if name: |
||
| 4031 | cmd.add_element("name", name) |
||
| 4032 | |||
| 4033 | if comment: |
||
| 4034 | cmd.add_element("comment", comment) |
||
| 4035 | |||
| 4036 | return self._send_xml_command(cmd) |
||
| 4037 | |||
| 4038 | def modify_alert( |
||
| 4039 | self, |
||
| 4040 | alert_id, |
||
| 4041 | *, |
||
| 4042 | name=None, |
||
| 4043 | comment=None, |
||
| 4044 | filter_id=None, |
||
| 4045 | event=None, |
||
| 4046 | event_data=None, |
||
| 4047 | condition=None, |
||
| 4048 | condition_data=None, |
||
| 4049 | method=None, |
||
| 4050 | method_data=None |
||
| 4051 | ): |
||
| 4052 | """Modifies an existing alert. |
||
| 4053 | |||
| 4054 | Arguments: |
||
| 4055 | alert_id (str) UUID of the alert to be modified. |
||
| 4056 | name (str, optional): Name of the Alert. |
||
| 4057 | condition (str, optional): The condition that must be satisfied |
||
| 4058 | for the alert to occur. |
||
| 4059 | condition (str, optional): The condition that must be satisfied for |
||
| 4060 | the alert to occur; if the event is either 'Updated SecInfo |
||
| 4061 | arrived' or 'New SecInfo arrived', condition must be 'Always'. |
||
| 4062 | Otherwise, condition can also be on of 'Severity at least', |
||
| 4063 | 'Filter count changed' or 'Filter count at least'. |
||
| 4064 | condition_data (dict, optional): Data that defines the condition |
||
| 4065 | event (str, optional): The event that must happen for the alert to |
||
| 4066 | occur, one of 'Task run status changed', |
||
| 4067 | 'Updated SecInfo arrived' or 'New SecInfo arrived' |
||
| 4068 | event_data (dict, optional): Data that defines the event |
||
| 4069 | method (str, optional): The method by which the user is alerted, |
||
| 4070 | one of 'SCP', 'Send', 'SMB', 'SNMP', 'Syslog' or 'Email'; |
||
| 4071 | if the event is neither 'Updated SecInfo arrived' nor |
||
| 4072 | 'New SecInfo arrived', method can also be one of 'Start Task', |
||
| 4073 | 'HTTP Get', 'Sourcefire Connector' or 'verinice Connector'. |
||
| 4074 | method_data (dict, optional): Data that defines the method |
||
| 4075 | filter_id (str, optional): Filter to apply when executing alert |
||
| 4076 | comment (str, optional): Comment for the alert |
||
| 4077 | |||
| 4078 | Returns: |
||
| 4079 | The response. See :py:meth:`send_command` for details. |
||
| 4080 | """ |
||
| 4081 | |||
| 4082 | if not alert_id: |
||
| 4083 | raise RequiredArgument("modify_alert requires an alert_id argument") |
||
| 4084 | |||
| 4085 | _check_event(event, condition, method) |
||
| 4086 | |||
| 4087 | cmd = XmlCommand("modify_alert") |
||
| 4088 | cmd.set_attribute("alert_id", str(alert_id)) |
||
| 4089 | |||
| 4090 | if name: |
||
| 4091 | cmd.add_element("name", name) |
||
| 4092 | |||
| 4093 | if comment: |
||
| 4094 | cmd.add_element("comment", comment) |
||
| 4095 | |||
| 4096 | if filter_id: |
||
| 4097 | cmd.add_element("filter", attrs={"id": filter_id}) |
||
| 4098 | |||
| 4099 | if condition: |
||
| 4100 | conditions = cmd.add_element("condition", condition) |
||
| 4101 | |||
| 4102 | if not condition_data is None: |
||
| 4103 | for key, value in condition_data.items(): |
||
| 4104 | _data = conditions.add_element("data", value) |
||
| 4105 | _data.add_element("name", key) |
||
| 4106 | |||
| 4107 | if event: |
||
| 4108 | events = cmd.add_element("event", event) |
||
| 4109 | |||
| 4110 | if not event_data is None: |
||
| 4111 | for key, value in event_data.items(): |
||
| 4112 | _data = events.add_element("data", value) |
||
| 4113 | _data.add_element("name", key) |
||
| 4114 | |||
| 4115 | if method: |
||
| 4116 | methods = cmd.add_element("method", method) |
||
| 4117 | |||
| 4118 | if not method_data is None: |
||
| 4119 | for key, value in method_data.items(): |
||
| 4120 | _data = methods.add_element("data", value) |
||
| 4121 | _data.add_element("name", key) |
||
| 4122 | |||
| 4123 | return self._send_xml_command(cmd) |
||
| 4124 | |||
| 4125 | def modify_asset(self, asset_id, comment=""): |
||
| 4126 | """Modifies an existing asset. |
||
| 4127 | |||
| 4128 | Arguments: |
||
| 4129 | asset_id (str) UUID of the asset to be modified. |
||
| 4130 | comment (str, optional): Comment for the asset. |
||
| 4131 | |||
| 4132 | Returns: |
||
| 4133 | The response. See :py:meth:`send_command` for details. |
||
| 4134 | """ |
||
| 4135 | if not asset_id: |
||
| 4136 | raise RequiredArgument("modify_asset requires an asset_id argument") |
||
| 4137 | |||
| 4138 | cmd = XmlCommand("modify_asset") |
||
| 4139 | cmd.set_attribute("asset_id", asset_id) |
||
| 4140 | cmd.add_element("comment", comment) |
||
| 4141 | |||
| 4142 | return self._send_xml_command(cmd) |
||
| 4143 | |||
| 4144 | def modify_auth(self, group_name, auth_conf_settings): |
||
| 4145 | """Modifies an existing auth. |
||
| 4146 | |||
| 4147 | Arguments: |
||
| 4148 | group_name (str) Name of the group to be modified. |
||
| 4149 | auth_conf_settings (dict): The new auth config. |
||
| 4150 | |||
| 4151 | Returns: |
||
| 4152 | The response. See :py:meth:`send_command` for details. |
||
| 4153 | """ |
||
| 4154 | if not group_name: |
||
| 4155 | raise RequiredArgument("modify_auth requires a group_name argument") |
||
| 4156 | if not auth_conf_settings: |
||
| 4157 | raise RequiredArgument( |
||
| 4158 | "modify_auth requires an " "auth_conf_settings argument" |
||
| 4159 | ) |
||
| 4160 | cmd = XmlCommand("modify_auth") |
||
| 4161 | _xmlgroup = cmd.add_element("group", attrs={"name": str(group_name)}) |
||
| 4162 | |||
| 4163 | for key, value in auth_conf_settings.items(): |
||
| 4164 | _xmlauthconf = _xmlgroup.add_element("auth_conf_setting") |
||
| 4165 | _xmlauthconf.add_element("key", key) |
||
| 4166 | _xmlauthconf.add_element("value", value) |
||
| 4167 | |||
| 4168 | return self._send_xml_command(cmd) |
||
| 4169 | |||
| 4170 | def modify_config_set_nvt_preference( |
||
| 4171 | self, config_id, name, nvt_oid, *, value=None |
||
| 4172 | ): |
||
| 4173 | """Modifies the nvt preferences of an existing scan config. |
||
| 4174 | |||
| 4175 | Arguments: |
||
| 4176 | config_id (str): UUID of scan config to modify. |
||
| 4177 | name (str): Name for preference to change. |
||
| 4178 | nvt_oid (str): OID of the NVT associated with preference to modify |
||
| 4179 | value (str, optional): New value for the preference. None to delete |
||
| 4180 | the preference and to use the default instead. |
||
| 4181 | """ |
||
| 4182 | if not config_id: |
||
| 4183 | raise RequiredArgument( |
||
| 4184 | "modify_config_set_nvt_preference requires config_id argument" |
||
| 4185 | ) |
||
| 4186 | |||
| 4187 | if not nvt_oid: |
||
| 4188 | raise RequiredArgument( |
||
| 4189 | "modify_config_set_nvt_preference requires a nvt_oid argument" |
||
| 4190 | ) |
||
| 4191 | |||
| 4192 | if not name: |
||
| 4193 | raise RequiredArgument( |
||
| 4194 | "modify_config_set_nvt_preference requires a name argument" |
||
| 4195 | ) |
||
| 4196 | |||
| 4197 | cmd = XmlCommand("modify_config") |
||
| 4198 | cmd.set_attribute("config_id", str(config_id)) |
||
| 4199 | |||
| 4200 | _xmlpref = cmd.add_element("preference") |
||
| 4201 | |||
| 4202 | _xmlpref.add_element("nvt", attrs={"oid": nvt_oid}) |
||
| 4203 | _xmlpref.add_element("name", name) |
||
| 4204 | |||
| 4205 | if value: |
||
| 4206 | _xmlpref.add_element("value", _to_base64(value)) |
||
| 4207 | |||
| 4208 | return self._send_xml_command(cmd) |
||
| 4209 | |||
| 4210 | def modify_config_set_comment(self, config_id, comment=""): |
||
| 4211 | """Modifies the comment of an existing scan config |
||
| 4212 | |||
| 4213 | Arguments: |
||
| 4214 | config_id (str): UUID of scan config to modify. |
||
| 4215 | comment (str, optional): Comment to set on a config. Default: '' |
||
| 4216 | """ |
||
| 4217 | if not config_id: |
||
| 4218 | raise RequiredArgument( |
||
| 4219 | "modify_config_set_comment requires a config_id argument" |
||
| 4220 | ) |
||
| 4221 | |||
| 4222 | cmd = XmlCommand("modify_config") |
||
| 4223 | cmd.set_attribute("config_id", str(config_id)) |
||
| 4224 | |||
| 4225 | cmd.add_element("comment", comment) |
||
| 4226 | |||
| 4227 | return self._send_xml_command(cmd) |
||
| 4228 | |||
| 4229 | def modify_config_set_scanner_preference( |
||
| 4230 | self, config_id, name, *, value=None |
||
| 4231 | ): |
||
| 4232 | """Modifies the scanner preferences of an existing scan config |
||
| 4233 | |||
| 4234 | Arguments: |
||
| 4235 | config_id (str): UUID of scan config to modify. |
||
| 4236 | name (str): Name of the scanner preference to change |
||
| 4237 | value (str, optional): New value for the preference. None to delete |
||
| 4238 | the preference and to use the default instead. |
||
| 4239 | |||
| 4240 | """ |
||
| 4241 | if not config_id: |
||
| 4242 | raise RequiredArgument( |
||
| 4243 | "modify_config_set_scanner_preference requires a config_id " |
||
| 4244 | "argument" |
||
| 4245 | ) |
||
| 4246 | |||
| 4247 | if not name: |
||
| 4248 | raise RequiredArgument( |
||
| 4249 | "modify_config_set_scanner_preference requires a name argument" |
||
| 4250 | ) |
||
| 4251 | |||
| 4252 | cmd = XmlCommand("modify_config") |
||
| 4253 | cmd.set_attribute("config_id", str(config_id)) |
||
| 4254 | |||
| 4255 | _xmlpref = cmd.add_element("preference") |
||
| 4256 | |||
| 4257 | _xmlpref.add_element("name", name) |
||
| 4258 | |||
| 4259 | if value: |
||
| 4260 | _xmlpref.add_element("value", _to_base64(value)) |
||
| 4261 | |||
| 4262 | return self._send_xml_command(cmd) |
||
| 4263 | |||
| 4264 | def modify_config_set_nvt_selection(self, config_id, family, nvt_oids): |
||
| 4265 | """Modifies the selected nvts of an existing scan config |
||
| 4266 | |||
| 4267 | The manager updates the given family in the config to include only the |
||
| 4268 | given NVTs. |
||
| 4269 | |||
| 4270 | Arguments: |
||
| 4271 | config_id (str): UUID of scan config to modify. |
||
| 4272 | family (str): Name of the NVT family to include NVTs from |
||
| 4273 | nvt_oids (list): List of NVTs to select for the family. |
||
| 4274 | """ |
||
| 4275 | if not config_id: |
||
| 4276 | raise RequiredArgument( |
||
| 4277 | "modify_config_set_nvt_selection requires a config_id " |
||
| 4278 | "argument" |
||
| 4279 | ) |
||
| 4280 | |||
| 4281 | if not family: |
||
| 4282 | raise RequiredArgument( |
||
| 4283 | "modify_config_set_nvt_selection requires a family argument" |
||
| 4284 | ) |
||
| 4285 | |||
| 4286 | if not _is_list_like(nvt_oids): |
||
| 4287 | raise InvalidArgument( |
||
| 4288 | "modify_config_set_nvt_selection requires an iterable as " |
||
| 4289 | "nvt_oids argument" |
||
| 4290 | ) |
||
| 4291 | |||
| 4292 | cmd = XmlCommand("modify_config") |
||
| 4293 | cmd.set_attribute("config_id", str(config_id)) |
||
| 4294 | |||
| 4295 | _xmlnvtsel = cmd.add_element("nvt_selection") |
||
| 4296 | _xmlnvtsel.add_element("family", family) |
||
| 4297 | |||
| 4298 | for nvt in nvt_oids: |
||
| 4299 | _xmlnvtsel.add_element("nvt", attrs={"oid": nvt}) |
||
| 4300 | |||
| 4301 | return self._send_xml_command(cmd) |
||
| 4302 | |||
| 4303 | def modify_config_set_family_selection( |
||
| 4304 | self, |
||
| 4305 | config_id, |
||
| 4306 | families, |
||
| 4307 | *, |
||
| 4308 | auto_add_new_families=True, |
||
| 4309 | auto_add_new_nvts=True |
||
| 4310 | ): |
||
| 4311 | """ |
||
| 4312 | Selected the NVTs of a scan config at a family level. |
||
| 4313 | |||
| 4314 | Arguments: |
||
| 4315 | config_id (str): UUID of scan config to modify. |
||
| 4316 | families (list): List of NVT family names to select. |
||
| 4317 | auto_add_new_families (boolean, optional): Whether new families |
||
| 4318 | should be added to the scan config automatically. Default: True. |
||
| 4319 | auto_add_new_nvts (boolean, optional): Whether new NVTs in the |
||
| 4320 | selected families should be added to the scan config |
||
| 4321 | automatically. Default: True. |
||
| 4322 | """ |
||
| 4323 | if not config_id: |
||
| 4324 | raise RequiredArgument( |
||
| 4325 | "modify_config_set_family_selection requires a config_id " |
||
| 4326 | "argument" |
||
| 4327 | ) |
||
| 4328 | |||
| 4329 | if not _is_list_like(families): |
||
| 4330 | raise InvalidArgument( |
||
| 4331 | "modify_config_set_family_selection requires a list as " |
||
| 4332 | "families argument" |
||
| 4333 | ) |
||
| 4334 | |||
| 4335 | cmd = XmlCommand("modify_config") |
||
| 4336 | cmd.set_attribute("config_id", str(config_id)) |
||
| 4337 | |||
| 4338 | _xmlfamsel = cmd.add_element("family_selection") |
||
| 4339 | _xmlfamsel.add_element("growing", _to_bool(auto_add_new_families)) |
||
| 4340 | |||
| 4341 | for family in families: |
||
| 4342 | _xmlfamily = _xmlfamsel.add_element("family") |
||
| 4343 | _xmlfamily.add_element("name", family) |
||
| 4344 | _xmlfamily.add_element("all", "1") |
||
| 4345 | _xmlfamily.add_element("growing", _to_bool(auto_add_new_nvts)) |
||
| 4346 | |||
| 4347 | return self._send_xml_command(cmd) |
||
| 4348 | |||
| 4349 | def modify_config(self, config_id, selection=None, **kwargs): |
||
| 4350 | """Modifies an existing scan config. |
||
| 4351 | |||
| 4352 | DEPRECATED. Please use *modify_config_set_* methods instead. |
||
| 4353 | |||
| 4354 | modify_config has four modes to operate depending on the selection. |
||
| 4355 | |||
| 4356 | Arguments: |
||
| 4357 | config_id (str): UUID of scan config to modify. |
||
| 4358 | selection (str): one of 'scan_pref', 'nvt_pref', 'nvt_selection' or |
||
| 4359 | 'family_selection' |
||
| 4360 | name (str, optional): New name for preference. |
||
| 4361 | value(str, optional): New value for preference. |
||
| 4362 | nvt_oids (list, optional): List of NVTs associated with preference |
||
| 4363 | to modify. |
||
| 4364 | family (str,optional): Name of family to modify. |
||
| 4365 | |||
| 4366 | Returns: |
||
| 4367 | The response. See :py:meth:`send_command` for details. |
||
| 4368 | """ |
||
| 4369 | if not config_id: |
||
| 4370 | raise RequiredArgument("modify_config required config_id argument") |
||
| 4371 | |||
| 4372 | if selection is None: |
||
| 4373 | deprecation( |
||
| 4374 | "Using modify_config to update the comment of a scan config is" |
||
| 4375 | "deprecated. Please use modify_config_set_comment instead." |
||
| 4376 | ) |
||
| 4377 | return self.modify_config_set_comment( |
||
| 4378 | config_id, kwargs.get("comment") |
||
| 4379 | ) |
||
| 4380 | |||
| 4381 | if selection not in ( |
||
| 4382 | "nvt_pref", |
||
| 4383 | "scan_pref", |
||
| 4384 | "family_selection", |
||
| 4385 | "nvt_selection", |
||
| 4386 | ): |
||
| 4387 | raise InvalidArgument( |
||
| 4388 | "selection must be one of nvt_pref, " |
||
| 4389 | "scan_pref, family_selection or " |
||
| 4390 | "nvt_selection" |
||
| 4391 | ) |
||
| 4392 | |||
| 4393 | if selection == "nvt_pref": |
||
| 4394 | deprecation( |
||
| 4395 | "Using modify_config to update a nvt preference of a scan " |
||
| 4396 | "config is deprecated. Please use " |
||
| 4397 | "modify_config_set_nvt_preference instead." |
||
| 4398 | ) |
||
| 4399 | return self.modify_config_set_nvt_preference(config_id, **kwargs) |
||
| 4400 | |||
| 4401 | if selection == "scan_pref": |
||
| 4402 | deprecation( |
||
| 4403 | "Using modify_config to update a scanner preference of a " |
||
| 4404 | "scan config is deprecated. Please use " |
||
| 4405 | "modify_config_set_scanner_preference instead." |
||
| 4406 | ) |
||
| 4407 | return self.modify_config_set_scanner_preference( |
||
| 4408 | config_id, **kwargs |
||
| 4409 | ) |
||
| 4410 | |||
| 4411 | if selection == "nvt_selection": |
||
| 4412 | deprecation( |
||
| 4413 | "Using modify_config to update a nvt selection of a " |
||
| 4414 | "scan config is deprecated. Please use " |
||
| 4415 | "modify_config_set_nvt_selection instead." |
||
| 4416 | ) |
||
| 4417 | return self.modify_config_set_nvt_selection(config_id, **kwargs) |
||
| 4418 | |||
| 4419 | deprecation( |
||
| 4420 | "Using modify_config to update a family selection of a " |
||
| 4421 | "scan config is deprecated. Please use " |
||
| 4422 | "modify_config_set_family_selection instead." |
||
| 4423 | ) |
||
| 4424 | return self.modify_config_set_family_selection(config_id, **kwargs) |
||
| 4425 | |||
| 4426 | def modify_credential( |
||
| 4427 | self, |
||
| 4428 | credential_id, |
||
| 4429 | credential_type=None, |
||
| 4430 | *, |
||
| 4431 | name=None, |
||
| 4432 | comment=None, |
||
| 4433 | allow_insecure=None, |
||
| 4434 | certificate=None, |
||
| 4435 | key_phrase=None, |
||
| 4436 | private_key=None, |
||
| 4437 | login=None, |
||
| 4438 | password=None, |
||
| 4439 | auth_algorithm=None, |
||
| 4440 | community=None, |
||
| 4441 | privacy_algorithm=None, |
||
| 4442 | privacy_password=None |
||
| 4443 | ): |
||
| 4444 | """Modifies an existing credential. |
||
| 4445 | |||
| 4446 | Arguments: |
||
| 4447 | credential_id (str): UUID of the credential |
||
| 4448 | credential_type (str, optional): The credential type. One of 'cc', |
||
| 4449 | 'snmp', 'up', 'usk' |
||
| 4450 | name (str, optional): Name of the credential |
||
| 4451 | comment (str, optional): Comment for the credential |
||
| 4452 | allow_insecure (boolean, optional): Whether to allow insecure use of |
||
| 4453 | the credential |
||
| 4454 | certificate (str, optional): Certificate for the credential |
||
| 4455 | key_phrase (str, optional): Key passphrase for the private key |
||
| 4456 | private_key (str, optional): Private key to use for login |
||
| 4457 | login (str, optional): Username for the credential |
||
| 4458 | password (str, optional): Password for the credential |
||
| 4459 | auth_algorithm (str, optional): The auth_algorithm, |
||
| 4460 | either md5 or sha1. |
||
| 4461 | community (str, optional): The SNMP community |
||
| 4462 | privacy_algorithm (str, optional): The SNMP privacy algorithm, |
||
| 4463 | either aes or des. |
||
| 4464 | privacy_password (str, optional): The SNMP privacy password |
||
| 4465 | |||
| 4466 | Returns: |
||
| 4467 | The response. See :py:meth:`send_command` for details. |
||
| 4468 | """ |
||
| 4469 | if not credential_id: |
||
| 4470 | raise RequiredArgument( |
||
| 4471 | "modify_credential requires " "a credential_id attribute" |
||
| 4472 | ) |
||
| 4473 | |||
| 4474 | cmd = XmlCommand("modify_credential") |
||
| 4475 | cmd.set_attribute("credential_id", credential_id) |
||
| 4476 | |||
| 4477 | if credential_type: |
||
| 4478 | if credential_type not in CREDENTIAL_TYPES: |
||
| 4479 | raise InvalidArgument( |
||
| 4480 | "modify_credential requires type " |
||
| 4481 | "to be either cc, snmp, up or usk" |
||
| 4482 | ) |
||
| 4483 | |||
| 4484 | if comment: |
||
| 4485 | cmd.add_element("comment", comment) |
||
| 4486 | |||
| 4487 | if name: |
||
| 4488 | cmd.add_element("name", name) |
||
| 4489 | |||
| 4490 | if allow_insecure is not None: |
||
| 4491 | cmd.add_element("allow_insecure", _to_bool(allow_insecure)) |
||
| 4492 | |||
| 4493 | if certificate: |
||
| 4494 | cmd.add_element("certificate", certificate) |
||
| 4495 | |||
| 4496 | if key_phrase is not None or private_key: |
||
| 4497 | if key_phrase is not None and not private_key: |
||
| 4498 | raise RequiredArgument( |
||
| 4499 | "modify_credential requires " |
||
| 4500 | "key_phrase and private_key arguments" |
||
| 4501 | ) |
||
| 4502 | |||
| 4503 | _xmlkey = cmd.add_element("key") |
||
| 4504 | _xmlkey.add_element("private", private_key) |
||
| 4505 | |||
| 4506 | if key_phrase is not None: |
||
| 4507 | _xmlkey.add_element("phrase", key_phrase) |
||
| 4508 | |||
| 4509 | if login: |
||
| 4510 | cmd.add_element("login", login) |
||
| 4511 | |||
| 4512 | if password: |
||
| 4513 | cmd.add_element("password", password) |
||
| 4514 | |||
| 4515 | if auth_algorithm is not None: |
||
| 4516 | if auth_algorithm not in ("md5", "sha1"): |
||
| 4517 | raise InvalidArgument( |
||
| 4518 | "modify_credential requires " |
||
| 4519 | "auth_algorithm to be either " |
||
| 4520 | "md5 or sha1" |
||
| 4521 | ) |
||
| 4522 | cmd.add_element("auth_algorithm", auth_algorithm) |
||
| 4523 | |||
| 4524 | if community: |
||
| 4525 | cmd.add_element("community", community) |
||
| 4526 | |||
| 4527 | if privacy_algorithm is not None: |
||
| 4528 | if privacy_algorithm not in ("aes", "des"): |
||
| 4529 | raise InvalidArgument( |
||
| 4530 | "modify_credential requires " |
||
| 4531 | "privacy_algorithm to be either" |
||
| 4532 | "aes or des" |
||
| 4533 | ) |
||
| 4534 | |||
| 4535 | _xmlprivacy = cmd.add_element("privacy") |
||
| 4536 | _xmlprivacy.add_element("algorithm", privacy_algorithm) |
||
| 4537 | |||
| 4538 | if privacy_password is not None: |
||
| 4539 | _xmlprivacy.add_element("password", privacy_password) |
||
| 4540 | |||
| 4541 | return self._send_xml_command(cmd) |
||
| 4542 | |||
| 4543 | def modify_filter( |
||
| 4544 | self, filter_id, *, comment=None, name=None, term=None, filter_type=None |
||
| 4545 | ): |
||
| 4546 | """Modifies an existing filter. |
||
| 4547 | |||
| 4548 | Arguments: |
||
| 4549 | filter_id (str): UUID of the filter to be modified |
||
| 4550 | comment (str, optional): Comment on filter. |
||
| 4551 | name (str, optional): Name of filter. |
||
| 4552 | term (str, optional): Filter term. |
||
| 4553 | filter_type (str, optional): Resource type filter applies to. |
||
| 4554 | |||
| 4555 | Returns: |
||
| 4556 | The response. See :py:meth:`send_command` for details. |
||
| 4557 | """ |
||
| 4558 | if not filter_id: |
||
| 4559 | raise RequiredArgument( |
||
| 4560 | "modify_filter requires a filter_id " "attribute" |
||
| 4561 | ) |
||
| 4562 | |||
| 4563 | cmd = XmlCommand("modify_filter") |
||
| 4564 | cmd.set_attribute("filter_id", filter_id) |
||
| 4565 | |||
| 4566 | if comment: |
||
| 4567 | cmd.add_element("comment", comment) |
||
| 4568 | |||
| 4569 | if name: |
||
| 4570 | cmd.add_element("name", name) |
||
| 4571 | |||
| 4572 | if term: |
||
| 4573 | cmd.add_element("term", term) |
||
| 4574 | |||
| 4575 | if filter_type: |
||
| 4576 | filter_type = filter_type.lower() |
||
| 4577 | if filter_type not in FILTER_TYPES: |
||
| 4578 | raise InvalidArgument( |
||
| 4579 | "modify_filter requires type to be one of {0} but " |
||
| 4580 | "was {1}".format(", ".join(FILTER_TYPES), filter_type) |
||
| 4581 | ) |
||
| 4582 | cmd.add_element("type", filter_type) |
||
| 4583 | |||
| 4584 | return self._send_xml_command(cmd) |
||
| 4585 | |||
| 4586 | def modify_group(self, group_id, *, comment=None, name=None, users=None): |
||
| 4587 | """Modifies an existing group. |
||
| 4588 | |||
| 4589 | Arguments: |
||
| 4590 | group_id (str): UUID of group to modify. |
||
| 4591 | comment (str, optional): Comment on group. |
||
| 4592 | name (str, optional): Name of group. |
||
| 4593 | users (list, optional): List of user names to be in the group |
||
| 4594 | |||
| 4595 | Returns: |
||
| 4596 | The response. See :py:meth:`send_command` for details. |
||
| 4597 | """ |
||
| 4598 | if not group_id: |
||
| 4599 | raise RequiredArgument("modify_group requires a group_id argument") |
||
| 4600 | |||
| 4601 | cmd = XmlCommand("modify_group") |
||
| 4602 | cmd.set_attribute("group_id", group_id) |
||
| 4603 | |||
| 4604 | if comment: |
||
| 4605 | cmd.add_element("comment", comment) |
||
| 4606 | |||
| 4607 | if name: |
||
| 4608 | cmd.add_element("name", name) |
||
| 4609 | |||
| 4610 | if users: |
||
| 4611 | cmd.add_element("users", _to_comma_list(users)) |
||
| 4612 | |||
| 4613 | return self._send_xml_command(cmd) |
||
| 4614 | |||
| 4615 | View Code Duplication | def modify_note( |
|
| 4616 | self, |
||
| 4617 | note_id, |
||
| 4618 | text, |
||
| 4619 | *, |
||
| 4620 | seconds_active=None, |
||
| 4621 | hosts=None, |
||
| 4622 | port=None, |
||
| 4623 | result_id=None, |
||
| 4624 | severity=None, |
||
| 4625 | task_id=None, |
||
| 4626 | threat=None |
||
| 4627 | ): |
||
| 4628 | """Modifies an existing note. |
||
| 4629 | |||
| 4630 | Arguments: |
||
| 4631 | note_id (str): UUID of note to modify. |
||
| 4632 | text (str): The text of the note. |
||
| 4633 | seconds_active (int, optional): Seconds note will be active. |
||
| 4634 | -1 on always, 0 off. |
||
| 4635 | hosts (list, optional): A list of hosts addresses |
||
| 4636 | port (int, optional): Port to which note applies. |
||
| 4637 | result_id (str, optional): Result to which note applies. |
||
| 4638 | severity (descimal, optional): Severity to which note applies. |
||
| 4639 | task_id (str, optional): Task to which note applies. |
||
| 4640 | threat (str, optional): Threat level to which note applies. One of |
||
| 4641 | High, Medium, Low, Alarm, Log or Debug. Will be converted to |
||
| 4642 | severity. |
||
| 4643 | |||
| 4644 | Returns: |
||
| 4645 | The response. See :py:meth:`send_command` for details. |
||
| 4646 | """ |
||
| 4647 | if not note_id: |
||
| 4648 | raise RequiredArgument("modify_note requires a note_id attribute") |
||
| 4649 | |||
| 4650 | if not text: |
||
| 4651 | raise RequiredArgument("modify_note requires a text element") |
||
| 4652 | |||
| 4653 | cmd = XmlCommand("modify_note") |
||
| 4654 | cmd.set_attribute("note_id", note_id) |
||
| 4655 | cmd.add_element("text", text) |
||
| 4656 | |||
| 4657 | if not seconds_active is None: |
||
| 4658 | cmd.add_element("active", str(seconds_active)) |
||
| 4659 | |||
| 4660 | if hosts: |
||
| 4661 | cmd.add_element("hosts", _to_comma_list(hosts)) |
||
| 4662 | |||
| 4663 | if port: |
||
| 4664 | cmd.add_element("port", str(port)) |
||
| 4665 | |||
| 4666 | if result_id: |
||
| 4667 | cmd.add_element("result", attrs={"id": result_id}) |
||
| 4668 | |||
| 4669 | if severity: |
||
| 4670 | cmd.add_element("severity", str(severity)) |
||
| 4671 | |||
| 4672 | if task_id: |
||
| 4673 | cmd.add_element("task", attrs={"id": task_id}) |
||
| 4674 | |||
| 4675 | if threat is not None: |
||
| 4676 | cmd.add_element("threat", threat) |
||
| 4677 | |||
| 4678 | if threat not in THREAD_TYPES: |
||
| 4679 | raise InvalidArgument( |
||
| 4680 | "modify_note threat argument {0} is invalid. threat must " |
||
| 4681 | "be one of {1}".format(threat, ", ".join(THREAD_TYPES)) |
||
| 4682 | ) |
||
| 4683 | |||
| 4684 | return self._send_xml_command(cmd) |
||
| 4685 | |||
| 4686 | View Code Duplication | def modify_override( |
|
| 4687 | self, |
||
| 4688 | override_id, |
||
| 4689 | text, |
||
| 4690 | *, |
||
| 4691 | seconds_active=None, |
||
| 4692 | hosts=None, |
||
| 4693 | port=None, |
||
| 4694 | result_id=None, |
||
| 4695 | severity=None, |
||
| 4696 | new_severity=None, |
||
| 4697 | task_id=None, |
||
| 4698 | threat=None, |
||
| 4699 | new_threat=None |
||
| 4700 | ): |
||
| 4701 | """Modifies an existing override. |
||
| 4702 | |||
| 4703 | Arguments: |
||
| 4704 | override_id (str): UUID of override to modify. |
||
| 4705 | text (str): The text of the override. |
||
| 4706 | seconds_active (int, optional): Seconds override will be active. |
||
| 4707 | -1 on always, 0 off. |
||
| 4708 | hosts (list, optional): A list of host addresses |
||
| 4709 | port (int, optional): Port to which override applies. |
||
| 4710 | result_id (str, optional): Result to which override applies. |
||
| 4711 | severity (decimal, optional): Severity to which override applies. |
||
| 4712 | new_severity (decimal, optional): New severity score for result. |
||
| 4713 | task_id (str, optional): Task to which override applies. |
||
| 4714 | threat (str, optional): Threat level to which override applies. One |
||
| 4715 | of High, Medium, Low, Alarm, Log or Debug. Will be converted to |
||
| 4716 | severity. |
||
| 4717 | new_threat (str, optional): New threat level for results. One |
||
| 4718 | of High, Medium, Low, Alarm, Log or Debug. Will be converted to |
||
| 4719 | new_severity. |
||
| 4720 | |||
| 4721 | Returns: |
||
| 4722 | The response. See :py:meth:`send_command` for details. |
||
| 4723 | """ |
||
| 4724 | if not override_id: |
||
| 4725 | raise RequiredArgument( |
||
| 4726 | "modify_override requires a override_id " "argument" |
||
| 4727 | ) |
||
| 4728 | if not text: |
||
| 4729 | raise RequiredArgument("modify_override requires a text argument") |
||
| 4730 | |||
| 4731 | cmd = XmlCommand("modify_override") |
||
| 4732 | cmd.set_attribute("override_id", override_id) |
||
| 4733 | cmd.add_element("text", text) |
||
| 4734 | |||
| 4735 | if not seconds_active is None: |
||
| 4736 | cmd.add_element("active", str(seconds_active)) |
||
| 4737 | |||
| 4738 | if hosts: |
||
| 4739 | cmd.add_element("hosts", _to_comma_list(hosts)) |
||
| 4740 | |||
| 4741 | if port: |
||
| 4742 | cmd.add_element("port", str(port)) |
||
| 4743 | |||
| 4744 | if result_id: |
||
| 4745 | cmd.add_element("result", attrs={"id": result_id}) |
||
| 4746 | |||
| 4747 | if severity: |
||
| 4748 | cmd.add_element("severity", str(severity)) |
||
| 4749 | |||
| 4750 | if new_severity: |
||
| 4751 | cmd.add_element("new_severity", str(new_severity)) |
||
| 4752 | |||
| 4753 | if task_id: |
||
| 4754 | cmd.add_element("task", attrs={"id": task_id}) |
||
| 4755 | |||
| 4756 | if threat is not None: |
||
| 4757 | if threat not in THREAD_TYPES: |
||
| 4758 | raise InvalidArgument( |
||
| 4759 | "modify_override threat argument {0} is invalid. threat" |
||
| 4760 | "must be one of {1}".format(threat, ", ".join(THREAD_TYPES)) |
||
| 4761 | ) |
||
| 4762 | cmd.add_element("threat", threat) |
||
| 4763 | |||
| 4764 | if new_threat is not None: |
||
| 4765 | if new_threat not in THREAD_TYPES: |
||
| 4766 | raise InvalidArgument( |
||
| 4767 | "modify_override new_threat argument {0} is invalid. " |
||
| 4768 | "new_threat must be one of {1}".format( |
||
| 4769 | new_threat, ", ".join(THREAD_TYPES) |
||
| 4770 | ) |
||
| 4771 | ) |
||
| 4772 | |||
| 4773 | cmd.add_element("new_threat", new_threat) |
||
| 4774 | |||
| 4775 | return self._send_xml_command(cmd) |
||
| 4776 | |||
| 4777 | def modify_permission( |
||
| 4778 | self, |
||
| 4779 | permission_id, |
||
| 4780 | *, |
||
| 4781 | comment=None, |
||
| 4782 | name=None, |
||
| 4783 | resource_id=None, |
||
| 4784 | resource_type=None, |
||
| 4785 | subject_id=None, |
||
| 4786 | subject_type=None |
||
| 4787 | ): |
||
| 4788 | """Modifies an existing permission. |
||
| 4789 | |||
| 4790 | Arguments: |
||
| 4791 | permission_id (str): UUID of permission to be modified. |
||
| 4792 | comment (str, optional): The comment on the permission. |
||
| 4793 | name (str, optional): Permission name, currently the name of |
||
| 4794 | a command. |
||
| 4795 | subject_id (str, optional): UUID of subject to whom the permission |
||
| 4796 | is granted |
||
| 4797 | subject_type (str, optional): Type of the subject user, group or |
||
| 4798 | role |
||
| 4799 | resource_id (str, optional): UUID of entity to which the permission |
||
| 4800 | applies |
||
| 4801 | resource_type (str, optional): Type of the resource. For Super |
||
| 4802 | permissions user, group or role |
||
| 4803 | |||
| 4804 | Returns: |
||
| 4805 | The response. See :py:meth:`send_command` for details. |
||
| 4806 | """ |
||
| 4807 | if not permission_id: |
||
| 4808 | raise RequiredArgument( |
||
| 4809 | "modify_permission requires " "a permission_id element" |
||
| 4810 | ) |
||
| 4811 | |||
| 4812 | cmd = XmlCommand("modify_permission") |
||
| 4813 | cmd.set_attribute("permission_id", permission_id) |
||
| 4814 | |||
| 4815 | if comment: |
||
| 4816 | cmd.add_element("comment", comment) |
||
| 4817 | |||
| 4818 | if name: |
||
| 4819 | cmd.add_element("name", name) |
||
| 4820 | |||
| 4821 | if resource_id or resource_type: |
||
| 4822 | if not resource_id: |
||
| 4823 | raise RequiredArgument( |
||
| 4824 | "modify_permission requires resource_id for resource_type" |
||
| 4825 | ) |
||
| 4826 | |||
| 4827 | if not resource_type: |
||
| 4828 | raise RequiredArgument( |
||
| 4829 | "modify_permission requires resource_type for resource_id" |
||
| 4830 | ) |
||
| 4831 | |||
| 4832 | _xmlresource = cmd.add_element( |
||
| 4833 | "resource", attrs={"id": resource_id} |
||
| 4834 | ) |
||
| 4835 | _xmlresource.add_element("type", resource_type) |
||
| 4836 | |||
| 4837 | if subject_id or subject_type: |
||
| 4838 | if not subject_id: |
||
| 4839 | raise RequiredArgument( |
||
| 4840 | "modify_permission requires a subject_id for subject_type" |
||
| 4841 | ) |
||
| 4842 | |||
| 4843 | if subject_type not in SUBJECT_TYPES: |
||
| 4844 | raise InvalidArgument( |
||
| 4845 | "modify_permission requires subject_type to be either " |
||
| 4846 | "user, group or role" |
||
| 4847 | ) |
||
| 4848 | |||
| 4849 | _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
||
| 4850 | _xmlsubject.add_element("type", subject_type) |
||
| 4851 | |||
| 4852 | return self._send_xml_command(cmd) |
||
| 4853 | |||
| 4854 | def modify_port_list(self, port_list_id, *, comment=None, name=None): |
||
| 4855 | """Modifies an existing port list. |
||
| 4856 | |||
| 4857 | Arguments: |
||
| 4858 | port_list_id (str): UUID of port list to modify. |
||
| 4859 | name (str, optional): Name of port list. |
||
| 4860 | comment (str, optional): Comment on port list. |
||
| 4861 | |||
| 4862 | Returns: |
||
| 4863 | The response. See :py:meth:`send_command` for details. |
||
| 4864 | """ |
||
| 4865 | if not port_list_id: |
||
| 4866 | raise RequiredArgument( |
||
| 4867 | "modify_port_list requires " "a port_list_id attribute" |
||
| 4868 | ) |
||
| 4869 | cmd = XmlCommand("modify_port_list") |
||
| 4870 | cmd.set_attribute("port_list_id", port_list_id) |
||
| 4871 | |||
| 4872 | if comment: |
||
| 4873 | cmd.add_element("comment", comment) |
||
| 4874 | |||
| 4875 | if name: |
||
| 4876 | cmd.add_element("name", name) |
||
| 4877 | |||
| 4878 | return self._send_xml_command(cmd) |
||
| 4879 | |||
| 4880 | def modify_report_format( |
||
| 4881 | self, |
||
| 4882 | report_format_id, |
||
| 4883 | *, |
||
| 4884 | active=None, |
||
| 4885 | name=None, |
||
| 4886 | summary=None, |
||
| 4887 | param_name=None, |
||
| 4888 | param_value=None |
||
| 4889 | ): |
||
| 4890 | """Modifies an existing report format. |
||
| 4891 | |||
| 4892 | Arguments: |
||
| 4893 | report_format_id (str) UUID of report format to modify. |
||
| 4894 | active (boolean, optional): Whether the report format is active. |
||
| 4895 | name (str, optional): The name of the report format. |
||
| 4896 | summary (str, optional): A summary of the report format. |
||
| 4897 | param_name (str, optional): The name of the param. |
||
| 4898 | param_value (str, optional): The value of the param. |
||
| 4899 | |||
| 4900 | Returns: |
||
| 4901 | The response. See :py:meth:`send_command` for details. |
||
| 4902 | """ |
||
| 4903 | if not report_format_id: |
||
| 4904 | raise RequiredArgument( |
||
| 4905 | "modify_report requires " "a report_format_id attribute" |
||
| 4906 | ) |
||
| 4907 | |||
| 4908 | cmd = XmlCommand("modify_report_format") |
||
| 4909 | cmd.set_attribute("report_format_id", report_format_id) |
||
| 4910 | |||
| 4911 | if active is not None: |
||
| 4912 | cmd.add_element("active", _to_bool(active)) |
||
| 4913 | |||
| 4914 | if name: |
||
| 4915 | cmd.add_element("name", name) |
||
| 4916 | |||
| 4917 | if summary: |
||
| 4918 | cmd.add_element("summary", summary) |
||
| 4919 | |||
| 4920 | if param_name: |
||
| 4921 | _xmlparam = cmd.add_element("param") |
||
| 4922 | _xmlparam.add_element("name", param_name) |
||
| 4923 | |||
| 4924 | if param_value is not None: |
||
| 4925 | _xmlparam.add_element("value", param_value) |
||
| 4926 | |||
| 4927 | return self._send_xml_command(cmd) |
||
| 4928 | |||
| 4929 | def modify_role(self, role_id, *, comment=None, name=None, users=None): |
||
| 4930 | """Modifies an existing role. |
||
| 4931 | |||
| 4932 | Arguments: |
||
| 4933 | role_id (str): UUID of role to modify. |
||
| 4934 | comment (str, optional): Name of role. |
||
| 4935 | name (str, optional): Comment on role. |
||
| 4936 | users (list, optional): List of user names. |
||
| 4937 | |||
| 4938 | Returns: |
||
| 4939 | The response. See :py:meth:`send_command` for details. |
||
| 4940 | """ |
||
| 4941 | if not role_id: |
||
| 4942 | raise RequiredArgument("modify_role requires a role_id argument") |
||
| 4943 | |||
| 4944 | cmd = XmlCommand("modify_role") |
||
| 4945 | cmd.set_attribute("role_id", role_id) |
||
| 4946 | |||
| 4947 | if comment: |
||
| 4948 | cmd.add_element("comment", comment) |
||
| 4949 | |||
| 4950 | if name: |
||
| 4951 | cmd.add_element("name", name) |
||
| 4952 | |||
| 4953 | if users: |
||
| 4954 | cmd.add_element("users", _to_comma_list(users)) |
||
| 4955 | |||
| 4956 | return self._send_xml_command(cmd) |
||
| 4957 | |||
| 4958 | def modify_scanner( |
||
| 4959 | self, |
||
| 4960 | scanner_id, |
||
| 4961 | *, |
||
| 4962 | scanner_type=None, |
||
| 4963 | host=None, |
||
| 4964 | port=None, |
||
| 4965 | comment=None, |
||
| 4966 | name=None, |
||
| 4967 | ca_pub=None, |
||
| 4968 | credential_id=None |
||
| 4969 | ): |
||
| 4970 | """Modifies an existing scanner. |
||
| 4971 | |||
| 4972 | Arguments: |
||
| 4973 | scanner_id (str): UUID of scanner to modify. |
||
| 4974 | scanner_type (str, optional): New type of the Scanner. Must be one |
||
| 4975 | of '1' (OSP Scanner), '2' (OpenVAS Scanner), '3' CVE Scanner or |
||
| 4976 | '4' (GMP Scanner). |
||
| 4977 | host (str, optional): Host of the scanner. |
||
| 4978 | port (int, optional): Port of the scanner. |
||
| 4979 | comment (str, optional): Comment on scanner. |
||
| 4980 | name (str, optional): Name of scanner. |
||
| 4981 | ca_pub (str, optional): Certificate of CA to verify scanner's |
||
| 4982 | certificate. |
||
| 4983 | credential_id (str, optional): UUID of the client certificate |
||
| 4984 | credential for the Scanner. |
||
| 4985 | |||
| 4986 | Returns: |
||
| 4987 | The response. See :py:meth:`send_command` for details. |
||
| 4988 | """ |
||
| 4989 | if not scanner_id: |
||
| 4990 | raise RequiredArgument( |
||
| 4991 | "modify_scanner requires a scanner_id argument" |
||
| 4992 | ) |
||
| 4993 | |||
| 4994 | if scanner_type is not None and scanner_type not in SCANNER_TYPES: |
||
| 4995 | raise InvalidArgument( |
||
| 4996 | "modify_scanner requires a scanner_type " |
||
| 4997 | 'argument which must be either "1" for OSP, ' |
||
| 4998 | '"2" for OpenVAS (Classic), "3" for CVE or ' |
||
| 4999 | '"4" for GMP Scanner.' |
||
| 5000 | ) |
||
| 5001 | |||
| 5002 | cmd = XmlCommand("modify_scanner") |
||
| 5003 | cmd.set_attribute("scanner_id", scanner_id) |
||
| 5004 | |||
| 5005 | if scanner_type: |
||
| 5006 | cmd.add_element("type", scanner_type) |
||
| 5007 | |||
| 5008 | if host: |
||
| 5009 | cmd.add_element("host", host) |
||
| 5010 | |||
| 5011 | if port: |
||
| 5012 | cmd.add_element("port", str(port)) |
||
| 5013 | |||
| 5014 | if comment: |
||
| 5015 | cmd.add_element("comment", comment) |
||
| 5016 | |||
| 5017 | if name: |
||
| 5018 | cmd.add_element("name", name) |
||
| 5019 | |||
| 5020 | if ca_pub: |
||
| 5021 | cmd.add_element("ca_pub", ca_pub) |
||
| 5022 | |||
| 5023 | if credential_id: |
||
| 5024 | cmd.add_element("credential", attrs={"id": str(credential_id)}) |
||
| 5025 | |||
| 5026 | return self._send_xml_command(cmd) |
||
| 5027 | |||
| 5028 | View Code Duplication | def modify_schedule( |
|
| 5029 | self, |
||
| 5030 | schedule_id, |
||
| 5031 | *, |
||
| 5032 | comment=None, |
||
| 5033 | name=None, |
||
| 5034 | first_time_minute=None, |
||
| 5035 | first_time_hour=None, |
||
| 5036 | first_time_day_of_month=None, |
||
| 5037 | first_time_month=None, |
||
| 5038 | first_time_year=None, |
||
| 5039 | duration=None, |
||
| 5040 | duration_unit=None, |
||
| 5041 | period=None, |
||
| 5042 | period_unit=None, |
||
| 5043 | timezone=None |
||
| 5044 | ): |
||
| 5045 | """Modifies an existing schedule. |
||
| 5046 | |||
| 5047 | Arguments: |
||
| 5048 | schedule_id (str): UUID of schedule to modify. |
||
| 5049 | name (str, optional): Name of the schedule |
||
| 5050 | comment (str, optional): Comment for the schedule |
||
| 5051 | first_time_minute (int, optional): First time minute the schedule |
||
| 5052 | will run. Must be an integer >= 0. |
||
| 5053 | first_time_hour (int, optional): First time hour the schedule |
||
| 5054 | will run. Must be an integer >= 0. |
||
| 5055 | first_time_day_of_month (int, optional): First time day of month the |
||
| 5056 | schedule will run. Must be an integer > 0 <= 31. |
||
| 5057 | first_time_month (int, optional): First time month the schedule |
||
| 5058 | will run. Must be an integer >= 1 <= 12. |
||
| 5059 | first_time_year (int, optional): First time year the schedule |
||
| 5060 | will run |
||
| 5061 | duration (int, optional): How long the Manager will run the |
||
| 5062 | scheduled task for until it gets paused if not finished yet. |
||
| 5063 | duration_unit (str, optional): Unit of the duration. One of second, |
||
| 5064 | minute, hour, day, week, month, year, decade. Required if |
||
| 5065 | duration is set. |
||
| 5066 | period (int, optional): How often the Manager will repeat the |
||
| 5067 | scheduled task. Must be an integer > 0. |
||
| 5068 | period_unit (str, optional): Unit of the period. One of second, |
||
| 5069 | minute, hour, day, week, month, year, decade. Required if |
||
| 5070 | period is set. |
||
| 5071 | timezone (str, optional): The timezone the schedule will follow |
||
| 5072 | |||
| 5073 | Returns: |
||
| 5074 | The response. See :py:meth:`send_command` for details. |
||
| 5075 | """ |
||
| 5076 | if not schedule_id: |
||
| 5077 | raise RequiredArgument( |
||
| 5078 | "modify_schedule requires a schedule_id" "argument" |
||
| 5079 | ) |
||
| 5080 | |||
| 5081 | cmd = XmlCommand("modify_schedule") |
||
| 5082 | cmd.set_attribute("schedule_id", schedule_id) |
||
| 5083 | |||
| 5084 | if comment: |
||
| 5085 | cmd.add_element("comment", comment) |
||
| 5086 | |||
| 5087 | if name: |
||
| 5088 | cmd.add_element("name", name) |
||
| 5089 | |||
| 5090 | if ( |
||
| 5091 | first_time_minute is not None |
||
| 5092 | or first_time_hour is not None |
||
| 5093 | or first_time_day_of_month is not None |
||
| 5094 | or first_time_month is not None |
||
| 5095 | or first_time_year is not None |
||
| 5096 | ): |
||
| 5097 | |||
| 5098 | if first_time_minute is None: |
||
| 5099 | raise RequiredArgument( |
||
| 5100 | "Setting first_time requires first_time_minute argument" |
||
| 5101 | ) |
||
| 5102 | elif ( |
||
| 5103 | not isinstance(first_time_minute, numbers.Integral) |
||
| 5104 | or first_time_minute < 0 |
||
| 5105 | ): |
||
| 5106 | raise InvalidArgument( |
||
| 5107 | "first_time_minute argument of modify_schedule needs to be " |
||
| 5108 | "an integer greater or equal 0" |
||
| 5109 | ) |
||
| 5110 | |||
| 5111 | if first_time_hour is None: |
||
| 5112 | raise RequiredArgument( |
||
| 5113 | "Setting first_time requires first_time_hour argument" |
||
| 5114 | ) |
||
| 5115 | elif ( |
||
| 5116 | not isinstance(first_time_hour, numbers.Integral) |
||
| 5117 | or first_time_hour < 0 |
||
| 5118 | ): |
||
| 5119 | raise InvalidArgument( |
||
| 5120 | "first_time_hour argument of modify_schedule needs to be " |
||
| 5121 | "an integer greater or equal 0" |
||
| 5122 | ) |
||
| 5123 | |||
| 5124 | if first_time_day_of_month is None: |
||
| 5125 | raise RequiredArgument( |
||
| 5126 | "Setting first_time requires first_time_day_of_month " |
||
| 5127 | "argument" |
||
| 5128 | ) |
||
| 5129 | elif ( |
||
| 5130 | not isinstance(first_time_day_of_month, numbers.Integral) |
||
| 5131 | or first_time_day_of_month < 1 |
||
| 5132 | or first_time_day_of_month > 31 |
||
| 5133 | ): |
||
| 5134 | raise InvalidArgument( |
||
| 5135 | "first_time_day_of_month argument of modify_schedule needs " |
||
| 5136 | "to be an integer between 1 and 31" |
||
| 5137 | ) |
||
| 5138 | |||
| 5139 | if first_time_month is None: |
||
| 5140 | raise RequiredArgument( |
||
| 5141 | "Setting first_time requires first_time_month argument" |
||
| 5142 | ) |
||
| 5143 | elif ( |
||
| 5144 | not isinstance(first_time_month, numbers.Integral) |
||
| 5145 | or first_time_month < 1 |
||
| 5146 | or first_time_month > 12 |
||
| 5147 | ): |
||
| 5148 | raise InvalidArgument( |
||
| 5149 | "first_time_month argument of modify_schedule needs " |
||
| 5150 | "to be an integer between 1 and 12" |
||
| 5151 | ) |
||
| 5152 | |||
| 5153 | if first_time_year is None: |
||
| 5154 | raise RequiredArgument( |
||
| 5155 | "Setting first_time requires first_time_year argument" |
||
| 5156 | ) |
||
| 5157 | elif ( |
||
| 5158 | not isinstance(first_time_year, numbers.Integral) |
||
| 5159 | or first_time_year < 1970 |
||
| 5160 | ): |
||
| 5161 | raise InvalidArgument( |
||
| 5162 | "first_time_year argument of create_schedule needs " |
||
| 5163 | "to be an integer greater or equal 1970" |
||
| 5164 | ) |
||
| 5165 | |||
| 5166 | _xmlftime = cmd.add_element("first_time") |
||
| 5167 | _xmlftime.add_element("minute", str(first_time_minute)) |
||
| 5168 | _xmlftime.add_element("hour", str(first_time_hour)) |
||
| 5169 | _xmlftime.add_element("day_of_month", str(first_time_day_of_month)) |
||
| 5170 | _xmlftime.add_element("month", str(first_time_month)) |
||
| 5171 | _xmlftime.add_element("year", str(first_time_year)) |
||
| 5172 | |||
| 5173 | if duration is not None: |
||
| 5174 | if not duration_unit: |
||
| 5175 | raise RequiredArgument( |
||
| 5176 | "Setting duration requires duration_unit argument" |
||
| 5177 | ) |
||
| 5178 | |||
| 5179 | if not duration_unit in TIME_UNITS: |
||
| 5180 | raise InvalidArgument( |
||
| 5181 | "duration_unit must be one of {units} but {actual} has " |
||
| 5182 | "been passed".format( |
||
| 5183 | units=", ".join(TIME_UNITS), actual=duration_unit |
||
| 5184 | ) |
||
| 5185 | ) |
||
| 5186 | |||
| 5187 | if not isinstance(duration, numbers.Integral) or duration < 1: |
||
| 5188 | raise InvalidArgument( |
||
| 5189 | "duration argument must be an integer greater than 0" |
||
| 5190 | ) |
||
| 5191 | |||
| 5192 | _xmlduration = cmd.add_element("duration", str(duration)) |
||
| 5193 | _xmlduration.add_element("unit", duration_unit) |
||
| 5194 | |||
| 5195 | if period is not None: |
||
| 5196 | if not period_unit: |
||
| 5197 | raise RequiredArgument( |
||
| 5198 | "Setting period requires period_unit argument" |
||
| 5199 | ) |
||
| 5200 | |||
| 5201 | if not period_unit in TIME_UNITS: |
||
| 5202 | raise InvalidArgument( |
||
| 5203 | "period_unit must be one of {units} but {actual} has " |
||
| 5204 | "been passed".format( |
||
| 5205 | units=", ".join(TIME_UNITS), actual=period_unit |
||
| 5206 | ) |
||
| 5207 | ) |
||
| 5208 | |||
| 5209 | if not isinstance(period, numbers.Integral) or period < 1: |
||
| 5210 | raise InvalidArgument( |
||
| 5211 | "period argument must be an integer greater than 0" |
||
| 5212 | ) |
||
| 5213 | |||
| 5214 | _xmlperiod = cmd.add_element("period", str(period)) |
||
| 5215 | _xmlperiod.add_element("unit", period_unit) |
||
| 5216 | |||
| 5217 | if timezone: |
||
| 5218 | cmd.add_element("timezone", timezone) |
||
| 5219 | |||
| 5220 | return self._send_xml_command(cmd) |
||
| 5221 | |||
| 5222 | def modify_setting(self, setting_id=None, name=None, value=None): |
||
| 5223 | """Modifies an existing setting. |
||
| 5224 | |||
| 5225 | Arguments: |
||
| 5226 | setting_id (str, optional): UUID of the setting to be changed. |
||
| 5227 | name (str, optional): The name of the setting. Either setting_id or |
||
| 5228 | name must be passed. |
||
| 5229 | value (str): The value of the setting. |
||
| 5230 | |||
| 5231 | Returns: |
||
| 5232 | The response. See :py:meth:`send_command` for details. |
||
| 5233 | """ |
||
| 5234 | if not setting_id and not name: |
||
| 5235 | raise RequiredArgument( |
||
| 5236 | "modify_setting requires a setting_id or name argument" |
||
| 5237 | ) |
||
| 5238 | |||
| 5239 | if value is None: |
||
| 5240 | raise RequiredArgument("modify_setting requires a value argument") |
||
| 5241 | |||
| 5242 | cmd = XmlCommand("modify_setting") |
||
| 5243 | |||
| 5244 | if setting_id: |
||
| 5245 | cmd.set_attribute("setting_id", setting_id) |
||
| 5246 | else: |
||
| 5247 | cmd.add_element("name", name) |
||
| 5248 | |||
| 5249 | cmd.add_element("value", _to_base64(value)) |
||
| 5250 | |||
| 5251 | return self._send_xml_command(cmd) |
||
| 5252 | |||
| 5253 | View Code Duplication | def modify_tag( |
|
| 5254 | self, |
||
| 5255 | tag_id, |
||
| 5256 | *, |
||
| 5257 | comment=None, |
||
| 5258 | name=None, |
||
| 5259 | value=None, |
||
| 5260 | active=None, |
||
| 5261 | resource_id=None, |
||
| 5262 | resource_type=None |
||
| 5263 | ): |
||
| 5264 | """Modifies an existing tag. |
||
| 5265 | |||
| 5266 | Arguments: |
||
| 5267 | tag_id (str): UUID of the tag. |
||
| 5268 | comment (str, optional): Comment to add to the tag. |
||
| 5269 | name (str, optional): Name of the tag. |
||
| 5270 | value (str, optional): Value of the tag. |
||
| 5271 | active (boolean, optional): Whether the tag is active. |
||
| 5272 | resource_id (str, optional): IDs of the resource to which to |
||
| 5273 | attach the tag. Required if resource_type is set. |
||
| 5274 | resource_type (str, optional): Type of the resource to which to |
||
| 5275 | attach the tag. Required if resource_id is set. |
||
| 5276 | |||
| 5277 | Returns: |
||
| 5278 | The response. See :py:meth:`send_command` for details. |
||
| 5279 | """ |
||
| 5280 | if not tag_id: |
||
| 5281 | raise RequiredArgument("modify_tag requires a tag_id element") |
||
| 5282 | |||
| 5283 | cmd = XmlCommand("modify_tag") |
||
| 5284 | cmd.set_attribute("tag_id", str(tag_id)) |
||
| 5285 | |||
| 5286 | if comment: |
||
| 5287 | cmd.add_element("comment", comment) |
||
| 5288 | |||
| 5289 | if name: |
||
| 5290 | cmd.add_element("name", name) |
||
| 5291 | |||
| 5292 | if value: |
||
| 5293 | cmd.add_element("value", value) |
||
| 5294 | |||
| 5295 | if active is not None: |
||
| 5296 | cmd.add_element("active", _to_bool(active)) |
||
| 5297 | |||
| 5298 | if resource_id or resource_type: |
||
| 5299 | if not resource_id: |
||
| 5300 | raise RequiredArgument( |
||
| 5301 | "modify_tag requires resource_id argument when " |
||
| 5302 | "resource_type is set" |
||
| 5303 | ) |
||
| 5304 | |||
| 5305 | if not resource_type: |
||
| 5306 | raise RequiredArgument( |
||
| 5307 | "modify_tag requires resource_type argument when " |
||
| 5308 | "resource_id is set" |
||
| 5309 | ) |
||
| 5310 | |||
| 5311 | _xmlresource = cmd.add_element( |
||
| 5312 | "resource", attrs={"id": resource_id} |
||
| 5313 | ) |
||
| 5314 | _xmlresource.add_element("type", resource_type) |
||
| 5315 | |||
| 5316 | return self._send_xml_command(cmd) |
||
| 5317 | |||
| 5318 | def modify_target( |
||
| 5319 | self, |
||
| 5320 | target_id, |
||
| 5321 | *, |
||
| 5322 | name=None, |
||
| 5323 | comment=None, |
||
| 5324 | hosts=None, |
||
| 5325 | exclude_hosts=None, |
||
| 5326 | ssh_credential_id=None, |
||
| 5327 | ssh_credential_port=None, |
||
| 5328 | smb_credential_id=None, |
||
| 5329 | esxi_credential_id=None, |
||
| 5330 | snmp_credential_id=None, |
||
| 5331 | alive_tests=None, |
||
| 5332 | reverse_lookup_only=None, |
||
| 5333 | reverse_lookup_unify=None, |
||
| 5334 | port_list_id=None |
||
| 5335 | ): |
||
| 5336 | """Modifies an existing target. |
||
| 5337 | |||
| 5338 | Arguments: |
||
| 5339 | target_id (uuid) ID of target to modify. |
||
| 5340 | comment (str, optional): Comment on target. |
||
| 5341 | name (str, optional): Name of target. |
||
| 5342 | hosts (list, optional): List of target hosts. |
||
| 5343 | exclude_hosts (list, optional): A list of hosts to exclude. |
||
| 5344 | ssh_credential (str, optional): UUID of SSH credential to |
||
| 5345 | use on target. |
||
| 5346 | ssh_credential_port (int, optional): The port to use for ssh |
||
| 5347 | credential |
||
| 5348 | smb_credential (str, optional): UUID of SMB credential to use |
||
| 5349 | on target. |
||
| 5350 | esxi_credential (str, optional): UUID of ESXi credential to use |
||
| 5351 | on target. |
||
| 5352 | snmp_credential (str, optional): UUID of SNMP credential to use |
||
| 5353 | on target. |
||
| 5354 | port_list (str, optional): UUID of port list describing ports to |
||
| 5355 | scan. |
||
| 5356 | alive_tests (str, optional): Which alive tests to use. |
||
| 5357 | reverse_lookup_only (boolean, optional): Whether to scan only hosts |
||
| 5358 | that have names. |
||
| 5359 | reverse_lookup_unify (boolean, optional): Whether to scan only one |
||
| 5360 | IP when multiple IPs have the same name. |
||
| 5361 | |||
| 5362 | Returns: |
||
| 5363 | The response. See :py:meth:`send_command` for details. |
||
| 5364 | """ |
||
| 5365 | if not target_id: |
||
| 5366 | raise RequiredArgument( |
||
| 5367 | "modify_target requires a " "target_id argument" |
||
| 5368 | ) |
||
| 5369 | |||
| 5370 | cmd = XmlCommand("modify_target") |
||
| 5371 | cmd.set_attribute("target_id", target_id) |
||
| 5372 | |||
| 5373 | if comment: |
||
| 5374 | cmd.add_element("comment", comment) |
||
| 5375 | |||
| 5376 | if name: |
||
| 5377 | cmd.add_element("name", name) |
||
| 5378 | |||
| 5379 | if hosts: |
||
| 5380 | cmd.add_element("hosts", _to_comma_list(hosts)) |
||
| 5381 | |||
| 5382 | if exclude_hosts: |
||
| 5383 | cmd.add_element("exclude_hosts", _to_comma_list(exclude_hosts)) |
||
| 5384 | |||
| 5385 | if alive_tests: |
||
| 5386 | if not alive_tests in ALIVE_TESTS: |
||
| 5387 | raise InvalidArgument( |
||
| 5388 | "alive_tests must be one of {tests} but " |
||
| 5389 | "{actual} has been passed".format( |
||
| 5390 | tests="|".join(ALIVE_TESTS), actual=alive_tests |
||
| 5391 | ) |
||
| 5392 | ) |
||
| 5393 | cmd.add_element("alive_tests", alive_tests) |
||
| 5394 | |||
| 5395 | if ssh_credential_id: |
||
| 5396 | _xmlssh = cmd.add_element( |
||
| 5397 | "ssh_credential", attrs={"id": ssh_credential_id} |
||
| 5398 | ) |
||
| 5399 | |||
| 5400 | if ssh_credential_port: |
||
| 5401 | _xmlssh.add_element("port", str(ssh_credential_port)) |
||
| 5402 | |||
| 5403 | if smb_credential_id: |
||
| 5404 | cmd.add_element("smb_credential", attrs={"id": smb_credential_id}) |
||
| 5405 | |||
| 5406 | if esxi_credential_id: |
||
| 5407 | cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id}) |
||
| 5408 | |||
| 5409 | if snmp_credential_id: |
||
| 5410 | cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id}) |
||
| 5411 | |||
| 5412 | if not reverse_lookup_only is None: |
||
| 5413 | cmd.add_element( |
||
| 5414 | "reverse_lookup_only", _to_bool(reverse_lookup_only) |
||
| 5415 | ) |
||
| 5416 | |||
| 5417 | if not reverse_lookup_unify is None: |
||
| 5418 | cmd.add_element( |
||
| 5419 | "reverse_lookup_unify", _to_bool(reverse_lookup_unify) |
||
| 5420 | ) |
||
| 5421 | |||
| 5422 | if port_list_id: |
||
| 5423 | cmd.add_element("port_list", attrs={"id": port_list_id}) |
||
| 5424 | |||
| 5425 | return self._send_xml_command(cmd) |
||
| 5426 | |||
| 5427 | def modify_task( |
||
| 5428 | self, |
||
| 5429 | task_id, |
||
| 5430 | *, |
||
| 5431 | name=None, |
||
| 5432 | config_id=None, |
||
| 5433 | target_id=None, |
||
| 5434 | scanner_id=None, |
||
| 5435 | alterable=None, |
||
| 5436 | hosts_ordering=None, |
||
| 5437 | schedule_id=None, |
||
| 5438 | schedule_periods=None, |
||
| 5439 | comment=None, |
||
| 5440 | alert_ids=None, |
||
| 5441 | observers=None, |
||
| 5442 | preferences=None |
||
| 5443 | ): |
||
| 5444 | """Modifies an existing task. |
||
| 5445 | |||
| 5446 | Arguments: |
||
| 5447 | task_id (str) UUID of task to modify. |
||
| 5448 | name (str, optional): The name of the task. |
||
| 5449 | config_id (str, optional): UUID of scan config to use by the task |
||
| 5450 | target_id (str, optional): UUID of target to be scanned |
||
| 5451 | scanner_id (str, optional): UUID of scanner to use for scanning the |
||
| 5452 | target |
||
| 5453 | comment (str, optional):The comment on the task. |
||
| 5454 | alert_ids (list, optional): List of UUIDs for alerts to be applied |
||
| 5455 | to the task |
||
| 5456 | hosts_ordering (str, optional): The order hosts are scanned in |
||
| 5457 | schedule_id (str, optional): UUID of a schedule when the task should |
||
| 5458 | be run. |
||
| 5459 | schedule_periods (int, optional): A limit to the number of times |
||
| 5460 | the task will be scheduled, or 0 for no limit. |
||
| 5461 | observers (list, optional): List of names or ids of users which |
||
| 5462 | should be allowed to observe this task |
||
| 5463 | preferences (dict, optional): Name/Value pairs of scanner |
||
| 5464 | preferences. |
||
| 5465 | |||
| 5466 | Returns: |
||
| 5467 | The response. See :py:meth:`send_command` for details. |
||
| 5468 | """ |
||
| 5469 | if not task_id: |
||
| 5470 | raise RequiredArgument("modify_task requires a task_id argument") |
||
| 5471 | |||
| 5472 | cmd = XmlCommand("modify_task") |
||
| 5473 | cmd.set_attribute("task_id", task_id) |
||
| 5474 | |||
| 5475 | if name: |
||
| 5476 | cmd.add_element("name", name) |
||
| 5477 | |||
| 5478 | if comment: |
||
| 5479 | cmd.add_element("comment", comment) |
||
| 5480 | |||
| 5481 | if config_id: |
||
| 5482 | cmd.add_element("config", attrs={"id": config_id}) |
||
| 5483 | |||
| 5484 | if target_id: |
||
| 5485 | cmd.add_element("target", attrs={"id": target_id}) |
||
| 5486 | |||
| 5487 | if not alterable is None: |
||
| 5488 | cmd.add_element("alterable", _to_bool(alterable)) |
||
| 5489 | |||
| 5490 | if hosts_ordering: |
||
| 5491 | # not sure about the possible values for hosts_orderning |
||
| 5492 | # it seems gvmd does not check the param |
||
| 5493 | # gsa allows to select 'sequential', 'random' or 'reverse' |
||
| 5494 | cmd.add_element("hosts_ordering", hosts_ordering) |
||
| 5495 | |||
| 5496 | if scanner_id: |
||
| 5497 | cmd.add_element("scanner", attrs={"id": scanner_id}) |
||
| 5498 | |||
| 5499 | if schedule_id: |
||
| 5500 | cmd.add_element("schedule", attrs={"id": schedule_id}) |
||
| 5501 | |||
| 5502 | View Code Duplication | if schedule_periods is not None: |
|
| 5503 | if ( |
||
| 5504 | not isinstance(schedule_periods, numbers.Integral) |
||
| 5505 | or schedule_periods < 0 |
||
| 5506 | ): |
||
| 5507 | raise InvalidArgument( |
||
| 5508 | "schedule_periods must be an integer greater or equal " |
||
| 5509 | "than 0" |
||
| 5510 | ) |
||
| 5511 | cmd.add_element("schedule_periods", str(schedule_periods)) |
||
| 5512 | |||
| 5513 | if alert_ids is not None: |
||
| 5514 | if not _is_list_like(alert_ids): |
||
| 5515 | raise InvalidArgument("alert_ids argument must be a list") |
||
| 5516 | |||
| 5517 | for alert in alert_ids: |
||
| 5518 | cmd.add_element("alert", attrs={"id": str(alert)}) |
||
| 5519 | |||
| 5520 | if observers is not None: |
||
| 5521 | if not _is_list_like(observers): |
||
| 5522 | raise InvalidArgument("obeservers argument must be a list") |
||
| 5523 | |||
| 5524 | cmd.add_element("observers", _to_comma_list(observers)) |
||
| 5525 | |||
| 5526 | if preferences is not None: |
||
| 5527 | if not isinstance(preferences, collections.abc.Mapping): |
||
| 5528 | raise InvalidArgument('preferences argument must be a dict') |
||
| 5529 | |||
| 5530 | _xmlprefs = cmd.add_element("preferences") |
||
| 5531 | for pref_name, pref_value in preferences.items(): |
||
| 5532 | _xmlpref = _xmlprefs.add_element("preference") |
||
| 5533 | _xmlpref.add_element("scanner_name", pref_name) |
||
| 5534 | _xmlpref.add_element("value", str(pref_value)) |
||
| 5535 | |||
| 5536 | return self._send_xml_command(cmd) |
||
| 5537 | |||
| 5538 | def modify_user( |
||
| 5539 | self, |
||
| 5540 | user_id=None, |
||
| 5541 | name=None, |
||
| 5542 | *, |
||
| 5543 | new_name=None, |
||
| 5544 | password=None, |
||
| 5545 | role_ids=None, |
||
| 5546 | hosts=None, |
||
| 5547 | hosts_allow=False, |
||
| 5548 | ifaces=None, |
||
| 5549 | ifaces_allow=False |
||
| 5550 | ): |
||
| 5551 | """Modifies an existing user. |
||
| 5552 | |||
| 5553 | Arguments: |
||
| 5554 | user_id (str, optional): UUID of the user to be modified. Overrides |
||
| 5555 | name element argument. |
||
| 5556 | name (str, optional): The name of the user to be modified. Either |
||
| 5557 | user_id or name must be passed. |
||
| 5558 | new_name (str, optional): The new name for the user. |
||
| 5559 | password (str, optional): The password for the user. |
||
| 5560 | roles_id (list, optional): List of roles UUIDs for the user. |
||
| 5561 | hosts (list, optional): User access rules: List of hosts. |
||
| 5562 | hosts_allow (boolean,optional): If True, allow only listed, |
||
| 5563 | otherwise forbid listed. |
||
| 5564 | ifaces (list, optional): User access rules: List |
||
| 5565 | of ifaces. |
||
| 5566 | ifaces_allow (boolean, optional): If True, allow only listed, |
||
| 5567 | otherwise forbid listed. |
||
| 5568 | |||
| 5569 | Returns: |
||
| 5570 | The response. See :py:meth:`send_command` for details. |
||
| 5571 | """ |
||
| 5572 | if not user_id and not name: |
||
| 5573 | raise RequiredArgument( |
||
| 5574 | "modify_user requires an user_id or name argument" |
||
| 5575 | ) |
||
| 5576 | |||
| 5577 | cmd = XmlCommand("modify_user") |
||
| 5578 | |||
| 5579 | if user_id: |
||
| 5580 | cmd.set_attribute("user_id", user_id) |
||
| 5581 | else: |
||
| 5582 | cmd.add_element("name", name) |
||
| 5583 | |||
| 5584 | if new_name: |
||
| 5585 | cmd.add_element("new_name", new_name) |
||
| 5586 | |||
| 5587 | if password: |
||
| 5588 | cmd.add_element("password", password) |
||
| 5589 | |||
| 5590 | if role_ids: |
||
| 5591 | for role in role_ids: |
||
| 5592 | cmd.add_element("role", attrs={"id": role}) |
||
| 5593 | |||
| 5594 | if hosts: |
||
| 5595 | cmd.add_element( |
||
| 5596 | "hosts", |
||
| 5597 | _to_comma_list(hosts), |
||
| 5598 | attrs={"allow": _to_bool(hosts_allow)}, |
||
| 5599 | ) |
||
| 5600 | |||
| 5601 | if ifaces: |
||
| 5602 | cmd.add_element( |
||
| 5603 | "ifaces", |
||
| 5604 | _to_comma_list(ifaces), |
||
| 5605 | attrs={"allow": _to_bool(ifaces_allow)}, |
||
| 5606 | ) |
||
| 5607 | |||
| 5608 | return self._send_xml_command(cmd) |
||
| 5609 | |||
| 5610 | def move_task(self, task_id, *, slave_id=None): |
||
| 5611 | """Move an existing task to another GMP slave scanner or the master |
||
| 5612 | |||
| 5613 | Arguments: |
||
| 5614 | task_id (str): UUID of the task to be moved |
||
| 5615 | slave_id (str, optional): UUID of slave to reassign the task to, |
||
| 5616 | empty for master. |
||
| 5617 | |||
| 5618 | Returns: |
||
| 5619 | The response. See :py:meth:`send_command` for details. |
||
| 5620 | """ |
||
| 5621 | if not task_id: |
||
| 5622 | raise InvalidArgument("move_task requires an task_id argument") |
||
| 5623 | |||
| 5624 | cmd = XmlCommand("move_task") |
||
| 5625 | cmd.set_attribute("task_id", task_id) |
||
| 5626 | |||
| 5627 | if not slave_id is None: |
||
| 5628 | cmd.set_attribute("slave_id", slave_id) |
||
| 5629 | |||
| 5630 | return self._send_xml_command(cmd) |
||
| 5631 | |||
| 5632 | def restore(self, entity_id): |
||
| 5633 | """Restore an entity from the trashcan |
||
| 5634 | |||
| 5635 | Arguments: |
||
| 5636 | entity_id (str): ID of the entity to be restored from the trashcan |
||
| 5637 | |||
| 5638 | Returns: |
||
| 5639 | The response. See :py:meth:`send_command` for details. |
||
| 5640 | """ |
||
| 5641 | if not entity_id: |
||
| 5642 | raise InvalidArgument("restore requires an entity_id argument") |
||
| 5643 | |||
| 5644 | cmd = XmlCommand("restore") |
||
| 5645 | cmd.set_attribute("id", entity_id) |
||
| 5646 | |||
| 5647 | return self._send_xml_command(cmd) |
||
| 5648 | |||
| 5649 | def resume_task(self, task_id): |
||
| 5650 | """Resume an existing stopped task |
||
| 5651 | |||
| 5652 | Arguments: |
||
| 5653 | task_id (str): UUID of the task to be resumed |
||
| 5654 | |||
| 5655 | Returns: |
||
| 5656 | The response. See :py:meth:`send_command` for details. |
||
| 5657 | """ |
||
| 5658 | if not task_id: |
||
| 5659 | raise InvalidArgument("resume_task requires an task_id argument") |
||
| 5660 | |||
| 5661 | cmd = XmlCommand("resume_task") |
||
| 5662 | cmd.set_attribute("task_id", task_id) |
||
| 5663 | |||
| 5664 | return self._send_xml_command(cmd) |
||
| 5665 | |||
| 5666 | def start_task(self, task_id): |
||
| 5667 | """Start an existing task |
||
| 5668 | |||
| 5669 | Arguments: |
||
| 5670 | task_id (str): UUID of the task to be started |
||
| 5671 | |||
| 5672 | Returns: |
||
| 5673 | The response. See :py:meth:`send_command` for details. |
||
| 5674 | """ |
||
| 5675 | if not task_id: |
||
| 5676 | raise InvalidArgument("start_task requires an task_id argument") |
||
| 5677 | |||
| 5678 | cmd = XmlCommand("start_task") |
||
| 5679 | cmd.set_attribute("task_id", task_id) |
||
| 5680 | |||
| 5681 | return self._send_xml_command(cmd) |
||
| 5682 | |||
| 5683 | def stop_task(self, task_id): |
||
| 5684 | """Stop an existing running task |
||
| 5685 | |||
| 5686 | Arguments: |
||
| 5687 | task_id (str): UUID of the task to be stopped |
||
| 5688 | |||
| 5689 | Returns: |
||
| 5690 | The response. See :py:meth:`send_command` for details. |
||
| 5691 | """ |
||
| 5692 | if not task_id: |
||
| 5693 | raise InvalidArgument("stop_task requires an task_id argument") |
||
| 5694 | |||
| 5695 | cmd = XmlCommand("stop_task") |
||
| 5696 | cmd.set_attribute("task_id", task_id) |
||
| 5697 | |||
| 5698 | return self._send_xml_command(cmd) |
||
| 5699 | |||
| 5700 | def sync_cert(self): |
||
| 5701 | """Request a synchronization with the CERT feed service |
||
| 5702 | |||
| 5703 | Returns: |
||
| 5704 | The response. See :py:meth:`send_command` for details. |
||
| 5705 | """ |
||
| 5706 | return self._send_xml_command(XmlCommand("sync_cert")) |
||
| 5707 | |||
| 5708 | def sync_config(self): |
||
| 5709 | """Request an OSP config synchronization with scanner |
||
| 5710 | |||
| 5711 | Returns: |
||
| 5712 | The response. See :py:meth:`send_command` for details. |
||
| 5713 | """ |
||
| 5714 | return self._send_xml_command(XmlCommand("sync_config")) |
||
| 5715 | |||
| 5716 | def sync_feed(self): |
||
| 5717 | """Request a synchronization with the NVT feed service |
||
| 5718 | |||
| 5719 | Returns: |
||
| 5720 | The response. See :py:meth:`send_command` for details. |
||
| 5721 | """ |
||
| 5722 | return self._send_xml_command(XmlCommand("sync_feed")) |
||
| 5723 | |||
| 5724 | def sync_scap(self): |
||
| 5725 | """Request a synchronization with the SCAP feed service |
||
| 5726 | |||
| 5727 | Returns: |
||
| 5728 | The response. See :py:meth:`send_command` for details. |
||
| 5729 | """ |
||
| 5730 | return self._send_xml_command(XmlCommand("sync_scap")) |
||
| 5731 | |||
| 5732 | def test_alert(self, alert_id): |
||
| 5733 | """Run an alert |
||
| 5734 | |||
| 5735 | Invoke a test run of an alert |
||
| 5736 | |||
| 5737 | Arguments: |
||
| 5738 | alert_id (str): UUID of the alert to be tested |
||
| 5739 | |||
| 5740 | Returns: |
||
| 5741 | The response. See :py:meth:`send_command` for details. |
||
| 5742 | """ |
||
| 5743 | if not alert_id: |
||
| 5744 | raise InvalidArgument("test_alert requires an alert_id argument") |
||
| 5745 | |||
| 5746 | cmd = XmlCommand("test_alert") |
||
| 5747 | cmd.set_attribute("alert_id", alert_id) |
||
| 5748 | |||
| 5749 | return self._send_xml_command(cmd) |
||
| 5750 | |||
| 5751 | def trigger_alert( |
||
| 5752 | self, |
||
| 5753 | alert_id, |
||
| 5754 | report_id, |
||
| 5755 | *, |
||
| 5756 | filter=None, |
||
| 5757 | filter_id=None, |
||
| 5758 | report_format_id=None, |
||
| 5759 | delta_report_id=None |
||
| 5760 | ): |
||
| 5761 | """Run an alert by ignoring its event and conditions |
||
| 5762 | |||
| 5763 | The alert is triggered to run immediately with the provided filtered |
||
| 5764 | report by ignoring the even and condition settings. |
||
| 5765 | |||
| 5766 | Arguments: |
||
| 5767 | alert_id (str): UUID of the alert to be run |
||
| 5768 | report_id (str): UUID of the report to be provided to the alert |
||
| 5769 | filter (str, optional): Filter term to use to filter results in the |
||
| 5770 | report |
||
| 5771 | filter_id (str, optional): UUID of filter to use to filter results |
||
| 5772 | in the report |
||
| 5773 | report_format_id (str, optional): UUID of report format to use |
||
| 5774 | delta_report_id (str, optional): UUID of an existing report to |
||
| 5775 | compare report to. |
||
| 5776 | |||
| 5777 | Returns: |
||
| 5778 | The response. See :py:meth:`send_command` for details. |
||
| 5779 | """ |
||
| 5780 | if not alert_id: |
||
| 5781 | raise RequiredArgument("run_alert requires a alert_id argument") |
||
| 5782 | |||
| 5783 | if not report_id: |
||
| 5784 | raise RequiredArgument("run_alert requires a report_id argument") |
||
| 5785 | |||
| 5786 | cmd = XmlCommand("get_reports") |
||
| 5787 | cmd.set_attribute("report_id", report_id) |
||
| 5788 | cmd.set_attribute("alert_id", alert_id) |
||
| 5789 | |||
| 5790 | if filter: |
||
| 5791 | cmd.set_attribute("filter", filter) |
||
| 5792 | |||
| 5793 | if filter_id: |
||
| 5794 | cmd.set_attribute("filt_id", filter_id) |
||
| 5795 | |||
| 5796 | if report_format_id: |
||
| 5797 | cmd.set_attribute("format_id", report_format_id) |
||
| 5798 | |||
| 5799 | if delta_report_id: |
||
| 5800 | cmd.set_attribute("delta_report_id", delta_report_id) |
||
| 5801 | |||
| 5802 | return self._send_xml_command(cmd) |
||
| 5803 | |||
| 5804 | def verify_agent(self, agent_id): |
||
| 5805 | """Verify an existing agent |
||
| 5806 | |||
| 5807 | Verifies the trust level of an existing agent. It will be checked |
||
| 5808 | whether signature of the agent currently matches the agent. This |
||
| 5809 | includes the agent installer file. It is *not* verified if the agent |
||
| 5810 | works as expected by the user. |
||
| 5811 | |||
| 5812 | Arguments: |
||
| 5813 | agent_id (str): UUID of the agent to be verified |
||
| 5814 | |||
| 5815 | Returns: |
||
| 5816 | The response. See :py:meth:`send_command` for details. |
||
| 5817 | """ |
||
| 5818 | if not agent_id: |
||
| 5819 | raise InvalidArgument("verify_agent requires an agent_id argument") |
||
| 5820 | |||
| 5821 | cmd = XmlCommand("verify_agent") |
||
| 5822 | cmd.set_attribute("agent_id", agent_id) |
||
| 5823 | |||
| 5824 | return self._send_xml_command(cmd) |
||
| 5825 | |||
| 5826 | def verify_report_format(self, report_format_id): |
||
| 5827 | """Verify an existing report format |
||
| 5828 | |||
| 5829 | Verifies the trust level of an existing report format. It will be |
||
| 5830 | checked whether the signature of the report format currently matches the |
||
| 5831 | report format. This includes the script and files used to generate |
||
| 5832 | reports of this format. It is *not* verified if the report format works |
||
| 5833 | as expected by the user. |
||
| 5834 | |||
| 5835 | Arguments: |
||
| 5836 | report_format_id (str): UUID of the report format to be verified |
||
| 5837 | |||
| 5838 | Returns: |
||
| 5839 | The response. See :py:meth:`send_command` for details. |
||
| 5840 | """ |
||
| 5841 | if not report_format_id: |
||
| 5842 | raise InvalidArgument( |
||
| 5843 | "verify_report_format requires a report_format_id argument" |
||
| 5844 | ) |
||
| 5845 | |||
| 5846 | cmd = XmlCommand("verify_report_format") |
||
| 5847 | cmd.set_attribute("report_format_id", report_format_id) |
||
| 5848 | |||
| 5849 | return self._send_xml_command(cmd) |
||
| 5850 | |||
| 5851 | def verify_scanner(self, scanner_id): |
||
| 5852 | """Verify an existing scanner |
||
| 5853 | |||
| 5854 | Verifies if it is possible to connect to an existing scanner. It is |
||
| 5855 | *not* verified if the scanner works as expected by the user. |
||
| 5856 | |||
| 5857 | Arguments: |
||
| 5858 | scanner_id (str): UUID of the scanner to be verified |
||
| 5859 | |||
| 5860 | Returns: |
||
| 5861 | The response. See :py:meth:`send_command` for details. |
||
| 5862 | """ |
||
| 5863 | if not scanner_id: |
||
| 5864 | raise InvalidArgument( |
||
| 5865 | "verify_scanner requires a scanner_id argument" |
||
| 5866 | ) |
||
| 5867 | |||
| 5868 | cmd = XmlCommand("verify_scanner") |
||
| 5869 | cmd.set_attribute("scanner_id", scanner_id) |
||
| 5870 | |||
| 5871 | return self._send_xml_command(cmd) |
||
| 5872 |