| Total Complexity | 327 |
| Total Lines | 1514 |
| Duplicated Lines | 18.63 % |
| 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 gmp.xml 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 | import defusedxml.lxml as secET |
||
| 20 | |||
| 21 | from lxml import etree |
||
| 22 | |||
| 23 | FILTER_NAMES = [ |
||
| 24 | 'Agent', |
||
| 25 | 'Alert', |
||
| 26 | 'Asset', |
||
| 27 | 'Config', |
||
| 28 | 'Credential', |
||
| 29 | 'Filter', |
||
| 30 | 'Group', |
||
| 31 | 'Note', |
||
| 32 | 'Override', |
||
| 33 | 'Permission', |
||
| 34 | 'Port List', |
||
| 35 | 'Report', |
||
| 36 | 'Report Format', |
||
| 37 | 'Result', |
||
| 38 | 'Role', |
||
| 39 | 'Schedule', |
||
| 40 | 'SecInfo', |
||
| 41 | 'Tag', |
||
| 42 | 'Target', |
||
| 43 | 'Task', |
||
| 44 | 'User', |
||
| 45 | ] |
||
| 46 | |||
| 47 | class XmlCommandElement: |
||
| 48 | |||
| 49 | def __init__(self, element): |
||
| 50 | self._element = element |
||
| 51 | |||
| 52 | def add_element(self, name, text=None, attrs=None): |
||
| 53 | node = etree.SubElement(self._element, name, attrib=attrs) |
||
| 54 | node.text = text |
||
| 55 | return XmlCommandElement(node) |
||
| 56 | |||
| 57 | def set_attribute(self, name, value): |
||
| 58 | self._element.set(name, value) |
||
| 59 | |||
| 60 | def append_xml_str(self, xml_text): |
||
| 61 | """Append a xml element in string format.""" |
||
| 62 | node = secET.fromstring(xml_text) |
||
| 63 | self._element.append(node) |
||
| 64 | |||
| 65 | def to_string(self): |
||
| 66 | return etree.tostring(self._element).decode('utf-8') |
||
| 67 | |||
| 68 | def __str__(self): |
||
| 69 | return self.to_string() |
||
| 70 | |||
| 71 | |||
| 72 | class XmlCommand(XmlCommandElement): |
||
| 73 | |||
| 74 | def __init__(self, name): |
||
| 75 | super().__init__(etree.Element(name)) |
||
| 76 | |||
| 77 | |||
| 78 | class _GmpCommandFactory: |
||
| 79 | |||
| 80 | """Factory to create gmp - Greenbone Manangement Protocol - commands |
||
| 81 | """ |
||
| 82 | |||
| 83 | def create_agent_command(self, installer, signature, name, comment='', |
||
| 84 | copy='', howto_install='', howto_use=''): |
||
| 85 | |||
| 86 | cmd = XmlCommand('create_agent') |
||
| 87 | cmd.add_element('installer', installer) |
||
| 88 | cmd.add_element('signature', signature) |
||
| 89 | cmd.add_element('name', name) |
||
| 90 | |||
| 91 | if comment: |
||
| 92 | cmd.add_element('comment', comment) |
||
| 93 | |||
| 94 | if copy: |
||
| 95 | cmd.add_element('copy', copy) |
||
| 96 | |||
| 97 | if howto_install: |
||
| 98 | cmd.add_element('howto_install', howto_install) |
||
| 99 | |||
| 100 | if howto_use: |
||
| 101 | cmd.add_element('howto_use', howto_use) |
||
| 102 | |||
| 103 | return cmd.to_string() |
||
| 104 | |||
| 105 | def create_alert_command(self, name, condition, event, method, filter_id='', |
||
| 106 | copy='', comment=''): |
||
| 107 | |||
| 108 | cmd = XmlCommand('create_alert') |
||
| 109 | cmd.add_element('name', name) |
||
| 110 | |||
| 111 | if len(condition) > 1: |
||
| 112 | conditions = cmd.add_element('condition', condition[0]) |
||
| 113 | for value, key in condition[1].items(): |
||
| 114 | _data = conditions.add_element('data', value) |
||
| 115 | _data.add_element('name', key) |
||
| 116 | |||
| 117 | elif condition[0] == "Always": |
||
| 118 | conditions = cmd.add_element('condition', condition[0]) |
||
| 119 | |||
| 120 | if len(event) > 1: |
||
| 121 | events = cmd.add_element('event', event[0]) |
||
| 122 | for value, key in event[1].items(): |
||
| 123 | _data = events.add_element('data', value) |
||
| 124 | _data.add_element('name', key) |
||
| 125 | |||
| 126 | if len(method) > 1: |
||
| 127 | methods = cmd.add_element('method', method[0]) |
||
| 128 | for value, key in method[1].items(): |
||
| 129 | _data = methods.add_element('data', value) |
||
| 130 | _data.add_element('name', key) |
||
| 131 | |||
| 132 | if filter_id: |
||
| 133 | cmd.add_element('filter', attrs={'id': filter_id}) |
||
| 134 | |||
| 135 | if copy: |
||
| 136 | cmd.add_element('copy', copy) |
||
| 137 | |||
| 138 | if comment: |
||
| 139 | cmd.add_element('comment', comment) |
||
| 140 | |||
| 141 | return cmd.to_string() |
||
| 142 | |||
| 143 | def create_asset_command(self, name, asset_type, comment=''): |
||
| 144 | if asset_type not in ('host', 'os'): |
||
| 145 | raise ValueError('create_asset requires asset_type to be either ' |
||
| 146 | 'host or os') |
||
| 147 | cmd = XmlCommand('create_asset') |
||
| 148 | asset = cmd.add_element('asset') |
||
| 149 | asset.add_element('type', asset_type) |
||
| 150 | asset.add_element('name', name) |
||
| 151 | |||
| 152 | if comment: |
||
| 153 | asset.add_element('comment', comment) |
||
| 154 | |||
| 155 | return cmd.to_string() |
||
| 156 | |||
| 157 | def create_authenticate_command(self, username, password): |
||
| 158 | """Generates string for authentification on gvmd |
||
| 159 | |||
| 160 | Creates the gmp authentication xml string. |
||
| 161 | Inserts the username and password into it. |
||
| 162 | |||
| 163 | Keyword Arguments: |
||
| 164 | username {str} -- Username for GVM User |
||
| 165 | password {str} -- Password for GVM User |
||
| 166 | """ |
||
| 167 | cmd = XmlCommand('authenticate') |
||
| 168 | |||
| 169 | credentials = cmd.add_element('credentials') |
||
| 170 | credentials.add_element('username', username) |
||
| 171 | credentials.add_element('password', password) |
||
| 172 | |||
| 173 | return cmd.to_string() |
||
| 174 | |||
| 175 | def create_config_command(self, copy_id, name): |
||
| 176 | """Generates xml string for create config on gvmd.""" |
||
| 177 | cmd = XmlCommand('create_config') |
||
| 178 | cmd.add_element('copy', copy_id) |
||
| 179 | cmd.add_element('name', name) |
||
| 180 | |||
| 181 | return cmd.to_string() |
||
| 182 | |||
| 183 | View Code Duplication | def create_credential_command(self, name, kwargs): |
|
| 184 | """Generates xml string for create credential on gvmd.""" |
||
| 185 | cmd = XmlCommand('create_credential') |
||
| 186 | cmd.add_element('name', name) |
||
| 187 | |||
| 188 | comment = kwargs.get('comment', '') |
||
| 189 | if comment: |
||
| 190 | cmd.add_element('comment', comment) |
||
| 191 | |||
| 192 | copy = kwargs.get('copy', '') |
||
| 193 | if copy: |
||
| 194 | cmd.add_element('copy', copy) |
||
| 195 | |||
| 196 | allow_insecure = kwargs.get('allow_insecure', '') |
||
| 197 | if allow_insecure: |
||
| 198 | cmd.add_element('allow_insecure', allow_insecure) |
||
| 199 | |||
| 200 | certificate = kwargs.get('certificate', '') |
||
| 201 | if certificate: |
||
| 202 | cmd.add_element('certificate', certificate) |
||
| 203 | |||
| 204 | key = kwargs.get('key', '') |
||
| 205 | if key: |
||
| 206 | phrase = key['phrase'] |
||
| 207 | private = key['private'] |
||
| 208 | if not phrase: |
||
| 209 | raise ValueError('create_credential requires a phrase element') |
||
| 210 | if not private: |
||
| 211 | raise ValueError('create_credential requires a ' |
||
| 212 | 'private element') |
||
| 213 | |||
| 214 | _xmlkey = cmd.add_element('key') |
||
| 215 | _xmlkey.add_element('phrase', phrase) |
||
| 216 | _xmlkey.add_element('private', private) |
||
| 217 | |||
| 218 | login = kwargs.get('login', '') |
||
| 219 | if login: |
||
| 220 | cmd.add_element('login', login) |
||
| 221 | |||
| 222 | password = kwargs.get('password', '') |
||
| 223 | if password: |
||
| 224 | cmd.add_element('password', password) |
||
| 225 | |||
| 226 | auth_algorithm = kwargs.get('auth_algorithm', '') |
||
| 227 | if auth_algorithm: |
||
| 228 | if auth_algorithm not in ('md5', 'sha1'): |
||
| 229 | raise ValueError('create_credential requires auth_algorithm ' |
||
| 230 | 'to be either md5 or sha1') |
||
| 231 | cmd.add_element('auth_algorithm', auth_algorithm) |
||
| 232 | |||
| 233 | community = kwargs.get('community', '') |
||
| 234 | if community: |
||
| 235 | cmd.add_element('community', community) |
||
| 236 | |||
| 237 | privacy = kwargs.get('privacy', '') |
||
| 238 | if privacy: |
||
| 239 | algorithm = privacy.algorithm |
||
| 240 | if algorithm not in ('aes', 'des'): |
||
| 241 | raise ValueError('create_credential requires algorithm ' |
||
| 242 | 'to be either aes or des') |
||
| 243 | p_password = privacy.password |
||
| 244 | _xmlprivacy = cmd.add_element('privacy') |
||
| 245 | _xmlprivacy.add_element('algorithm', algorithm) |
||
| 246 | _xmlprivacy.add_element('password', p_password) |
||
| 247 | |||
| 248 | cred_type = kwargs.get('type', '') |
||
| 249 | if cred_type: |
||
| 250 | if cred_type not in ('cc', 'snmp', 'up', 'usk'): |
||
| 251 | raise ValueError('create_credential requires type ' |
||
| 252 | 'to be either cc, snmp, up or usk') |
||
| 253 | cmd.add_element('type', cred_type) |
||
| 254 | |||
| 255 | return cmd.to_string() |
||
| 256 | |||
| 257 | def create_filter_command(self, name, make_unique, kwargs): |
||
| 258 | """Generates xml string for create filter on gvmd.""" |
||
| 259 | |||
| 260 | cmd = XmlCommand('create_filter') |
||
| 261 | _xmlname = cmd.add_element('name', name) |
||
| 262 | if make_unique: |
||
| 263 | _xmlname.add_element('make_unique', '1') |
||
| 264 | else: |
||
| 265 | _xmlname.add_element('make_unique', '0') |
||
| 266 | |||
| 267 | comment = kwargs.get('comment', '') |
||
| 268 | if comment: |
||
| 269 | cmd.add_element('comment', comment) |
||
| 270 | |||
| 271 | copy = kwargs.get('copy', '') |
||
| 272 | if copy: |
||
| 273 | cmd.add_element('copy', copy) |
||
| 274 | |||
| 275 | term = kwargs.get('term', '') |
||
| 276 | if term: |
||
| 277 | cmd.add_element('term', term) |
||
| 278 | |||
| 279 | filter_type = kwargs.get('type', '') |
||
| 280 | if filter_type: |
||
| 281 | if filter_type not in FILTER_NAMES: |
||
| 282 | raise ValueError('create_filter requires type ' |
||
| 283 | 'to be either cc, snmp, up or usk') |
||
| 284 | cmd.add_element('type', filter_type) |
||
| 285 | |||
| 286 | return cmd.to_string() |
||
| 287 | |||
| 288 | def create_group_command(self, name, kwargs): |
||
| 289 | """Generates xml string for create group on gvmd.""" |
||
| 290 | |||
| 291 | cmd = XmlCommand('create_group') |
||
| 292 | cmd.add_element('name', name) |
||
| 293 | |||
| 294 | comment = kwargs.get('comment', '') |
||
| 295 | if comment: |
||
| 296 | cmd.add_element('comment', comment) |
||
| 297 | |||
| 298 | copy = kwargs.get('copy', '') |
||
| 299 | if copy: |
||
| 300 | cmd.add_element('copy', copy) |
||
| 301 | |||
| 302 | special = kwargs.get('special', '') |
||
| 303 | if special: |
||
| 304 | _xmlspecial = cmd.add_element('specials') |
||
| 305 | _xmlspecial.add_element('full') |
||
| 306 | |||
| 307 | users = kwargs.get('users', '') |
||
| 308 | if users: |
||
| 309 | cmd.add_element('users', users) |
||
| 310 | |||
| 311 | return cmd.to_string() |
||
| 312 | |||
| 313 | def create_note_command(self, text, nvt_oid, kwargs): |
||
| 314 | """Generates xml string for create note on gvmd.""" |
||
| 315 | |||
| 316 | cmd = XmlCommand('create_note') |
||
| 317 | cmd.add_element('text', text) |
||
| 318 | cmd.add_element('nvt', attrs={"oid": nvt_oid}) |
||
| 319 | |||
| 320 | active = kwargs.get('active', '') |
||
| 321 | if active: |
||
| 322 | cmd.add_element('active', active) |
||
| 323 | |||
| 324 | comment = kwargs.get('comment', '') |
||
| 325 | if comment: |
||
| 326 | cmd.add_element('comment', comment) |
||
| 327 | |||
| 328 | copy = kwargs.get('copy', '') |
||
| 329 | if copy: |
||
| 330 | cmd.add_element('copy', copy) |
||
| 331 | |||
| 332 | hosts = kwargs.get('hosts', '') |
||
| 333 | if hosts: |
||
| 334 | cmd.add_element('hosts', hosts) |
||
| 335 | |||
| 336 | port = kwargs.get('port', '') |
||
| 337 | if port: |
||
| 338 | cmd.add_element('port', port) |
||
| 339 | |||
| 340 | result_id = kwargs.get('result_id', '') |
||
| 341 | if result_id: |
||
| 342 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 343 | |||
| 344 | severity = kwargs.get('severity', '') |
||
| 345 | if severity: |
||
| 346 | cmd.add_element('severity', severity) |
||
| 347 | |||
| 348 | task_id = kwargs.get('task_id', '') |
||
| 349 | if task_id: |
||
| 350 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 351 | |||
| 352 | threat = kwargs.get('threat', '') |
||
| 353 | if threat: |
||
| 354 | cmd.add_element('threat', threat) |
||
| 355 | |||
| 356 | return cmd.to_string() |
||
| 357 | |||
| 358 | def create_override_command(self, text, nvt_oid, kwargs): |
||
| 359 | """Generates xml string for create override on gvmd.""" |
||
| 360 | |||
| 361 | cmd = XmlCommand('create_override') |
||
| 362 | cmd.add_element('text', text) |
||
| 363 | cmd.add_element('nvt', attrs={'oid': nvt_oid}) |
||
| 364 | |||
| 365 | active = kwargs.get('active', '') |
||
| 366 | if active: |
||
| 367 | cmd.add_element('active', active) |
||
| 368 | |||
| 369 | comment = kwargs.get('comment', '') |
||
| 370 | if comment: |
||
| 371 | cmd.add_element('comment', comment) |
||
| 372 | |||
| 373 | copy = kwargs.get('copy', '') |
||
| 374 | if copy: |
||
| 375 | cmd.add_element('copy', copy) |
||
| 376 | |||
| 377 | hosts = kwargs.get('hosts', '') |
||
| 378 | if hosts: |
||
| 379 | cmd.add_element('hosts', hosts) |
||
| 380 | |||
| 381 | port = kwargs.get('port', '') |
||
| 382 | if port: |
||
| 383 | cmd.add_element('port', port) |
||
| 384 | |||
| 385 | result_id = kwargs.get('result_id', '') |
||
| 386 | if result_id: |
||
| 387 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 388 | |||
| 389 | severity = kwargs.get('severity', '') |
||
| 390 | if severity: |
||
| 391 | cmd.add_element('severity', severity) |
||
| 392 | |||
| 393 | new_severity = kwargs.get('new_severity', '') |
||
| 394 | if new_severity: |
||
| 395 | cmd.add_element('new_severity', new_severity) |
||
| 396 | |||
| 397 | task_id = kwargs.get('task_id', '') |
||
| 398 | if task_id: |
||
| 399 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 400 | |||
| 401 | threat = kwargs.get('threat', '') |
||
| 402 | if threat: |
||
| 403 | cmd.add_element('threat', threat) |
||
| 404 | |||
| 405 | new_threat = kwargs.get('new_threat', '') |
||
| 406 | if new_threat: |
||
| 407 | cmd.add_element('new_threat', new_threat) |
||
| 408 | |||
| 409 | return cmd.to_string() |
||
| 410 | |||
| 411 | def create_permission_command(self, name, subject_id, type, kwargs): |
||
| 412 | # pretty(gmp.create_permission('get_version', |
||
| 413 | # 'cc9cac5e-39a3-11e4-abae-406186ea4fc5', 'role')) |
||
| 414 | # libs.gvm_connection.GMPError: Error in NAME |
||
| 415 | # TODO: Research why!! |
||
| 416 | |||
| 417 | if not name: |
||
| 418 | raise ValueError('create_permission requires a name element') |
||
| 419 | if not subject_id: |
||
| 420 | raise ValueError('create_permission requires a subject_id element') |
||
| 421 | if type not in ('user', 'group', 'role'): |
||
| 422 | raise ValueError('create_permission requires type ' |
||
| 423 | 'to be either user, group or role') |
||
| 424 | |||
| 425 | cmd = XmlCommand('create_permission') |
||
| 426 | cmd.add_element('name', name) |
||
| 427 | _xmlsubject = cmd.add_element('subject', attrs={'id': subject_id}) |
||
| 428 | _xmlsubject.add_element('type', type) |
||
| 429 | |||
| 430 | comment = kwargs.get('comment', '') |
||
| 431 | if comment: |
||
| 432 | cmd.add_element('comment', comment) |
||
| 433 | |||
| 434 | copy = kwargs.get('copy', '') |
||
| 435 | if copy: |
||
| 436 | cmd.add_element('copy', copy) |
||
| 437 | |||
| 438 | resource = kwargs.get('resource', '') |
||
| 439 | if resource: |
||
| 440 | resource_id = resource.id |
||
| 441 | resource_type = resource.type |
||
| 442 | _xmlresource = cmd.add_element('resource', |
||
| 443 | attrs={'id': resource_id}) |
||
| 444 | _xmlresource.add_element('type', resource_type) |
||
| 445 | |||
| 446 | return cmd.to_string() |
||
| 447 | |||
| 448 | def create_port_list_command(self, name, port_range, kwargs): |
||
| 449 | """Generates xml string for create port list on gvmd.""" |
||
| 450 | if not name: |
||
| 451 | raise ValueError('create_port_list requires a name element') |
||
| 452 | if not port_range: |
||
| 453 | raise ValueError('create_port_list requires a port_range element') |
||
| 454 | |||
| 455 | cmd = XmlCommand('create_port_list') |
||
| 456 | cmd.add_element('name', name) |
||
| 457 | cmd.add_element('port_range', port_range) |
||
| 458 | |||
| 459 | comment = kwargs.get('comment', '') |
||
| 460 | if comment: |
||
| 461 | cmd.add_element('comment', comment) |
||
| 462 | |||
| 463 | copy = kwargs.get('copy', '') |
||
| 464 | if copy: |
||
| 465 | cmd.add_element('copy', copy) |
||
| 466 | |||
| 467 | return cmd.to_string() |
||
| 468 | |||
| 469 | def create_port_range_command(self, port_list_id, start, end, type, |
||
| 470 | comment=''): |
||
| 471 | """Generates xml string for create port range on gvmd.""" |
||
| 472 | |||
| 473 | if not port_list_id: |
||
| 474 | raise ValueError('create_port_range requires ' |
||
| 475 | 'a port_list_id element') |
||
| 476 | if not type: |
||
| 477 | raise ValueError('create_port_range requires a type element') |
||
| 478 | |||
| 479 | cmd = XmlCommand('create_port_range') |
||
| 480 | cmd.add_element('port_list', attrs={'id': port_list_id}) |
||
| 481 | cmd.add_element('start', start) |
||
| 482 | cmd.add_element('end', end) |
||
| 483 | cmd.add_element('type', type) |
||
| 484 | |||
| 485 | if comment: |
||
| 486 | cmd.add_element('comment', comment) |
||
| 487 | |||
| 488 | return cmd.to_string() |
||
| 489 | |||
| 490 | def create_report_command(self, report_xml_string, kwargs): |
||
| 491 | """Generates xml string for create report on gvmd.""" |
||
| 492 | |||
| 493 | if not report_xml_string: |
||
| 494 | raise ValueError('create_report requires a report') |
||
| 495 | |||
| 496 | task_id = kwargs.get('task_id', '') |
||
| 497 | task_name = kwargs.get('task_name', '') |
||
| 498 | |||
| 499 | cmd = XmlCommand('create_report') |
||
| 500 | comment = kwargs.get('comment', '') |
||
| 501 | if task_id: |
||
| 502 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 503 | elif task_name: |
||
| 504 | _xmltask = cmd.add_element('task') |
||
| 505 | _xmltask.add_element('name', task_name) |
||
| 506 | if comment: |
||
| 507 | _xmltask.add_element('comment', comment) |
||
| 508 | else: |
||
| 509 | raise ValueError('create_report requires an id or name for a task') |
||
| 510 | |||
| 511 | in_assets = kwargs.get('in_assets', '') |
||
| 512 | if in_assets: |
||
| 513 | cmd.add_element('in_assets', in_assets) |
||
| 514 | |||
| 515 | cmd.append_xml_str(report_xml_string) |
||
| 516 | |||
| 517 | return cmd.to_string() |
||
| 518 | |||
| 519 | def create_role_command(self, name, kwargs): |
||
| 520 | """Generates xml string for create role on gvmd.""" |
||
| 521 | |||
| 522 | if not name: |
||
| 523 | raise ValueError('create_role requires a name element') |
||
| 524 | |||
| 525 | cmd = XmlCommand('create_role') |
||
| 526 | cmd.add_element('name', name) |
||
| 527 | |||
| 528 | comment = kwargs.get('comment', '') |
||
| 529 | if comment: |
||
| 530 | cmd.add_element('comment', comment) |
||
| 531 | |||
| 532 | copy = kwargs.get('copy', '') |
||
| 533 | if copy: |
||
| 534 | cmd.add_element('copy', copy) |
||
| 535 | |||
| 536 | users = kwargs.get('users', '') |
||
| 537 | if users: |
||
| 538 | cmd.add_element('users', users) |
||
| 539 | |||
| 540 | return cmd.to_string() |
||
| 541 | |||
| 542 | def create_scanner_command(self, name, host, port, type, ca_pub, |
||
| 543 | credential_id, kwargs): |
||
| 544 | """Generates xml string for create scanner on gvmd.""" |
||
| 545 | if not name: |
||
| 546 | raise ValueError('create_scanner requires a name element') |
||
| 547 | if not host: |
||
| 548 | raise ValueError('create_scanner requires a host element') |
||
| 549 | if not port: |
||
| 550 | raise ValueError('create_scanner requires a port element') |
||
| 551 | if not type: |
||
| 552 | raise ValueError('create_scanner requires a type element') |
||
| 553 | if not ca_pub: |
||
| 554 | raise ValueError('create_scanner requires a ca_pub element') |
||
| 555 | if not credential_id: |
||
| 556 | raise ValueError('create_scanner requires a credential_id element') |
||
| 557 | |||
| 558 | cmd = XmlCommand('create_scanner') |
||
| 559 | cmd.add_element('name', name) |
||
| 560 | cmd.add_element('host', host) |
||
| 561 | cmd.add_element('port', port) |
||
| 562 | cmd.add_element('type', type) |
||
| 563 | cmd.add_element('ca_pub', ca_pub) |
||
| 564 | cmd.add_element('credential', attrs={'id': str(credential_id)}) |
||
| 565 | |||
| 566 | comment = kwargs.get('comment', '') |
||
| 567 | if comment: |
||
| 568 | cmd.add_element('comment', comment) |
||
| 569 | |||
| 570 | copy = kwargs.get('copy', '') |
||
| 571 | if copy: |
||
| 572 | cmd.add_element('copy', copy) |
||
| 573 | |||
| 574 | return cmd.to_string() |
||
| 575 | |||
| 576 | View Code Duplication | def create_schedule_command(self, name, kwargs): |
|
| 577 | """Generates xml string for create schedule on gvmd.""" |
||
| 578 | if not name: |
||
| 579 | raise ValueError('create_schedule requires a name element') |
||
| 580 | |||
| 581 | cmd = XmlCommand('create_schedule') |
||
| 582 | cmd.add_element('name', name) |
||
| 583 | |||
| 584 | comment = kwargs.get('comment', '') |
||
| 585 | if comment: |
||
| 586 | cmd.add_element('comment', comment) |
||
| 587 | |||
| 588 | copy = kwargs.get('copy', '') |
||
| 589 | if copy: |
||
| 590 | cmd.add_element('copy', copy) |
||
| 591 | |||
| 592 | first_time = kwargs.get('first_time', '') |
||
| 593 | if first_time: |
||
| 594 | first_time_minute = first_time['minute'] |
||
| 595 | first_time_hour = first_time['hour'] |
||
| 596 | first_time_day_of_month = first_time['day_of_month'] |
||
| 597 | first_time_month = first_time['month'] |
||
| 598 | first_time_year = first_time['year'] |
||
| 599 | |||
| 600 | _xmlftime = cmd.add_element('first_time') |
||
| 601 | _xmlftime.add_element('minute', first_time_minute) |
||
| 602 | _xmlftime.add_element('hour', str(first_time_hour)) |
||
| 603 | _xmlftime.add_element('day_of_month', str(first_time_day_of_month)) |
||
| 604 | _xmlftime.add_element('month', str(first_time_month)) |
||
| 605 | _xmlftime.add_element('year', str(first_time_year)) |
||
| 606 | |||
| 607 | duration = kwargs.get('duration', '') |
||
| 608 | if len(duration) > 1: |
||
| 609 | _xmlduration = cmd.add_element('duration', str(duration[0])) |
||
| 610 | _xmlduration.add_element('unit', str(duration[1])) |
||
| 611 | |||
| 612 | period = kwargs.get('period', '') |
||
| 613 | if len(period) > 1: |
||
| 614 | _xmlperiod = cmd.add_element('period', str(period[0])) |
||
| 615 | _xmlperiod.add_element('unit', str(period[1])) |
||
| 616 | |||
| 617 | timezone = kwargs.get('timezone', '') |
||
| 618 | if timezone: |
||
| 619 | cmd.add_element('timezone', str(timezone)) |
||
| 620 | |||
| 621 | return cmd.to_string() |
||
| 622 | |||
| 623 | def create_tag_command(self, name, resource_id, resource_type, kwargs): |
||
| 624 | """Generates xml string for create tag on gvmd.""" |
||
| 625 | |||
| 626 | cmd = XmlCommand('create_tag') |
||
| 627 | cmd.add_element('name', name) |
||
| 628 | _xmlresource = cmd.add_element('resource', |
||
| 629 | attrs={'id': str(resource_id)}) |
||
| 630 | _xmlresource.add_element('type', resource_type) |
||
| 631 | |||
| 632 | comment = kwargs.get('comment', '') |
||
| 633 | if comment: |
||
| 634 | cmd.add_element('comment', comment) |
||
| 635 | |||
| 636 | copy = kwargs.get('copy', '') |
||
| 637 | if copy: |
||
| 638 | cmd.add_element('copy', copy) |
||
| 639 | |||
| 640 | value = kwargs.get('value', '') |
||
| 641 | if value: |
||
| 642 | cmd.add_element('value', value) |
||
| 643 | |||
| 644 | active = kwargs.get('active', '') |
||
| 645 | if active: |
||
| 646 | cmd.add_element('active', active) |
||
| 647 | |||
| 648 | return cmd.to_string() |
||
| 649 | |||
| 650 | def create_target_command(self, name, make_unique, kwargs): |
||
| 651 | """Generates xml string for create target on gvmd.""" |
||
| 652 | if not name: |
||
| 653 | raise ValueError('create_target requires a name element') |
||
| 654 | |||
| 655 | cmd = XmlCommand('create_target') |
||
| 656 | _xmlname = cmd.add_element('name', name) |
||
| 657 | if make_unique: |
||
| 658 | _xmlname.add_element('make_unique', '1') |
||
| 659 | else: |
||
| 660 | _xmlname.add_element('make_unique', '0') |
||
| 661 | |||
| 662 | if 'asset_hosts' in kwargs: |
||
| 663 | hosts = kwargs.get('asset_hosts') |
||
| 664 | filter = hosts['filter'] |
||
| 665 | cmd.add_element('asset_hosts', attrs={'filter': str(filter)}) |
||
| 666 | elif 'hosts' in kwargs: |
||
| 667 | hosts = kwargs.get('hosts') |
||
| 668 | cmd.add_element('hosts', hosts) |
||
| 669 | else: |
||
| 670 | raise ValueError('create_target requires either a hosts or ' |
||
| 671 | 'an asset_hosts element') |
||
| 672 | |||
| 673 | if 'comment' in kwargs: |
||
| 674 | cmd.add_element('comment', kwargs.get('comment')) |
||
| 675 | |||
| 676 | if 'copy' in kwargs: |
||
| 677 | # NOTE: It seems that hosts/asset_hosts is silently ignored by the |
||
| 678 | # server when copy is supplied. But for specification conformance |
||
| 679 | # we raise the ValueError above and consider copy optional. |
||
| 680 | cmd.add_element('copy', kwargs.get('copy')) |
||
| 681 | |||
| 682 | if 'exclude_hosts' in kwargs: |
||
| 683 | cmd.add_element('exclude_hosts', kwargs.get('exclude_hosts')) |
||
| 684 | |||
| 685 | if 'ssh_credential' in kwargs: |
||
| 686 | ssh_credential = kwargs.get('ssh_credential') |
||
| 687 | if 'id' in ssh_credential: |
||
| 688 | _xmlssh = cmd.add_element('ssh_credential', '', |
||
| 689 | attrs={'id': ssh_credential['id']}) |
||
| 690 | if 'port' in ssh_credential: |
||
| 691 | _xmlssh.add_element('port', ssh_credential['port']) |
||
| 692 | else: |
||
| 693 | raise ValueError('ssh_credential requires an id attribute') |
||
| 694 | |||
| 695 | if 'smb_credential' in kwargs: |
||
| 696 | smb_credential = kwargs.get('smb_credential') |
||
| 697 | if 'id' in smb_credential: |
||
| 698 | cmd.add_element('smb_credential', |
||
| 699 | attrs={'id': smb_credential['id']}) |
||
| 700 | else: |
||
| 701 | raise ValueError('smb_credential requires an id attribute') |
||
| 702 | |||
| 703 | if 'esxi_credential' in kwargs: |
||
| 704 | esxi_credential = kwargs.get('esxi_credential') |
||
| 705 | if 'id' in esxi_credential: |
||
| 706 | cmd.add_element('esxi_credential', |
||
| 707 | attrs={'id': esxi_credential['id']}) |
||
| 708 | else: |
||
| 709 | raise ValueError('esxi_credential requires an id attribute') |
||
| 710 | |||
| 711 | if 'snmp_credential' in kwargs: |
||
| 712 | snmp_credential = kwargs.get('snmp_credential') |
||
| 713 | if 'id' in snmp_credential: |
||
| 714 | cmd.add_element('snmp_credential', |
||
| 715 | attrs={'id': snmp_credential['id']}) |
||
| 716 | else: |
||
| 717 | raise ValueError('snmp_credential requires an id attribute') |
||
| 718 | |||
| 719 | if 'alive_tests' in kwargs: |
||
| 720 | # NOTE: As the alive_tests are referenced by their name and some |
||
| 721 | # names contain ampersand ('&') characters it should be considered |
||
| 722 | # replacing any characters special to XML in the variable with |
||
| 723 | # their corresponding entities. |
||
| 724 | cmd.add_element('alive_tests', kwargs.get('alive_tests')) |
||
| 725 | |||
| 726 | if 'reverse_lookup_only' in kwargs: |
||
| 727 | reverse_lookup_only = kwargs.get('reverse_lookup_only') |
||
| 728 | if reverse_lookup_only: |
||
| 729 | cmd.add_element('reverse_lookup_only', '1') |
||
| 730 | else: |
||
| 731 | cmd.add_element('reverse_lookup_only', '0') |
||
| 732 | |||
| 733 | if 'reverse_lookup_unify' in kwargs: |
||
| 734 | reverse_lookup_unify = kwargs.get('reverse_lookup_unify') |
||
| 735 | if reverse_lookup_unify: |
||
| 736 | cmd.add_element('reverse_lookup_unify', '1') |
||
| 737 | else: |
||
| 738 | cmd.add_element('reverse_lookup_unify', '0') |
||
| 739 | |||
| 740 | if 'port_range' in kwargs: |
||
| 741 | cmd.add_element('port_range', kwargs.get('port_range')) |
||
| 742 | |||
| 743 | if 'port_list' in kwargs: |
||
| 744 | port_list = kwargs.get('port_list') |
||
| 745 | if 'id' in port_list: |
||
| 746 | cmd.add_element('port_list', |
||
| 747 | attrs={'id': str(port_list['id'])}) |
||
| 748 | else: |
||
| 749 | raise ValueError('port_list requires an id attribute') |
||
| 750 | |||
| 751 | return cmd.to_string() |
||
| 752 | |||
| 753 | def create_task_command(self, name, config_id, target_id, scanner_id, |
||
| 754 | alert_ids=None, comment=''): |
||
| 755 | """Generates xml string for create task on gvmd.""" |
||
| 756 | |||
| 757 | if alert_ids is None: |
||
| 758 | alert_ids = [] |
||
| 759 | cmd = XmlCommand('create_task') |
||
| 760 | cmd.add_element('name', name) |
||
| 761 | cmd.add_element('comment', comment) |
||
| 762 | cmd.add_element('config', attrs={'id': config_id}) |
||
| 763 | cmd.add_element('target', attrs={'id': target_id}) |
||
| 764 | cmd.add_element('scanner', attrs={'id': scanner_id}) |
||
| 765 | |||
| 766 | #if given the alert_id is wrapped and integrated suitably as xml |
||
| 767 | if len(alert_ids) > 0: |
||
| 768 | if isinstance(alert_ids, str): |
||
| 769 | #if a single id is given as a string wrap it into a list |
||
| 770 | alert_ids = [alert_ids] |
||
| 771 | if isinstance(alert_ids, list): |
||
| 772 | #parse all given alert id's |
||
| 773 | for alert in alert_ids: |
||
| 774 | cmd.add_element('alert', attrs={'id': str(alert)}) |
||
| 775 | |||
| 776 | return cmd.to_string() |
||
| 777 | |||
| 778 | def create_user_command(self, name, password, copy='', hosts_allow='0', |
||
| 779 | ifaces_allow='0', role_ids=(), hosts=None, |
||
| 780 | ifaces=None): |
||
| 781 | """Generates xml string for create user on gvmd.""" |
||
| 782 | cmd = XmlCommand('create_user') |
||
| 783 | cmd.add_element('name', name) |
||
| 784 | |||
| 785 | if copy: |
||
| 786 | cmd.add_element('copy', copy) |
||
| 787 | |||
| 788 | if password: |
||
| 789 | cmd.add_element('password', password) |
||
| 790 | |||
| 791 | if hosts is not None: |
||
| 792 | cmd.add_element('hosts', hosts, attrs={'allow': str(hosts_allow)}) |
||
| 793 | |||
| 794 | if ifaces is not None: |
||
| 795 | cmd.add_element('ifaces', ifaces, |
||
| 796 | attrs={'allow': str(ifaces_allow)}) |
||
| 797 | |||
| 798 | if len(role_ids) > 0: |
||
| 799 | for role in role_ids: |
||
| 800 | cmd.add_element('role', attrs={'allow': str(role)}) |
||
| 801 | |||
| 802 | return cmd.to_string() |
||
| 803 | |||
| 804 | def modify_agent_command(self, agent_id, name='', comment=''): |
||
| 805 | """Generates xml string for modify agent on gvmd.""" |
||
| 806 | if not agent_id: |
||
| 807 | raise ValueError('modify_agent requires an agent_id element') |
||
| 808 | |||
| 809 | cmd = XmlCommand('modify_agent') |
||
| 810 | cmd.set_attribute('agent_id', str(agent_id)) |
||
| 811 | if name: |
||
| 812 | cmd.add_element('name', name) |
||
| 813 | if comment: |
||
| 814 | cmd.add_element('comment', comment) |
||
| 815 | |||
| 816 | return cmd.to_string() |
||
| 817 | |||
| 818 | def modify_alert_command(self, alert_id, kwargs): |
||
| 819 | """Generates xml string for modify alert on gvmd.""" |
||
| 820 | |||
| 821 | if not alert_id: |
||
| 822 | raise ValueError('modify_alert requires an agent_id element') |
||
| 823 | |||
| 824 | cmd = XmlCommand('modify_alert') |
||
| 825 | cmd.set_attribute('alert_id', str(alert_id)) |
||
| 826 | |||
| 827 | name = kwargs.get('name', '') |
||
| 828 | if name: |
||
| 829 | cmd.add_element('name', name) |
||
| 830 | |||
| 831 | comment = kwargs.get('comment', '') |
||
| 832 | if comment: |
||
| 833 | cmd.add_element('comment', comment) |
||
| 834 | |||
| 835 | filter_id = kwargs.get('filter_id', '') |
||
| 836 | if filter_id: |
||
| 837 | cmd.add_element('filter', attrs={'id': filter_id}) |
||
| 838 | |||
| 839 | event = kwargs.get('event', '') |
||
| 840 | if len(event) > 1: |
||
| 841 | _xmlevent = cmd.add_element('event', event[0]) |
||
| 842 | for value, key in event[1].items(): |
||
| 843 | _xmldata = _xmlevent.add_element('data', value) |
||
| 844 | _xmldata.add_element('name', key) |
||
| 845 | |||
| 846 | condition = kwargs.get('condition', '') |
||
| 847 | if len(condition) > 1: |
||
| 848 | _xmlcond = cmd.add_element('condition', condition[0]) |
||
| 849 | for value, key in condition[1].items(): |
||
| 850 | _xmldata = _xmlcond.add_element('data', value) |
||
| 851 | _xmldata.add_element('name', key) |
||
| 852 | |||
| 853 | method = kwargs.get('method', '') |
||
| 854 | if len(method) > 1: |
||
| 855 | _xmlmethod = cmd.add_element('method', method[0]) |
||
| 856 | for value, key in method[1].items(): |
||
| 857 | _xmldata = _xmlmethod.add_element('data', value) |
||
| 858 | _xmldata.add_element('name', key) |
||
| 859 | |||
| 860 | return cmd.to_string() |
||
| 861 | |||
| 862 | def modify_auth_command(self, group_name, auth_conf_settings): |
||
| 863 | """Generates xml string for modify auth on gvmd.""" |
||
| 864 | if not group_name: |
||
| 865 | raise ValueError('modify_auth requires a group element ' |
||
| 866 | 'with a name attribute') |
||
| 867 | if not auth_conf_settings: |
||
| 868 | raise ValueError('modify_auth requires ' |
||
| 869 | 'an auth_conf_settings element') |
||
| 870 | cmd = XmlCommand('modify_auth') |
||
| 871 | _xmlgroup = cmd.add_element('group', attrs={'name': str(group_name)}) |
||
| 872 | |||
| 873 | for key, value in auth_conf_settings.items(): |
||
| 874 | _xmlauthconf = _xmlgroup.add_element('auth_conf_setting') |
||
| 875 | _xmlauthconf.add_element('key', key) |
||
| 876 | _xmlauthconf.add_element('value', value) |
||
| 877 | |||
| 878 | return cmd.to_string() |
||
| 879 | |||
| 880 | def modify_config_command(self, selection, kwargs): |
||
| 881 | """Generates xml string for modify config on gvmd.""" |
||
| 882 | if selection not in ('nvt_pref', 'sca_pref', |
||
| 883 | 'family_selection', 'nvt_selection'): |
||
| 884 | raise ValueError('selection must be one of nvt_pref, sca_pref, ' |
||
| 885 | 'family_selection or nvt_selection') |
||
| 886 | config_id = kwargs.get('config_id') |
||
| 887 | |||
| 888 | cmd = XmlCommand('modify_config') |
||
| 889 | cmd.set_attribute('config_id', str(config_id)) |
||
| 890 | |||
| 891 | if selection in 'nvt_pref': |
||
| 892 | nvt_oid = kwargs.get('nvt_oid') |
||
| 893 | name = kwargs.get('name') |
||
| 894 | value = kwargs.get('value') |
||
| 895 | _xmlpref = cmd.add_element('preference') |
||
| 896 | _xmlpref.add_element('nvt', attrs={'oid': nvt_oid}) |
||
| 897 | _xmlpref.add_element('name', name) |
||
| 898 | _xmlpref.add_element('value', value) |
||
| 899 | |||
| 900 | elif selection in 'nvt_selection': |
||
| 901 | nvt_oid = kwargs.get('nvt_oid') |
||
| 902 | family = kwargs.get('family') |
||
| 903 | _xmlnvtsel = cmd.add_element('nvt_selection') |
||
| 904 | _xmlnvtsel.add_element('family', family) |
||
| 905 | |||
| 906 | if isinstance(nvt_oid, list): |
||
| 907 | for nvt in nvt_oid: |
||
| 908 | _xmlnvtsel.add_element('nvt', attrs={'oid': nvt}) |
||
| 909 | else: |
||
| 910 | _xmlnvtsel.add_element('nvt', attrs={'oid': nvt}) |
||
| 911 | |||
| 912 | elif selection in 'family_selection': |
||
| 913 | family = kwargs.get('family') |
||
| 914 | _xmlfamsel = cmd.add_element('family_selection') |
||
| 915 | _xmlfamsel.add_element('growing', '1') |
||
| 916 | _xmlfamily = _xmlfamsel.add_element('family') |
||
| 917 | _xmlfamily.add_element('name', family) |
||
| 918 | _xmlfamily.add_element('all', '1') |
||
| 919 | _xmlfamily.add_element('growing', '1') |
||
| 920 | else: |
||
| 921 | raise NotImplementedError |
||
| 922 | |||
| 923 | return cmd.to_string() |
||
| 924 | |||
| 925 | View Code Duplication | def modify_credential_command(self, credential_id, kwargs): |
|
| 926 | """Generates xml string for modify credential on gvmd.""" |
||
| 927 | if not credential_id: |
||
| 928 | raise ValueError('modify_credential requires ' |
||
| 929 | 'a credential_id attribute') |
||
| 930 | |||
| 931 | cmd = XmlCommand('modify_credential') |
||
| 932 | cmd.set_attribute('credential_id', credential_id) |
||
| 933 | |||
| 934 | comment = kwargs.get('comment', '') |
||
| 935 | if comment: |
||
| 936 | cmd.add_element('comment', comment) |
||
| 937 | |||
| 938 | name = kwargs.get('name', '') |
||
| 939 | if name: |
||
| 940 | cmd.add_element('name', name) |
||
| 941 | |||
| 942 | allow_insecure = kwargs.get('allow_insecure', '') |
||
| 943 | if allow_insecure: |
||
| 944 | cmd.add_element('allow_insecure', allow_insecure) |
||
| 945 | |||
| 946 | certificate = kwargs.get('certificate', '') |
||
| 947 | if certificate: |
||
| 948 | cmd.add_element('certificate', certificate) |
||
| 949 | |||
| 950 | key = kwargs.get('key', '') |
||
| 951 | if key: |
||
| 952 | phrase = key['phrase'] |
||
| 953 | private = key['private'] |
||
| 954 | if not phrase: |
||
| 955 | raise ValueError('modify_credential requires a phrase element') |
||
| 956 | if not private: |
||
| 957 | raise ValueError('modify_credential requires ' |
||
| 958 | 'a private element') |
||
| 959 | _xmlkey = cmd.add_element('key') |
||
| 960 | _xmlkey.add_element('phrase', phrase) |
||
| 961 | _xmlkey.add_element('private', private) |
||
| 962 | |||
| 963 | login = kwargs.get('login', '') |
||
| 964 | if login: |
||
| 965 | cmd.add_element('login', login) |
||
| 966 | |||
| 967 | password = kwargs.get('password', '') |
||
| 968 | if password: |
||
| 969 | cmd.add_element('password', password) |
||
| 970 | |||
| 971 | auth_algorithm = kwargs.get('auth_algorithm', '') |
||
| 972 | if auth_algorithm: |
||
| 973 | if auth_algorithm not in ('md5', 'sha1'): |
||
| 974 | raise ValueError('modify_credential requires auth_algorithm ' |
||
| 975 | 'to be either md5 or sha1') |
||
| 976 | cmd.add_element('auth_algorithm', auth_algorithm) |
||
| 977 | |||
| 978 | community = kwargs.get('community', '') |
||
| 979 | if community: |
||
| 980 | cmd.add_element('community', community) |
||
| 981 | |||
| 982 | privacy = kwargs.get('privacy', '') |
||
| 983 | if privacy: |
||
| 984 | algorithm = privacy.algorithm |
||
| 985 | if algorithm not in ('aes', 'des'): |
||
| 986 | raise ValueError('modify_credential requires algorithm ' |
||
| 987 | 'to be either aes or des') |
||
| 988 | p_password = privacy.password |
||
| 989 | _xmlprivacy = cmd.add_element('privacy') |
||
| 990 | _xmlprivacy.add_element('algorithm', algorithm) |
||
| 991 | _xmlprivacy.add_element('password', p_password) |
||
| 992 | |||
| 993 | cred_type = kwargs.get('type', '') |
||
| 994 | if cred_type: |
||
| 995 | if cred_type not in ('cc', 'snmp', 'up', 'usk'): |
||
| 996 | raise ValueError('modify_credential requires type ' |
||
| 997 | 'to be either cc, snmp, up or usk') |
||
| 998 | cmd.add_element('type', cred_type) |
||
| 999 | |||
| 1000 | return cmd.to_string() |
||
| 1001 | |||
| 1002 | def modify_filter_command(self, filter_id, kwargs): |
||
| 1003 | """Generates xml string for modify filter on gvmd.""" |
||
| 1004 | if not filter_id: |
||
| 1005 | raise ValueError('modify_filter requires a filter_id attribute') |
||
| 1006 | |||
| 1007 | cmd = XmlCommand('modify_filter') |
||
| 1008 | cmd.set_attribute('filter_id', filter_id) |
||
| 1009 | |||
| 1010 | comment = kwargs.get('comment', '') |
||
| 1011 | if comment: |
||
| 1012 | cmd.add_element('comment', comment) |
||
| 1013 | |||
| 1014 | name = kwargs.get('name', '') |
||
| 1015 | if name: |
||
| 1016 | cmd.add_element('name', name) |
||
| 1017 | |||
| 1018 | copy = kwargs.get('copy', '') |
||
| 1019 | if copy: |
||
| 1020 | cmd.add_element('copy', copy) |
||
| 1021 | |||
| 1022 | term = kwargs.get('term', '') |
||
| 1023 | if term: |
||
| 1024 | cmd.add_element('term', term) |
||
| 1025 | |||
| 1026 | filter_type = kwargs.get('type', '') |
||
| 1027 | if filter_type: |
||
| 1028 | if filter_type not in ('cc', 'snmp', 'up', 'usk'): |
||
| 1029 | raise ValueError('modify_filter requires type ' |
||
| 1030 | 'to be either cc, snmp, up or usk') |
||
| 1031 | cmd.add_element('type', filter_type) |
||
| 1032 | |||
| 1033 | return cmd.to_string() |
||
| 1034 | |||
| 1035 | View Code Duplication | def modify_group_command(self, group_id, kwargs): |
|
| 1036 | """Generates xml string for modify group on gvmd.""" |
||
| 1037 | if not group_id: |
||
| 1038 | raise ValueError('modify_group requires a group_id attribute') |
||
| 1039 | |||
| 1040 | cmd = XmlCommand('modify_group') |
||
| 1041 | cmd.set_attribute('group_id', group_id) |
||
| 1042 | |||
| 1043 | comment = kwargs.get('comment', '') |
||
| 1044 | if comment: |
||
| 1045 | cmd.add_element('comment', comment) |
||
| 1046 | |||
| 1047 | name = kwargs.get('name', '') |
||
| 1048 | if name: |
||
| 1049 | cmd.add_element('name', name) |
||
| 1050 | |||
| 1051 | users = kwargs.get('users', '') |
||
| 1052 | if users: |
||
| 1053 | cmd.add_element('users', users) |
||
| 1054 | |||
| 1055 | return cmd.to_string() |
||
| 1056 | |||
| 1057 | def modify_note_command(self, note_id, text, kwargs): |
||
| 1058 | """Generates xml string for modify note on gvmd.""" |
||
| 1059 | if not note_id: |
||
| 1060 | raise ValueError('modify_note requires a note_id attribute') |
||
| 1061 | if not text: |
||
| 1062 | raise ValueError('modify_note requires a text element') |
||
| 1063 | |||
| 1064 | cmd = XmlCommand('modify_note') |
||
| 1065 | cmd.set_attribute('note_id', note_id) |
||
| 1066 | cmd.add_element('text', text) |
||
| 1067 | |||
| 1068 | active = kwargs.get('active', '') |
||
| 1069 | if active: |
||
| 1070 | cmd.add_element('active', active) |
||
| 1071 | |||
| 1072 | hosts = kwargs.get('hosts', '') |
||
| 1073 | if hosts: |
||
| 1074 | cmd.add_element('hosts', hosts) |
||
| 1075 | |||
| 1076 | port = kwargs.get('port', '') |
||
| 1077 | if port: |
||
| 1078 | cmd.add_element('port', port) |
||
| 1079 | |||
| 1080 | result_id = kwargs.get('result_id', '') |
||
| 1081 | if result_id: |
||
| 1082 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 1083 | |||
| 1084 | severity = kwargs.get('severity', '') |
||
| 1085 | if severity: |
||
| 1086 | cmd.add_element('severity', severity) |
||
| 1087 | |||
| 1088 | task_id = kwargs.get('task_id', '') |
||
| 1089 | if task_id: |
||
| 1090 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 1091 | |||
| 1092 | threat = kwargs.get('threat', '') |
||
| 1093 | if threat: |
||
| 1094 | cmd.add_element('threat', threat) |
||
| 1095 | |||
| 1096 | return cmd.to_string() |
||
| 1097 | |||
| 1098 | def modify_override_command(self, override_id, text, kwargs): |
||
| 1099 | """Generates xml string for modify override on gvmd.""" |
||
| 1100 | cmd = XmlCommand('modify_override') |
||
| 1101 | cmd.set_attribute('override_id', override_id) |
||
| 1102 | cmd.add_element('text', text) |
||
| 1103 | |||
| 1104 | active = kwargs.get('active', '') |
||
| 1105 | if active: |
||
| 1106 | cmd.add_element('active', active) |
||
| 1107 | |||
| 1108 | hosts = kwargs.get('hosts', '') |
||
| 1109 | if hosts: |
||
| 1110 | cmd.add_element('hosts', hosts) |
||
| 1111 | |||
| 1112 | port = kwargs.get('port', '') |
||
| 1113 | if port: |
||
| 1114 | cmd.add_element('port', port) |
||
| 1115 | |||
| 1116 | result_id = kwargs.get('result_id', '') |
||
| 1117 | if result_id: |
||
| 1118 | cmd.add_element('result', attrs={'id': result_id}) |
||
| 1119 | |||
| 1120 | severity = kwargs.get('severity', '') |
||
| 1121 | if severity: |
||
| 1122 | cmd.add_element('severity', severity) |
||
| 1123 | |||
| 1124 | new_severity = kwargs.get('new_severity', '') |
||
| 1125 | if new_severity: |
||
| 1126 | cmd.add_element('new_severity', new_severity) |
||
| 1127 | |||
| 1128 | task_id = kwargs.get('task_id', '') |
||
| 1129 | if task_id: |
||
| 1130 | cmd.add_element('task', attrs={'id': task_id}) |
||
| 1131 | |||
| 1132 | threat = kwargs.get('threat', '') |
||
| 1133 | if threat: |
||
| 1134 | cmd.add_element('threat', threat) |
||
| 1135 | |||
| 1136 | new_threat = kwargs.get('new_threat', '') |
||
| 1137 | if new_threat: |
||
| 1138 | cmd.add_element('new_threat', new_threat) |
||
| 1139 | |||
| 1140 | return cmd.to_string() |
||
| 1141 | |||
| 1142 | def modify_permission_command(self, permission_id, kwargs): |
||
| 1143 | """Generates xml string for modify permission on gvmd.""" |
||
| 1144 | if not permission_id: |
||
| 1145 | raise ValueError('modify_permission requires ' |
||
| 1146 | 'a permission_id element') |
||
| 1147 | |||
| 1148 | cmd = XmlCommand('modify_permission') |
||
| 1149 | cmd.set_attribute('permission_id', permission_id) |
||
| 1150 | |||
| 1151 | comment = kwargs.get('comment', '') |
||
| 1152 | if comment: |
||
| 1153 | cmd.add_element('comment', comment) |
||
| 1154 | |||
| 1155 | name = kwargs.get('name', '') |
||
| 1156 | if name: |
||
| 1157 | cmd.add_element('name', name) |
||
| 1158 | |||
| 1159 | resource = kwargs.get('resource', '') |
||
| 1160 | if resource: |
||
| 1161 | resource_id = resource['id'] |
||
| 1162 | resource_type = resource['type'] |
||
| 1163 | _xmlresource = cmd.add_element('resource', |
||
| 1164 | attrs={'id': resource_id}) |
||
| 1165 | _xmlresource.add_element('type', resource_type) |
||
| 1166 | |||
| 1167 | subject = kwargs.get('subject', '') |
||
| 1168 | if subject: |
||
| 1169 | subject_id = subject['id'] |
||
| 1170 | subject_type = subject['type'] |
||
| 1171 | _xmlsubject = cmd.add_element('subject', attrs={'id': subject_id}) |
||
| 1172 | _xmlsubject.add_element('type', subject_type) |
||
| 1173 | |||
| 1174 | return cmd.to_string() |
||
| 1175 | |||
| 1176 | def modify_port_list_command(self, port_list_id, kwargs): |
||
| 1177 | """Generates xml string for modify port list on gvmd.""" |
||
| 1178 | if not port_list_id: |
||
| 1179 | raise ValueError('modify_port_list requires ' |
||
| 1180 | 'a port_list_id attribute') |
||
| 1181 | cmd = XmlCommand('modify_port_list') |
||
| 1182 | cmd.set_attribute('port_list_id', port_list_id) |
||
| 1183 | |||
| 1184 | comment = kwargs.get('comment', '') |
||
| 1185 | if comment: |
||
| 1186 | cmd.add_element('comment', comment) |
||
| 1187 | |||
| 1188 | name = kwargs.get('name', '') |
||
| 1189 | if name: |
||
| 1190 | cmd.add_element('name', name) |
||
| 1191 | |||
| 1192 | return cmd.to_string() |
||
| 1193 | |||
| 1194 | def modify_report_format_command(self, report_format_id, kwargs): |
||
| 1195 | """Generates xml string for modify report format on gvmd.""" |
||
| 1196 | if len(kwargs) < 1: |
||
| 1197 | raise Exception('modify_report_format: Missing parameter') |
||
| 1198 | |||
| 1199 | cmd = XmlCommand('modify_report_format') |
||
| 1200 | cmd.set_attribute('report_format_id', report_format_id) |
||
| 1201 | |||
| 1202 | active = kwargs.get('active', '') |
||
| 1203 | if active: |
||
| 1204 | cmd.add_element('active', active) |
||
| 1205 | |||
| 1206 | name = kwargs.get('name', '') |
||
| 1207 | if name: |
||
| 1208 | cmd.add_element('name', name) |
||
| 1209 | |||
| 1210 | summary = kwargs.get('summary', '') |
||
| 1211 | if summary: |
||
| 1212 | cmd.add_element('summary', summary) |
||
| 1213 | |||
| 1214 | param = kwargs.get('param', '') |
||
| 1215 | if param: |
||
| 1216 | p_name = param[0] |
||
| 1217 | p_value = param[1] |
||
| 1218 | _xmlparam = cmd.add_element('param') |
||
| 1219 | _xmlparam.add_element('name', p_name) |
||
| 1220 | _xmlparam.add_element('value', p_value) |
||
| 1221 | |||
| 1222 | return cmd.to_string() |
||
| 1223 | |||
| 1224 | View Code Duplication | def modify_role_command(self, role_id, kwargs): |
|
| 1225 | """Generates xml string for modify role on gvmd.""" |
||
| 1226 | if not role_id: |
||
| 1227 | raise ValueError('modify_role requires a role_id element') |
||
| 1228 | |||
| 1229 | cmd = XmlCommand('modify_role') |
||
| 1230 | cmd.set_attribute('role_id', role_id) |
||
| 1231 | |||
| 1232 | comment = kwargs.get('comment', '') |
||
| 1233 | if comment: |
||
| 1234 | cmd.add_element('comment', comment) |
||
| 1235 | |||
| 1236 | name = kwargs.get('name', '') |
||
| 1237 | if name: |
||
| 1238 | cmd.add_element('name', name) |
||
| 1239 | |||
| 1240 | users = kwargs.get('users', '') |
||
| 1241 | if users: |
||
| 1242 | cmd.add_element('users', users) |
||
| 1243 | |||
| 1244 | return cmd.to_string() |
||
| 1245 | |||
| 1246 | def modify_scanner_command(self, scanner_id, host, port, scanner_type, |
||
| 1247 | kwargs): |
||
| 1248 | """Generates xml string for modify scanner on gvmd.""" |
||
| 1249 | if not scanner_id: |
||
| 1250 | raise ValueError('modify_scanner requires a scanner_id element') |
||
| 1251 | if not host: |
||
| 1252 | raise ValueError('modify_scanner requires a host element') |
||
| 1253 | if not port: |
||
| 1254 | raise ValueError('modify_scanner requires a port element') |
||
| 1255 | if not scanner_type: |
||
| 1256 | raise ValueError('modify_scanner requires a type element') |
||
| 1257 | |||
| 1258 | cmd = XmlCommand('modify_scanner') |
||
| 1259 | cmd.set_attribute('scanner_id', scanner_id) |
||
| 1260 | cmd.add_element('host', host) |
||
| 1261 | cmd.add_element('port', port) |
||
| 1262 | cmd.add_element('type', scanner_type) |
||
| 1263 | |||
| 1264 | comment = kwargs.get('comment', '') |
||
| 1265 | if comment: |
||
| 1266 | cmd.add_element('comment', comment) |
||
| 1267 | |||
| 1268 | name = kwargs.get('name', '') |
||
| 1269 | if name: |
||
| 1270 | cmd.add_element('name', name) |
||
| 1271 | |||
| 1272 | ca_pub = kwargs.get('ca_pub', '') |
||
| 1273 | if ca_pub: |
||
| 1274 | cmd.add_element('ca_pub', ca_pub) |
||
| 1275 | |||
| 1276 | credential_id = kwargs.get('credential_id', '') |
||
| 1277 | if credential_id: |
||
| 1278 | cmd.add_element('credential', attrs={'id': str(credential_id)}) |
||
| 1279 | |||
| 1280 | return cmd.to_string() |
||
| 1281 | |||
| 1282 | View Code Duplication | def modify_schedule_command(self, schedule_id, kwargs): |
|
| 1283 | """Generates xml string for modify schedule on gvmd.""" |
||
| 1284 | if not schedule_id: |
||
| 1285 | raise ValueError('modify_schedule requires a schedule_id element') |
||
| 1286 | |||
| 1287 | cmd = XmlCommand('modify_schedule') |
||
| 1288 | cmd.set_attribute('schedule_id', schedule_id) |
||
| 1289 | comment = kwargs.get('comment', '') |
||
| 1290 | if comment: |
||
| 1291 | cmd.add_element('comment', comment) |
||
| 1292 | |||
| 1293 | name = kwargs.get('name', '') |
||
| 1294 | if name: |
||
| 1295 | cmd.add_element('name', name) |
||
| 1296 | |||
| 1297 | first_time = kwargs.get('first_time', '') |
||
| 1298 | if first_time: |
||
| 1299 | first_time_minute = first_time['minute'] |
||
| 1300 | first_time_hour = first_time['hour'] |
||
| 1301 | first_time_day_of_month = first_time['day_of_month'] |
||
| 1302 | first_time_month = first_time['month'] |
||
| 1303 | first_time_year = first_time['year'] |
||
| 1304 | |||
| 1305 | _xmlftime = cmd.add_element('first_time') |
||
| 1306 | _xmlftime.add_element('minute', str(first_time_minute)) |
||
| 1307 | _xmlftime.add_element('hour', str(first_time_hour)) |
||
| 1308 | _xmlftime.add_element('day_of_month', str(first_time_day_of_month)) |
||
| 1309 | _xmlftime.add_element('month', str(first_time_month)) |
||
| 1310 | _xmlftime.add_element('year', str(first_time_year)) |
||
| 1311 | |||
| 1312 | duration = kwargs.get('duration', '') |
||
| 1313 | if len(duration) > 1: |
||
| 1314 | _xmlduration = cmd.add_element('duration', str(duration[0])) |
||
| 1315 | _xmlduration.add_element('unit', str(duration[1])) |
||
| 1316 | |||
| 1317 | period = kwargs.get('period', '') |
||
| 1318 | if len(period) > 1: |
||
| 1319 | _xmlperiod = cmd.add_element('period', str(period[0])) |
||
| 1320 | _xmlperiod.add_element('unit', str(period[1])) |
||
| 1321 | |||
| 1322 | timezone = kwargs.get('timezone', '') |
||
| 1323 | if timezone: |
||
| 1324 | cmd.add_element('timezone', str(timezone)) |
||
| 1325 | |||
| 1326 | return cmd.to_string() |
||
| 1327 | |||
| 1328 | def modify_tag_command(self, tag_id, kwargs): |
||
| 1329 | """Generates xml string for modify tag on gvmd.""" |
||
| 1330 | if not tag_id: |
||
| 1331 | raise ValueError('modify_tag requires a tag_id element') |
||
| 1332 | |||
| 1333 | cmd = XmlCommand('modify_tag') |
||
| 1334 | cmd.set_attribute('tag_id', str(tag_id)) |
||
| 1335 | |||
| 1336 | comment = kwargs.get('comment', '') |
||
| 1337 | if comment: |
||
| 1338 | cmd.add_element('comment', comment) |
||
| 1339 | |||
| 1340 | name = kwargs.get('name', '') |
||
| 1341 | if name: |
||
| 1342 | cmd.add_element('name', name) |
||
| 1343 | |||
| 1344 | value = kwargs.get('value', '') |
||
| 1345 | if value: |
||
| 1346 | cmd.add_element('value', value) |
||
| 1347 | |||
| 1348 | active = kwargs.get('active', '') |
||
| 1349 | if active: |
||
| 1350 | cmd.add_element('active', value) |
||
| 1351 | |||
| 1352 | resource = kwargs.get('resource', '') |
||
| 1353 | if resource: |
||
| 1354 | resource_id = resource['id'] |
||
| 1355 | resource_type = resource['type'] |
||
| 1356 | _xmlresource = cmd.add_element('resource', |
||
| 1357 | attrs={'resource_id': resource_id}) |
||
| 1358 | _xmlresource.add_element('type', resource_type) |
||
| 1359 | |||
| 1360 | return cmd.to_string() |
||
| 1361 | |||
| 1362 | def modify_target_command(self, target_id, kwargs): |
||
| 1363 | """Generates xml string for modify target on gvmd.""" |
||
| 1364 | if not target_id: |
||
| 1365 | raise ValueError('modify_target requires a target_id element') |
||
| 1366 | |||
| 1367 | cmd = XmlCommand('modify_target') |
||
| 1368 | cmd.set_attribute('target_id', target_id) |
||
| 1369 | |||
| 1370 | comment = kwargs.get('comment', '') |
||
| 1371 | if comment: |
||
| 1372 | cmd.add_element('comment', comment) |
||
| 1373 | |||
| 1374 | name = kwargs.get('name', '') |
||
| 1375 | if name: |
||
| 1376 | cmd.add_element('name', name) |
||
| 1377 | |||
| 1378 | hosts = kwargs.get('hosts', '') |
||
| 1379 | if hosts: |
||
| 1380 | cmd.add_element('hosts', hosts) |
||
| 1381 | |||
| 1382 | copy = kwargs.get('copy', '') |
||
| 1383 | if copy: |
||
| 1384 | cmd.add_element('copy', copy) |
||
| 1385 | |||
| 1386 | exclude_hosts = kwargs.get('exclude_hosts', '') |
||
| 1387 | if exclude_hosts: |
||
| 1388 | cmd.add_element('exclude_hosts', exclude_hosts) |
||
| 1389 | |||
| 1390 | alive_tests = kwargs.get('alive_tests', '') |
||
| 1391 | if alive_tests: |
||
| 1392 | cmd.add_element('alive_tests', alive_tests) |
||
| 1393 | |||
| 1394 | reverse_lookup_only = kwargs.get('reverse_lookup_only', '') |
||
| 1395 | if reverse_lookup_only: |
||
| 1396 | cmd.add_element('reverse_lookup_only', reverse_lookup_only) |
||
| 1397 | |||
| 1398 | reverse_lookup_unify = kwargs.get('reverse_lookup_unify', '') |
||
| 1399 | if reverse_lookup_unify: |
||
| 1400 | cmd.add_element('reverse_lookup_unify', reverse_lookup_unify) |
||
| 1401 | |||
| 1402 | port_range = kwargs.get('port_range', '') |
||
| 1403 | if port_range: |
||
| 1404 | cmd.add_element('port_range', port_range) |
||
| 1405 | |||
| 1406 | port_list = kwargs.get('port_list', '') |
||
| 1407 | if port_list: |
||
| 1408 | cmd.add_element('port_list', attrs={'id': str(port_list)}) |
||
| 1409 | |||
| 1410 | return cmd.to_string() |
||
| 1411 | |||
| 1412 | def modify_task_command(self, task_id, kwargs): |
||
| 1413 | """Generates xml string for modify task on gvmd.""" |
||
| 1414 | if not task_id: |
||
| 1415 | raise ValueError('modify_task requires a task_id element') |
||
| 1416 | |||
| 1417 | cmd = XmlCommand('modify_task') |
||
| 1418 | cmd.set_attribute('task_id', task_id) |
||
| 1419 | |||
| 1420 | name = kwargs.get('name', '') |
||
| 1421 | if name: |
||
| 1422 | cmd.add_element('name', name) |
||
| 1423 | |||
| 1424 | comment = kwargs.get('comment', '') |
||
| 1425 | if comment: |
||
| 1426 | cmd.add_element('comment', comment) |
||
| 1427 | |||
| 1428 | target_id = kwargs.get('target_id', '') |
||
| 1429 | if target_id: |
||
| 1430 | cmd.add_element('target', attrs={'id': target_id}) |
||
| 1431 | |||
| 1432 | scanner = kwargs.get('scanner', '') |
||
| 1433 | if scanner: |
||
| 1434 | cmd.add_element('scanner', attrs={'id': scanner}) |
||
| 1435 | |||
| 1436 | schedule_periods = kwargs.get('schedule_periods', '') |
||
| 1437 | if schedule_periods: |
||
| 1438 | cmd.add_element('schedule_periods', str(schedule_periods)) |
||
| 1439 | |||
| 1440 | schedule = kwargs.get('schedule', '') |
||
| 1441 | if schedule: |
||
| 1442 | cmd.add_element('schedule', attrs={'id': str(schedule)}) |
||
| 1443 | |||
| 1444 | alert = kwargs.get('alert', '') |
||
| 1445 | if alert: |
||
| 1446 | cmd.add_element('alert', attrs={'id': str(alert)}) |
||
| 1447 | |||
| 1448 | observers = kwargs.get('observers', '') |
||
| 1449 | if observers: |
||
| 1450 | cmd.add_element('observers', str(observers)) |
||
| 1451 | |||
| 1452 | preferences = kwargs.get('preferences', '') |
||
| 1453 | if preferences: |
||
| 1454 | _xmlprefs = cmd.add_element('preferences') |
||
| 1455 | for n in range(len(preferences["scanner_name"])): |
||
| 1456 | preferences_scanner_name = preferences["scanner_name"][n] |
||
| 1457 | preferences_value = preferences["value"][n] |
||
| 1458 | _xmlpref = _xmlprefs.add_element('preference') |
||
| 1459 | _xmlpref.add_element('scanner_name', preferences_scanner_name) |
||
| 1460 | _xmlpref.add_element('value', preferences_value) |
||
| 1461 | |||
| 1462 | file = kwargs.get('file', '') |
||
| 1463 | if file: |
||
| 1464 | file_name = file['name'] |
||
| 1465 | file_action = file['action'] |
||
| 1466 | if file_action != "update" and file_action != "remove": |
||
| 1467 | raise ValueError('action can only be "update" or "remove"!') |
||
| 1468 | cmd.add_element('file', attrs={'name': file_name, |
||
| 1469 | 'action': file_action}) |
||
| 1470 | |||
| 1471 | return cmd.to_string() |
||
| 1472 | |||
| 1473 | def modify_user_command(self, kwargs): |
||
| 1474 | """Generates xml string for modify user on gvmd.""" |
||
| 1475 | user_id = kwargs.get('user_id', '') |
||
| 1476 | name = kwargs.get('name', '') |
||
| 1477 | |||
| 1478 | if not user_id and not name: |
||
| 1479 | raise ValueError('modify_user requires ' |
||
| 1480 | 'either a user_id or a name element') |
||
| 1481 | |||
| 1482 | cmd = XmlCommand('modify_user') |
||
| 1483 | cmd.set_attribute('user_id', str(user_id)) |
||
| 1484 | |||
| 1485 | new_name = kwargs.get('new_name', '') |
||
| 1486 | if new_name: |
||
| 1487 | cmd.add_element('new_name', new_name) |
||
| 1488 | |||
| 1489 | password = kwargs.get('password', '') |
||
| 1490 | if password: |
||
| 1491 | cmd.add_element('password', password) |
||
| 1492 | |||
| 1493 | role_ids = kwargs.get('role_ids', '') |
||
| 1494 | if len(role_ids) > 0: |
||
| 1495 | for role in role_ids: |
||
| 1496 | cmd.add_element('role', attrs={'id': str(role)}) |
||
| 1497 | |||
| 1498 | hosts = kwargs.get('hosts', '') |
||
| 1499 | hosts_allow = kwargs.get('hosts_allow', '') |
||
| 1500 | if hosts or hosts_allow: |
||
| 1501 | cmd.add_element('hosts', hosts, attrs={'allow': str(hosts_allow)}) |
||
| 1502 | |||
| 1503 | ifaces = kwargs.get('ifaces', '') |
||
| 1504 | ifaces_allow = kwargs.get('ifaces_allow', '') |
||
| 1505 | if ifaces or ifaces_allow: |
||
| 1506 | cmd.add_element('ifaces', ifaces, |
||
| 1507 | attrs={'allow': str(ifaces_allow)}) |
||
| 1508 | |||
| 1509 | sources = kwargs.get('sources', '') |
||
| 1510 | if sources: |
||
| 1511 | cmd.add_element('sources', sources) |
||
| 1512 | |||
| 1513 | return cmd.to_string() |
||
| 1514 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.