| Total Complexity | 549 |
| Total Lines | 3414 |
| Duplicated Lines | 9.37 % |
| 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 | import logging |
||
| 24 | |||
| 25 | from lxml import etree |
||
| 26 | |||
| 27 | from gvm.errors import InvalidArgument, RequiredArgument |
||
| 28 | from gvm.utils import get_version_string |
||
| 29 | from gvm.xml import _GmpCommandFactory as GmpCommandFactory, XmlCommand |
||
| 30 | |||
| 31 | from .base import GvmProtocol |
||
| 32 | |||
| 33 | logger = logging.getLogger(__name__) |
||
| 34 | |||
| 35 | PROTOCOL_VERSION = (7,) |
||
| 36 | |||
| 37 | FILTER_TYPES = [ |
||
| 38 | 'agent', |
||
| 39 | 'alert', |
||
| 40 | 'asset', |
||
| 41 | 'config', |
||
| 42 | 'credential', |
||
| 43 | 'filter', |
||
| 44 | 'group', |
||
| 45 | 'note', |
||
| 46 | 'override', |
||
| 47 | 'permission', |
||
| 48 | 'port_list', |
||
| 49 | 'report', |
||
| 50 | 'report_format', |
||
| 51 | 'result', |
||
| 52 | 'role', |
||
| 53 | 'schedule', |
||
| 54 | 'secinfo', |
||
| 55 | 'tag', |
||
| 56 | 'target', |
||
| 57 | 'task', |
||
| 58 | 'user', |
||
| 59 | ] |
||
| 60 | |||
| 61 | TIME_UNITS = [ |
||
| 62 | 'second', |
||
| 63 | 'minute', |
||
| 64 | 'hour', |
||
| 65 | 'day', |
||
| 66 | 'week', |
||
| 67 | 'month', |
||
| 68 | 'year', |
||
| 69 | 'decade', |
||
| 70 | ] |
||
| 71 | |||
| 72 | ALIVE_TESTS = [ |
||
| 73 | 'ICMP, TCP Service & ARP Ping', |
||
| 74 | 'TCP Service & ARP Ping', |
||
| 75 | 'ICMP & ARP Ping', |
||
| 76 | 'ICMP & TCP Service Ping', |
||
| 77 | 'ARP Ping', |
||
| 78 | 'TCP Service Ping', |
||
| 79 | 'ICMP Ping', |
||
| 80 | 'Scan Config Default', |
||
| 81 | ] |
||
| 82 | |||
| 83 | def _check_command_status(xml): |
||
| 84 | """Check gmp response |
||
| 85 | |||
| 86 | Look into the gmp response and check for the status in the root element |
||
| 87 | |||
| 88 | Arguments: |
||
| 89 | xml {string} -- XML-Source |
||
| 90 | |||
| 91 | Returns: |
||
| 92 | bool -- True if valid, otherwise False |
||
| 93 | """ |
||
| 94 | |||
| 95 | if xml is 0 or xml is None: |
||
| 96 | logger.error('XML Command is empty') |
||
| 97 | return False |
||
| 98 | |||
| 99 | try: |
||
| 100 | parser = etree.XMLParser(encoding='utf-8', recover=True) |
||
| 101 | |||
| 102 | root = etree.XML(xml, parser=parser) |
||
| 103 | status = root.attrib['status'] |
||
| 104 | return status is not None and status[0] == '2' |
||
| 105 | |||
| 106 | except etree.Error as e: |
||
| 107 | logger.error('etree.XML(xml): %s', e) |
||
| 108 | return False |
||
| 109 | |||
| 110 | |||
| 111 | def _to_bool(value): |
||
| 112 | return '1' if value else '0' |
||
| 113 | |||
| 114 | |||
| 115 | def _add_filter(cmd, filter, filter_id): |
||
| 116 | if filter: |
||
| 117 | cmd.set_attribute('filter', filter) |
||
| 118 | |||
| 119 | if filter_id: |
||
| 120 | cmd.set_attribute('filt_id', filter_id) |
||
| 121 | |||
| 122 | |||
| 123 | class Gmp(GvmProtocol): |
||
|
|
|||
| 124 | """Python interface for Greenbone Management Protocol |
||
| 125 | |||
| 126 | This class implements the `Greenbone Management Protocol version 7`_ |
||
| 127 | |||
| 128 | Attributes: |
||
| 129 | connection (:class:`gvm.connections.GvmConnection`): Connection to use |
||
| 130 | to talk with the gvmd daemon. See :mod:`gvm.connections` for |
||
| 131 | possible connection types. |
||
| 132 | transform (`callable`_, optional): Optional transform callable to |
||
| 133 | convert response data. After each request the callable gets passed |
||
| 134 | the plain response data which can be used to check the data and/or |
||
| 135 | conversion into different representaitions like a xml dom. |
||
| 136 | |||
| 137 | See :mod:`gvm.transforms` for existing transforms. |
||
| 138 | |||
| 139 | .. _Greenbone Management Protocol version 7: |
||
| 140 | https://docs.greenbone.net/API/GMP/gmp-7.0.html |
||
| 141 | .. _callable: |
||
| 142 | https://docs.python.org/3.6/library/functions.html#callable |
||
| 143 | """ |
||
| 144 | |||
| 145 | def __init__(self, connection, transform=None): |
||
| 146 | super().__init__(connection, transform) |
||
| 147 | |||
| 148 | # Is authenticated on gvmd |
||
| 149 | self._authenticated = False |
||
| 150 | |||
| 151 | # GMP Message Creator |
||
| 152 | self._generator = GmpCommandFactory() |
||
| 153 | |||
| 154 | @staticmethod |
||
| 155 | def get_protocol_version(): |
||
| 156 | """Allow to determine the Greenbone Management Protocol version. |
||
| 157 | |||
| 158 | Returns: |
||
| 159 | str: Implemented version of the Greenbone Management Protocol |
||
| 160 | """ |
||
| 161 | return get_version_string(PROTOCOL_VERSION) |
||
| 162 | |||
| 163 | def is_authenticated(self): |
||
| 164 | """Checks if the user is authenticated |
||
| 165 | |||
| 166 | If the user is authenticated privilged GMP commands like get_tasks |
||
| 167 | may be send to gvmd. |
||
| 168 | |||
| 169 | Returns: |
||
| 170 | bool: True if an authenticated connection to gvmd has been |
||
| 171 | established. |
||
| 172 | """ |
||
| 173 | return self._authenticated |
||
| 174 | |||
| 175 | def authenticate(self, username, password): |
||
| 176 | """Authenticate to gvmd. |
||
| 177 | |||
| 178 | The generated authenticate command will be send to server. |
||
| 179 | Afterwards the response is read, transformed and returned. |
||
| 180 | |||
| 181 | Arguments: |
||
| 182 | username (str): Username |
||
| 183 | password (str): Password |
||
| 184 | |||
| 185 | Returns: |
||
| 186 | any, str by default: Transformed response from server. |
||
| 187 | """ |
||
| 188 | cmd = XmlCommand('authenticate') |
||
| 189 | |||
| 190 | if not username: |
||
| 191 | raise RequiredArgument('authenticate requires username') |
||
| 192 | |||
| 193 | if not password: |
||
| 194 | raise RequiredArgument('authenticate requires password') |
||
| 195 | |||
| 196 | credentials = cmd.add_element('credentials') |
||
| 197 | credentials.add_element('username', username) |
||
| 198 | credentials.add_element('password', password) |
||
| 199 | |||
| 200 | self._send(cmd.to_string()) |
||
| 201 | response = self._read() |
||
| 202 | |||
| 203 | if _check_command_status(response): |
||
| 204 | self._authenticated = True |
||
| 205 | |||
| 206 | return self._transform(response) |
||
| 207 | |||
| 208 | def create_agent(self, installer, signature, name, comment=None, |
||
| 209 | howto_install=None, howto_use=None): |
||
| 210 | """Create a new agent |
||
| 211 | |||
| 212 | Arguments: |
||
| 213 | installer (str): A base64 encoded file that installs the agent on a |
||
| 214 | target machine |
||
| 215 | signature: (str): A detached OpenPGP signature of the installer |
||
| 216 | name (str): A name for the agent |
||
| 217 | comment (str, optional): A comment for the agent |
||
| 218 | howto_install (str, optional): A file that describes how to install |
||
| 219 | the agent |
||
| 220 | howto_use (str, optional): A file that describes how to use the |
||
| 221 | agent |
||
| 222 | |||
| 223 | Returns: |
||
| 224 | The response. See :py:meth:`send_command` for details. |
||
| 225 | """ |
||
| 226 | if not name: |
||
| 227 | raise RequiredArgument('create_agent requires name argument') |
||
| 228 | |||
| 229 | if not installer: |
||
| 230 | raise RequiredArgument('create_agent requires installer argument') |
||
| 231 | |||
| 232 | if not signature: |
||
| 233 | raise RequiredArgument('create_agent requires signature argument') |
||
| 234 | |||
| 235 | cmd = XmlCommand('create_agent') |
||
| 236 | cmd.add_element('installer', installer) |
||
| 237 | cmd.add_element('signature', signature) |
||
| 238 | cmd.add_element('name', name) |
||
| 239 | |||
| 240 | if comment: |
||
| 241 | cmd.add_element('comment', comment) |
||
| 242 | |||
| 243 | if howto_install: |
||
| 244 | cmd.add_element('howto_install', howto_install) |
||
| 245 | |||
| 246 | if howto_use: |
||
| 247 | cmd.add_element('howto_use', howto_use) |
||
| 248 | |||
| 249 | return self._send_xml_command(cmd) |
||
| 250 | |||
| 251 | def clone_agent(self, agent_id): |
||
| 252 | """Clone an existing agent |
||
| 253 | |||
| 254 | Arguments: |
||
| 255 | copy (str): UUID of an existing agent to clone from |
||
| 256 | |||
| 257 | Returns: |
||
| 258 | The response. See :py:meth:`send_command` for details. |
||
| 259 | """ |
||
| 260 | cmd = XmlCommand('create_agent') |
||
| 261 | cmd.add_element('copy', agent_id) |
||
| 262 | return self._send_xml_command(cmd) |
||
| 263 | |||
| 264 | def create_alert(self, name, condition, event, method, method_data=None, |
||
| 265 | event_data=None, condition_data=None, filter_id=None, |
||
| 266 | comment=None): |
||
| 267 | """Create a new alert |
||
| 268 | |||
| 269 | Arguments: |
||
| 270 | name (str): Name of the new Alert |
||
| 271 | condition (str): The condition that must be satisfied for the alert |
||
| 272 | to occur. |
||
| 273 | event (str): The event that must happen for the alert to occur |
||
| 274 | method (str): The method by which the user is alerted |
||
| 275 | condition_data (dict, optional): Data that defines the condition |
||
| 276 | event_data (dict, optional): Data that defines the event |
||
| 277 | method_data (dict, optional): Data that defines the method |
||
| 278 | filter_id (str, optional): Filter to apply when executing alert |
||
| 279 | comment (str, optional): Comment for the alert |
||
| 280 | |||
| 281 | Returns: |
||
| 282 | The response. See :py:meth:`send_command` for details. |
||
| 283 | """ |
||
| 284 | if not name: |
||
| 285 | raise RequiredArgument('create_alert requires name argument') |
||
| 286 | |||
| 287 | if condition is None or len(condition) == 0: |
||
| 288 | raise RequiredArgument('create_alert requires condition argument') |
||
| 289 | |||
| 290 | if event is None or len(event) == 0: |
||
| 291 | raise RequiredArgument('create_alert requires event argument') |
||
| 292 | |||
| 293 | cmd = XmlCommand('create_alert') |
||
| 294 | cmd.add_element('name', name) |
||
| 295 | |||
| 296 | conditions = cmd.add_element('condition', condition) |
||
| 297 | |||
| 298 | if not condition_data is None: |
||
| 299 | for value, key in condition_data.items(): |
||
| 300 | _data = conditions.add_element('data', value) |
||
| 301 | _data.add_element('name', key) |
||
| 302 | |||
| 303 | events = cmd.add_element('event', event) |
||
| 304 | |||
| 305 | if not event_data is None: |
||
| 306 | for value, key in event_data.items(): |
||
| 307 | _data = events.add_element('data', value) |
||
| 308 | _data.add_element('name', key) |
||
| 309 | |||
| 310 | methods = cmd.add_element('method', method) |
||
| 311 | |||
| 312 | if not method_data is None: |
||
| 313 | for value, key in method_data.items(): |
||
| 314 | _data = methods.add_element('data', value) |
||
| 315 | _data.add_element('name', key) |
||
| 316 | |||
| 317 | if filter_id: |
||
| 318 | cmd.add_element('filter', attrs={'id': filter_id}) |
||
| 319 | |||
| 320 | if comment: |
||
| 321 | cmd.add_element('comment', comment) |
||
| 322 | |||
| 323 | return self._send_xml_command(cmd) |
||
| 324 | |||
| 325 | def clone_alert(self, alert_id): |
||
| 326 | """Clone an existing alert |
||
| 327 | |||
| 328 | Arguments: |
||
| 329 | copy (str): UUID of an existing alert to clone from |
||
| 330 | |||
| 331 | Returns: |
||
| 332 | The response. See :py:meth:`send_command` for details. |
||
| 333 | """ |
||
| 334 | cmd = XmlCommand('create_alert') |
||
| 335 | cmd.add_element('copy', alert_id) |
||
| 336 | return self._send_xml_command(cmd) |
||
| 337 | |||
| 338 | def create_asset(self, name, asset_type, comment=None): |
||
| 339 | """Create a new asset |
||
| 340 | |||
| 341 | Arguments: |
||
| 342 | name (str): Name for the new asset |
||
| 343 | asset_type (str): Either 'os' or 'host' |
||
| 344 | comment (str, optional): Comment for the new asset |
||
| 345 | |||
| 346 | Returns: |
||
| 347 | The response. See :py:meth:`send_command` for details. |
||
| 348 | """ |
||
| 349 | if asset_type not in ('host', 'os'): |
||
| 350 | raise InvalidArgument( |
||
| 351 | 'create_asset requires asset_type to be either host or os') |
||
| 352 | |||
| 353 | if not name: |
||
| 354 | raise RequiredArgument('create_asset requires name argument') |
||
| 355 | |||
| 356 | cmd = XmlCommand('create_asset') |
||
| 357 | asset = cmd.add_element('asset') |
||
| 358 | asset.add_element('type', asset_type) |
||
| 359 | asset.add_element('name', name) |
||
| 360 | |||
| 361 | if comment: |
||
| 362 | asset.add_element('comment', comment) |
||
| 363 | |||
| 364 | return self._send_xml_command(cmd) |
||
| 365 | |||
| 366 | def create_config(self, name, copy): |
||
| 367 | """Create a new scan config from an existing one |
||
| 368 | |||
| 369 | Arguments: |
||
| 370 | name (str): Name of the new scan config |
||
| 371 | copy (str): UUID of the existing scan config |
||
| 372 | |||
| 373 | Returns: |
||
| 374 | The response. See :py:meth:`send_command` for details. |
||
| 375 | """ |
||
| 376 | if not name: |
||
| 377 | raise RequiredArgument('create_config requires name argument') |
||
| 378 | |||
| 379 | if not copy: |
||
| 380 | raise RequiredArgument('create_config requires copy argument') |
||
| 381 | |||
| 382 | cmd = XmlCommand('create_config') |
||
| 383 | cmd.add_element('copy', copy) |
||
| 384 | cmd.add_element('name', name) |
||
| 385 | return self._send_xml_command(cmd) |
||
| 386 | |||
| 387 | def create_credential(self, name, comment=None, allow_insecure=False, |
||
| 388 | certificate=None, key_phrase=None, private_key=None, |
||
| 389 | login=None, password=None, auth_algorithm=None, |
||
| 390 | community=None, privacy_algorithm=None, |
||
| 391 | privacy_password=None, credential_type=None): |
||
| 392 | """Create a new credential |
||
| 393 | |||
| 394 | Arguments: |
||
| 395 | name (str): Name of the new credential |
||
| 396 | comment (str, optional): Comment for the credential |
||
| 397 | allow_insecure (boolean, optional): Whether to allow insecure use of |
||
| 398 | the credential |
||
| 399 | certificate (str, optional): Certificate for the credential |
||
| 400 | key_phrase (str, optional): Key passphrase for the private key |
||
| 401 | private_key (str, optional): Private key to use for login |
||
| 402 | login (str, optional): Username for the credential |
||
| 403 | password (str, optional): Password for the credential |
||
| 404 | community (str, optional): The SNMP community |
||
| 405 | privacy_alogorithm (str, optional): The SNMP privacy algorithm, |
||
| 406 | either aes or des. |
||
| 407 | privacy_password (str, optional): The SNMP privacy password |
||
| 408 | credential_type (str, optional): The credential type. One of 'cc', |
||
| 409 | 'snmp', 'up', 'usk' |
||
| 410 | |||
| 411 | Returns: |
||
| 412 | The response. See :py:meth:`send_command` for details. |
||
| 413 | """ |
||
| 414 | if not name: |
||
| 415 | raise RequiredArgument('create_credential requires name argument') |
||
| 416 | |||
| 417 | cmd = XmlCommand('create_credential') |
||
| 418 | cmd.add_element('name', name) |
||
| 419 | |||
| 420 | if comment: |
||
| 421 | cmd.add_element('comment', comment) |
||
| 422 | |||
| 423 | if allow_insecure: |
||
| 424 | cmd.add_element('allow_insecure', '1') |
||
| 425 | |||
| 426 | if certificate: |
||
| 427 | cmd.add_element('certificate', certificate) |
||
| 428 | |||
| 429 | if not key_phrase is None and private_key: |
||
| 430 | _xmlkey = cmd.add_element('key') |
||
| 431 | _xmlkey.add_element('phrase', key_phrase) |
||
| 432 | _xmlkey.add_element('private', private_key) |
||
| 433 | |||
| 434 | if login: |
||
| 435 | cmd.add_element('login', login) |
||
| 436 | |||
| 437 | if password: |
||
| 438 | cmd.add_element('password', password) |
||
| 439 | |||
| 440 | if auth_algorithm: |
||
| 441 | if auth_algorithm not in ('md5', 'sha1'): |
||
| 442 | raise InvalidArgument( |
||
| 443 | 'create_credential requires auth_algorithm to be either ' |
||
| 444 | 'md5 or sha1') |
||
| 445 | cmd.add_element('auth_algorithm', auth_algorithm) |
||
| 446 | |||
| 447 | if community: |
||
| 448 | cmd.add_element('community', community) |
||
| 449 | |||
| 450 | if privacy_algorithm and privacy_password: |
||
| 451 | if privacy_algorithm not in ('aes', 'des'): |
||
| 452 | raise InvalidArgument( |
||
| 453 | 'create_credential requires algorithm to be either aes or ' |
||
| 454 | 'des') |
||
| 455 | _xmlprivacy = cmd.add_element('privacy') |
||
| 456 | _xmlprivacy.add_element('algorithm', privacy_algorithm) |
||
| 457 | _xmlprivacy.add_element('password', privacy_password) |
||
| 458 | |||
| 459 | if credential_type: |
||
| 460 | if credential_type not in ('cc', 'snmp', 'up', 'usk'): |
||
| 461 | raise InvalidArgument( |
||
| 462 | 'create_credential requires type to be either cc, snmp, up ' |
||
| 463 | ' or usk') |
||
| 464 | cmd.add_element('type', credential_type) |
||
| 465 | |||
| 466 | return self._send_xml_command(cmd) |
||
| 467 | |||
| 468 | def clone_credential(self, credential_id): |
||
| 469 | """Clone an existing credential |
||
| 470 | |||
| 471 | Arguments: |
||
| 472 | copy (str): UUID of an existing credential to clone from |
||
| 473 | |||
| 474 | Returns: |
||
| 475 | The response. See :py:meth:`send_command` for details. |
||
| 476 | """ |
||
| 477 | cmd = XmlCommand('create_credential') |
||
| 478 | cmd.add_element('copy', credential_id) |
||
| 479 | return self._send_xml_command(cmd) |
||
| 480 | |||
| 481 | def create_filter(self, name, make_unique=False, filter_type=None, |
||
| 482 | comment=None, term=None): |
||
| 483 | """Create a new filter |
||
| 484 | |||
| 485 | Arguments: |
||
| 486 | name (str): Name of the new filter |
||
| 487 | make_unique (boolean, optional): |
||
| 488 | filter_type (str, optional): Filter for entity type |
||
| 489 | comment (str, optional): Comment for the filter |
||
| 490 | term (str, optional): Filter term e.g. 'name=foo' |
||
| 491 | |||
| 492 | Returns: |
||
| 493 | The response. See :py:meth:`send_command` for details. |
||
| 494 | """ |
||
| 495 | if not name: |
||
| 496 | raise RequiredArgument('create_filter requires a name argument') |
||
| 497 | |||
| 498 | cmd = XmlCommand('create_filter') |
||
| 499 | _xmlname = cmd.add_element('name', name) |
||
| 500 | if make_unique: |
||
| 501 | _xmlname.add_element('make_unique', '1') |
||
| 502 | |||
| 503 | if comment: |
||
| 504 | cmd.add_element('comment', comment) |
||
| 505 | |||
| 506 | if term: |
||
| 507 | cmd.add_element('term', term) |
||
| 508 | |||
| 509 | if filter_type: |
||
| 510 | filter_type = filter_type.lower() |
||
| 511 | if filter_type not in FILTER_TYPES: |
||
| 512 | raise InvalidArgument( |
||
| 513 | 'create_filter requires type to be one of {0} but ' |
||
| 514 | 'was {1}'.format(', '.join(FILTER_TYPES), filter_type)) |
||
| 515 | cmd.add_element('type', filter_type) |
||
| 516 | |||
| 517 | return self._send_xml_command(cmd) |
||
| 518 | |||
| 519 | def clone_filter(self, filter_id): |
||
| 520 | """Clone an existing filter |
||
| 521 | |||
| 522 | Arguments: |
||
| 523 | copy (str): UUID of an existing filter to clone from |
||
| 524 | |||
| 525 | Returns: |
||
| 526 | The response. See :py:meth:`send_command` for details. |
||
| 527 | """ |
||
| 528 | cmd = XmlCommand('create_filter') |
||
| 529 | cmd.add_element('copy', filter_id) |
||
| 530 | return self._send_xml_command(cmd) |
||
| 531 | |||
| 532 | def create_group(self, name, comment=None, special=False, users=None): |
||
| 533 | """Create a new group |
||
| 534 | |||
| 535 | Arguments: |
||
| 536 | name (str): Name of the new group |
||
| 537 | comment (str, optional): Comment for the group |
||
| 538 | special (boolean, optional): Create permission giving members full |
||
| 539 | access to each other's entities |
||
| 540 | users (list, optional): List of user names to be in the group |
||
| 541 | |||
| 542 | Returns: |
||
| 543 | The response. See :py:meth:`send_command` for details. |
||
| 544 | """ |
||
| 545 | if not name: |
||
| 546 | raise RequiredArgument('create_group requires a name argument') |
||
| 547 | |||
| 548 | cmd = XmlCommand('create_group') |
||
| 549 | cmd.add_element('name', name) |
||
| 550 | |||
| 551 | if comment: |
||
| 552 | cmd.add_element('comment', comment) |
||
| 553 | |||
| 554 | if special: |
||
| 555 | _xmlspecial = cmd.add_element('specials') |
||
| 556 | _xmlspecial.add_element('full') |
||
| 557 | |||
| 558 | if users: |
||
| 559 | cmd.add_element('users', ','.join(users)) |
||
| 560 | |||
| 561 | return self._send_xml_command(cmd) |
||
| 562 | |||
| 563 | def clone_group(self, group_id): |
||
| 564 | """Clone an existing group |
||
| 565 | |||
| 566 | Arguments: |
||
| 567 | copy (str): UUID of an existing group to clone from |
||
| 568 | |||
| 569 | Returns: |
||
| 570 | The response. See :py:meth:`send_command` for details. |
||
| 571 | """ |
||
| 572 | cmd = XmlCommand('create_group') |
||
| 573 | cmd.add_element('copy', group_id) |
||
| 574 | return self._send_xml_command(cmd) |
||
| 575 | |||
| 576 | def create_note(self, text, nvt_oid, seconds_active=None, comment=None, |
||
| 577 | hosts=None, result_id=None, severity=None, task_id=None, |
||
| 578 | threat=None, port=None): |
||
| 579 | """Create a new note |
||
| 580 | |||
| 581 | Arguments: |
||
| 582 | text (str): Text of the new note |
||
| 583 | nvt_id (str): OID of the nvt to which note applies |
||
| 584 | seconds_active (int, optional): Seconds note will be active. -1 on |
||
| 585 | always, 0 off |
||
| 586 | comment (str, optional): Comment for the note |
||
| 587 | hosts (list, optional): A list of hosts addresses |
||
| 588 | port (str, optional): Port to which the note applies |
||
| 589 | result_id (str, optional): UUID of a result to which note applies |
||
| 590 | severity (decimal, optional): Severity to which note applies |
||
| 591 | task_id (str, optional): UUID of task to which note applies |
||
| 592 | threat (str, optional): Threat level to which note applies. Will be |
||
| 593 | converted to severity |
||
| 594 | |||
| 595 | Returns: |
||
| 596 | The response. See :py:meth:`send_command` for details. |
||
| 597 | """ |
||
| 598 | if not text: |
||
| 599 | raise RequiredArgument('create_note requires a text argument') |
||
| 600 | |||
| 601 | if not nvt_oid: |
||
| 602 | raise RequiredArgument('create_note requires a nvt_oid argument') |
||
| 603 | |||
| 604 | cmd = XmlCommand('create_note') |
||
| 605 | cmd.add_element('text', text) |
||
| 606 | cmd.add_element('nvt', attrs={"oid": nvt_oid}) |
||
| 607 | |||
| 608 | if not seconds_active is None: |
||
| 609 | cmd.add_element('active', str(seconds_active)) |
||
| 610 | |||
| 611 | if comment: |
||
| 612 | cmd.add_element('comment', comment) |
||
| 613 | |||
| 614 | if hosts: |
||
| 615 | cmd.add_element('hosts', ', '.join(hosts)) |
||
| 616 | |||
| 617 | if port: |
||
| 618 | cmd.add_element('port', port) |
||
| 619 | |||
| 620 | if result_id: |
||
| 621 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 622 | |||
| 623 | if severity: |
||
| 624 | cmd.add_element('severity', severity) |
||
| 625 | |||
| 626 | if task_id: |
||
| 627 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 628 | |||
| 629 | if threat: |
||
| 630 | cmd.add_element('threat', threat) |
||
| 631 | |||
| 632 | return self._send_xml_command(cmd) |
||
| 633 | |||
| 634 | def clone_note(self, note_id): |
||
| 635 | """Clone an existing note |
||
| 636 | |||
| 637 | Arguments: |
||
| 638 | copy (str): UUID of an existing note to clone from |
||
| 639 | |||
| 640 | Returns: |
||
| 641 | The response. See :py:meth:`send_command` for details. |
||
| 642 | """ |
||
| 643 | cmd = XmlCommand('create_note') |
||
| 644 | cmd.add_element('copy', note_id) |
||
| 645 | return self._send_xml_command(cmd) |
||
| 646 | |||
| 647 | View Code Duplication | def create_override(self, text, nvt_oid, seconds_active=None, hosts=None, |
|
| 648 | port=None, result_id=None, severity=None, comment=None, |
||
| 649 | new_severity=None, task_id=None, threat=None, |
||
| 650 | new_threat=None): |
||
| 651 | """Create a new override |
||
| 652 | |||
| 653 | Arguments: |
||
| 654 | text (str): Text of the new override |
||
| 655 | nvt_id (str): OID of the nvt to which override applies |
||
| 656 | seconds_active (int, optional): Seconds override will be active. |
||
| 657 | -1 on always, 0 off |
||
| 658 | comment (str, optional): Comment for the override |
||
| 659 | hosts (list, optional): A list of host addresses |
||
| 660 | port (str, optional): Port ot which the override applies |
||
| 661 | result_id (str, optional): UUID of a result to which override |
||
| 662 | applies |
||
| 663 | severity (decimal, optional): Severity to which override applies |
||
| 664 | new_severity (decimal, optional): New severity for result |
||
| 665 | task_id (str, optional): UUID of task to which override applies |
||
| 666 | threat (str, optional): Threat level to which override applies. Will |
||
| 667 | be converted to severity |
||
| 668 | new_threat (str, optional): New threat level for result, will be |
||
| 669 | converted to a new_severity |
||
| 670 | |||
| 671 | Returns: |
||
| 672 | The response. See :py:meth:`send_command` for details. |
||
| 673 | """ |
||
| 674 | if not text: |
||
| 675 | raise RequiredArgument('create_override requires a text argument') |
||
| 676 | |||
| 677 | if not nvt_oid: |
||
| 678 | raise RequiredArgument('create_override requires a nvt_oid ' |
||
| 679 | 'argument') |
||
| 680 | |||
| 681 | cmd = XmlCommand('create_override') |
||
| 682 | cmd.add_element('text', text) |
||
| 683 | cmd.add_element('nvt', attrs={'oid': nvt_oid}) |
||
| 684 | |||
| 685 | if not seconds_active is None: |
||
| 686 | cmd.add_element('active', str(seconds_active)) |
||
| 687 | |||
| 688 | if comment: |
||
| 689 | cmd.add_element('comment', comment) |
||
| 690 | |||
| 691 | if hosts: |
||
| 692 | cmd.add_element('hosts', ', '.join(hosts)) |
||
| 693 | |||
| 694 | if port: |
||
| 695 | cmd.add_element('port', port) |
||
| 696 | |||
| 697 | if result_id: |
||
| 698 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 699 | |||
| 700 | if severity: |
||
| 701 | cmd.add_element('severity', severity) |
||
| 702 | |||
| 703 | if new_severity: |
||
| 704 | cmd.add_element('new_severity', new_severity) |
||
| 705 | |||
| 706 | if task_id: |
||
| 707 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 708 | |||
| 709 | if threat: |
||
| 710 | cmd.add_element('threat', threat) |
||
| 711 | |||
| 712 | if new_threat: |
||
| 713 | cmd.add_element('new_threat', new_threat) |
||
| 714 | |||
| 715 | return self._send_xml_command(cmd) |
||
| 716 | |||
| 717 | def clone_override(self, override_id): |
||
| 718 | """Clone an existing override |
||
| 719 | |||
| 720 | Arguments: |
||
| 721 | copy (str): UUID of an existing override to clone from |
||
| 722 | |||
| 723 | Returns: |
||
| 724 | The response. See :py:meth:`send_command` for details. |
||
| 725 | """ |
||
| 726 | cmd = XmlCommand('create_override') |
||
| 727 | cmd.add_element('copy', override_id) |
||
| 728 | return self._send_xml_command(cmd) |
||
| 729 | |||
| 730 | View Code Duplication | def create_permission(self, name, subject_id, subject_type, |
|
| 731 | resource_id=None, resource_type=None, |
||
| 732 | comment=None): |
||
| 733 | """Create a new permission |
||
| 734 | |||
| 735 | Arguments: |
||
| 736 | name (str): Name of the new permission |
||
| 737 | subject_id (str): UUID of subject to whom the permission is granted |
||
| 738 | subject_type (str): Type of the subject user, group or role |
||
| 739 | comment (str, optional): Comment for the permission |
||
| 740 | resource_id (str, optional): UUID of entity to which the permission |
||
| 741 | applies |
||
| 742 | resource_type (str, optional): Type of the resource. For Super |
||
| 743 | permissions user, group or role |
||
| 744 | |||
| 745 | Returns: |
||
| 746 | The response. See :py:meth:`send_command` for details. |
||
| 747 | """ |
||
| 748 | if not name: |
||
| 749 | raise RequiredArgument('create_permission requires a name argument') |
||
| 750 | |||
| 751 | if not subject_id: |
||
| 752 | raise RequiredArgument( |
||
| 753 | 'create_permission requires a subject_id argument') |
||
| 754 | |||
| 755 | if subject_type not in ('user', 'group', 'role'): |
||
| 756 | raise InvalidArgument( |
||
| 757 | 'create_permission requires subject_type to be either user, ' |
||
| 758 | 'group or role') |
||
| 759 | |||
| 760 | cmd = XmlCommand('create_permission') |
||
| 761 | cmd.add_element('name', name) |
||
| 762 | |||
| 763 | _xmlsubject = cmd.add_element('subject', attrs={'id': subject_id}) |
||
| 764 | _xmlsubject.add_element('type', type) |
||
| 765 | |||
| 766 | if comment: |
||
| 767 | cmd.add_element('comment', comment) |
||
| 768 | |||
| 769 | if resource_id and resource_type: |
||
| 770 | _xmlresource = cmd.add_element('resource', |
||
| 771 | attrs={'id': resource_id}) |
||
| 772 | _xmlresource.add_element('type', resource_type) |
||
| 773 | |||
| 774 | |||
| 775 | return self._send_xml_command(cmd) |
||
| 776 | |||
| 777 | def clone_permission(self, permission_id): |
||
| 778 | """Clone an existing permission |
||
| 779 | |||
| 780 | Arguments: |
||
| 781 | copy (str): UUID of an existing permission to clone from |
||
| 782 | |||
| 783 | Returns: |
||
| 784 | The response. See :py:meth:`send_command` for details. |
||
| 785 | """ |
||
| 786 | cmd = XmlCommand('create_permission') |
||
| 787 | cmd.add_element('copy', permission_id) |
||
| 788 | return self._send_xml_command(cmd) |
||
| 789 | |||
| 790 | def create_port_list(self, name, port_range, comment=None): |
||
| 791 | """Create a new port list |
||
| 792 | |||
| 793 | Arguments: |
||
| 794 | name (str): Name of the new port list |
||
| 795 | port_range (str): Port list ranges e.g. `"T: 1-1234"` for tcp port |
||
| 796 | 1 - 1234 |
||
| 797 | comment (str, optional): Comment for the port list |
||
| 798 | |||
| 799 | Returns: |
||
| 800 | The response. See :py:meth:`send_command` for details. |
||
| 801 | """ |
||
| 802 | if not name: |
||
| 803 | raise RequiredArgument('create_port_list requires a name argument') |
||
| 804 | |||
| 805 | if not port_range: |
||
| 806 | raise RequiredArgument( |
||
| 807 | 'create_port_list requires a port_range argument') |
||
| 808 | |||
| 809 | cmd = XmlCommand('create_port_list') |
||
| 810 | cmd.add_element('name', name) |
||
| 811 | cmd.add_element('port_range', port_range) |
||
| 812 | |||
| 813 | if comment: |
||
| 814 | cmd.add_element('comment', comment) |
||
| 815 | |||
| 816 | return self._send_xml_command(cmd) |
||
| 817 | |||
| 818 | def clone_port_list(self, port_list_id): |
||
| 819 | """Clone an existing port list |
||
| 820 | |||
| 821 | Arguments: |
||
| 822 | copy (str): UUID of an existing port list to clone from |
||
| 823 | |||
| 824 | Returns: |
||
| 825 | The response. See :py:meth:`send_command` for details. |
||
| 826 | """ |
||
| 827 | cmd = XmlCommand('create_port_list') |
||
| 828 | cmd.add_element('copy', port_list_id) |
||
| 829 | return self._send_xml_command(cmd) |
||
| 830 | |||
| 831 | def create_port_range(self, port_list_id, start, end, port_range_type, |
||
| 832 | comment=None): |
||
| 833 | """Create new port range |
||
| 834 | |||
| 835 | Arguments: |
||
| 836 | port_list_id (str): UUID of the port list to which to add the range |
||
| 837 | start (int): The first port in the range |
||
| 838 | end (int): The last port in the range |
||
| 839 | type (str): The type of the ports: TCP, UDP, ... |
||
| 840 | comment (str, optional): Comment for the port range |
||
| 841 | |||
| 842 | Returns: |
||
| 843 | The response. See :py:meth:`send_command` for details. |
||
| 844 | """ |
||
| 845 | if not port_list_id: |
||
| 846 | raise RequiredArgument('create_port_range requires ' |
||
| 847 | 'a port_list_id argument') |
||
| 848 | |||
| 849 | if not port_range_type: |
||
| 850 | raise RequiredArgument( |
||
| 851 | 'create_port_range requires a port_range_type argument') |
||
| 852 | |||
| 853 | if not start: |
||
| 854 | raise RequiredArgument( |
||
| 855 | 'create_port_range requires a start argument') |
||
| 856 | |||
| 857 | if not end: |
||
| 858 | raise RequiredArgument( |
||
| 859 | 'create_port_range requires a end argument') |
||
| 860 | |||
| 861 | cmd = XmlCommand('create_port_range') |
||
| 862 | cmd.add_element('port_list', attrs={'id': port_list_id}) |
||
| 863 | cmd.add_element('start', start) |
||
| 864 | cmd.add_element('end', end) |
||
| 865 | cmd.add_element('type', port_range_type) |
||
| 866 | |||
| 867 | if comment: |
||
| 868 | cmd.add_element('comment', comment) |
||
| 869 | |||
| 870 | return self._send_xml_command(cmd) |
||
| 871 | |||
| 872 | def import_report(self, report, task_id=None, task_name=None, |
||
| 873 | task_comment=None, in_assets=None): |
||
| 874 | """Import a Report |
||
| 875 | |||
| 876 | Arguments: |
||
| 877 | report (str): Report XML as string to import |
||
| 878 | task_id (str, optional): UUID of task to import report to |
||
| 879 | task_name (str, optional): Name of task to be createed if task_id is |
||
| 880 | not present. Either task_id or task_name must be passed |
||
| 881 | task_comment (str, optional): Comment for task to be created if |
||
| 882 | task_id is not present |
||
| 883 | in_asset (boolean, optional): Whether to create or update assets |
||
| 884 | using the report |
||
| 885 | |||
| 886 | Returns: |
||
| 887 | The response. See :py:meth:`send_command` for details. |
||
| 888 | """ |
||
| 889 | if not report: |
||
| 890 | raise RequiredArgument('create_report requires a report argument') |
||
| 891 | |||
| 892 | cmd = XmlCommand('create_report') |
||
| 893 | |||
| 894 | if task_id: |
||
| 895 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 896 | elif task_name: |
||
| 897 | _xmltask = cmd.add_element('task') |
||
| 898 | _xmltask.add_element('name', task_name) |
||
| 899 | |||
| 900 | if task_comment: |
||
| 901 | _xmltask.add_element('comment', task_comment) |
||
| 902 | else: |
||
| 903 | raise RequiredArgument( |
||
| 904 | 'import_report requires a task_id or task_name argument') |
||
| 905 | |||
| 906 | if not in_assets is None: |
||
| 907 | if in_assets: |
||
| 908 | cmd.add_element('in_assets', '1') |
||
| 909 | else: |
||
| 910 | cmd.add_element('in_assets', '0') |
||
| 911 | try: |
||
| 912 | cmd.append_xml_str(report) |
||
| 913 | except etree.XMLSyntaxError as e: |
||
| 914 | raise InvalidArgument( |
||
| 915 | 'Invalid xml passed as report to import_report', e) |
||
| 916 | |||
| 917 | return self._send_xml_command(cmd) |
||
| 918 | |||
| 919 | def create_role(self, name, comment=None, users=None): |
||
| 920 | """Create a new role |
||
| 921 | |||
| 922 | Arguments: |
||
| 923 | name (str): Name of the role |
||
| 924 | comment (str, optional): Comment for the role |
||
| 925 | users (list, optional): List of user names to add to the role |
||
| 926 | |||
| 927 | Returns: |
||
| 928 | The response. See :py:meth:`send_command` for details. |
||
| 929 | """ |
||
| 930 | |||
| 931 | if not name: |
||
| 932 | raise RequiredArgument('create_role requires a name argument') |
||
| 933 | |||
| 934 | cmd = XmlCommand('create_role') |
||
| 935 | cmd.add_element('name', name) |
||
| 936 | |||
| 937 | if comment: |
||
| 938 | cmd.add_element('comment', comment) |
||
| 939 | |||
| 940 | if users: |
||
| 941 | cmd.add_element('users', ",".join(users)) |
||
| 942 | |||
| 943 | return self._send_xml_command(cmd) |
||
| 944 | |||
| 945 | def clone_role(self, role_id): |
||
| 946 | """Clone an existing role |
||
| 947 | |||
| 948 | Arguments: |
||
| 949 | copy (str): UUID of an existing role to clone from |
||
| 950 | |||
| 951 | Returns: |
||
| 952 | The response. See :py:meth:`send_command` for details. |
||
| 953 | """ |
||
| 954 | cmd = XmlCommand('create_role') |
||
| 955 | cmd.add_element('copy', role_id) |
||
| 956 | return self._send_xml_command(cmd) |
||
| 957 | |||
| 958 | def create_scanner(self, name, host, port, scanner_type, ca_pub, |
||
| 959 | credential_id, comment=None): |
||
| 960 | """Create a new scanner |
||
| 961 | |||
| 962 | Arguments: |
||
| 963 | name (str): Name of the scanner |
||
| 964 | host (str): The host of the scanner |
||
| 965 | port (str): The port of the scanner |
||
| 966 | scanner_type (str): The type of the scanner |
||
| 967 | ca_pub (str): Certificate of CA to verify scanner certificate |
||
| 968 | credential_id (str): UUID of client certificate credential for the |
||
| 969 | scanner |
||
| 970 | comment (str, optional): Comment for the scanner |
||
| 971 | |||
| 972 | Returns: |
||
| 973 | The response. See :py:meth:`send_command` for details. |
||
| 974 | """ |
||
| 975 | if not name: |
||
| 976 | raise RequiredArgument('create_scanner requires a name argument') |
||
| 977 | |||
| 978 | if not host: |
||
| 979 | raise RequiredArgument('create_scanner requires a host argument') |
||
| 980 | |||
| 981 | if not port: |
||
| 982 | raise RequiredArgument('create_scanner requires a port argument') |
||
| 983 | |||
| 984 | if not type: |
||
| 985 | raise RequiredArgument('create_scanner requires a scanner_type ' |
||
| 986 | 'argument') |
||
| 987 | if not ca_pub: |
||
| 988 | raise RequiredArgument('create_scanner requires a ca_pub argument') |
||
| 989 | |||
| 990 | if not credential_id: |
||
| 991 | raise RequiredArgument('create_scanner requires a credential_id ' |
||
| 992 | 'argument') |
||
| 993 | |||
| 994 | cmd = XmlCommand('create_scanner') |
||
| 995 | cmd.add_element('name', name) |
||
| 996 | cmd.add_element('host', host) |
||
| 997 | cmd.add_element('port', port) |
||
| 998 | cmd.add_element('type', scanner_type) |
||
| 999 | cmd.add_element('ca_pub', ca_pub) |
||
| 1000 | cmd.add_element('credential', attrs={'id': str(credential_id)}) |
||
| 1001 | |||
| 1002 | if comment: |
||
| 1003 | cmd.add_element('comment', comment) |
||
| 1004 | |||
| 1005 | return self._send_xml_command(cmd) |
||
| 1006 | |||
| 1007 | def clone_scanner(self, scanner_id): |
||
| 1008 | """Clone an existing scanner |
||
| 1009 | |||
| 1010 | Arguments: |
||
| 1011 | copy (str): UUID of an existing scanner to clone from |
||
| 1012 | |||
| 1013 | Returns: |
||
| 1014 | The response. See :py:meth:`send_command` for details. |
||
| 1015 | """ |
||
| 1016 | cmd = XmlCommand('create_scanner') |
||
| 1017 | cmd.add_element('copy', scanner_id) |
||
| 1018 | return self._send_xml_command(cmd) |
||
| 1019 | |||
| 1020 | def create_schedule(self, name, comment=None, first_time_minute=None, |
||
| 1021 | first_time_hour=None, first_time_day_of_month=None, |
||
| 1022 | first_time_month=None, first_time_year=None, |
||
| 1023 | duration=None, duration_unit=None, period=None, |
||
| 1024 | period_unit=None, timezone=None): |
||
| 1025 | """Create a new schedule |
||
| 1026 | |||
| 1027 | Arguments: |
||
| 1028 | name (str): Name of the schedule |
||
| 1029 | comment (str, optional): Comment for the schedule |
||
| 1030 | first_time_minute (int, optional): First time minute the schedule |
||
| 1031 | will run |
||
| 1032 | first_time_hour (int, optional): First time hour the schedule |
||
| 1033 | will run |
||
| 1034 | first_time_day_of_month (int, optional): First time day of month the |
||
| 1035 | schedule will run |
||
| 1036 | first_time_month (int, optional): First time month the schedule |
||
| 1037 | will run |
||
| 1038 | first_time_year (int, optional): First time year the schedule |
||
| 1039 | will run |
||
| 1040 | duration (int, optional): How long the Manager will run the |
||
| 1041 | scheduled task for until it gets paused if not finished yet. |
||
| 1042 | duration_unit (str, optional): Unit of the duration. One of second, |
||
| 1043 | minute, hour, day, week, month, year, decade. Required if |
||
| 1044 | duration is set. |
||
| 1045 | period (int, optional): How often the Manager will repeat the |
||
| 1046 | scheduled task |
||
| 1047 | period_unit (str, optional): Unit of the period. One of second, |
||
| 1048 | minute, hour, day, week, month, year, decade. Required if |
||
| 1049 | period is set. |
||
| 1050 | timezone (str, optional): The timezone the schedule will follow |
||
| 1051 | |||
| 1052 | Returns: |
||
| 1053 | The response. See :py:meth:`send_command` for details. |
||
| 1054 | """ |
||
| 1055 | if not name: |
||
| 1056 | raise RequiredArgument('create_schedule requires a name argument') |
||
| 1057 | |||
| 1058 | cmd = XmlCommand('create_schedule') |
||
| 1059 | cmd.add_element('name', name) |
||
| 1060 | |||
| 1061 | if comment: |
||
| 1062 | cmd.add_element('comment', comment) |
||
| 1063 | |||
| 1064 | View Code Duplication | if first_time_minute or first_time_hour or first_time_day_of_month or \ |
|
| 1065 | first_time_month or first_time_year: |
||
| 1066 | |||
| 1067 | if not first_time_minute: |
||
| 1068 | raise RequiredArgument( |
||
| 1069 | 'Setting first_time requires first_time_minute argument') |
||
| 1070 | if not first_time_hour: |
||
| 1071 | raise RequiredArgument( |
||
| 1072 | 'Setting first_time requires first_time_hour argument') |
||
| 1073 | if not first_time_day_of_month: |
||
| 1074 | raise RequiredArgument( |
||
| 1075 | 'Setting first_time requires first_time_day_of_month ' |
||
| 1076 | 'argument') |
||
| 1077 | if not first_time_month: |
||
| 1078 | raise RequiredArgument( |
||
| 1079 | 'Setting first_time requires first_time_month argument') |
||
| 1080 | if not first_time_year: |
||
| 1081 | raise RequiredArgument( |
||
| 1082 | 'Setting first_time requires first_time_year argument') |
||
| 1083 | |||
| 1084 | _xmlftime = cmd.add_element('first_time') |
||
| 1085 | _xmlftime.add_element('minute', str(first_time_minute)) |
||
| 1086 | _xmlftime.add_element('hour', str(first_time_hour)) |
||
| 1087 | _xmlftime.add_element('day_of_month', str(first_time_day_of_month)) |
||
| 1088 | _xmlftime.add_element('month', str(first_time_month)) |
||
| 1089 | _xmlftime.add_element('year', str(first_time_year)) |
||
| 1090 | |||
| 1091 | View Code Duplication | if duration: |
|
| 1092 | if not duration_unit: |
||
| 1093 | raise RequiredArgument( |
||
| 1094 | 'Setting duration requires duration_unit argument') |
||
| 1095 | |||
| 1096 | if not duration_unit in TIME_UNITS: |
||
| 1097 | raise InvalidArgument( |
||
| 1098 | 'duration_unit must be one of {units} but {actual} has ' |
||
| 1099 | 'been passed'.format( |
||
| 1100 | units=', '.join(TIME_UNITS), actual=duration_unit)) |
||
| 1101 | |||
| 1102 | _xmlduration = cmd.add_element('duration', str(duration)) |
||
| 1103 | _xmlduration.add_element('unit', duration_unit) |
||
| 1104 | |||
| 1105 | View Code Duplication | if period: |
|
| 1106 | if not period_unit: |
||
| 1107 | raise RequiredArgument( |
||
| 1108 | 'Setting period requires period_unit argument') |
||
| 1109 | |||
| 1110 | if not period_unit in TIME_UNITS: |
||
| 1111 | raise InvalidArgument( |
||
| 1112 | 'period_unit must be one of {units} but {actual} has ' |
||
| 1113 | 'been passed'.format( |
||
| 1114 | units=', '.join(TIME_UNITS), actual=period_unit)) |
||
| 1115 | |||
| 1116 | _xmlperiod = cmd.add_element('period', str(period)) |
||
| 1117 | _xmlperiod.add_element('unit', period_unit) |
||
| 1118 | |||
| 1119 | if timezone: |
||
| 1120 | cmd.add_element('timezone', timezone) |
||
| 1121 | |||
| 1122 | return self._send_xml_command(cmd) |
||
| 1123 | |||
| 1124 | def clone_schedule(self, schedule_id): |
||
| 1125 | """Clone an existing schedule |
||
| 1126 | |||
| 1127 | Arguments: |
||
| 1128 | copy (str): UUID of an existing schedule to clone from |
||
| 1129 | |||
| 1130 | Returns: |
||
| 1131 | The response. See :py:meth:`send_command` for details. |
||
| 1132 | """ |
||
| 1133 | cmd = XmlCommand('create_schedule') |
||
| 1134 | cmd.add_element('copy', schedule_id) |
||
| 1135 | return self._send_xml_command(cmd) |
||
| 1136 | |||
| 1137 | def create_tag(self, name, resource_id, resource_type, value=None, |
||
| 1138 | comment=None, active=None): |
||
| 1139 | """Create a new tag |
||
| 1140 | |||
| 1141 | Arguments: |
||
| 1142 | name (str): Name of the tag. A full tag name consisting of namespace |
||
| 1143 | and predicate e.g. `foo:bar`. |
||
| 1144 | resource_id (str): ID of the resource the tag is to be attached to. |
||
| 1145 | resource_type (str): Entity type the tag is to be attached to |
||
| 1146 | value (str, optional): Value associated with the tag |
||
| 1147 | comment (str, optional): Comment for the tag |
||
| 1148 | active (boolean, optional): Whether the tag should be active |
||
| 1149 | |||
| 1150 | Returns: |
||
| 1151 | The response. See :py:meth:`send_command` for details. |
||
| 1152 | """ |
||
| 1153 | cmd = XmlCommand('create_tag') |
||
| 1154 | cmd.add_element('name', name) |
||
| 1155 | _xmlresource = cmd.add_element('resource', |
||
| 1156 | attrs={'id': str(resource_id)}) |
||
| 1157 | _xmlresource.add_element('type', resource_type) |
||
| 1158 | |||
| 1159 | if comment: |
||
| 1160 | cmd.add_element('comment', comment) |
||
| 1161 | |||
| 1162 | if value: |
||
| 1163 | cmd.add_element('value', value) |
||
| 1164 | |||
| 1165 | if not active is None: |
||
| 1166 | if active: |
||
| 1167 | cmd.add_element('active', '1') |
||
| 1168 | else: |
||
| 1169 | cmd.add_element('active', '0') |
||
| 1170 | |||
| 1171 | return self._send_xml_command(cmd) |
||
| 1172 | |||
| 1173 | def clone_tag(self, tag_id): |
||
| 1174 | """Clone an existing tag |
||
| 1175 | |||
| 1176 | Arguments: |
||
| 1177 | copy (str): UUID of an existing tag to clone from |
||
| 1178 | |||
| 1179 | Returns: |
||
| 1180 | The response. See :py:meth:`send_command` for details. |
||
| 1181 | """ |
||
| 1182 | cmd = XmlCommand('create_tag') |
||
| 1183 | cmd.add_element('copy', tag_id) |
||
| 1184 | return self._send_xml_command(cmd) |
||
| 1185 | |||
| 1186 | def create_target(self, name, make_unique=False, asset_hosts_filter=None, |
||
| 1187 | hosts=None, comment=None, exclude_hosts=None, |
||
| 1188 | ssh_credential_id=None, ssh_credential_port=None, |
||
| 1189 | smb_credential_id=None, esxi_credential_id=None, |
||
| 1190 | snmp_credential_id=None, alive_tests=None, |
||
| 1191 | reverse_lookup_only=None, reverse_lookup_unify=None, |
||
| 1192 | port_range=None, port_list_id=None): |
||
| 1193 | """Create a new target |
||
| 1194 | |||
| 1195 | Arguments: |
||
| 1196 | name (str): Name of the target |
||
| 1197 | make_unique (boolean, optional): Append a unique suffix if the name |
||
| 1198 | already exists |
||
| 1199 | asset_hosts_filter (str, optional): Filter to select target host |
||
| 1200 | from assets hosts |
||
| 1201 | hosts (list, optional): List of hosts addresses to scan |
||
| 1202 | exclude_hosts (list, optional): List of hosts addresses to exclude |
||
| 1203 | from scan |
||
| 1204 | comment (str, optional): Comment for the target |
||
| 1205 | ssh_credential_id (str, optional): UUID of a ssh credential to use |
||
| 1206 | on target |
||
| 1207 | ssh_credential_port (str, optional): The port to use for ssh |
||
| 1208 | credential |
||
| 1209 | smb_credential_id (str, optional): UUID of a smb credential to use |
||
| 1210 | on target |
||
| 1211 | snmp_credential_id (str, optional): UUID of a snmp credential to use |
||
| 1212 | on target |
||
| 1213 | esxi_credential_id (str, optional): UUID of a esxi credential to use |
||
| 1214 | on target |
||
| 1215 | alive_tests (str, optional): Which alive tests to use |
||
| 1216 | reverse_lookup_only (boolean, optional): Whether to scan only hosts |
||
| 1217 | that have names |
||
| 1218 | reverse_lookup_unify (boolean, optional): Whether to scan only one |
||
| 1219 | IP when multiple IPs have the same name. |
||
| 1220 | port_range (str, optional): Port range for the target |
||
| 1221 | port_list_id (str, optional): UUID of the port list to use on target |
||
| 1222 | |||
| 1223 | Returns: |
||
| 1224 | The response. See :py:meth:`send_command` for details. |
||
| 1225 | """ |
||
| 1226 | if not name: |
||
| 1227 | raise RequiredArgument('create_target requires a name argument') |
||
| 1228 | |||
| 1229 | cmd = XmlCommand('create_target') |
||
| 1230 | _xmlname = cmd.add_element('name', name) |
||
| 1231 | if make_unique: |
||
| 1232 | _xmlname.add_element('make_unique', '1') |
||
| 1233 | |||
| 1234 | if asset_hosts_filter: |
||
| 1235 | cmd.add_element('asset_hosts', |
||
| 1236 | attrs={'filter': str(asset_hosts_filter)}) |
||
| 1237 | elif hosts: |
||
| 1238 | cmd.add_element('hosts', ', '.join(hosts)) |
||
| 1239 | else: |
||
| 1240 | raise RequiredArgument('create_target requires either a hosts or ' |
||
| 1241 | 'an asset_hosts_filter argument') |
||
| 1242 | |||
| 1243 | if comment: |
||
| 1244 | cmd.add_element('comment', comment) |
||
| 1245 | |||
| 1246 | if exclude_hosts: |
||
| 1247 | cmd.add_element('exclude_hosts', ', '.join(exclude_hosts)) |
||
| 1248 | |||
| 1249 | if ssh_credential_id: |
||
| 1250 | _xmlssh = cmd.add_element('ssh_credential', |
||
| 1251 | attrs={'id': ssh_credential_id}) |
||
| 1252 | if ssh_credential_port: |
||
| 1253 | _xmlssh.add_element('port', ssh_credential_port) |
||
| 1254 | |||
| 1255 | if smb_credential_id: |
||
| 1256 | cmd.add_element('smb_credential', attrs={'id': smb_credential_id}) |
||
| 1257 | |||
| 1258 | if esxi_credential_id: |
||
| 1259 | cmd.add_element('esxi_credential', attrs={'id': esxi_credential_id}) |
||
| 1260 | |||
| 1261 | if snmp_credential_id: |
||
| 1262 | cmd.add_element('snmp_credential', attrs={'id': snmp_credential_id}) |
||
| 1263 | |||
| 1264 | if alive_tests: |
||
| 1265 | cmd.add_element('alive_tests', alive_tests) |
||
| 1266 | |||
| 1267 | if not reverse_lookup_only is None: |
||
| 1268 | if reverse_lookup_only: |
||
| 1269 | cmd.add_element('reverse_lookup_only', '1') |
||
| 1270 | else: |
||
| 1271 | cmd.add_element('reverse_lookup_only', '0') |
||
| 1272 | |||
| 1273 | if not reverse_lookup_unify is None: |
||
| 1274 | if reverse_lookup_unify: |
||
| 1275 | cmd.add_element('reverse_lookup_unify', '1') |
||
| 1276 | else: |
||
| 1277 | cmd.add_element('reverse_lookup_unify', '0') |
||
| 1278 | |||
| 1279 | if port_range: |
||
| 1280 | cmd.add_element('port_range', port_range) |
||
| 1281 | |||
| 1282 | if port_list_id: |
||
| 1283 | cmd.add_element('port_list', attrs={'id': port_list_id}) |
||
| 1284 | |||
| 1285 | return self._send_xml_command(cmd) |
||
| 1286 | |||
| 1287 | def clone_target(self, target_id): |
||
| 1288 | """Clone an existing target |
||
| 1289 | |||
| 1290 | Arguments: |
||
| 1291 | copy (str): UUID of an existing target to clone from |
||
| 1292 | |||
| 1293 | Returns: |
||
| 1294 | The response. See :py:meth:`send_command` for details. |
||
| 1295 | """ |
||
| 1296 | cmd = XmlCommand('create_target') |
||
| 1297 | cmd.add_element('copy', target_id) |
||
| 1298 | return self._send_xml_command(cmd) |
||
| 1299 | |||
| 1300 | def create_task(self, name, config_id, target_id, scanner_id, |
||
| 1301 | alterable=None, hosts_ordering=None, schedule_id=None, |
||
| 1302 | alert_ids=None, comment=None, schedule_periods=None, |
||
| 1303 | observers=None): |
||
| 1304 | """Create a new task |
||
| 1305 | |||
| 1306 | Arguments: |
||
| 1307 | name (str): Name of the task |
||
| 1308 | config_id (str): UUID of scan config to use by the task |
||
| 1309 | target_id (str): UUID of target to be scanned |
||
| 1310 | scanner_id (str): UUID of scanner to use for scanning the target |
||
| 1311 | comment (str, optional): Comment for the task |
||
| 1312 | alterable (boolean, optional): Wether the task should be alterable |
||
| 1313 | alert_ids (list, optional): List of UUIDs for alerts to be applied |
||
| 1314 | to the task |
||
| 1315 | hosts_ordering (str, optional): The order hosts are scanned in |
||
| 1316 | schedule_id (str, optional): UUID of a schedule when the task should |
||
| 1317 | be run. |
||
| 1318 | schedule_periods (int, optional): A limit to the number of times the |
||
| 1319 | task will be scheduled, or 0 for no limit |
||
| 1320 | observers (list, optional): List of user names which should be |
||
| 1321 | allowed to observe this task |
||
| 1322 | |||
| 1323 | Returns: |
||
| 1324 | The response. See :py:meth:`send_command` for details. |
||
| 1325 | """ |
||
| 1326 | if not name: |
||
| 1327 | raise RequiredArgument('create_task requires a name argument') |
||
| 1328 | |||
| 1329 | if not config_id: |
||
| 1330 | raise RequiredArgument('create_task requires a config_id argument') |
||
| 1331 | |||
| 1332 | if not target_id: |
||
| 1333 | raise RequiredArgument('create_task requires a target_id argument') |
||
| 1334 | |||
| 1335 | if not scanner_id: |
||
| 1336 | raise RequiredArgument('create_task requires a scanner_id argument') |
||
| 1337 | |||
| 1338 | cmd = XmlCommand('create_task') |
||
| 1339 | cmd.add_element('name', name) |
||
| 1340 | cmd.add_element('config', attrs={'id': config_id}) |
||
| 1341 | cmd.add_element('target', attrs={'id': target_id}) |
||
| 1342 | cmd.add_element('scanner', attrs={'id': scanner_id}) |
||
| 1343 | |||
| 1344 | if comment: |
||
| 1345 | cmd.add_element('comment', comment) |
||
| 1346 | |||
| 1347 | if not alterable is None: |
||
| 1348 | if alterable: |
||
| 1349 | cmd.add_element('alterable', '1') |
||
| 1350 | else: |
||
| 1351 | cmd.add_element('alterable', '0') |
||
| 1352 | |||
| 1353 | if hosts_ordering: |
||
| 1354 | cmd.add_element('hosts_ordering', hosts_ordering) |
||
| 1355 | |||
| 1356 | if alert_ids: |
||
| 1357 | if isinstance(alert_ids, str): |
||
| 1358 | logger.warning( |
||
| 1359 | 'Please pass a list as alert_ids parameter to create_task. ' |
||
| 1360 | 'Passing a string is deprecated and will be removed in ' |
||
| 1361 | 'future.') |
||
| 1362 | |||
| 1363 | #if a single id is given as a string wrap it into a list |
||
| 1364 | alert_ids = [alert_ids] |
||
| 1365 | if isinstance(alert_ids, list): |
||
| 1366 | #parse all given alert id's |
||
| 1367 | for alert in alert_ids: |
||
| 1368 | cmd.add_element('alert', attrs={'id': str(alert)}) |
||
| 1369 | |||
| 1370 | if schedule_id: |
||
| 1371 | cmd.add_element('schedule', schedule_id) |
||
| 1372 | |||
| 1373 | if schedule_periods: |
||
| 1374 | cmd.add_element('schedule_periods', str(schedule_periods)) |
||
| 1375 | |||
| 1376 | if observers: |
||
| 1377 | cmd.add_element('observers', ' '.join(observers)) |
||
| 1378 | |||
| 1379 | return self._send_xml_command(cmd) |
||
| 1380 | |||
| 1381 | def clone_task(self, task_id): |
||
| 1382 | """Clone an existing task |
||
| 1383 | |||
| 1384 | Arguments: |
||
| 1385 | task_id (str): UUID of existing task to clone from |
||
| 1386 | |||
| 1387 | Returns: |
||
| 1388 | The response. See :py:meth:`send_command` for details. |
||
| 1389 | """ |
||
| 1390 | cmd = XmlCommand('create_task') |
||
| 1391 | cmd.add_element('copy', task_id) |
||
| 1392 | return self._send_xml_command(cmd) |
||
| 1393 | |||
| 1394 | def create_user(self, name, password=None, hosts=None, hosts_allow=False, |
||
| 1395 | ifaces=None, ifaces_allow=False, role_ids=None): |
||
| 1396 | """Create a new user |
||
| 1397 | |||
| 1398 | Arguments: |
||
| 1399 | name (str): Name of the user |
||
| 1400 | password (str, optional): Password of the user |
||
| 1401 | hosts (list, optional): A list of host addresses (IPs, DNS names) |
||
| 1402 | hosts_allow (boolean, optional): If True allow only access to passed |
||
| 1403 | hosts otherwise deny access. Default is False for deny hosts. |
||
| 1404 | ifaces (list, optional): A list of interface names |
||
| 1405 | ifaces_allow (boolean, optional): If True allow only access to |
||
| 1406 | passed interfaces otherwise deny access. Default is False for |
||
| 1407 | deny interfaces. |
||
| 1408 | role_ids (list, optional): A list of role UUIDs for the user |
||
| 1409 | |||
| 1410 | Returns: |
||
| 1411 | The response. See :py:meth:`send_command` for details. |
||
| 1412 | """ |
||
| 1413 | if not name: |
||
| 1414 | raise RequiredArgument('create_user requires a name argument') |
||
| 1415 | |||
| 1416 | cmd = XmlCommand('create_user') |
||
| 1417 | cmd.add_element('name', name) |
||
| 1418 | |||
| 1419 | if password: |
||
| 1420 | cmd.add_element('password', password) |
||
| 1421 | |||
| 1422 | if hosts: |
||
| 1423 | cmd.add_element('hosts', ', '.join(hosts), |
||
| 1424 | attrs={'allow': '1' if hosts_allow else '0'}) |
||
| 1425 | |||
| 1426 | if ifaces: |
||
| 1427 | cmd.add_element('ifaces', ', '.join(ifaces), |
||
| 1428 | attrs={'allow': '1' if ifaces_allow else '0'}) |
||
| 1429 | |||
| 1430 | if role_ids: |
||
| 1431 | for role in role_ids: |
||
| 1432 | cmd.add_element('role', attrs={'id': role}) |
||
| 1433 | |||
| 1434 | return self._send_xml_command(cmd) |
||
| 1435 | |||
| 1436 | def clone_user(self, user_id): |
||
| 1437 | """Clone an existing user |
||
| 1438 | |||
| 1439 | Arguments: |
||
| 1440 | user_id (str): UUID of existing user to clone from |
||
| 1441 | |||
| 1442 | Returns: |
||
| 1443 | The response. See :py:meth:`send_command` for details. |
||
| 1444 | """ |
||
| 1445 | cmd = XmlCommand('create_user') |
||
| 1446 | cmd.add_element('copy', user_id) |
||
| 1447 | return self._send_xml_command(cmd) |
||
| 1448 | |||
| 1449 | def delete_agent(self, **kwargs): |
||
| 1450 | cmd = self._generator.delete_agent_command(kwargs) |
||
| 1451 | return self.send_command(cmd) |
||
| 1452 | |||
| 1453 | def delete_alert(self, **kwargs): |
||
| 1454 | cmd = self._generator.delete_alert_command(kwargs) |
||
| 1455 | return self.send_command(cmd) |
||
| 1456 | |||
| 1457 | def delete_asset(self, asset_id, ultimate=0): |
||
| 1458 | cmd = self._generator.delete_asset_command(asset_id, ultimate) |
||
| 1459 | return self.send_command(cmd) |
||
| 1460 | |||
| 1461 | def delete_config(self, config_id, ultimate=0): |
||
| 1462 | cmd = self._generator.delete_config_command(config_id, ultimate) |
||
| 1463 | return self.send_command(cmd) |
||
| 1464 | |||
| 1465 | def delete_credential(self, credential_id, ultimate=0): |
||
| 1466 | cmd = self._generator.delete_credential_command(credential_id, ultimate) |
||
| 1467 | return self.send_command(cmd) |
||
| 1468 | |||
| 1469 | def delete_filter(self, filter_id, ultimate=0): |
||
| 1470 | cmd = self._generator.delete_filter_command(filter_id, ultimate) |
||
| 1471 | return self.send_command(cmd) |
||
| 1472 | |||
| 1473 | def delete_group(self, group_id, ultimate=0): |
||
| 1474 | cmd = self._generator.delete_group_command(group_id, ultimate) |
||
| 1475 | return self.send_command(cmd) |
||
| 1476 | |||
| 1477 | def delete_note(self, note_id, ultimate=0): |
||
| 1478 | cmd = self._generator.delete_note_command(note_id, ultimate) |
||
| 1479 | return self.send_command(cmd) |
||
| 1480 | |||
| 1481 | def delete_override(self, override_id, ultimate=0): |
||
| 1482 | cmd = self._generator.delete_override_command(override_id, ultimate) |
||
| 1483 | return self.send_command(cmd) |
||
| 1484 | |||
| 1485 | def delete_permission(self, permission_id, ultimate=0): |
||
| 1486 | cmd = self._generator.delete_permission_command(permission_id, ultimate) |
||
| 1487 | return self.send_command(cmd) |
||
| 1488 | |||
| 1489 | def delete_port_list(self, port_list_id, ultimate=0): |
||
| 1490 | cmd = self._generator.delete_port_list_command(port_list_id, ultimate) |
||
| 1491 | return self.send_command(cmd) |
||
| 1492 | |||
| 1493 | def delete_port_range(self, port_range_id): |
||
| 1494 | cmd = self._generator.delete_port_range_command(port_range_id) |
||
| 1495 | return self.send_command(cmd) |
||
| 1496 | |||
| 1497 | def delete_report(self, report_id): |
||
| 1498 | cmd = self._generator.delete_report_command(report_id) |
||
| 1499 | return self.send_command(cmd) |
||
| 1500 | |||
| 1501 | def delete_report_format(self, report_format_id, ultimate=0): |
||
| 1502 | cmd = self._generator.delete_report_format_command( |
||
| 1503 | report_format_id, ultimate) |
||
| 1504 | return self.send_command(cmd) |
||
| 1505 | |||
| 1506 | def delete_role(self, role_id, ultimate=0): |
||
| 1507 | cmd = self._generator.delete_role_command(role_id, ultimate) |
||
| 1508 | return self.send_command(cmd) |
||
| 1509 | |||
| 1510 | def delete_scanner(self, scanner_id, ultimate=0): |
||
| 1511 | cmd = self._generator.delete_scanner_command(scanner_id, ultimate) |
||
| 1512 | return self.send_command(cmd) |
||
| 1513 | |||
| 1514 | def delete_schedule(self, schedule_id, ultimate=0): |
||
| 1515 | cmd = self._generator.delete_schedule_command(schedule_id, ultimate) |
||
| 1516 | return self.send_command(cmd) |
||
| 1517 | |||
| 1518 | def delete_tag(self, tag_id, ultimate=0): |
||
| 1519 | cmd = self._generator.delete_tag_command(tag_id, ultimate) |
||
| 1520 | return self.send_command(cmd) |
||
| 1521 | |||
| 1522 | def delete_target(self, target_id, ultimate=0): |
||
| 1523 | cmd = self._generator.delete_target_command(target_id, ultimate) |
||
| 1524 | return self.send_command(cmd) |
||
| 1525 | |||
| 1526 | def delete_task(self, task_id, ultimate=0): |
||
| 1527 | cmd = self._generator.delete_task_command(task_id, ultimate) |
||
| 1528 | return self.send_command(cmd) |
||
| 1529 | |||
| 1530 | def delete_user(self, **kwargs): |
||
| 1531 | cmd = self._generator.delete_user_command(kwargs) |
||
| 1532 | return self.send_command(cmd) |
||
| 1533 | |||
| 1534 | def describe_auth(self): |
||
| 1535 | """Describe authentication methods |
||
| 1536 | |||
| 1537 | Returns a list of all used authentication methods if such a list is |
||
| 1538 | available. |
||
| 1539 | |||
| 1540 | Returns: |
||
| 1541 | The response. See :py:meth:`send_command` for details. |
||
| 1542 | """ |
||
| 1543 | return self._send_xml_command(XmlCommand('describe_auth')) |
||
| 1544 | |||
| 1545 | def empty_trashcan(self): |
||
| 1546 | """Empty the trashcan |
||
| 1547 | |||
| 1548 | Remove all entities from the trashcan. **Attention:** this command can |
||
| 1549 | not be reverted |
||
| 1550 | |||
| 1551 | Returns: |
||
| 1552 | The response. See :py:meth:`send_command` for details. |
||
| 1553 | """ |
||
| 1554 | return self._send_xml_command(XmlCommand('empty_trashcan')) |
||
| 1555 | |||
| 1556 | def get_agents(self, filter=None, filter_id=None, trash=None, details=None, |
||
| 1557 | format=None): |
||
| 1558 | """Request a list of agents |
||
| 1559 | |||
| 1560 | Arguments: |
||
| 1561 | filter (str, optional): Filter term to use for the query |
||
| 1562 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1563 | the query |
||
| 1564 | trash (boolean, optional): True to request the filters in the |
||
| 1565 | trashcan |
||
| 1566 | details (boolean, optional): Whether to include agents package |
||
| 1567 | information when no format was provided |
||
| 1568 | format (str, optional): One of "installer", "howto_install" or |
||
| 1569 | "howto_use" |
||
| 1570 | |||
| 1571 | Returns: |
||
| 1572 | The response. See :py:meth:`send_command` for details. |
||
| 1573 | """ |
||
| 1574 | cmd = XmlCommand('get_agents') |
||
| 1575 | |||
| 1576 | _add_filter(cmd, filter, filter_id) |
||
| 1577 | |||
| 1578 | if not trash is None: |
||
| 1579 | cmd.set_attribute('trash', _to_bool(trash)) |
||
| 1580 | |||
| 1581 | if not details is None: |
||
| 1582 | cmd.set_attribute('details', _to_bool(details)) |
||
| 1583 | |||
| 1584 | if format: |
||
| 1585 | if not format in ('installer', 'howto_install', 'howto_use'): |
||
| 1586 | raise InvalidArgument( |
||
| 1587 | 'installer argument needs to be one of installer, ' |
||
| 1588 | 'howto_install or howto_use') |
||
| 1589 | |||
| 1590 | cmd.set_attribute('format', format) |
||
| 1591 | |||
| 1592 | return self._send_xml_command(cmd) |
||
| 1593 | |||
| 1594 | def get_agent(self, agent_id): |
||
| 1595 | """Request a single agent |
||
| 1596 | |||
| 1597 | Arguments: |
||
| 1598 | agent_id (str): UUID of an existing agent |
||
| 1599 | |||
| 1600 | Returns: |
||
| 1601 | The response. See :py:meth:`send_command` for details. |
||
| 1602 | """ |
||
| 1603 | cmd = XmlCommand('get_agents') |
||
| 1604 | cmd.set_attribute('agent_id', agent_id) |
||
| 1605 | return self._send_xml_command(cmd) |
||
| 1606 | |||
| 1607 | def get_aggregates(self, **kwargs): |
||
| 1608 | cmd = XmlCommand('get_aggregates') |
||
| 1609 | cmd.set_attributes(kwargs) |
||
| 1610 | return self._send_xml_command(cmd) |
||
| 1611 | |||
| 1612 | def get_alerts(self, filter=None, filter_id=None, trash=None, tasks=None): |
||
| 1613 | """Request a list of alerts |
||
| 1614 | |||
| 1615 | Arguments: |
||
| 1616 | filter (str, optional): Filter term to use for the query |
||
| 1617 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1618 | the query |
||
| 1619 | trash (boolean, optional): True to request the alerts in the |
||
| 1620 | trashcan |
||
| 1621 | tasks (boolean, optional): Whether to include the tasks using the |
||
| 1622 | alerts |
||
| 1623 | Returns: |
||
| 1624 | The response. See :py:meth:`send_command` for details. |
||
| 1625 | """ |
||
| 1626 | cmd = XmlCommand('get_alerts') |
||
| 1627 | |||
| 1628 | _add_filter(cmd, filter, filter_id) |
||
| 1629 | |||
| 1630 | if not trash is None: |
||
| 1631 | cmd.set_attribute('trash', _to_bool(trash)) |
||
| 1632 | |||
| 1633 | if not tasks is None: |
||
| 1634 | cmd.set_attribute('tasks', _to_bool(tasks)) |
||
| 1635 | |||
| 1636 | return self._send_xml_command(cmd) |
||
| 1637 | |||
| 1638 | def get_alert(self, alert_id): |
||
| 1639 | """Request a single alert |
||
| 1640 | |||
| 1641 | Arguments: |
||
| 1642 | alert_id (str): UUID of an existing alert |
||
| 1643 | |||
| 1644 | Returns: |
||
| 1645 | The response. See :py:meth:`send_command` for details. |
||
| 1646 | """ |
||
| 1647 | cmd = XmlCommand('get_alerts') |
||
| 1648 | cmd.set_attribute('alert_id', alert_id) |
||
| 1649 | return self._send_xml_command(cmd) |
||
| 1650 | |||
| 1651 | def get_assets(self, filter=None, filter_id=None): |
||
| 1652 | """Request a list of assets |
||
| 1653 | |||
| 1654 | Arguments: |
||
| 1655 | filter (str, optional): Filter term to use for the query |
||
| 1656 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1657 | the query |
||
| 1658 | |||
| 1659 | Returns: |
||
| 1660 | The response. See :py:meth:`send_command` for details. |
||
| 1661 | """ |
||
| 1662 | cmd = XmlCommand('get_assets') |
||
| 1663 | |||
| 1664 | _add_filter(cmd, filter, filter_id) |
||
| 1665 | |||
| 1666 | return self._send_xml_command(cmd) |
||
| 1667 | |||
| 1668 | def get_asset(self, asset_id): |
||
| 1669 | """Request a single asset |
||
| 1670 | |||
| 1671 | Arguments: |
||
| 1672 | asset_id (str): UUID of an existing asset |
||
| 1673 | |||
| 1674 | Returns: |
||
| 1675 | The response. See :py:meth:`send_command` for details. |
||
| 1676 | """ |
||
| 1677 | cmd = XmlCommand('get_assets') |
||
| 1678 | cmd.set_attribute('asset_id', asset_id) |
||
| 1679 | return self._send_xml_command(cmd) |
||
| 1680 | |||
| 1681 | def get_credentials(self, filter=None, filter_id=None, scanners=None, |
||
| 1682 | trash=None, targets=None, format=None): |
||
| 1683 | """Request a list of credentials |
||
| 1684 | |||
| 1685 | Arguments: |
||
| 1686 | filter (str, optional): Filter term to use for the query |
||
| 1687 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1688 | the query |
||
| 1689 | scanners (boolean, optional): Whether to include a list of scanners |
||
| 1690 | using the credentials |
||
| 1691 | trash (boolean, optional): Whether to get the trashcan credentials |
||
| 1692 | instead |
||
| 1693 | targets (boolean, optional): Whether to include a list of targets |
||
| 1694 | using the credentials |
||
| 1695 | format (str, optional): One of "key", "rpm", "deb" or "exe" |
||
| 1696 | |||
| 1697 | Returns: |
||
| 1698 | The response. See :py:meth:`send_command` for details. |
||
| 1699 | """ |
||
| 1700 | cmd = XmlCommand('get_credentials') |
||
| 1701 | |||
| 1702 | _add_filter(cmd, filter, filter_id) |
||
| 1703 | |||
| 1704 | if not scanners is None: |
||
| 1705 | cmd.set_attribute('scanners', _to_bool(scanners)) |
||
| 1706 | |||
| 1707 | if not trash is None: |
||
| 1708 | cmd.set_attribute('trash', _to_bool(trash)) |
||
| 1709 | |||
| 1710 | if not targets is None: |
||
| 1711 | cmd.set_attribute('targets', _to_bool(targets)) |
||
| 1712 | |||
| 1713 | if format: |
||
| 1714 | if not format in ('key', 'rpm', 'deb', 'exe'): |
||
| 1715 | raise InvalidArgument( |
||
| 1716 | 'format argument needs to one of key, rpm, deb or exe') |
||
| 1717 | |||
| 1718 | cmd.set_attribute('format', format) |
||
| 1719 | |||
| 1720 | return self.send_command(cmd) |
||
| 1721 | |||
| 1722 | def get_credential(self, credential_id): |
||
| 1723 | """Request a single credential |
||
| 1724 | |||
| 1725 | Arguments: |
||
| 1726 | credential_id (str): UUID of an existing credential |
||
| 1727 | |||
| 1728 | Returns: |
||
| 1729 | The response. See :py:meth:`send_command` for details. |
||
| 1730 | """ |
||
| 1731 | cmd = XmlCommand('get_credentials') |
||
| 1732 | cmd.set_attribute('credential_id', credential_id) |
||
| 1733 | return self._send_xml_command(cmd) |
||
| 1734 | |||
| 1735 | def get_configs(self, filter=None, filter_id=None, trash=None, details=None, |
||
| 1736 | families=None, preferences=None, tasks=None): |
||
| 1737 | """Request a list of scan configs |
||
| 1738 | |||
| 1739 | Arguments: |
||
| 1740 | filter (str, optional): Filter term to use for the query |
||
| 1741 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1742 | the query |
||
| 1743 | trash (boolean, optional): Whether to get the trashcan scan configs |
||
| 1744 | instead |
||
| 1745 | details (boolean, optional): Whether to get config families, |
||
| 1746 | preferences, nvt selectors and tasks. |
||
| 1747 | families (boolean, optional): Whether to include the families if no |
||
| 1748 | details are requested |
||
| 1749 | preferences (boolean, optional): Whether to include the preferences |
||
| 1750 | if no details are requested |
||
| 1751 | tasks (boolean, optional): Whether to get tasks using this config |
||
| 1752 | |||
| 1753 | Returns: |
||
| 1754 | The response. See :py:meth:`send_command` for details. |
||
| 1755 | """ |
||
| 1756 | cmd = XmlCommand('get_credentials') |
||
| 1757 | |||
| 1758 | _add_filter(cmd, filter, filter_id) |
||
| 1759 | |||
| 1760 | if not trash is None: |
||
| 1761 | cmd.set_attribute('trash', _to_bool(trash)) |
||
| 1762 | |||
| 1763 | if not details is None: |
||
| 1764 | cmd.set_attribute('details', _to_bool(details)) |
||
| 1765 | |||
| 1766 | if not families is None: |
||
| 1767 | cmd.set_attribute('families', _to_bool(families)) |
||
| 1768 | |||
| 1769 | if not preferences is None: |
||
| 1770 | cmd.set_attribute('preferences', _to_bool(preferences)) |
||
| 1771 | |||
| 1772 | if not tasks is None: |
||
| 1773 | cmd.set_attribute('tasks', _to_bool(tasks)) |
||
| 1774 | |||
| 1775 | return self._send_xml_command(cmd) |
||
| 1776 | |||
| 1777 | def get_config(self, config_id): |
||
| 1778 | """Request a single scan config |
||
| 1779 | |||
| 1780 | Arguments: |
||
| 1781 | config_id (str): UUID of an existing scan config |
||
| 1782 | |||
| 1783 | Returns: |
||
| 1784 | The response. See :py:meth:`send_command` for details. |
||
| 1785 | """ |
||
| 1786 | cmd = XmlCommand('get_configs') |
||
| 1787 | cmd.set_attribute('config_id', config_id) |
||
| 1788 | return self._send_xml_command(cmd) |
||
| 1789 | |||
| 1790 | def get_feeds(self): |
||
| 1791 | """Request the list of feeds |
||
| 1792 | |||
| 1793 | Returns: |
||
| 1794 | The response. See :py:meth:`send_command` for details. |
||
| 1795 | """ |
||
| 1796 | return self._send_xml_command(XmlCommand('get_feeds')) |
||
| 1797 | |||
| 1798 | def get_feed(self, feed_type): |
||
| 1799 | """Request a single feed |
||
| 1800 | |||
| 1801 | Arguments: |
||
| 1802 | feed_type (str): Type of single feed to get: NVT, CERT or SCAP |
||
| 1803 | |||
| 1804 | Returns: |
||
| 1805 | The response. See :py:meth:`send_command` for details. |
||
| 1806 | """ |
||
| 1807 | feed_type = feed_type.upper() |
||
| 1808 | |||
| 1809 | if not feed_type in ('NVT', 'CERT', 'SCAP'): |
||
| 1810 | raise InvalidArgument( |
||
| 1811 | 'get_feed type arguments must be one of NVT, CERT or SCAP') |
||
| 1812 | |||
| 1813 | cmd = XmlCommand('get_feeds') |
||
| 1814 | cmd.set_attribute('type', feed_type) |
||
| 1815 | |||
| 1816 | return self._send_xml_command(cmd) |
||
| 1817 | |||
| 1818 | def get_filters(self, filter=None, filter_id=None, trash=None, alerts=None): |
||
| 1819 | """Request a list of filters |
||
| 1820 | |||
| 1821 | Arguments: |
||
| 1822 | filter (str, optional): Filter term to use for the query |
||
| 1823 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1824 | the query |
||
| 1825 | trash (boolean, optional): Whether to get the trashcan filters |
||
| 1826 | instead |
||
| 1827 | alerts (boolean, optional): Whether to include list of alerts that |
||
| 1828 | use the filter. |
||
| 1829 | |||
| 1830 | Returns: |
||
| 1831 | The response. See :py:meth:`send_command` for details. |
||
| 1832 | """ |
||
| 1833 | cmd = XmlCommand('get_filters') |
||
| 1834 | |||
| 1835 | _add_filter(cmd, filter, filter_id) |
||
| 1836 | |||
| 1837 | if not trash is None: |
||
| 1838 | cmd.set_attribute('trash', _to_bool(trash)) |
||
| 1839 | |||
| 1840 | if not alerts is None: |
||
| 1841 | cmd.set_attribute('alerts', _to_bool(alerts)) |
||
| 1842 | |||
| 1843 | return self._send_xml_command(cmd) |
||
| 1844 | |||
| 1845 | def get_filter(self, filter_id): |
||
| 1846 | """Request a single filter |
||
| 1847 | |||
| 1848 | Arguments: |
||
| 1849 | filter_id (str): UUID of an existing filter |
||
| 1850 | |||
| 1851 | Returns: |
||
| 1852 | The response. See :py:meth:`send_command` for details. |
||
| 1853 | """ |
||
| 1854 | cmd = XmlCommand('get_filters') |
||
| 1855 | cmd.set_attribute('filter_id', filter_id) |
||
| 1856 | return self._send_xml_command(cmd) |
||
| 1857 | |||
| 1858 | def get_groups(self, filter=None, filter_id=None, trash=None): |
||
| 1859 | """Request a list of groups |
||
| 1860 | |||
| 1861 | Arguments: |
||
| 1862 | filter (str, optional): Filter term to use for the query |
||
| 1863 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1864 | the query |
||
| 1865 | trash (boolean, optional): Whether to get the trashcan groups |
||
| 1866 | instead |
||
| 1867 | Returns: |
||
| 1868 | The response. See :py:meth:`send_command` for details. |
||
| 1869 | """ |
||
| 1870 | cmd = XmlCommand('get_groups') |
||
| 1871 | |||
| 1872 | _add_filter(cmd, filter, filter_id) |
||
| 1873 | |||
| 1874 | if not trash is None: |
||
| 1875 | cmd.set_attribute('trash', _to_bool(trash)) |
||
| 1876 | |||
| 1877 | return self._send_xml_command(cmd) |
||
| 1878 | |||
| 1879 | def get_group(self, group_id): |
||
| 1880 | """Request a single group |
||
| 1881 | |||
| 1882 | Arguments: |
||
| 1883 | group_id (str): UUID of an existing group |
||
| 1884 | |||
| 1885 | Returns: |
||
| 1886 | The response. See :py:meth:`send_command` for details. |
||
| 1887 | """ |
||
| 1888 | cmd = XmlCommand('get_groups') |
||
| 1889 | cmd.set_attribute('group_id', group_id) |
||
| 1890 | return self._send_xml_command(cmd) |
||
| 1891 | |||
| 1892 | def get_info_list(self, info_type, filter=None, filter_id=None, |
||
| 1893 | name=None, details=None): |
||
| 1894 | """Request a list of security information |
||
| 1895 | |||
| 1896 | Arguments: |
||
| 1897 | info_type (str): Type must be either CERT_BUND_ADV, CPE, CVE, |
||
| 1898 | DFN_CERT_ADV, OVALDEF, NVT or ALLINFO |
||
| 1899 | filter (str, optional): Filter term to use for the query |
||
| 1900 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1901 | the query |
||
| 1902 | name (str, optional): Name or identifier of the requested |
||
| 1903 | information |
||
| 1904 | details (boolean, optional): Whether to include information about |
||
| 1905 | references to this information |
||
| 1906 | |||
| 1907 | Returns: |
||
| 1908 | The response. See :py:meth:`send_command` for details. |
||
| 1909 | """ |
||
| 1910 | info_type = info_type.upper() |
||
| 1911 | |||
| 1912 | if not info_type in ( |
||
| 1913 | 'CERT_BUND_ADV', 'CPE', 'CVE', 'DFN_CERT_ADV', 'OVALDEF', 'NVT', |
||
| 1914 | 'ALLINFO'): |
||
| 1915 | raise InvalidArgument( |
||
| 1916 | 'get_info_list info_type argument must be one of CERT_BUND_ADV' |
||
| 1917 | ', CPE, CVE, DFN_CERT_ADV, OVALDEF, NVT or ALLINFO') |
||
| 1918 | |||
| 1919 | cmd = XmlCommand('get_groups') |
||
| 1920 | |||
| 1921 | _add_filter(cmd, filter, filter_id) |
||
| 1922 | |||
| 1923 | if name: |
||
| 1924 | cmd.set_attribute('name', name) |
||
| 1925 | |||
| 1926 | if not details is None: |
||
| 1927 | cmd.set_attribute('details', _to_bool(details)) |
||
| 1928 | |||
| 1929 | return self._send_xml_command(cmd) |
||
| 1930 | |||
| 1931 | def get_info(self, info_id): |
||
| 1932 | """Request a single secinfo |
||
| 1933 | |||
| 1934 | Arguments: |
||
| 1935 | info_id (str): UUID of an existing secinfo |
||
| 1936 | |||
| 1937 | Returns: |
||
| 1938 | The response. See :py:meth:`send_command` for details. |
||
| 1939 | """ |
||
| 1940 | cmd = XmlCommand('get_infos') |
||
| 1941 | cmd.set_attribute('info_id', info_id) |
||
| 1942 | return self._send_xml_command(cmd) |
||
| 1943 | |||
| 1944 | def get_notes(self, filter=None, filter_id=None, nvt_oid=None, |
||
| 1945 | task_id=None, details=None, result=None): |
||
| 1946 | """Request a list of notes |
||
| 1947 | |||
| 1948 | Arguments: |
||
| 1949 | filter (str, optional): Filter term to use for the query |
||
| 1950 | filter_id (str, optional): UUID of an existing filter to use for |
||
| 1951 | the query |
||
| 1952 | nvt_oid (str, optional): OID of a nvt |
||
| 1953 | task_id (str, optional): UUID of a task |
||
| 1954 | details (boolean, optional): |
||
| 1955 | result (boolean, optional): |
||
| 1956 | |||
| 1957 | Returns: |
||
| 1958 | The response. See :py:meth:`send_command` for details. |
||
| 1959 | """ |
||
| 1960 | cmd = XmlCommand('get_notes') |
||
| 1961 | |||
| 1962 | _add_filter(cmd, filter, filter_id) |
||
| 1963 | |||
| 1964 | if nvt_oid: |
||
| 1965 | cmd.set_attribute('nvt_oid', nvt_oid) |
||
| 1966 | |||
| 1967 | if task_id: |
||
| 1968 | cmd.set_attribute('task_id', task_id) |
||
| 1969 | |||
| 1970 | if not details is None: |
||
| 1971 | cmd.set_attribute('details', _to_bool(details)) |
||
| 1972 | |||
| 1973 | if not result is None: |
||
| 1974 | cmd.set_attribute('result', _to_bool(result)) |
||
| 1975 | |||
| 1976 | return self._send_xml_command(cmd) |
||
| 1977 | |||
| 1978 | def get_note(self, note_id): |
||
| 1979 | """Request a single note |
||
| 1980 | |||
| 1981 | Arguments: |
||
| 1982 | note_id (str): UUID of an existing note |
||
| 1983 | |||
| 1984 | Returns: |
||
| 1985 | The response. See :py:meth:`send_command` for details. |
||
| 1986 | """ |
||
| 1987 | cmd = XmlCommand('get_notes') |
||
| 1988 | cmd.set_attribute('note_id', note_id) |
||
| 1989 | return self._send_xml_command(cmd) |
||
| 1990 | |||
| 1991 | def get_nvts(self, details=None, preferences=None, preference_count=None, |
||
| 1992 | timeout=None, config_id=None, preferences_config_id=None, |
||
| 1993 | family=None, sort_order=None, sort_field=None): |
||
| 1994 | """Request a list of nvts |
||
| 1995 | |||
| 1996 | Arguments: |
||
| 1997 | details (boolean, optional): Whether to include full details |
||
| 1998 | preferences (boolean, optional): Whether to include nvt preferences |
||
| 1999 | preference_count (boolean, optional): Whether to include preference |
||
| 2000 | count |
||
| 2001 | timeout (boolean, optional): Whether to include the special timeout |
||
| 2002 | preference |
||
| 2003 | config_id (str, optional): UUID of scan config to which to limit the |
||
| 2004 | NVT listing |
||
| 2005 | preferences_config_id (str, optional): UUID of scan config to use |
||
| 2006 | for preference values |
||
| 2007 | family (str, optional): Family to which to limit NVT listing |
||
| 2008 | sort_order (str, optional): Sort order |
||
| 2009 | sort_field (str, optional): Sort field |
||
| 2010 | |||
| 2011 | Returns: |
||
| 2012 | The response. See :py:meth:`send_command` for details. |
||
| 2013 | """ |
||
| 2014 | cmd = XmlCommand('get_notes') |
||
| 2015 | |||
| 2016 | if not details is None: |
||
| 2017 | cmd.set_attribute('details', _to_bool(details)) |
||
| 2018 | |||
| 2019 | if not preferences is None: |
||
| 2020 | cmd.set_attribute('preferences', _to_bool(preferences)) |
||
| 2021 | |||
| 2022 | if not preference_count is None: |
||
| 2023 | cmd.set_attribute('preference_count', _to_bool(preference_count)) |
||
| 2024 | |||
| 2025 | if not timeout is None: |
||
| 2026 | cmd.set_attribute('timeout', _to_bool(timeout)) |
||
| 2027 | |||
| 2028 | if config_id: |
||
| 2029 | cmd.set_attribute('config_id', config_id) |
||
| 2030 | |||
| 2031 | if preferences_config_id: |
||
| 2032 | cmd.set_attribute('preferences_config_id', preferences_config_id) |
||
| 2033 | |||
| 2034 | if family: |
||
| 2035 | cmd.set_attribute('family', family) |
||
| 2036 | |||
| 2037 | if sort_order: |
||
| 2038 | cmd.set_attribute('sort_order', sort_order) |
||
| 2039 | |||
| 2040 | if sort_field: |
||
| 2041 | cmd.set_attribute('sort_field', sort_field) |
||
| 2042 | |||
| 2043 | return self._send_xml_command(cmd) |
||
| 2044 | |||
| 2045 | def get_nvt(self, nvt_id): |
||
| 2046 | """Request a single nvt |
||
| 2047 | |||
| 2048 | Arguments: |
||
| 2049 | nvt_id (str): OID of an existing nvt |
||
| 2050 | |||
| 2051 | Returns: |
||
| 2052 | The response. See :py:meth:`send_command` for details. |
||
| 2053 | """ |
||
| 2054 | cmd = XmlCommand('get_nvts') |
||
| 2055 | cmd.set_attribute('nvt_id', nvt_id) |
||
| 2056 | return self._send_xml_command(cmd) |
||
| 2057 | |||
| 2058 | def get_nvt_families(self, **kwargs): |
||
| 2059 | cmd = self._generator.get_nvt_families_command(kwargs) |
||
| 2060 | return self.send_command(cmd) |
||
| 2061 | |||
| 2062 | def get_overrides(self, **kwargs): |
||
| 2063 | cmd = self._generator.get_overrides_command(kwargs) |
||
| 2064 | return self.send_command(cmd) |
||
| 2065 | |||
| 2066 | def get_override(self, override_id): |
||
| 2067 | """Request a single override |
||
| 2068 | |||
| 2069 | Arguments: |
||
| 2070 | override_id (str): UUID of an existing override |
||
| 2071 | |||
| 2072 | Returns: |
||
| 2073 | The response. See :py:meth:`send_command` for details. |
||
| 2074 | """ |
||
| 2075 | cmd = XmlCommand('get_overrides') |
||
| 2076 | cmd.set_attribute('override_id', override_id) |
||
| 2077 | return self._send_xml_command(cmd) |
||
| 2078 | |||
| 2079 | def get_permissions(self, **kwargs): |
||
| 2080 | cmd = self._generator.get_permissions_command(kwargs) |
||
| 2081 | return self.send_command(cmd) |
||
| 2082 | |||
| 2083 | def get_permission(self, permission_id): |
||
| 2084 | """Request a single permission |
||
| 2085 | |||
| 2086 | Arguments: |
||
| 2087 | permission_id (str): UUID of an existing permission |
||
| 2088 | |||
| 2089 | Returns: |
||
| 2090 | The response. See :py:meth:`send_command` for details. |
||
| 2091 | """ |
||
| 2092 | cmd = XmlCommand('get_permissions') |
||
| 2093 | cmd.set_attribute('permission_id', permission_id) |
||
| 2094 | return self._send_xml_command(cmd) |
||
| 2095 | |||
| 2096 | def get_port_lists(self, **kwargs): |
||
| 2097 | cmd = self._generator.get_port_lists_command(kwargs) |
||
| 2098 | return self.send_command(cmd) |
||
| 2099 | |||
| 2100 | def get_port_list(self, port_list_id): |
||
| 2101 | """Request a single port list |
||
| 2102 | |||
| 2103 | Arguments: |
||
| 2104 | port_list_id (str): UUID of an existing port list |
||
| 2105 | |||
| 2106 | Returns: |
||
| 2107 | The response. See :py:meth:`send_command` for details. |
||
| 2108 | """ |
||
| 2109 | cmd = XmlCommand('get_port_lists') |
||
| 2110 | cmd.set_attribute('port_list_id', port_list_id) |
||
| 2111 | return self._send_xml_command(cmd) |
||
| 2112 | |||
| 2113 | def get_preferences(self, **kwargs): |
||
| 2114 | cmd = self._generator.get_preferences_command(kwargs) |
||
| 2115 | return self.send_command(cmd) |
||
| 2116 | |||
| 2117 | def get_reports(self, **kwargs): |
||
| 2118 | cmd = self._generator.get_reports_command(kwargs) |
||
| 2119 | return self.send_command(cmd) |
||
| 2120 | |||
| 2121 | def get_report(self, report_id): |
||
| 2122 | """Request a single report |
||
| 2123 | |||
| 2124 | Arguments: |
||
| 2125 | report_id (str): UUID of an existing report |
||
| 2126 | |||
| 2127 | Returns: |
||
| 2128 | The response. See :py:meth:`send_command` for details. |
||
| 2129 | """ |
||
| 2130 | cmd = XmlCommand('get_reports') |
||
| 2131 | cmd.set_attribute('report_id', report_id) |
||
| 2132 | return self._send_xml_command(cmd) |
||
| 2133 | |||
| 2134 | def get_report_formats(self, **kwargs): |
||
| 2135 | cmd = self._generator.get_report_formats_command(kwargs) |
||
| 2136 | return self.send_command(cmd) |
||
| 2137 | |||
| 2138 | def get_report_format(self, report_format_id): |
||
| 2139 | """Request a single report format |
||
| 2140 | |||
| 2141 | Arguments: |
||
| 2142 | report_format_id (str): UUID of an existing report format |
||
| 2143 | |||
| 2144 | Returns: |
||
| 2145 | The response. See :py:meth:`send_command` for details. |
||
| 2146 | """ |
||
| 2147 | cmd = XmlCommand('get_report_formats') |
||
| 2148 | cmd.set_attribute('report_format_id', report_format_id) |
||
| 2149 | return self._send_xml_command(cmd) |
||
| 2150 | |||
| 2151 | def get_results(self, **kwargs): |
||
| 2152 | cmd = self._generator.get_results_command(kwargs) |
||
| 2153 | return self.send_command(cmd) |
||
| 2154 | |||
| 2155 | def get_result(self, result_id): |
||
| 2156 | """Request a single result |
||
| 2157 | |||
| 2158 | Arguments: |
||
| 2159 | result_id (str): UUID of an existing result |
||
| 2160 | |||
| 2161 | Returns: |
||
| 2162 | The response. See :py:meth:`send_command` for details. |
||
| 2163 | """ |
||
| 2164 | cmd = XmlCommand('get_results') |
||
| 2165 | cmd.set_attribute('result_id', result_id) |
||
| 2166 | return self._send_xml_command(cmd) |
||
| 2167 | |||
| 2168 | def get_roles(self, **kwargs): |
||
| 2169 | cmd = self._generator.get_roles_command(kwargs) |
||
| 2170 | return self.send_command(cmd) |
||
| 2171 | |||
| 2172 | def get_role(self, role_id): |
||
| 2173 | """Request a single role |
||
| 2174 | |||
| 2175 | Arguments: |
||
| 2176 | role_id (str): UUID of an existing role |
||
| 2177 | |||
| 2178 | Returns: |
||
| 2179 | The response. See :py:meth:`send_command` for details. |
||
| 2180 | """ |
||
| 2181 | cmd = XmlCommand('get_roles') |
||
| 2182 | cmd.set_attribute('role_id', role_id) |
||
| 2183 | return self._send_xml_command(cmd) |
||
| 2184 | |||
| 2185 | def get_scanners(self, **kwargs): |
||
| 2186 | cmd = self._generator.get_scanners_command(kwargs) |
||
| 2187 | return self.send_command(cmd) |
||
| 2188 | |||
| 2189 | def get_scanner(self, scanner_id): |
||
| 2190 | """Request a single scanner |
||
| 2191 | |||
| 2192 | Arguments: |
||
| 2193 | scanner_id (str): UUID of an existing scanner |
||
| 2194 | |||
| 2195 | Returns: |
||
| 2196 | The response. See :py:meth:`send_command` for details. |
||
| 2197 | """ |
||
| 2198 | cmd = XmlCommand('get_scanners') |
||
| 2199 | cmd.set_attribute('scanner_id', scanner_id) |
||
| 2200 | return self._send_xml_command(cmd) |
||
| 2201 | |||
| 2202 | def get_schedules(self, **kwargs): |
||
| 2203 | cmd = self._generator.get_schedules_command(kwargs) |
||
| 2204 | return self.send_command(cmd) |
||
| 2205 | |||
| 2206 | def get_schedule(self, schedule_id): |
||
| 2207 | """Request a single schedule |
||
| 2208 | |||
| 2209 | Arguments: |
||
| 2210 | schedule_id (str): UUID of an existing schedule |
||
| 2211 | |||
| 2212 | Returns: |
||
| 2213 | The response. See :py:meth:`send_command` for details. |
||
| 2214 | """ |
||
| 2215 | cmd = XmlCommand('get_schedules') |
||
| 2216 | cmd.set_attribute('schedule_id', schedule_id) |
||
| 2217 | return self._send_xml_command(cmd) |
||
| 2218 | |||
| 2219 | def get_settings(self, **kwargs): |
||
| 2220 | cmd = self._generator.get_settings_command(kwargs) |
||
| 2221 | return self.send_command(cmd) |
||
| 2222 | |||
| 2223 | def get_setting(self, setting_id): |
||
| 2224 | """Request a single setting |
||
| 2225 | |||
| 2226 | Arguments: |
||
| 2227 | setting_id (str): UUID of an existing setting |
||
| 2228 | |||
| 2229 | Returns: |
||
| 2230 | The response. See :py:meth:`send_command` for details. |
||
| 2231 | """ |
||
| 2232 | cmd = XmlCommand('get_settings') |
||
| 2233 | cmd.set_attribute('setting_id', setting_id) |
||
| 2234 | return self._send_xml_command(cmd) |
||
| 2235 | |||
| 2236 | def get_system_reports(self, **kwargs): |
||
| 2237 | cmd = self._generator.get_system_reports_command(kwargs) |
||
| 2238 | return self.send_command(cmd) |
||
| 2239 | |||
| 2240 | def get_tags(self, **kwargs): |
||
| 2241 | cmd = self._generator.get_tags_command(kwargs) |
||
| 2242 | return self.send_command(cmd) |
||
| 2243 | |||
| 2244 | def get_tag(self, tag_id): |
||
| 2245 | """Request a single tag |
||
| 2246 | |||
| 2247 | Arguments: |
||
| 2248 | tag_id (str): UUID of an existing tag |
||
| 2249 | |||
| 2250 | Returns: |
||
| 2251 | The response. See :py:meth:`send_command` for details. |
||
| 2252 | """ |
||
| 2253 | cmd = XmlCommand('get_tags') |
||
| 2254 | cmd.set_attribute('tag_id', tag_id) |
||
| 2255 | return self._send_xml_command(cmd) |
||
| 2256 | |||
| 2257 | def get_targets(self, **kwargs): |
||
| 2258 | cmd = self._generator.get_targets_command(kwargs) |
||
| 2259 | return self.send_command(cmd) |
||
| 2260 | |||
| 2261 | def get_target(self, target_id): |
||
| 2262 | """Request a single target |
||
| 2263 | |||
| 2264 | Arguments: |
||
| 2265 | target_id (str): UUID of an existing target |
||
| 2266 | |||
| 2267 | Returns: |
||
| 2268 | The response. See :py:meth:`send_command` for details. |
||
| 2269 | """ |
||
| 2270 | cmd = XmlCommand('get_targets') |
||
| 2271 | cmd.set_attribute('target_id', target_id) |
||
| 2272 | return self._send_xml_command(cmd) |
||
| 2273 | |||
| 2274 | def get_tasks(self, **kwargs): |
||
| 2275 | cmd = self._generator.get_tasks_command(kwargs) |
||
| 2276 | return self.send_command(cmd) |
||
| 2277 | |||
| 2278 | def get_task(self, task_id): |
||
| 2279 | """Request a single task |
||
| 2280 | |||
| 2281 | Arguments: |
||
| 2282 | task_id (str): UUID of an existing task |
||
| 2283 | |||
| 2284 | Returns: |
||
| 2285 | The response. See :py:meth:`send_command` for details. |
||
| 2286 | """ |
||
| 2287 | cmd = XmlCommand('get_tasks') |
||
| 2288 | cmd.set_attribute('task_id', task_id) |
||
| 2289 | return self._send_xml_command(cmd) |
||
| 2290 | |||
| 2291 | def get_users(self, **kwargs): |
||
| 2292 | cmd = self._generator.get_users_command(kwargs) |
||
| 2293 | return self.send_command(cmd) |
||
| 2294 | |||
| 2295 | def get_user(self, user_id): |
||
| 2296 | """Request a single user |
||
| 2297 | |||
| 2298 | Arguments: |
||
| 2299 | user_id (str): UUID of an existing user |
||
| 2300 | |||
| 2301 | Returns: |
||
| 2302 | The response. See :py:meth:`send_command` for details. |
||
| 2303 | """ |
||
| 2304 | cmd = XmlCommand('get_users') |
||
| 2305 | cmd.set_attribute('user_id', user_id) |
||
| 2306 | return self._send_xml_command(cmd) |
||
| 2307 | |||
| 2308 | def get_version(self): |
||
| 2309 | """Get the Greenbone Manager Protocol version used by the remote gvmd |
||
| 2310 | |||
| 2311 | Returns: |
||
| 2312 | The response. See :py:meth:`send_command` for details. |
||
| 2313 | """ |
||
| 2314 | return self._send_xml_command(XmlCommand('get_version')) |
||
| 2315 | |||
| 2316 | def help(self, format=None, type=''): |
||
| 2317 | """Get the help text |
||
| 2318 | |||
| 2319 | Arguments: |
||
| 2320 | format (str, optional): One of "html", "rnc", "text" or "xml |
||
| 2321 | type (str, optional): One of "brief" or "". Default "" |
||
| 2322 | |||
| 2323 | Returns: |
||
| 2324 | The response. See :py:meth:`send_command` for details. |
||
| 2325 | """ |
||
| 2326 | cmd = XmlCommand('help') |
||
| 2327 | |||
| 2328 | cmd.set_attribute('type', type) |
||
| 2329 | |||
| 2330 | if format: |
||
| 2331 | if not format.lower() in ('html', 'rnc', 'text', 'xml'): |
||
| 2332 | raise InvalidArgument( |
||
| 2333 | 'help format Argument must be one of html, rnc, text or ' |
||
| 2334 | 'xml') |
||
| 2335 | |||
| 2336 | cmd.set_attribute('format', format) |
||
| 2337 | |||
| 2338 | return self.send_command(cmd) |
||
| 2339 | |||
| 2340 | def modify_agent(self, agent_id, name=None, comment=None): |
||
| 2341 | """Modifies an existing agent |
||
| 2342 | |||
| 2343 | Arguments: |
||
| 2344 | agent_id (str) UUID of the agent to be modified. |
||
| 2345 | name (str, optional): Name of the new credential |
||
| 2346 | comment (str, optional): Comment for the credential |
||
| 2347 | """ |
||
| 2348 | if not agent_id: |
||
| 2349 | raise RequiredArgument('modify_agent requires agent_id argument') |
||
| 2350 | |||
| 2351 | cmd = XmlCommand('modify_agent') |
||
| 2352 | cmd.set_attribute('agent_id', str(agent_id)) |
||
| 2353 | if name: |
||
| 2354 | cmd.add_element('name', name) |
||
| 2355 | if comment: |
||
| 2356 | cmd.add_element('comment', comment) |
||
| 2357 | |||
| 2358 | return self._send_xml_command(cmd) |
||
| 2359 | |||
| 2360 | def modify_alert(self, alert_id, name=None, comment=None, |
||
| 2361 | filter_id=None, event=None, event_data=None, |
||
| 2362 | condition=None, condition_data=None, method=None, |
||
| 2363 | method_data=None): |
||
| 2364 | """Modifies an existing alert. |
||
| 2365 | |||
| 2366 | Arguments: |
||
| 2367 | alert_id (str) UUID of the alert to be modified. |
||
| 2368 | name (str, optional): Name of the Alert. |
||
| 2369 | condition (str, optional): The condition that must be satisfied |
||
| 2370 | for the alert to occur. |
||
| 2371 | condition_data (dict, optional): Data that defines the condition |
||
| 2372 | event (str, optional): The event that must happen for the alert |
||
| 2373 | to occur. |
||
| 2374 | event_data (dict, optional): Data that defines the event |
||
| 2375 | method (str, optional): The method by which the user is alerted |
||
| 2376 | method_data (dict, optional): Data that defines the method |
||
| 2377 | filter_id (str, optional): Filter to apply when executing alert |
||
| 2378 | comment (str, optional): Comment for the alert |
||
| 2379 | """ |
||
| 2380 | |||
| 2381 | if not alert_id: |
||
| 2382 | raise RequiredArgument('modify_alert requires an alert_id argument') |
||
| 2383 | |||
| 2384 | cmd = XmlCommand('modify_alert') |
||
| 2385 | cmd.set_attribute('alert_id', str(alert_id)) |
||
| 2386 | |||
| 2387 | if name: |
||
| 2388 | cmd.add_element('name', name) |
||
| 2389 | |||
| 2390 | if comment: |
||
| 2391 | cmd.add_element('comment', comment) |
||
| 2392 | |||
| 2393 | if filter_id: |
||
| 2394 | cmd.add_element('filter', attrs={'id': filter_id}) |
||
| 2395 | |||
| 2396 | conditions = cmd.add_element('condition', condition) |
||
| 2397 | |||
| 2398 | if not condition_data is None: |
||
| 2399 | for value, key in condition_data.items(): |
||
| 2400 | _data = conditions.add_element('data', value) |
||
| 2401 | _data.add_element('name', key) |
||
| 2402 | |||
| 2403 | events = cmd.add_element('event', event) |
||
| 2404 | |||
| 2405 | if not event_data is None: |
||
| 2406 | for value, key in event_data.items(): |
||
| 2407 | _data = events.add_element('data', value) |
||
| 2408 | _data.add_element('name', key) |
||
| 2409 | |||
| 2410 | methods = cmd.add_element('method', method) |
||
| 2411 | |||
| 2412 | if not method_data is None: |
||
| 2413 | for value, key in method_data.items(): |
||
| 2414 | _data = methods.add_element('data', value) |
||
| 2415 | _data.add_element('name', key) |
||
| 2416 | |||
| 2417 | return self._send_xml_command(cmd) |
||
| 2418 | |||
| 2419 | def modify_asset(self, asset_id, comment): |
||
| 2420 | """Modifies an existing asset. |
||
| 2421 | |||
| 2422 | Arguments: |
||
| 2423 | asset_id (str) UUID of the asset to be modified. |
||
| 2424 | comment (str, optional): Comment for the asset. |
||
| 2425 | """ |
||
| 2426 | if not asset_id: |
||
| 2427 | raise RequiredArgument('modify_asset requires an asset_id argument') |
||
| 2428 | |||
| 2429 | cmd = XmlCommand('modify_asset') |
||
| 2430 | cmd.set_attribute('asset_id', asset_id) |
||
| 2431 | cmd.add_element('comment', comment) |
||
| 2432 | |||
| 2433 | return self._send_xml_command(cmd) |
||
| 2434 | |||
| 2435 | def modify_auth(self, group_name, auth_conf_settings): |
||
| 2436 | """Modifies an existing auth. |
||
| 2437 | Arguments: |
||
| 2438 | group_name (str) Name of the group to be modified. |
||
| 2439 | auth_conf_settings (dict): The new auth config. |
||
| 2440 | """ |
||
| 2441 | if not group_name: |
||
| 2442 | raise RequiredArgument('modify_auth requires a group_name argument') |
||
| 2443 | if not auth_conf_settings: |
||
| 2444 | raise RequiredArgument('modify_auth requires an ' |
||
| 2445 | 'auth_conf_settings argument') |
||
| 2446 | cmd = XmlCommand('modify_auth') |
||
| 2447 | _xmlgroup = cmd.add_element('group', attrs={'name': str(group_name)}) |
||
| 2448 | |||
| 2449 | for key, value in auth_conf_settings.items(): |
||
| 2450 | _xmlauthconf = _xmlgroup.add_element('auth_conf_setting') |
||
| 2451 | _xmlauthconf.add_element('key', key) |
||
| 2452 | _xmlauthconf.add_element('value', value) |
||
| 2453 | |||
| 2454 | return self._send_xml_command(cmd) |
||
| 2455 | |||
| 2456 | def modify_config(self, selection, config_id=None, nvt_oids=None, name=None, |
||
| 2457 | value=None, family=None): |
||
| 2458 | """Modifies an existing scan config. |
||
| 2459 | |||
| 2460 | Arguments: |
||
| 2461 | selection (str): one of 'nvt_pref', nvt_selection or |
||
| 2462 | family_selection' |
||
| 2463 | config_id (str, optional): UUID of scan config to modify. |
||
| 2464 | name (str, optional): New name for preference. |
||
| 2465 | value(str, optional): New value for preference. |
||
| 2466 | nvt_oids (list, optional): List of NVTs associated with preference |
||
| 2467 | to modify. |
||
| 2468 | family (str,optional): Name of family to modify. |
||
| 2469 | """ |
||
| 2470 | if selection not in ('nvt_pref', 'scan_pref', |
||
| 2471 | 'family_selection', 'nvt_selection'): |
||
| 2472 | raise InvalidArgument('selection must be one of nvt_pref, ' |
||
| 2473 | 'scan_pref, family_selection or ' |
||
| 2474 | 'nvt_selection') |
||
| 2475 | |||
| 2476 | cmd = XmlCommand('modify_config') |
||
| 2477 | cmd.set_attribute('config_id', str(config_id)) |
||
| 2478 | |||
| 2479 | if selection == 'nvt_pref': |
||
| 2480 | _xmlpref = cmd.add_element('preference') |
||
| 2481 | if not nvt_oids: |
||
| 2482 | raise InvalidArgument('modify_config requires a nvt_oids ' |
||
| 2483 | 'argument') |
||
| 2484 | _xmlpref.add_element('nvt', attrs={'oid': nvt_oids[0]}) |
||
| 2485 | _xmlpref.add_element('name', name) |
||
| 2486 | _xmlpref.add_element('value', value) |
||
| 2487 | |||
| 2488 | elif selection == 'nvt_selection': |
||
| 2489 | _xmlnvtsel = cmd.add_element('nvt_selection') |
||
| 2490 | _xmlnvtsel.add_element('family', family) |
||
| 2491 | |||
| 2492 | if nvt_oids: |
||
| 2493 | for nvt in nvt_oids: |
||
| 2494 | _xmlnvtsel.add_element('nvt', attrs={'oid': nvt}) |
||
| 2495 | else: |
||
| 2496 | raise InvalidArgument('modify_config requires a nvt_oid ' |
||
| 2497 | 'argument') |
||
| 2498 | |||
| 2499 | elif selection == 'family_selection': |
||
| 2500 | _xmlfamsel = cmd.add_element('family_selection') |
||
| 2501 | _xmlfamsel.add_element('growing', '1') |
||
| 2502 | _xmlfamily = _xmlfamsel.add_element('family') |
||
| 2503 | _xmlfamily.add_element('name', family) |
||
| 2504 | _xmlfamily.add_element('all', '1') |
||
| 2505 | _xmlfamily.add_element('growing', '1') |
||
| 2506 | |||
| 2507 | return self._send_xml_command(cmd) |
||
| 2508 | |||
| 2509 | def modify_credential(self, credential_id, name=None, comment=None, |
||
| 2510 | allow_insecure=None, certificate=None, |
||
| 2511 | key_phrase=None, private_key=None, login=None, |
||
| 2512 | password=None, auth_algorithm=None, community=None, |
||
| 2513 | privacy_algorithm=None, privacy_password=None, |
||
| 2514 | credential_type=None): |
||
| 2515 | """Modifies an existing credential. |
||
| 2516 | |||
| 2517 | Arguments: |
||
| 2518 | credential_id (str): UUID of the credential |
||
| 2519 | name (str, optional): Name of the credential |
||
| 2520 | comment (str, optional): Comment for the credential |
||
| 2521 | allow_insecure (boolean, optional): Whether to allow insecure use of |
||
| 2522 | the credential |
||
| 2523 | certificate (str, optional): Certificate for the credential |
||
| 2524 | key_phrase (str, optional): Key passphrase for the private key |
||
| 2525 | private_key (str, optional): Private key to use for login |
||
| 2526 | login (str, optional): Username for the credential |
||
| 2527 | password (str, optional): Password for the credential |
||
| 2528 | auth_algorithm (str, optional): The auth_algorithm, |
||
| 2529 | either md5 or sha1. |
||
| 2530 | community (str, optional): The SNMP community |
||
| 2531 | privacy_algorithm (str, optional): The SNMP privacy algorithm, |
||
| 2532 | either aes or des. |
||
| 2533 | privacy_password (str, optional): The SNMP privacy password |
||
| 2534 | credential_type (str, optional): The credential type. One of 'cc', |
||
| 2535 | 'snmp', 'up', 'usk' |
||
| 2536 | """ |
||
| 2537 | if not credential_id: |
||
| 2538 | raise RequiredArgument('modify_credential requires ' |
||
| 2539 | 'a credential_id attribute') |
||
| 2540 | |||
| 2541 | cmd = XmlCommand('modify_credential') |
||
| 2542 | cmd.set_attribute('credential_id', credential_id) |
||
| 2543 | |||
| 2544 | if comment: |
||
| 2545 | cmd.add_element('comment', comment) |
||
| 2546 | |||
| 2547 | if name: |
||
| 2548 | cmd.add_element('name', name) |
||
| 2549 | |||
| 2550 | if allow_insecure: |
||
| 2551 | cmd.add_element('allow_insecure', allow_insecure) |
||
| 2552 | |||
| 2553 | if certificate: |
||
| 2554 | cmd.add_element('certificate', certificate) |
||
| 2555 | |||
| 2556 | if key_phrase or private_key: |
||
| 2557 | if not key_phrase or not private_key: |
||
| 2558 | raise RequiredArgument('modify_credential requires ' |
||
| 2559 | 'a key_phrase and private_key arguments') |
||
| 2560 | _xmlkey = cmd.add_element('key') |
||
| 2561 | _xmlkey.add_element('phrase', key_phrase) |
||
| 2562 | _xmlkey.add_element('private', private_key) |
||
| 2563 | |||
| 2564 | if login: |
||
| 2565 | cmd.add_element('login', login) |
||
| 2566 | |||
| 2567 | if password: |
||
| 2568 | cmd.add_element('password', password) |
||
| 2569 | |||
| 2570 | if auth_algorithm: |
||
| 2571 | if auth_algorithm not in ('md5', 'sha1'): |
||
| 2572 | raise RequiredArgument('modify_credential requires ' |
||
| 2573 | 'auth_algorithm to be either ' |
||
| 2574 | 'md5 or sha1') |
||
| 2575 | cmd.add_element('auth_algorithm', auth_algorithm) |
||
| 2576 | |||
| 2577 | if community: |
||
| 2578 | cmd.add_element('community', community) |
||
| 2579 | |||
| 2580 | if privacy_algorithm: |
||
| 2581 | if privacy_algorithm not in ('aes', 'des'): |
||
| 2582 | raise RequiredArgument('modify_credential requires ' |
||
| 2583 | 'privacy_algorithm to be either' |
||
| 2584 | 'aes or des') |
||
| 2585 | _xmlprivacy = cmd.add_element('privacy') |
||
| 2586 | _xmlprivacy.add_element('algorithm', privacy_algorithm) |
||
| 2587 | _xmlprivacy.add_element('password', privacy_password) |
||
| 2588 | |||
| 2589 | if credential_type: |
||
| 2590 | if credential_type not in ('cc', 'snmp', 'up', 'usk'): |
||
| 2591 | raise RequiredArgument('modify_credential requires type ' |
||
| 2592 | 'to be either cc, snmp, up or usk') |
||
| 2593 | cmd.add_element('type', credential_type) |
||
| 2594 | |||
| 2595 | return self._send_xml_command(cmd) |
||
| 2596 | |||
| 2597 | def modify_filter(self, filter_id, comment=None, name=None, term=None, |
||
| 2598 | filter_type=None): |
||
| 2599 | """Modifies an existing filter. |
||
| 2600 | |||
| 2601 | Arguments: |
||
| 2602 | filter_id (str): UUID of the filter to be modified |
||
| 2603 | comment (str, optional): Comment on filter. |
||
| 2604 | name (str, optional): Name of filter. |
||
| 2605 | term (str, optional): Filter term. |
||
| 2606 | filter_type (str, optional): Resource type filter applies to. |
||
| 2607 | """ |
||
| 2608 | if not filter_id: |
||
| 2609 | raise RequiredArgument('modify_filter requires a filter_id ' |
||
| 2610 | 'attribute') |
||
| 2611 | |||
| 2612 | cmd = XmlCommand('modify_filter') |
||
| 2613 | cmd.set_attribute('filter_id', filter_id) |
||
| 2614 | |||
| 2615 | if comment: |
||
| 2616 | cmd.add_element('comment', comment) |
||
| 2617 | |||
| 2618 | if name: |
||
| 2619 | cmd.add_element('name', name) |
||
| 2620 | |||
| 2621 | if term: |
||
| 2622 | cmd.add_element('term', term) |
||
| 2623 | |||
| 2624 | if filter_type: |
||
| 2625 | filter_type = filter_type.lower() |
||
| 2626 | if filter_type not in FILTER_TYPES: |
||
| 2627 | raise InvalidArgument( |
||
| 2628 | 'modify_filter requires type to be one of {0} but ' |
||
| 2629 | 'was {1}'.format(', '.join(FILTER_TYPES), filter_type)) |
||
| 2630 | cmd.add_element('type', filter_type) |
||
| 2631 | |||
| 2632 | return self._send_xml_command(cmd) |
||
| 2633 | |||
| 2634 | def modify_group(self, group_id, comment=None, name=None, |
||
| 2635 | users=None): |
||
| 2636 | """Modifies an existing group. |
||
| 2637 | |||
| 2638 | Arguments: |
||
| 2639 | group_id (str): UUID of group to modify. |
||
| 2640 | comment (str, optional): Comment on group. |
||
| 2641 | name (str, optional): Name of group. |
||
| 2642 | users (list, optional): List of user names to be in the group |
||
| 2643 | """ |
||
| 2644 | if not group_id: |
||
| 2645 | raise RequiredArgument('modify_group requires a group_id argument') |
||
| 2646 | |||
| 2647 | cmd = XmlCommand('modify_group') |
||
| 2648 | cmd.set_attribute('group_id', group_id) |
||
| 2649 | |||
| 2650 | if comment: |
||
| 2651 | cmd.add_element('comment', comment) |
||
| 2652 | |||
| 2653 | if name: |
||
| 2654 | cmd.add_element('name', name) |
||
| 2655 | |||
| 2656 | if users: |
||
| 2657 | cmd.add_element('users', ','.join(users)) |
||
| 2658 | |||
| 2659 | return self._send_xml_command(cmd) |
||
| 2660 | |||
| 2661 | def modify_note(self, note_id, text, seconds_active=None, hosts=None, |
||
| 2662 | port=None, result_id=None, severity=None, task_id=None, |
||
| 2663 | threat=None): |
||
| 2664 | """Modifies an existing note. |
||
| 2665 | |||
| 2666 | Arguments: |
||
| 2667 | note_id (str): UUID of note to modify. |
||
| 2668 | text (str): The text of the note. |
||
| 2669 | seconds_active (int, optional): Seconds note will be active. |
||
| 2670 | -1 on always, 0 off. |
||
| 2671 | hosts (list, optional): A list of hosts addresses |
||
| 2672 | port (str, optional): Port to which note applies. |
||
| 2673 | result_id (str, optional): Result to which note applies. |
||
| 2674 | severity (str, optional): Severity to which note applies. |
||
| 2675 | task_id (str, optional): Task to which note applies. |
||
| 2676 | threat (str, optional): Threat level to which note applies. |
||
| 2677 | """ |
||
| 2678 | if not note_id: |
||
| 2679 | raise RequiredArgument('modify_note requires a note_id attribute') |
||
| 2680 | if not text: |
||
| 2681 | raise RequiredArgument('modify_note requires a text element') |
||
| 2682 | |||
| 2683 | cmd = XmlCommand('modify_note') |
||
| 2684 | cmd.set_attribute('note_id', note_id) |
||
| 2685 | cmd.add_element('text', text) |
||
| 2686 | |||
| 2687 | if not seconds_active is None: |
||
| 2688 | cmd.add_element('active', str(seconds_active)) |
||
| 2689 | |||
| 2690 | if hosts: |
||
| 2691 | cmd.add_element('hosts', ', '.join(hosts)) |
||
| 2692 | |||
| 2693 | if port: |
||
| 2694 | cmd.add_element('port', port) |
||
| 2695 | |||
| 2696 | if result_id: |
||
| 2697 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 2698 | |||
| 2699 | if severity: |
||
| 2700 | cmd.add_element('severity', severity) |
||
| 2701 | |||
| 2702 | if task_id: |
||
| 2703 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 2704 | |||
| 2705 | if threat: |
||
| 2706 | cmd.add_element('threat', threat) |
||
| 2707 | |||
| 2708 | return self._send_xml_command(cmd) |
||
| 2709 | |||
| 2710 | View Code Duplication | def modify_override(self, override_id, text, seconds_active=None, |
|
| 2711 | hosts=None, port=None, result_id=None, severity=None, |
||
| 2712 | new_severity=None, task_id=None, threat=None, |
||
| 2713 | new_threat=None): |
||
| 2714 | """Modifies an existing override. |
||
| 2715 | |||
| 2716 | Arguments: |
||
| 2717 | override_id (str): UUID of override to modify. |
||
| 2718 | text (str): The text of the override. |
||
| 2719 | seconds_active (int, optional): Seconds override will be active. |
||
| 2720 | -1 on always, 0 off. |
||
| 2721 | hosts (list, optional): A list of host addresses |
||
| 2722 | port (str, optional): Port to which override applies. |
||
| 2723 | result_id (str, optional): Result to which override applies. |
||
| 2724 | severity (str, optional): Severity to which override applies. |
||
| 2725 | new_severity (str, optional): New severity score for result. |
||
| 2726 | task_id (str, optional): Task to which override applies. |
||
| 2727 | threat (str, optional): Threat level to which override applies. |
||
| 2728 | new_threat (str, optional): New threat level for results. |
||
| 2729 | """ |
||
| 2730 | if not override_id: |
||
| 2731 | raise RequiredArgument('modify_override requires a override_id ' |
||
| 2732 | 'argument') |
||
| 2733 | if not text: |
||
| 2734 | raise RequiredArgument('modify_override requires a text argument') |
||
| 2735 | |||
| 2736 | cmd = XmlCommand('modify_override') |
||
| 2737 | cmd.set_attribute('override_id', override_id) |
||
| 2738 | cmd.add_element('text', text) |
||
| 2739 | |||
| 2740 | if not seconds_active is None: |
||
| 2741 | cmd.add_element('active', str(seconds_active)) |
||
| 2742 | |||
| 2743 | if hosts: |
||
| 2744 | cmd.add_element('hosts', ', '.join(hosts)) |
||
| 2745 | |||
| 2746 | if port: |
||
| 2747 | cmd.add_element('port', port) |
||
| 2748 | |||
| 2749 | if result_id: |
||
| 2750 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 2751 | |||
| 2752 | if severity: |
||
| 2753 | cmd.add_element('severity', severity) |
||
| 2754 | |||
| 2755 | if new_severity: |
||
| 2756 | cmd.add_element('new_severity', new_severity) |
||
| 2757 | |||
| 2758 | if task_id: |
||
| 2759 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 2760 | |||
| 2761 | if threat: |
||
| 2762 | cmd.add_element('threat', threat) |
||
| 2763 | |||
| 2764 | if new_threat: |
||
| 2765 | cmd.add_element('new_threat', new_threat) |
||
| 2766 | |||
| 2767 | return self._send_xml_command(cmd) |
||
| 2768 | |||
| 2769 | View Code Duplication | def modify_permission(self, permission_id, comment=None, name=None, |
|
| 2770 | resource_id=None, resource_type=None, |
||
| 2771 | subject_id=None, subject_type=None): |
||
| 2772 | """Modifies an existing permission. |
||
| 2773 | |||
| 2774 | Arguments: |
||
| 2775 | permission_id (str): UUID of permission to be modified. |
||
| 2776 | comment (str, optional): The comment on the permission. |
||
| 2777 | name (str, optional): Permission name, currently the name of |
||
| 2778 | a command. |
||
| 2779 | subject_id (str, optional): UUID of subject to whom the permission |
||
| 2780 | is granted |
||
| 2781 | subject_type (str, optional): Type of the subject user, group or |
||
| 2782 | role |
||
| 2783 | resource_id (str, optional): UUID of entity to which the permission |
||
| 2784 | applies |
||
| 2785 | resource_type (str, optional): Type of the resource. For Super |
||
| 2786 | permissions user, group or role |
||
| 2787 | """ |
||
| 2788 | if not permission_id: |
||
| 2789 | raise RequiredArgument('modify_permission requires ' |
||
| 2790 | 'a permission_id element') |
||
| 2791 | |||
| 2792 | cmd = XmlCommand('modify_permission') |
||
| 2793 | cmd.set_attribute('permission_id', permission_id) |
||
| 2794 | |||
| 2795 | if comment: |
||
| 2796 | cmd.add_element('comment', comment) |
||
| 2797 | |||
| 2798 | if name: |
||
| 2799 | cmd.add_element('name', name) |
||
| 2800 | |||
| 2801 | if resource_id and resource_type: |
||
| 2802 | _xmlresource = cmd.add_element('resource', |
||
| 2803 | attrs={'id': resource_id}) |
||
| 2804 | _xmlresource.add_element('type', resource_type) |
||
| 2805 | |||
| 2806 | if subject_id and subject_type: |
||
| 2807 | _xmlsubject = cmd.add_element('subject', |
||
| 2808 | attrs={'id': subject_id}) |
||
| 2809 | _xmlsubject.add_element('type', subject_type) |
||
| 2810 | |||
| 2811 | return self._send_xml_command(cmd) |
||
| 2812 | |||
| 2813 | def modify_port_list(self, port_list_id, comment=None, name=None, ): |
||
| 2814 | """Modifies an existing port list. |
||
| 2815 | |||
| 2816 | Arguments: |
||
| 2817 | port_list_id (str): UUID of port list to modify. |
||
| 2818 | name (str, optional): Name of port list. |
||
| 2819 | comment (str, optional): Comment on port list. |
||
| 2820 | """ |
||
| 2821 | if not port_list_id: |
||
| 2822 | raise RequiredArgument('modify_port_list requires ' |
||
| 2823 | 'a port_list_id attribute') |
||
| 2824 | cmd = XmlCommand('modify_port_list') |
||
| 2825 | cmd.set_attribute('port_list_id', port_list_id) |
||
| 2826 | |||
| 2827 | if comment: |
||
| 2828 | cmd.add_element('comment', comment) |
||
| 2829 | |||
| 2830 | if name: |
||
| 2831 | cmd.add_element('name', name) |
||
| 2832 | |||
| 2833 | return self._send_xml_command(cmd) |
||
| 2834 | |||
| 2835 | def modify_report(self, report_id, comment): |
||
| 2836 | """Modifies an existing report. |
||
| 2837 | |||
| 2838 | Arguments: |
||
| 2839 | report_id (str): UUID of report to modify. |
||
| 2840 | comment (str): The comment on the report. |
||
| 2841 | """ |
||
| 2842 | if not report_id: |
||
| 2843 | raise RequiredArgument('modify_report requires ' |
||
| 2844 | 'a report_id attribute') |
||
| 2845 | if not comment: |
||
| 2846 | raise RequiredArgument('modify_report requires ' |
||
| 2847 | 'a comment attribute') |
||
| 2848 | cmd = XmlCommand('modify_report') |
||
| 2849 | cmd.set_attribute('report_id', report_id) |
||
| 2850 | cmd.add_element('comment', comment) |
||
| 2851 | |||
| 2852 | return self._send_xml_command(cmd) |
||
| 2853 | |||
| 2854 | def modify_report_format(self, report_format_id, active=None, name=None, |
||
| 2855 | summary=None, param_name=None, param_value=None): |
||
| 2856 | """Modifies an existing report format. |
||
| 2857 | |||
| 2858 | Arguments: |
||
| 2859 | report_format_id (str) UUID of report format to modify. |
||
| 2860 | active (boolean, optional): Whether the report format is active. |
||
| 2861 | name (str, optional): The name of the report format. |
||
| 2862 | summary (str, optional): A summary of the report format. |
||
| 2863 | param_name (str, optional): The name of the param. |
||
| 2864 | param_value (str, optional): The value of the param. |
||
| 2865 | """ |
||
| 2866 | if not report_format_id: |
||
| 2867 | raise RequiredArgument('modify_report requires ' |
||
| 2868 | 'a report_format_id attribute') |
||
| 2869 | cmd = XmlCommand('modify_report_format') |
||
| 2870 | cmd.set_attribute('report_format_id', report_format_id) |
||
| 2871 | |||
| 2872 | if not active is None: |
||
| 2873 | cmd.add_element('active', '1' if active else '0') |
||
| 2874 | |||
| 2875 | if name: |
||
| 2876 | cmd.add_element('name', name) |
||
| 2877 | |||
| 2878 | if summary: |
||
| 2879 | cmd.add_element('summary', summary) |
||
| 2880 | |||
| 2881 | if param_name and param_value: |
||
| 2882 | _xmlparam = cmd.add_element('param') |
||
| 2883 | _xmlparam.add_element('name', param_name) |
||
| 2884 | _xmlparam.add_element('value', param_value) |
||
| 2885 | |||
| 2886 | return self._send_xml_command(cmd) |
||
| 2887 | |||
| 2888 | def modify_role(self, role_id, comment=None, name=None, users=None): |
||
| 2889 | """Modifies an existing role. |
||
| 2890 | |||
| 2891 | Arguments: |
||
| 2892 | role_id (str): UUID of role to modify. |
||
| 2893 | comment (str, optional): Name of role. |
||
| 2894 | name (str, optional): Comment on role. |
||
| 2895 | users (list, optional): List of user names. |
||
| 2896 | """ |
||
| 2897 | if not role_id: |
||
| 2898 | raise RequiredArgument('modify_role requires a role_id argument') |
||
| 2899 | |||
| 2900 | cmd = XmlCommand('modify_role') |
||
| 2901 | cmd.set_attribute('role_id', role_id) |
||
| 2902 | |||
| 2903 | if comment: |
||
| 2904 | cmd.add_element('comment', comment) |
||
| 2905 | |||
| 2906 | if name: |
||
| 2907 | cmd.add_element('name', name) |
||
| 2908 | |||
| 2909 | if users: |
||
| 2910 | cmd.add_element('users', ",".join(users)) |
||
| 2911 | |||
| 2912 | return self._send_xml_command(cmd) |
||
| 2913 | |||
| 2914 | def modify_scanner(self, scanner_id, host, port, scanner_type, |
||
| 2915 | comment=None, name=None, ca_pub=None, |
||
| 2916 | credential_id=None): |
||
| 2917 | """Modifies an existing scanner. |
||
| 2918 | |||
| 2919 | Arguments: |
||
| 2920 | scanner_id (str): UUID of scanner to modify. |
||
| 2921 | host (str): Host of the scanner. |
||
| 2922 | port (str): Port of the scanner. |
||
| 2923 | scanner_type (str): Type of the scanner. |
||
| 2924 | '1' for OSP, '2' for OpenVAS (classic) Scanner. |
||
| 2925 | comment (str, optional): Comment on scanner. |
||
| 2926 | name (str, optional): Name of scanner. |
||
| 2927 | ca_pub (str, optional): Certificate of CA to verify scanner's |
||
| 2928 | certificate. |
||
| 2929 | credential_id (str, optional): UUID of the client certificate credential |
||
| 2930 | for the Scanner. |
||
| 2931 | """ |
||
| 2932 | if not scanner_id: |
||
| 2933 | raise RequiredArgument('modify_scanner requires a scanner_id argument') |
||
| 2934 | if not host: |
||
| 2935 | raise RequiredArgument('modify_scanner requires a host argument') |
||
| 2936 | if not port: |
||
| 2937 | raise RequiredArgument('modify_scanner requires a port argument') |
||
| 2938 | if not scanner_type: |
||
| 2939 | raise RequiredArgument('modify_scanner requires a scanner_type ' |
||
| 2940 | 'argument') |
||
| 2941 | |||
| 2942 | cmd = XmlCommand('modify_scanner') |
||
| 2943 | cmd.set_attribute('scanner_id', scanner_id) |
||
| 2944 | cmd.add_element('host', host) |
||
| 2945 | cmd.add_element('port', port) |
||
| 2946 | if scanner_type not in ('1', '2'): |
||
| 2947 | raise InvalidArgument(' modify_scanner requires a scanner_type ' |
||
| 2948 | 'argument which must be either "1" for OSP ' |
||
| 2949 | 'or "2" OpenVAS (Classic).') |
||
| 2950 | cmd.add_element('type', scanner_type) |
||
| 2951 | |||
| 2952 | if comment: |
||
| 2953 | cmd.add_element('comment', comment) |
||
| 2954 | |||
| 2955 | if name: |
||
| 2956 | cmd.add_element('name', name) |
||
| 2957 | |||
| 2958 | if ca_pub: |
||
| 2959 | cmd.add_element('ca_pub', ca_pub) |
||
| 2960 | |||
| 2961 | if credential_id: |
||
| 2962 | cmd.add_element('credential', attrs={'id': str(credential_id)}) |
||
| 2963 | |||
| 2964 | return self._send_xml_command(cmd) |
||
| 2965 | |||
| 2966 | def modify_schedule(self, schedule_id, comment=None, name=None, |
||
| 2967 | first_time_minute=None, first_time_hour=None, |
||
| 2968 | first_time_day_of_month=None, first_time_month=None, |
||
| 2969 | first_time_year=None, duration=None, duration_unit=None, |
||
| 2970 | period=None, period_unit=None, timezone=None): |
||
| 2971 | """Modifies an existing schedule. |
||
| 2972 | |||
| 2973 | Arguments: |
||
| 2974 | schedule_id (str): UUID of schedule to modify. |
||
| 2975 | name (str): Name of the schedule |
||
| 2976 | comment (str, optional): Comment for the schedule |
||
| 2977 | first_time_minute (int, optional): First time minute the schedule |
||
| 2978 | will run |
||
| 2979 | first_time_hour (int, optional): First time hour the schedule |
||
| 2980 | will run |
||
| 2981 | first_time_day_of_month (int, optional): First time day of month the |
||
| 2982 | schedule will run |
||
| 2983 | first_time_month (int, optional): First time month the schedule |
||
| 2984 | will run |
||
| 2985 | first_time_year (int, optional): First time year the schedule |
||
| 2986 | will run |
||
| 2987 | duration (int, optional): How long the Manager will run the |
||
| 2988 | scheduled task for until it gets paused if not finished yet. |
||
| 2989 | duration_unit (str, optional): Unit of the duration. One of second, |
||
| 2990 | minute, hour, day, week, month, year, decade. Required if |
||
| 2991 | duration is set. |
||
| 2992 | period (int, optional): How often the Manager will repeat the |
||
| 2993 | scheduled task |
||
| 2994 | period_unit (str, optional): Unit of the period. One of second, |
||
| 2995 | minute, hour, day, week, month, year, decade. Required if |
||
| 2996 | period is set. |
||
| 2997 | timezone (str, optional): The timezone the schedule will follow |
||
| 2998 | """ |
||
| 2999 | if not schedule_id: |
||
| 3000 | raise RequiredArgument('modify_schedule requires a schedule_id' |
||
| 3001 | 'argument') |
||
| 3002 | |||
| 3003 | cmd = XmlCommand('modify_schedule') |
||
| 3004 | cmd.set_attribute('schedule_id', schedule_id) |
||
| 3005 | |||
| 3006 | if comment: |
||
| 3007 | cmd.add_element('comment', comment) |
||
| 3008 | |||
| 3009 | if name: |
||
| 3010 | cmd.add_element('name', name) |
||
| 3011 | |||
| 3012 | View Code Duplication | if first_time_minute or first_time_hour or first_time_day_of_month or \ |
|
| 3013 | first_time_month or first_time_year: |
||
| 3014 | |||
| 3015 | if not first_time_minute: |
||
| 3016 | raise RequiredArgument( |
||
| 3017 | 'Setting first_time requires first_time_minute argument') |
||
| 3018 | if not first_time_hour: |
||
| 3019 | raise RequiredArgument( |
||
| 3020 | 'Setting first_time requires first_time_hour argument') |
||
| 3021 | if not first_time_day_of_month: |
||
| 3022 | raise RequiredArgument( |
||
| 3023 | 'Setting first_time requires first_time_day_of_month ' |
||
| 3024 | 'argument') |
||
| 3025 | if not first_time_month: |
||
| 3026 | raise RequiredArgument( |
||
| 3027 | 'Setting first_time requires first_time_month argument') |
||
| 3028 | if not first_time_year: |
||
| 3029 | raise RequiredArgument( |
||
| 3030 | 'Setting first_time requires first_time_year argument') |
||
| 3031 | |||
| 3032 | _xmlftime = cmd.add_element('first_time') |
||
| 3033 | _xmlftime.add_element('minute', str(first_time_minute)) |
||
| 3034 | _xmlftime.add_element('hour', str(first_time_hour)) |
||
| 3035 | _xmlftime.add_element('day_of_month', str(first_time_day_of_month)) |
||
| 3036 | _xmlftime.add_element('month', str(first_time_month)) |
||
| 3037 | _xmlftime.add_element('year', str(first_time_year)) |
||
| 3038 | |||
| 3039 | View Code Duplication | if duration: |
|
| 3040 | if not duration_unit: |
||
| 3041 | raise RequiredArgument( |
||
| 3042 | 'Setting duration requires duration_unit argument') |
||
| 3043 | |||
| 3044 | if not duration_unit in TIME_UNITS: |
||
| 3045 | raise InvalidArgument( |
||
| 3046 | 'duration_unit must be one of {units} but {actual} has ' |
||
| 3047 | 'been passed'.format( |
||
| 3048 | units=', '.join(TIME_UNITS), actual=duration_unit)) |
||
| 3049 | |||
| 3050 | _xmlduration = cmd.add_element('duration', str(duration)) |
||
| 3051 | _xmlduration.add_element('unit', duration_unit) |
||
| 3052 | |||
| 3053 | View Code Duplication | if period: |
|
| 3054 | if not period_unit: |
||
| 3055 | raise RequiredArgument( |
||
| 3056 | 'Setting period requires period_unit argument') |
||
| 3057 | |||
| 3058 | if not period_unit in TIME_UNITS: |
||
| 3059 | raise InvalidArgument( |
||
| 3060 | 'period_unit must be one of {units} but {actual} has ' |
||
| 3061 | 'been passed'.format( |
||
| 3062 | units=', '.join(TIME_UNITS), actual=period_unit)) |
||
| 3063 | |||
| 3064 | _xmlperiod = cmd.add_element('period', str(period)) |
||
| 3065 | _xmlperiod.add_element('unit', period_unit) |
||
| 3066 | |||
| 3067 | if timezone: |
||
| 3068 | cmd.add_element('timezone', str(timezone)) |
||
| 3069 | |||
| 3070 | return self._send_xml_command(cmd) |
||
| 3071 | |||
| 3072 | def modify_setting(self, setting_id, name, value): |
||
| 3073 | """Modifies an existing setting. |
||
| 3074 | |||
| 3075 | Arguments: |
||
| 3076 | setting_id (str): UUID of the setting to be changed. |
||
| 3077 | name (str): The name of the setting. |
||
| 3078 | value (str): The value of the setting. |
||
| 3079 | """ |
||
| 3080 | if not setting_id: |
||
| 3081 | raise RequiredArgument('modify_setting requires a setting_id' |
||
| 3082 | 'argument') |
||
| 3083 | if not name: |
||
| 3084 | raise RequiredArgument('modify_setting requires a name argument') |
||
| 3085 | if not value: |
||
| 3086 | raise RequiredArgument('modify_setting requires a value argument') |
||
| 3087 | |||
| 3088 | cmd = XmlCommand('modify_setting') |
||
| 3089 | cmd.set_attribute('setting_id', setting_id) |
||
| 3090 | cmd.add_element('name', name) |
||
| 3091 | cmd.add_element('value', value) |
||
| 3092 | |||
| 3093 | return self._send_xml_command(cmd) |
||
| 3094 | |||
| 3095 | def modify_tag(self, tag_id, comment=None, name=None, value=None, |
||
| 3096 | active=None, resource_id=None, resource_type=None): |
||
| 3097 | """Modifies an existing tag. |
||
| 3098 | |||
| 3099 | Arguments: |
||
| 3100 | tag_id (str): UUID of the tag. |
||
| 3101 | comment (str, optional): Comment to add to the tag. |
||
| 3102 | name (str, optional): Name of the tag. |
||
| 3103 | value (str, optional): Value of the tag. |
||
| 3104 | active (boolean, optional): Whether the tag is active. |
||
| 3105 | resource_id (str, optional): IDs of the resource to which to |
||
| 3106 | attach the tag. |
||
| 3107 | resource_type (str, optional): Type of the resource to which to |
||
| 3108 | attach the tag. |
||
| 3109 | """ |
||
| 3110 | if not tag_id: |
||
| 3111 | raise RequiredArgument('modify_tag requires a tag_id element') |
||
| 3112 | |||
| 3113 | cmd = XmlCommand('modify_tag') |
||
| 3114 | cmd.set_attribute('tag_id', str(tag_id)) |
||
| 3115 | |||
| 3116 | if comment: |
||
| 3117 | cmd.add_element('comment', comment) |
||
| 3118 | |||
| 3119 | if name: |
||
| 3120 | cmd.add_element('name', name) |
||
| 3121 | |||
| 3122 | if value: |
||
| 3123 | cmd.add_element('value', value) |
||
| 3124 | |||
| 3125 | if not active is None: |
||
| 3126 | if active: |
||
| 3127 | cmd.add_element('active', '1') |
||
| 3128 | else: |
||
| 3129 | cmd.add_element('active', '0') |
||
| 3130 | |||
| 3131 | if resource_id and resource_type: |
||
| 3132 | _xmlresource = cmd.add_element('resource', |
||
| 3133 | attrs={'resource_id': resource_id}) |
||
| 3134 | _xmlresource.add_element('type', resource_type) |
||
| 3135 | |||
| 3136 | return self._send_xml_command(cmd) |
||
| 3137 | |||
| 3138 | def modify_target(self, target_id, name=None, comment=None, |
||
| 3139 | hosts=None, hosts_ordering=None, |
||
| 3140 | exclude_hosts=None, ssh_credential_id=None, |
||
| 3141 | smb_credential_id=None, esxi_credential_id=None, |
||
| 3142 | snmp_credential_id=None, alive_tests=None, |
||
| 3143 | reverse_lookup_only=None, reverse_lookup_unify=None, |
||
| 3144 | port_list_id=None): |
||
| 3145 | """Modifies an existing target. |
||
| 3146 | |||
| 3147 | Arguments: |
||
| 3148 | target_id (uuid) ID of target to modify. |
||
| 3149 | comment (str, optional): Comment on target. |
||
| 3150 | name (str, optional): Name of target. |
||
| 3151 | hosts (list, optional): List of target hosts. |
||
| 3152 | hosts_ordering (str, optional): The order hosts are scanned in. |
||
| 3153 | exclude_hosts (list, optional): A list of hosts to exclude. |
||
| 3154 | ssh_credential (str, optional): UUID of SSH credential to |
||
| 3155 | use on target. |
||
| 3156 | smb_credential (str, optional): UUID of SMB credential to use |
||
| 3157 | on target. |
||
| 3158 | esxi_credential (str, optional): UUID of ESXi credential to use |
||
| 3159 | on target. |
||
| 3160 | snmp_credential (str, optional): UUID of SNMP credential to use |
||
| 3161 | on target. |
||
| 3162 | port_list (str, optional): UUID of port list describing ports to |
||
| 3163 | scan. |
||
| 3164 | alive_tests (str, optional): Which alive tests to use. |
||
| 3165 | reverse_lookup_only (boolean, optional): Whether to scan only hosts |
||
| 3166 | that have names. |
||
| 3167 | reverse_lookup_unify (boolean, optional): Whether to scan only one |
||
| 3168 | IP when multiple IPs have the same name. |
||
| 3169 | """ |
||
| 3170 | if not target_id: |
||
| 3171 | raise RequiredArgument('modify_target requires a ' |
||
| 3172 | 'target_id argument') |
||
| 3173 | |||
| 3174 | cmd = XmlCommand('modify_target') |
||
| 3175 | cmd.set_attribute('target_id', target_id) |
||
| 3176 | |||
| 3177 | if comment: |
||
| 3178 | cmd.add_element('comment', comment) |
||
| 3179 | |||
| 3180 | if name: |
||
| 3181 | cmd.add_element('name', name) |
||
| 3182 | |||
| 3183 | if hosts: |
||
| 3184 | cmd.add_element('hosts', ', '.join(hosts)) |
||
| 3185 | |||
| 3186 | if hosts_ordering: |
||
| 3187 | cmd.add_element('hosts_ordering', ', '.join(hosts_ordering)) |
||
| 3188 | |||
| 3189 | if exclude_hosts: |
||
| 3190 | cmd.add_element('exclude_hosts', ', '.join(exclude_hosts)) |
||
| 3191 | |||
| 3192 | if alive_tests: |
||
| 3193 | if not alive_tests in ALIVE_TESTS: |
||
| 3194 | raise InvalidArgument( |
||
| 3195 | 'alive_tests must be one of {tests} but ' |
||
| 3196 | '{actual} has been passed'.format( |
||
| 3197 | tests='|'.join(ALIVE_TESTS), actual=alive_tests)) |
||
| 3198 | cmd.add_element('alive_tests', alive_tests) |
||
| 3199 | |||
| 3200 | if ssh_credential_id: |
||
| 3201 | cmd.add_element('ssh_credential', attrs={'id': ssh_credential_id}) |
||
| 3202 | |||
| 3203 | if smb_credential_id: |
||
| 3204 | cmd.add_element('smb_credential', attrs={'id': smb_credential_id}) |
||
| 3205 | |||
| 3206 | if esxi_credential_id: |
||
| 3207 | cmd.add_element('esxi_credential', attrs={'id': esxi_credential_id}) |
||
| 3208 | |||
| 3209 | if snmp_credential_id: |
||
| 3210 | cmd.add_element('snmp_credential', attrs={'id': snmp_credential_id}) |
||
| 3211 | |||
| 3212 | if not reverse_lookup_only is None: |
||
| 3213 | if reverse_lookup_only: |
||
| 3214 | cmd.add_element('reverse_lookup_only', '1') |
||
| 3215 | else: |
||
| 3216 | cmd.add_element('reverse_lookup_only', '0') |
||
| 3217 | |||
| 3218 | if not reverse_lookup_unify is None: |
||
| 3219 | if reverse_lookup_unify: |
||
| 3220 | cmd.add_element('reverse_lookup_unify', '1') |
||
| 3221 | else: |
||
| 3222 | cmd.add_element('reverse_lookup_unify', '0') |
||
| 3223 | |||
| 3224 | if port_list_id: |
||
| 3225 | cmd.add_element('port_list', attrs={'id': port_list_id}) |
||
| 3226 | |||
| 3227 | return self._send_xml_command(cmd) |
||
| 3228 | |||
| 3229 | def modify_task(self, task_id, name=None, comment=None, alert=None, |
||
| 3230 | observers=None, preferences=None, schedule=None, |
||
| 3231 | schedule_periods=None, scanner=None, file_name=None, |
||
| 3232 | file_action=None): |
||
| 3233 | """Modifies an existing task. |
||
| 3234 | |||
| 3235 | Arguments: |
||
| 3236 | task_id (str) UUID of task to modify. |
||
| 3237 | comment (str, optional):The comment on the task. |
||
| 3238 | alert (str, optional): UUID of Task alert. |
||
| 3239 | name (str, optional): The name of the task. |
||
| 3240 | observers (list, optional): Users allowed to observe this task. |
||
| 3241 | preferences (dict, optional): Compact name of preference, from |
||
| 3242 | scanner and its value |
||
| 3243 | schedule (str, optional): UUID of Task schedule. |
||
| 3244 | schedule_periods (int, optional): A limit to the number of times |
||
| 3245 | the task will be scheduled, or 0 for no limit. |
||
| 3246 | scanner (str, optional): UUID of Task scanner. |
||
| 3247 | file_name (str, optional): File to attach to task. |
||
| 3248 | file_action (str, optional): Action for the file: |
||
| 3249 | one of "update" or "remove" |
||
| 3250 | """ |
||
| 3251 | if not task_id: |
||
| 3252 | raise RequiredArgument('modify_task requires a task_id argument') |
||
| 3253 | |||
| 3254 | cmd = XmlCommand('modify_task') |
||
| 3255 | cmd.set_attribute('task_id', task_id) |
||
| 3256 | |||
| 3257 | if name: |
||
| 3258 | cmd.add_element('name', name) |
||
| 3259 | |||
| 3260 | if comment: |
||
| 3261 | cmd.add_element('comment', comment) |
||
| 3262 | |||
| 3263 | if scanner: |
||
| 3264 | cmd.add_element('scanner', attrs={'id': scanner}) |
||
| 3265 | |||
| 3266 | if schedule_periods: |
||
| 3267 | cmd.add_element('schedule_periods', str(schedule_periods)) |
||
| 3268 | |||
| 3269 | if schedule: |
||
| 3270 | cmd.add_element('schedule', attrs={'id': schedule}) |
||
| 3271 | |||
| 3272 | if alert: |
||
| 3273 | cmd.add_element('alert', attrs={'id': alert}) |
||
| 3274 | |||
| 3275 | if observers: |
||
| 3276 | cmd.add_element('observers', ', '.join(observers)) |
||
| 3277 | |||
| 3278 | if preferences: |
||
| 3279 | _xmlprefs = cmd.add_element('preferences') |
||
| 3280 | for pref_name, pref_value in preferences.items(): |
||
| 3281 | _xmlpref = _xmlprefs.add_element('preference') |
||
| 3282 | _xmlpref.add_element('scanner_name', pref_name) |
||
| 3283 | _xmlpref.add_element('value', pref_value) |
||
| 3284 | |||
| 3285 | if file_name and file_action: |
||
| 3286 | if file_action not in ('update', 'remove'): |
||
| 3287 | raise InvalidArgument('action can only be ' |
||
| 3288 | '"update" or "remove"!') |
||
| 3289 | cmd.add_element('file', attrs={'name': file_name, |
||
| 3290 | 'action': file_action}) |
||
| 3291 | |||
| 3292 | return self._send_xml_command(cmd) |
||
| 3293 | |||
| 3294 | def modify_user(self, user_id, name, new_name=None, password=None, |
||
| 3295 | role_ids=None, hosts=None, hosts_allow=None, |
||
| 3296 | ifaces=None, ifaces_allow=None, sources=None): |
||
| 3297 | """Modifies an existing user. |
||
| 3298 | |||
| 3299 | Arguments: |
||
| 3300 | user_id (str): UUID of the user to be modified. Overrides |
||
| 3301 | NAME element. |
||
| 3302 | name (str): The name of the user to be modified. |
||
| 3303 | new_name (str, optional): The new name for the user. |
||
| 3304 | password (str, optional): The password for the user. |
||
| 3305 | roles_id (list, optional): List of roles UUIDs for the user. |
||
| 3306 | hosts (list, optional): User access rules: List of hosts. |
||
| 3307 | hosts_allow (boolean,optional): If True, allow only listed, |
||
| 3308 | otherwise forbid listed. |
||
| 3309 | ifaces (list, optional): User access rules: List |
||
| 3310 | of ifaces. |
||
| 3311 | ifaces_allow (boolean, optional): If True, allow only listed, |
||
| 3312 | otherwise forbid listed. |
||
| 3313 | sources (list, optional): List of authentication sources for |
||
| 3314 | this user. |
||
| 3315 | """ |
||
| 3316 | if not user_id: |
||
| 3317 | raise RequiredArgument('modify_user requires a user_id argument') |
||
| 3318 | if not name: |
||
| 3319 | raise RequiredArgument('modify_user requires a name argument') |
||
| 3320 | |||
| 3321 | cmd = XmlCommand('modify_user') |
||
| 3322 | cmd.set_attribute('user_id', user_id) |
||
| 3323 | |||
| 3324 | if new_name: |
||
| 3325 | cmd.add_element('new_name', new_name) |
||
| 3326 | |||
| 3327 | if password: |
||
| 3328 | cmd.add_element('password', password) |
||
| 3329 | |||
| 3330 | if role_ids: |
||
| 3331 | for role in role_ids: |
||
| 3332 | cmd.add_element('role', attrs={'id': role}) |
||
| 3333 | |||
| 3334 | if hosts or hosts_allow: |
||
| 3335 | cmd.add_element('hosts', ', '.join(hosts), |
||
| 3336 | attrs={'allow': '1' if hosts_allow else '0'}) |
||
| 3337 | |||
| 3338 | if ifaces or ifaces_allow: |
||
| 3339 | cmd.add_element('ifaces', ', '.join(ifaces), |
||
| 3340 | attrs={'allow': '1' if ifaces_allow else '0'}) |
||
| 3341 | |||
| 3342 | if sources: |
||
| 3343 | cmd.add_element('sources', ', '.join(sources)) |
||
| 3344 | |||
| 3345 | return self._send_xml_command(cmd) |
||
| 3346 | |||
| 3347 | def move_task(self, task_id, slave_id): |
||
| 3348 | cmd = self._generator.move_task_command(task_id, slave_id) |
||
| 3349 | return self.send_command(cmd) |
||
| 3350 | |||
| 3351 | def restore(self, entity_id): |
||
| 3352 | cmd = self._generator.restore_command(entity_id) |
||
| 3353 | return self.send_command(cmd) |
||
| 3354 | |||
| 3355 | def resume_task(self, task_id): |
||
| 3356 | cmd = self._generator.resume_task_command(task_id) |
||
| 3357 | return self.send_command(cmd) |
||
| 3358 | |||
| 3359 | def start_task(self, task_id): |
||
| 3360 | cmd = self._generator.start_task_command(task_id) |
||
| 3361 | return self.send_command(cmd) |
||
| 3362 | |||
| 3363 | def stop_task(self, task_id): |
||
| 3364 | cmd = self._generator.stop_task_command(task_id) |
||
| 3365 | return self.send_command(cmd) |
||
| 3366 | |||
| 3367 | def sync_cert(self): |
||
| 3368 | """Request a synchronization with the CERT feed service |
||
| 3369 | |||
| 3370 | Returns: |
||
| 3371 | The response. See :py:meth:`send_command` for details. |
||
| 3372 | """ |
||
| 3373 | return self._send_xml_command(XmlCommand('sync_cert')) |
||
| 3374 | |||
| 3375 | def sync_config(self): |
||
| 3376 | """Request an OSP config synchronization with scanner |
||
| 3377 | |||
| 3378 | Returns: |
||
| 3379 | The response. See :py:meth:`send_command` for details. |
||
| 3380 | """ |
||
| 3381 | return self._send_xml_command(XmlCommand('sync_config')) |
||
| 3382 | |||
| 3383 | def sync_feed(self): |
||
| 3384 | """Request a synchronization with the NVT feed service |
||
| 3385 | |||
| 3386 | Returns: |
||
| 3387 | The response. See :py:meth:`send_command` for details. |
||
| 3388 | """ |
||
| 3389 | return self._send_xml_command(XmlCommand('sync_feed')) |
||
| 3390 | |||
| 3391 | def sync_scap(self): |
||
| 3392 | """Request a synchronization with the SCAP feed service |
||
| 3393 | |||
| 3394 | Returns: |
||
| 3395 | The response. See :py:meth:`send_command` for details. |
||
| 3396 | """ |
||
| 3397 | return self._send_xml_command(XmlCommand('sync_scap')) |
||
| 3398 | |||
| 3399 | def test_alert(self, alert_id): |
||
| 3400 | cmd = self._generator.test_alert_command(alert_id) |
||
| 3401 | return self.send_command(cmd) |
||
| 3402 | |||
| 3403 | def verify_agent(self, agent_id): |
||
| 3404 | cmd = self._generator.verify_agent_command(agent_id) |
||
| 3405 | return self.send_command(cmd) |
||
| 3406 | |||
| 3407 | def verify_report_format(self, report_format_id): |
||
| 3408 | cmd = self._generator.verify_report_format_command(report_format_id) |
||
| 3409 | return self.send_command(cmd) |
||
| 3410 | |||
| 3411 | def verify_scanner(self, scanner_id): |
||
| 3412 | cmd = self._generator.verify_scanner_command(scanner_id) |
||
| 3413 | return self.send_command(cmd) |
||
| 3414 |