Total Complexity | 198 |
Total Lines | 1033 |
Duplicated Lines | 4.07 % |
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.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 | class XmlCommandElement: |
||
24 | |||
25 | def __init__(self, element): |
||
26 | self._element = element |
||
27 | |||
28 | def add_element(self, name, text=None, attrs=None): |
||
29 | node = etree.SubElement(self._element, name, attrib=attrs) |
||
30 | node.text = text |
||
31 | return XmlCommandElement(node) |
||
32 | |||
33 | def set_attribute(self, name, value): |
||
34 | self._element.set(name, value) |
||
35 | |||
36 | def set_attributes(self, attrs): |
||
37 | """Set several attributes at once. |
||
38 | |||
39 | Arguments: |
||
40 | attrs (dict): Attributes to be set on the element |
||
41 | """ |
||
42 | for key, value in attrs.items(): |
||
43 | self._element.set(key, value) |
||
44 | |||
45 | def append_xml_str(self, xml_text): |
||
46 | """Append a xml element in string format.""" |
||
47 | node = secET.fromstring(xml_text) |
||
48 | self._element.append(node) |
||
49 | |||
50 | def to_string(self): |
||
51 | return etree.tostring(self._element).decode('utf-8') |
||
52 | |||
53 | def __str__(self): |
||
54 | return self.to_string() |
||
55 | |||
56 | |||
57 | class XmlCommand(XmlCommandElement): |
||
58 | |||
59 | def __init__(self, name): |
||
60 | super().__init__(etree.Element(name)) |
||
61 | |||
62 | |||
63 | class _GmpCommandFactory: |
||
64 | |||
65 | """Factory to create gmp - Greenbone Management Protocol - commands |
||
66 | """ |
||
67 | |||
68 | View Code Duplication | def modify_group_command(self, group_id, kwargs): |
|
69 | """Generates xml string for modify group on gvmd.""" |
||
70 | if not group_id: |
||
71 | raise ValueError('modify_group requires a group_id attribute') |
||
72 | |||
73 | cmd = XmlCommand('modify_group') |
||
74 | cmd.set_attribute('group_id', group_id) |
||
75 | |||
76 | comment = kwargs.get('comment', '') |
||
77 | if comment: |
||
78 | cmd.add_element('comment', comment) |
||
79 | |||
80 | name = kwargs.get('name', '') |
||
81 | if name: |
||
82 | cmd.add_element('name', name) |
||
83 | |||
84 | users = kwargs.get('users', '') |
||
85 | if users: |
||
86 | cmd.add_element('users', users) |
||
87 | |||
88 | return cmd.to_string() |
||
89 | |||
90 | def modify_note_command(self, note_id, text, kwargs): |
||
91 | """Generates xml string for modify note on gvmd.""" |
||
92 | if not note_id: |
||
93 | raise ValueError('modify_note requires a note_id attribute') |
||
94 | if not text: |
||
95 | raise ValueError('modify_note requires a text element') |
||
96 | |||
97 | cmd = XmlCommand('modify_note') |
||
98 | cmd.set_attribute('note_id', note_id) |
||
99 | cmd.add_element('text', text) |
||
100 | |||
101 | active = kwargs.get('active', '') |
||
102 | if active: |
||
103 | cmd.add_element('active', active) |
||
104 | |||
105 | hosts = kwargs.get('hosts', '') |
||
106 | if hosts: |
||
107 | cmd.add_element('hosts', hosts) |
||
108 | |||
109 | port = kwargs.get('port', '') |
||
110 | if port: |
||
111 | cmd.add_element('port', port) |
||
112 | |||
113 | result_id = kwargs.get('result_id', '') |
||
114 | if result_id: |
||
115 | cmd.add_element('result', attrs={'id': result_id}) |
||
116 | |||
117 | severity = kwargs.get('severity', '') |
||
118 | if severity: |
||
119 | cmd.add_element('severity', severity) |
||
120 | |||
121 | task_id = kwargs.get('task_id', '') |
||
122 | if task_id: |
||
123 | cmd.add_element('task', attrs={'id': task_id}) |
||
124 | |||
125 | threat = kwargs.get('threat', '') |
||
126 | if threat: |
||
127 | cmd.add_element('threat', threat) |
||
128 | |||
129 | return cmd.to_string() |
||
130 | |||
131 | def modify_override_command(self, override_id, text, kwargs): |
||
132 | """Generates xml string for modify override on gvmd.""" |
||
133 | cmd = XmlCommand('modify_override') |
||
134 | cmd.set_attribute('override_id', override_id) |
||
135 | cmd.add_element('text', text) |
||
136 | |||
137 | active = kwargs.get('active', '') |
||
138 | if active: |
||
139 | cmd.add_element('active', active) |
||
140 | |||
141 | hosts = kwargs.get('hosts', '') |
||
142 | if hosts: |
||
143 | cmd.add_element('hosts', hosts) |
||
144 | |||
145 | port = kwargs.get('port', '') |
||
146 | if port: |
||
147 | cmd.add_element('port', port) |
||
148 | |||
149 | result_id = kwargs.get('result_id', '') |
||
150 | if result_id: |
||
151 | cmd.add_element('result', attrs={'id': result_id}) |
||
152 | |||
153 | severity = kwargs.get('severity', '') |
||
154 | if severity: |
||
155 | cmd.add_element('severity', severity) |
||
156 | |||
157 | new_severity = kwargs.get('new_severity', '') |
||
158 | if new_severity: |
||
159 | cmd.add_element('new_severity', new_severity) |
||
160 | |||
161 | task_id = kwargs.get('task_id', '') |
||
162 | if task_id: |
||
163 | cmd.add_element('task', attrs={'id': task_id}) |
||
164 | |||
165 | threat = kwargs.get('threat', '') |
||
166 | if threat: |
||
167 | cmd.add_element('threat', threat) |
||
168 | |||
169 | new_threat = kwargs.get('new_threat', '') |
||
170 | if new_threat: |
||
171 | cmd.add_element('new_threat', new_threat) |
||
172 | |||
173 | return cmd.to_string() |
||
174 | |||
175 | def modify_permission_command(self, permission_id, kwargs): |
||
176 | """Generates xml string for modify permission on gvmd.""" |
||
177 | if not permission_id: |
||
178 | raise ValueError('modify_permission requires ' |
||
179 | 'a permission_id element') |
||
180 | |||
181 | cmd = XmlCommand('modify_permission') |
||
182 | cmd.set_attribute('permission_id', permission_id) |
||
183 | |||
184 | comment = kwargs.get('comment', '') |
||
185 | if comment: |
||
186 | cmd.add_element('comment', comment) |
||
187 | |||
188 | name = kwargs.get('name', '') |
||
189 | if name: |
||
190 | cmd.add_element('name', name) |
||
191 | |||
192 | resource = kwargs.get('resource', '') |
||
193 | if resource: |
||
194 | resource_id = resource['id'] |
||
195 | resource_type = resource['type'] |
||
196 | _xmlresource = cmd.add_element('resource', |
||
197 | attrs={'id': resource_id}) |
||
198 | _xmlresource.add_element('type', resource_type) |
||
199 | |||
200 | subject = kwargs.get('subject', '') |
||
201 | if subject: |
||
202 | subject_id = subject['id'] |
||
203 | subject_type = subject['type'] |
||
204 | _xmlsubject = cmd.add_element('subject', attrs={'id': subject_id}) |
||
205 | _xmlsubject.add_element('type', subject_type) |
||
206 | |||
207 | return cmd.to_string() |
||
208 | |||
209 | def modify_port_list_command(self, port_list_id, kwargs): |
||
210 | """Generates xml string for modify port list on gvmd.""" |
||
211 | if not port_list_id: |
||
212 | raise ValueError('modify_port_list requires ' |
||
213 | 'a port_list_id attribute') |
||
214 | cmd = XmlCommand('modify_port_list') |
||
215 | cmd.set_attribute('port_list_id', port_list_id) |
||
216 | |||
217 | comment = kwargs.get('comment', '') |
||
218 | if comment: |
||
219 | cmd.add_element('comment', comment) |
||
220 | |||
221 | name = kwargs.get('name', '') |
||
222 | if name: |
||
223 | cmd.add_element('name', name) |
||
224 | |||
225 | return cmd.to_string() |
||
226 | |||
227 | def modify_report_command(self, report_id, comment): |
||
228 | """Generates xml string for modify report on gvmd.""" |
||
229 | cmd = XmlCommand('modify_report') |
||
230 | cmd.set_attribute('report_id', report_id) |
||
231 | cmd.add_element('comment', comment) |
||
232 | return cmd.to_string() |
||
233 | |||
234 | def modify_report_format_command(self, report_format_id, kwargs): |
||
235 | """Generates xml string for modify report format on gvmd.""" |
||
236 | if len(kwargs) < 1: |
||
237 | raise Exception('modify_report_format: Missing parameter') |
||
238 | |||
239 | cmd = XmlCommand('modify_report_format') |
||
240 | cmd.set_attribute('report_format_id', report_format_id) |
||
241 | |||
242 | active = kwargs.get('active', '') |
||
243 | if active: |
||
244 | cmd.add_element('active', active) |
||
245 | |||
246 | name = kwargs.get('name', '') |
||
247 | if name: |
||
248 | cmd.add_element('name', name) |
||
249 | |||
250 | summary = kwargs.get('summary', '') |
||
251 | if summary: |
||
252 | cmd.add_element('summary', summary) |
||
253 | |||
254 | param = kwargs.get('param', '') |
||
255 | if param: |
||
256 | p_name = param[0] |
||
257 | p_value = param[1] |
||
258 | _xmlparam = cmd.add_element('param') |
||
259 | _xmlparam.add_element('name', p_name) |
||
260 | _xmlparam.add_element('value', p_value) |
||
261 | |||
262 | return cmd.to_string() |
||
263 | |||
264 | View Code Duplication | def modify_role_command(self, role_id, kwargs): |
|
265 | """Generates xml string for modify role on gvmd.""" |
||
266 | if not role_id: |
||
267 | raise ValueError('modify_role requires a role_id element') |
||
268 | |||
269 | cmd = XmlCommand('modify_role') |
||
270 | cmd.set_attribute('role_id', role_id) |
||
271 | |||
272 | comment = kwargs.get('comment', '') |
||
273 | if comment: |
||
274 | cmd.add_element('comment', comment) |
||
275 | |||
276 | name = kwargs.get('name', '') |
||
277 | if name: |
||
278 | cmd.add_element('name', name) |
||
279 | |||
280 | users = kwargs.get('users', '') |
||
281 | if users: |
||
282 | cmd.add_element('users', users) |
||
283 | |||
284 | return cmd.to_string() |
||
285 | |||
286 | def modify_scanner_command(self, scanner_id, host, port, scanner_type, |
||
287 | kwargs): |
||
288 | """Generates xml string for modify scanner on gvmd.""" |
||
289 | if not scanner_id: |
||
290 | raise ValueError('modify_scanner requires a scanner_id element') |
||
291 | if not host: |
||
292 | raise ValueError('modify_scanner requires a host element') |
||
293 | if not port: |
||
294 | raise ValueError('modify_scanner requires a port element') |
||
295 | if not scanner_type: |
||
296 | raise ValueError('modify_scanner requires a type element') |
||
297 | |||
298 | cmd = XmlCommand('modify_scanner') |
||
299 | cmd.set_attribute('scanner_id', scanner_id) |
||
300 | cmd.add_element('host', host) |
||
301 | cmd.add_element('port', port) |
||
302 | cmd.add_element('type', scanner_type) |
||
303 | |||
304 | comment = kwargs.get('comment', '') |
||
305 | if comment: |
||
306 | cmd.add_element('comment', comment) |
||
307 | |||
308 | name = kwargs.get('name', '') |
||
309 | if name: |
||
310 | cmd.add_element('name', name) |
||
311 | |||
312 | ca_pub = kwargs.get('ca_pub', '') |
||
313 | if ca_pub: |
||
314 | cmd.add_element('ca_pub', ca_pub) |
||
315 | |||
316 | credential_id = kwargs.get('credential_id', '') |
||
317 | if credential_id: |
||
318 | cmd.add_element('credential', attrs={'id': str(credential_id)}) |
||
319 | |||
320 | return cmd.to_string() |
||
321 | |||
322 | def modify_schedule_command(self, schedule_id, kwargs): |
||
323 | """Generates xml string for modify schedule on gvmd.""" |
||
324 | if not schedule_id: |
||
325 | raise ValueError('modify_schedule requires a schedule_id element') |
||
326 | |||
327 | cmd = XmlCommand('modify_schedule') |
||
328 | cmd.set_attribute('schedule_id', schedule_id) |
||
329 | comment = kwargs.get('comment', '') |
||
330 | if comment: |
||
331 | cmd.add_element('comment', comment) |
||
332 | |||
333 | name = kwargs.get('name', '') |
||
334 | if name: |
||
335 | cmd.add_element('name', name) |
||
336 | |||
337 | first_time = kwargs.get('first_time', '') |
||
338 | if first_time: |
||
339 | first_time_minute = first_time['minute'] |
||
340 | first_time_hour = first_time['hour'] |
||
341 | first_time_day_of_month = first_time['day_of_month'] |
||
342 | first_time_month = first_time['month'] |
||
343 | first_time_year = first_time['year'] |
||
344 | |||
345 | _xmlftime = cmd.add_element('first_time') |
||
346 | _xmlftime.add_element('minute', str(first_time_minute)) |
||
347 | _xmlftime.add_element('hour', str(first_time_hour)) |
||
348 | _xmlftime.add_element('day_of_month', str(first_time_day_of_month)) |
||
349 | _xmlftime.add_element('month', str(first_time_month)) |
||
350 | _xmlftime.add_element('year', str(first_time_year)) |
||
351 | |||
352 | duration = kwargs.get('duration', '') |
||
353 | if len(duration) > 1: |
||
354 | _xmlduration = cmd.add_element('duration', str(duration[0])) |
||
355 | _xmlduration.add_element('unit', str(duration[1])) |
||
356 | |||
357 | period = kwargs.get('period', '') |
||
358 | if len(period) > 1: |
||
359 | _xmlperiod = cmd.add_element('period', str(period[0])) |
||
360 | _xmlperiod.add_element('unit', str(period[1])) |
||
361 | |||
362 | timezone = kwargs.get('timezone', '') |
||
363 | if timezone: |
||
364 | cmd.add_element('timezone', str(timezone)) |
||
365 | |||
366 | return cmd.to_string() |
||
367 | |||
368 | def modify_setting_command(self, setting_id, name, value): |
||
369 | """Generates xml string for modify setting format on gvmd.""" |
||
370 | cmd = XmlCommand('modify_setting') |
||
371 | cmd.set_attribute('setting_id', setting_id) |
||
372 | cmd.add_element('name', name) |
||
373 | cmd.add_element('value', value) |
||
374 | |||
375 | return cmd.to_string() |
||
376 | |||
377 | def modify_tag_command(self, tag_id, kwargs): |
||
378 | """Generates xml string for modify tag on gvmd.""" |
||
379 | if not tag_id: |
||
380 | raise ValueError('modify_tag requires a tag_id element') |
||
381 | |||
382 | cmd = XmlCommand('modify_tag') |
||
383 | cmd.set_attribute('tag_id', str(tag_id)) |
||
384 | |||
385 | comment = kwargs.get('comment', '') |
||
386 | if comment: |
||
387 | cmd.add_element('comment', comment) |
||
388 | |||
389 | name = kwargs.get('name', '') |
||
390 | if name: |
||
391 | cmd.add_element('name', name) |
||
392 | |||
393 | value = kwargs.get('value', '') |
||
394 | if value: |
||
395 | cmd.add_element('value', value) |
||
396 | |||
397 | active = kwargs.get('active', '') |
||
398 | if active: |
||
399 | cmd.add_element('active', value) |
||
400 | |||
401 | resource = kwargs.get('resource', '') |
||
402 | if resource: |
||
403 | resource_id = resource['id'] |
||
404 | resource_type = resource['type'] |
||
405 | _xmlresource = cmd.add_element('resource', |
||
406 | attrs={'resource_id': resource_id}) |
||
407 | _xmlresource.add_element('type', resource_type) |
||
408 | |||
409 | return cmd.to_string() |
||
410 | |||
411 | def modify_target_command(self, target_id, kwargs): |
||
412 | """Generates xml string for modify target on gvmd.""" |
||
413 | if not target_id: |
||
414 | raise ValueError('modify_target requires a target_id element') |
||
415 | |||
416 | cmd = XmlCommand('modify_target') |
||
417 | cmd.set_attribute('target_id', target_id) |
||
418 | |||
419 | comment = kwargs.get('comment', '') |
||
420 | if comment: |
||
421 | cmd.add_element('comment', comment) |
||
422 | |||
423 | name = kwargs.get('name', '') |
||
424 | if name: |
||
425 | cmd.add_element('name', name) |
||
426 | |||
427 | hosts = kwargs.get('hosts', '') |
||
428 | if hosts: |
||
429 | cmd.add_element('hosts', hosts) |
||
430 | |||
431 | copy = kwargs.get('copy', '') |
||
432 | if copy: |
||
433 | cmd.add_element('copy', copy) |
||
434 | |||
435 | exclude_hosts = kwargs.get('exclude_hosts', '') |
||
436 | if exclude_hosts: |
||
437 | cmd.add_element('exclude_hosts', exclude_hosts) |
||
438 | |||
439 | alive_tests = kwargs.get('alive_tests', '') |
||
440 | if alive_tests: |
||
441 | cmd.add_element('alive_tests', alive_tests) |
||
442 | |||
443 | reverse_lookup_only = kwargs.get('reverse_lookup_only', '') |
||
444 | if reverse_lookup_only: |
||
445 | cmd.add_element('reverse_lookup_only', reverse_lookup_only) |
||
446 | |||
447 | reverse_lookup_unify = kwargs.get('reverse_lookup_unify', '') |
||
448 | if reverse_lookup_unify: |
||
449 | cmd.add_element('reverse_lookup_unify', reverse_lookup_unify) |
||
450 | |||
451 | port_range = kwargs.get('port_range', '') |
||
452 | if port_range: |
||
453 | cmd.add_element('port_range', port_range) |
||
454 | |||
455 | port_list = kwargs.get('port_list', '') |
||
456 | if port_list: |
||
457 | cmd.add_element('port_list', attrs={'id': str(port_list)}) |
||
458 | |||
459 | return cmd.to_string() |
||
460 | |||
461 | def modify_task_command(self, task_id, kwargs): |
||
462 | """Generates xml string for modify task on gvmd.""" |
||
463 | if not task_id: |
||
464 | raise ValueError('modify_task requires a task_id element') |
||
465 | |||
466 | cmd = XmlCommand('modify_task') |
||
467 | cmd.set_attribute('task_id', task_id) |
||
468 | |||
469 | name = kwargs.get('name', '') |
||
470 | if name: |
||
471 | cmd.add_element('name', name) |
||
472 | |||
473 | comment = kwargs.get('comment', '') |
||
474 | if comment: |
||
475 | cmd.add_element('comment', comment) |
||
476 | |||
477 | target_id = kwargs.get('target_id', '') |
||
478 | if target_id: |
||
479 | cmd.add_element('target', attrs={'id': target_id}) |
||
480 | |||
481 | scanner = kwargs.get('scanner', '') |
||
482 | if scanner: |
||
483 | cmd.add_element('scanner', attrs={'id': scanner}) |
||
484 | |||
485 | schedule_periods = kwargs.get('schedule_periods', '') |
||
486 | if schedule_periods: |
||
487 | cmd.add_element('schedule_periods', str(schedule_periods)) |
||
488 | |||
489 | schedule = kwargs.get('schedule', '') |
||
490 | if schedule: |
||
491 | cmd.add_element('schedule', attrs={'id': str(schedule)}) |
||
492 | |||
493 | alert = kwargs.get('alert', '') |
||
494 | if alert: |
||
495 | cmd.add_element('alert', attrs={'id': str(alert)}) |
||
496 | |||
497 | observers = kwargs.get('observers', '') |
||
498 | if observers: |
||
499 | cmd.add_element('observers', str(observers)) |
||
500 | |||
501 | preferences = kwargs.get('preferences', '') |
||
502 | if preferences: |
||
503 | _xmlprefs = cmd.add_element('preferences') |
||
504 | for n in range(len(preferences["scanner_name"])): |
||
505 | preferences_scanner_name = preferences["scanner_name"][n] |
||
506 | preferences_value = preferences["value"][n] |
||
507 | _xmlpref = _xmlprefs.add_element('preference') |
||
508 | _xmlpref.add_element('scanner_name', preferences_scanner_name) |
||
509 | _xmlpref.add_element('value', preferences_value) |
||
510 | |||
511 | file = kwargs.get('file', '') |
||
512 | if file: |
||
513 | file_name = file['name'] |
||
514 | file_action = file['action'] |
||
515 | if file_action != "update" and file_action != "remove": |
||
516 | raise ValueError('action can only be "update" or "remove"!') |
||
517 | cmd.add_element('file', attrs={'name': file_name, |
||
518 | 'action': file_action}) |
||
519 | |||
520 | return cmd.to_string() |
||
521 | |||
522 | def modify_user_command(self, kwargs): |
||
523 | """Generates xml string for modify user on gvmd.""" |
||
524 | user_id = kwargs.get('user_id', '') |
||
525 | name = kwargs.get('name', '') |
||
526 | |||
527 | if not user_id and not name: |
||
528 | raise ValueError('modify_user requires ' |
||
529 | 'either a user_id or a name element') |
||
530 | |||
531 | cmd = XmlCommand('modify_user') |
||
532 | cmd.set_attribute('user_id', str(user_id)) |
||
533 | |||
534 | new_name = kwargs.get('new_name', '') |
||
535 | if new_name: |
||
536 | cmd.add_element('new_name', new_name) |
||
537 | |||
538 | password = kwargs.get('password', '') |
||
539 | if password: |
||
540 | cmd.add_element('password', password) |
||
541 | |||
542 | role_ids = kwargs.get('role_ids', '') |
||
543 | if len(role_ids) > 0: |
||
544 | for role in role_ids: |
||
545 | cmd.add_element('role', attrs={'id': str(role)}) |
||
546 | |||
547 | hosts = kwargs.get('hosts', '') |
||
548 | hosts_allow = kwargs.get('hosts_allow', '') |
||
549 | if hosts or hosts_allow: |
||
550 | cmd.add_element('hosts', hosts, attrs={'allow': str(hosts_allow)}) |
||
551 | |||
552 | ifaces = kwargs.get('ifaces', '') |
||
553 | ifaces_allow = kwargs.get('ifaces_allow', '') |
||
554 | if ifaces or ifaces_allow: |
||
555 | cmd.add_element('ifaces', ifaces, |
||
556 | attrs={'allow': str(ifaces_allow)}) |
||
557 | |||
558 | sources = kwargs.get('sources', '') |
||
559 | if sources: |
||
560 | cmd.add_element('sources', sources) |
||
561 | |||
562 | return cmd.to_string() |
||
563 | |||
564 | def delete_agent_command(self, kwargs): |
||
565 | """Generates xml string for delete agent on gvmd""" |
||
566 | cmd = XmlCommand('delete_agent') |
||
567 | for key, value in kwargs.items(): |
||
568 | cmd.set_attribute(key, value) |
||
569 | |||
570 | return cmd.to_string() |
||
571 | |||
572 | def delete_alert_command(self, kwargs): |
||
573 | """Generates xml string for delete alert on gvmd""" |
||
574 | cmd = XmlCommand('delete_alert') |
||
575 | for key, value in kwargs.items(): |
||
576 | cmd.set_attribute(key, value) |
||
577 | |||
578 | return cmd.to_string() |
||
579 | |||
580 | def delete_asset_command(self, asset_id, ultimate=0): |
||
581 | """Generates xml string for delete asset on gvmd""" |
||
582 | cmd = XmlCommand('delete_asset') |
||
583 | cmd.set_attribute('asset_id', asset_id) |
||
584 | cmd.set_attribute('ultimate', ultimate) |
||
585 | |||
586 | return cmd.to_string() |
||
587 | |||
588 | def delete_config_command(self, config_id, ultimate=0): |
||
589 | """Generates xml string for delete config on gvmd""" |
||
590 | cmd = XmlCommand('delete_config') |
||
591 | cmd.set_attribute('config_id', config_id) |
||
592 | cmd.set_attribute('ultimate', ultimate) |
||
593 | |||
594 | return cmd.to_string() |
||
595 | |||
596 | def delete_credential_command(self, credential_id, ultimate=0): |
||
597 | """Generates xml string for delete credential on gvmd""" |
||
598 | cmd = XmlCommand('delete_credential') |
||
599 | cmd.set_attribute('credential_id', credential_id) |
||
600 | cmd.set_attribute('ultimate', ultimate) |
||
601 | return cmd.to_string() |
||
602 | |||
603 | def delete_filter_command(self, filter_id, ultimate=0): |
||
604 | """Generates xml string for delete filter on gvmd""" |
||
605 | cmd = XmlCommand('delete_filter') |
||
606 | cmd.set_attribute('filter_id', filter_id) |
||
607 | cmd.set_attribute('ultimate', ultimate) |
||
608 | |||
609 | return cmd.to_string() |
||
610 | |||
611 | def delete_group_command(self, group_id, ultimate=0): |
||
612 | """Generates xml string for delete group on gvmd""" |
||
613 | cmd = XmlCommand('delete_group') |
||
614 | cmd.set_attribute('group_id', group_id) |
||
615 | cmd.set_attribute('ultimate', ultimate) |
||
616 | |||
617 | return cmd.to_string() |
||
618 | |||
619 | def delete_note_command(self, note_id, ultimate=0): |
||
620 | """Generates xml string for delete note on gvmd""" |
||
621 | cmd = XmlCommand('delete_note') |
||
622 | cmd.set_attribute('note_id', note_id) |
||
623 | cmd.set_attribute('ultimate', ultimate) |
||
624 | |||
625 | return cmd.to_string() |
||
626 | |||
627 | def delete_override_command(self, override_id, ultimate=0): |
||
628 | """Generates xml string for delete override on gvmd""" |
||
629 | cmd = XmlCommand('delete_override') |
||
630 | cmd.set_attribute('override_id', override_id) |
||
631 | cmd.set_attribute('ultimate', ultimate) |
||
632 | |||
633 | return cmd.to_string() |
||
634 | |||
635 | def delete_permission_command(self, permission_id, ultimate=0): |
||
636 | """Generates xml string for delete permission on gvmd""" |
||
637 | cmd = XmlCommand('delete_permission') |
||
638 | cmd.set_attribute('permission_id', permission_id) |
||
639 | cmd.set_attribute('ultimate', ultimate) |
||
640 | |||
641 | return cmd.to_string() |
||
642 | |||
643 | def delete_port_list_command(self, port_list_id, ultimate=0): |
||
644 | """Generates xml string for delete port on gvmd""" |
||
645 | cmd = XmlCommand('delete_port_list') |
||
646 | cmd.set_attribute('port_list_id', port_list_id) |
||
647 | cmd.set_attribute('ultimate', ultimate) |
||
648 | |||
649 | return cmd.to_string() |
||
650 | |||
651 | def delete_port_range_command(self, port_range_id): |
||
652 | """Generates xml string for delete port on gvmd""" |
||
653 | cmd = XmlCommand('delete_port_range') |
||
654 | cmd.set_attribute('port_range_id', port_range_id) |
||
655 | |||
656 | return cmd.to_string() |
||
657 | |||
658 | def delete_report_command(self, report_id): |
||
659 | """Generates xml string for delete report on gvmd""" |
||
660 | cmd = XmlCommand('delete_report') |
||
661 | cmd.set_attribute('report_id', report_id) |
||
662 | |||
663 | return cmd.to_string() |
||
664 | |||
665 | def delete_report_format_command(self, report_format_id, ultimate=0): |
||
666 | """Generates xml string for delete report on gvmd""" |
||
667 | cmd = XmlCommand('delete_report_format') |
||
668 | cmd.set_attribute('report_format_id', report_format_id) |
||
669 | cmd.set_attribute('ultimate', ultimate) |
||
670 | |||
671 | return cmd.to_string() |
||
672 | |||
673 | def delete_role_command(self, role_id, ultimate=0): |
||
674 | """Generates xml string for delete role on gvmd""" |
||
675 | cmd = XmlCommand('delete_role') |
||
676 | cmd.set_attribute('role_id', role_id) |
||
677 | cmd.set_attribute('ultimate', ultimate) |
||
678 | |||
679 | return cmd.to_string() |
||
680 | |||
681 | def delete_scanner_command(self, scanner_id, ultimate=0): |
||
682 | """Generates xml string for delete scanner on gvmd""" |
||
683 | cmd = XmlCommand('delete_scanner') |
||
684 | cmd.set_attribute('scanner_id', scanner_id) |
||
685 | cmd.set_attribute('ultimate', ultimate) |
||
686 | |||
687 | return cmd.to_string() |
||
688 | |||
689 | def delete_schedule_command(self, schedule_id, ultimate=0): |
||
690 | """Generates xml string for delete schedule on gvmd""" |
||
691 | # if self.ask_yes_or_no('Are you sure to delete this schedule? '): |
||
692 | cmd = XmlCommand('delete_schedule') |
||
693 | cmd.set_attribute('schedule_id', schedule_id) |
||
694 | cmd.set_attribute('ultimate', ultimate) |
||
695 | |||
696 | return cmd.to_string() |
||
697 | |||
698 | def delete_tag_command(self, tag_id, ultimate=0): |
||
699 | """Generates xml string for delete tag on gvmd""" |
||
700 | cmd = XmlCommand('delete_tag') |
||
701 | cmd.set_attribute('tag_id', tag_id) |
||
702 | cmd.set_attribute('ultimate', ultimate) |
||
703 | |||
704 | return cmd.to_string() |
||
705 | |||
706 | def delete_target_command(self, target_id, ultimate=0): |
||
707 | """Generates xml string for delete target on gvmd""" |
||
708 | cmd = XmlCommand('delete_target') |
||
709 | cmd.set_attribute('target_id', target_id) |
||
710 | cmd.set_attribute('ultimate', ultimate) |
||
711 | |||
712 | return cmd.to_string() |
||
713 | |||
714 | def delete_task_command(self, task_id, ultimate=0): |
||
715 | """Generates xml string for delete task on gvmd""" |
||
716 | cmd = XmlCommand('delete_task') |
||
717 | cmd.set_attribute('task_id', task_id) |
||
718 | cmd.set_attribute('ultimate', ultimate) |
||
719 | |||
720 | return cmd.to_string() |
||
721 | |||
722 | def delete_user_command(self, kwargs): |
||
723 | """Generates xml string for delete user on gvmd""" |
||
724 | cmd = XmlCommand('delete_user') |
||
725 | |||
726 | user_id = kwargs.get('user_id', '') |
||
727 | if user_id: |
||
728 | cmd.set_attribute('user_id', user_id) |
||
729 | |||
730 | name = kwargs.get('name', '') |
||
731 | if name: |
||
732 | cmd.set_attribute('name', name) |
||
733 | |||
734 | inheritor_id = kwargs.get('inheritor_id', '') |
||
735 | if inheritor_id: |
||
736 | cmd.set_attribute('inheritor_id', inheritor_id) |
||
737 | |||
738 | inheritor_name = kwargs.get('inheritor_name', '') |
||
739 | if inheritor_name: |
||
740 | cmd.set_attribute('inheritor_name', inheritor_name) |
||
741 | |||
742 | return cmd.to_string() |
||
743 | |||
744 | def describe_auth_command(self): |
||
745 | """Generates xml string for describe auth on gvmd""" |
||
746 | cmd = XmlCommand('describe_auth') |
||
747 | return cmd.to_string() |
||
748 | |||
749 | def empty_trashcan_command(self): |
||
750 | """Generates xml string for empty trashcan on gvmd""" |
||
751 | cmd = XmlCommand('empty_trashcan') |
||
752 | return cmd.to_string() |
||
753 | |||
754 | def get_agents_command(self, kwargs): |
||
755 | """Generates xml string for get agents on gvmd.""" |
||
756 | cmd = XmlCommand('get_agents') |
||
757 | cmd.set_attributes(kwargs) |
||
758 | return cmd.to_string() |
||
759 | |||
760 | def get_aggregates_command(self, kwargs): |
||
761 | """Generates xml string for get aggregates on gvmd.""" |
||
762 | cmd = XmlCommand('get_aggregates') |
||
763 | cmd.set_attributes(kwargs) |
||
764 | return cmd.to_string() |
||
765 | |||
766 | def get_alerts_command(self, kwargs): |
||
767 | """Generates xml string for get alerts on gvmd.""" |
||
768 | cmd = XmlCommand('get_alerts') |
||
769 | cmd.set_attributes(kwargs) |
||
770 | return cmd.to_string() |
||
771 | |||
772 | def get_assets_command(self, kwargs): |
||
773 | """Generates xml string for get assets on gvmd.""" |
||
774 | cmd = XmlCommand('get_assets') |
||
775 | cmd.set_attributes(kwargs) |
||
776 | return cmd.to_string() |
||
777 | |||
778 | def get_credentials_command(self, kwargs): |
||
779 | """Generates xml string for get credentials on gvmd.""" |
||
780 | cmd = XmlCommand('get_credentials') |
||
781 | cmd.set_attributes(kwargs) |
||
782 | return cmd.to_string() |
||
783 | |||
784 | def get_configs_command(self, kwargs): |
||
785 | """Generates xml string for get configs on gvmd.""" |
||
786 | cmd = XmlCommand('get_configs') |
||
787 | cmd.set_attributes(kwargs) |
||
788 | return cmd.to_string() |
||
789 | |||
790 | def get_feeds_command(self, kwargs): |
||
791 | """Generates xml string for get feeds on gvmd.""" |
||
792 | cmd = XmlCommand('get_feeds') |
||
793 | cmd.set_attributes(kwargs) |
||
794 | return cmd.to_string() |
||
795 | |||
796 | def get_filters_command(self, kwargs): |
||
797 | """Generates xml string for get filters on gvmd.""" |
||
798 | cmd = XmlCommand('get_filters') |
||
799 | cmd.set_attributes(kwargs) |
||
800 | return cmd.to_string() |
||
801 | |||
802 | def get_groups_command(self, kwargs): |
||
803 | """Generates xml string for get groups on gvmd.""" |
||
804 | cmd = XmlCommand('get_groups') |
||
805 | cmd.set_attributes(kwargs) |
||
806 | return cmd.to_string() |
||
807 | |||
808 | def get_info_command(self, kwargs): |
||
809 | """Generates xml string for get info on gvmd.""" |
||
810 | cmd = XmlCommand('get_info') |
||
811 | cmd.set_attributes(kwargs) |
||
812 | return cmd.to_string() |
||
813 | |||
814 | def get_notes_command(self, kwargs): |
||
815 | """Generates xml string for get notes on gvmd.""" |
||
816 | cmd = XmlCommand('get_notes') |
||
817 | cmd.set_attributes(kwargs) |
||
818 | return cmd.to_string() |
||
819 | |||
820 | def get_nvts_command(self, kwargs): |
||
821 | """Generates xml string for get nvts on gvmd.""" |
||
822 | cmd = XmlCommand('get_nvts') |
||
823 | cmd.set_attributes(kwargs) |
||
824 | return cmd.to_string() |
||
825 | |||
826 | def get_nvt_families_command(self, kwargs): |
||
827 | """Generates xml string for get nvt on gvmd.""" |
||
828 | cmd = XmlCommand('get_nvt_families') |
||
829 | cmd.set_attributes(kwargs) |
||
830 | return cmd.to_string() |
||
831 | |||
832 | def get_overrides_command(self, kwargs): |
||
833 | """Generates xml string for get overrides on gvmd.""" |
||
834 | cmd = XmlCommand('get_overrides') |
||
835 | cmd.set_attributes(kwargs) |
||
836 | return cmd.to_string() |
||
837 | |||
838 | def get_permissions_command(self, kwargs): |
||
839 | """Generates xml string for get permissions on gvmd.""" |
||
840 | cmd = XmlCommand('get_permissions') |
||
841 | cmd.set_attributes(kwargs) |
||
842 | return cmd.to_string() |
||
843 | |||
844 | def get_port_lists_command(self, kwargs): |
||
845 | """Generates xml string for get port on gvmd.""" |
||
846 | cmd = XmlCommand('get_port_lists') |
||
847 | cmd.set_attributes(kwargs) |
||
848 | return cmd.to_string() |
||
849 | |||
850 | def get_preferences_command(self, kwargs): |
||
851 | """Generates xml string for get preferences on gvmd.""" |
||
852 | cmd = XmlCommand('get_preferences') |
||
853 | cmd.set_attributes(kwargs) |
||
854 | return cmd.to_string() |
||
855 | |||
856 | def get_reports_command(self, kwargs): |
||
857 | """Generates xml string for get reports on gvmd.""" |
||
858 | cmd = XmlCommand('get_reports') |
||
859 | cmd.set_attributes(kwargs) |
||
860 | return cmd.to_string() |
||
861 | |||
862 | def get_report_formats_command(self, kwargs): |
||
863 | """Generates xml string for get report on gvmd.""" |
||
864 | cmd = XmlCommand('get_report_formats') |
||
865 | cmd.set_attributes(kwargs) |
||
866 | return cmd.to_string() |
||
867 | |||
868 | def get_results_command(self, kwargs): |
||
869 | """Generates xml string for get results on gvmd.""" |
||
870 | cmd = XmlCommand('get_results') |
||
871 | cmd.set_attributes(kwargs) |
||
872 | return cmd.to_string() |
||
873 | |||
874 | def get_roles_command(self, kwargs): |
||
875 | """Generates xml string for get roles on gvmd.""" |
||
876 | cmd = XmlCommand('get_roles') |
||
877 | cmd.set_attributes(kwargs) |
||
878 | return cmd.to_string() |
||
879 | |||
880 | def get_scanners_command(self, kwargs): |
||
881 | """Generates xml string for get scanners on gvmd.""" |
||
882 | cmd = XmlCommand('get_scanners') |
||
883 | cmd.set_attributes(kwargs) |
||
884 | return cmd.to_string() |
||
885 | |||
886 | def get_schedules_command(self, kwargs): |
||
887 | """Generates xml string for get schedules on gvmd.""" |
||
888 | cmd = XmlCommand('get_schedules') |
||
889 | cmd.set_attributes(kwargs) |
||
890 | return cmd.to_string() |
||
891 | |||
892 | def get_settings_command(self, kwargs): |
||
893 | """Generates xml string for get settings on gvmd.""" |
||
894 | cmd = XmlCommand('get_settings') |
||
895 | cmd.set_attributes(kwargs) |
||
896 | return cmd.to_string() |
||
897 | |||
898 | def get_system_reports_command(self, kwargs): |
||
899 | """Generates xml string for get system on gvmd.""" |
||
900 | cmd = XmlCommand('get_system') |
||
901 | cmd.set_attributes(kwargs) |
||
902 | return cmd.to_string() |
||
903 | |||
904 | def get_tags_command(self, kwargs): |
||
905 | """Generates xml string for get tags on gvmd.""" |
||
906 | cmd = XmlCommand('get_tags') |
||
907 | cmd.set_attributes(kwargs) |
||
908 | return cmd.to_string() |
||
909 | |||
910 | def get_targets_command(self, kwargs): |
||
911 | """Generates xml string for get targets on gvmd.""" |
||
912 | cmd = XmlCommand('get_targets') |
||
913 | cmd.set_attributes(kwargs) |
||
914 | return cmd.to_string() |
||
915 | |||
916 | def get_tasks_command(self, kwargs): |
||
917 | """Generates xml string for get tasks on gvmd.""" |
||
918 | cmd = XmlCommand('get_tasks') |
||
919 | cmd.set_attributes(kwargs) |
||
920 | return cmd.to_string() |
||
921 | |||
922 | def get_users_command(self, kwargs): |
||
923 | """Generates xml string for get users on gvmd.""" |
||
924 | cmd = XmlCommand('get_users') |
||
925 | cmd.set_attributes(kwargs) |
||
926 | return cmd.to_string() |
||
927 | |||
928 | def get_version_command(self): |
||
929 | """Generates xml string for get version on gvmd.""" |
||
930 | cmd = XmlCommand('get_version') |
||
931 | return cmd.to_string() |
||
932 | |||
933 | def help_command(self, kwargs): |
||
934 | """Generates xml string for help on gvmd.""" |
||
935 | cmd = XmlCommand('help') |
||
936 | cmd.set_attributes(kwargs) |
||
937 | return cmd.to_string() |
||
938 | |||
939 | def move_task_command(self, task_id, slave_id): |
||
940 | """Generates xml string for move task on gvmd.""" |
||
941 | cmd = XmlCommand('move_task') |
||
942 | cmd.set_attribute('task_id', task_id) |
||
943 | cmd.set_attribute('slave_id', slave_id) |
||
944 | return cmd.to_string() |
||
945 | |||
946 | def restore_command(self, entity_id): |
||
947 | """Generates xml string for restore on gvmd.""" |
||
948 | cmd = XmlCommand('restore') |
||
949 | cmd.set_attribute('id', entity_id) |
||
950 | return cmd.to_string() |
||
951 | |||
952 | def resume_task_command(self, task_id): |
||
953 | """Generates xml string for resume task on gvmd.""" |
||
954 | cmd = XmlCommand('resume_task') |
||
955 | cmd.set_attribute('task_id', task_id) |
||
956 | return cmd.to_string() |
||
957 | |||
958 | def start_task_command(self, task_id): |
||
959 | """Generates xml string for start task on gvmd.""" |
||
960 | cmd = XmlCommand('start_task') |
||
961 | cmd.set_attribute('task_id', task_id) |
||
962 | return cmd.to_string() |
||
963 | |||
964 | def stop_task_command(self, task_id): |
||
965 | """Generates xml string for stop task on gvmd.""" |
||
966 | cmd = XmlCommand('stop_task') |
||
967 | cmd.set_attribute('task_id', task_id) |
||
968 | return cmd.to_string() |
||
969 | |||
970 | def sync_cert_command(self): |
||
971 | """Generates xml string for sync cert on gvmd.""" |
||
972 | cmd = XmlCommand('sync_cert') |
||
973 | return cmd.to_string() |
||
974 | |||
975 | def sync_config_command(self): |
||
976 | """Generates xml string for sync config on gvmd.""" |
||
977 | cmd = XmlCommand('sync_config') |
||
978 | return cmd.to_string() |
||
979 | |||
980 | def sync_feed_command(self): |
||
981 | """Generates xml string for sync feed on gvmd.""" |
||
982 | cmd = XmlCommand('sync_feed') |
||
983 | return cmd.to_string() |
||
984 | |||
985 | def sync_scap_command(self): |
||
986 | """Generates xml string for sync scap on gvmd.""" |
||
987 | cmd = XmlCommand('sync_scap') |
||
988 | return cmd.to_string() |
||
989 | |||
990 | def test_alert_command(self, alert_id): |
||
991 | """Generates xml string for test alert on gvmd.""" |
||
992 | cmd = XmlCommand('test_alert') |
||
993 | cmd.set_attribute('alert_id', alert_id) |
||
994 | return cmd.to_string() |
||
995 | |||
996 | def verify_agent_command(self, agent_id): |
||
997 | """Generates xml string for verify agent on gvmd.""" |
||
998 | cmd = XmlCommand('verify_agent') |
||
999 | cmd.set_attribute('agent_id', agent_id) |
||
1000 | return cmd.to_string() |
||
1001 | |||
1002 | def verify_report_format_command(self, report_format_id): |
||
1003 | """Generates xml string for verify report format on gvmd.""" |
||
1004 | cmd = XmlCommand('verify_report_format') |
||
1005 | cmd.set_attribute('report_format_id', report_format_id) |
||
1006 | return cmd.to_string() |
||
1007 | |||
1008 | def verify_scanner_command(self, scanner_id): |
||
1009 | """Generates xml string for verify scanner on gvmd.""" |
||
1010 | cmd = XmlCommand('verify_scanner') |
||
1011 | cmd.set_attribute('scanner_id', scanner_id) |
||
1012 | return cmd.to_string() |
||
1013 | |||
1014 | |||
1015 | def pretty_print(xml): |
||
1016 | """Prints beautiful XML-Code |
||
1017 | |||
1018 | This function gets an object of list<lxml.etree._Element> |
||
1019 | or directly a lxml element. |
||
1020 | Print it with good readable format. |
||
1021 | |||
1022 | Arguments: |
||
1023 | xml: List<lxml.etree.Element> or directly a lxml element |
||
1024 | """ |
||
1025 | if isinstance(xml, list): |
||
1026 | for item in xml: |
||
1027 | if etree.iselement(item): |
||
1028 | print(etree.tostring(item, pretty_print=True).decode('utf-8')) |
||
1029 | else: |
||
1030 | print(item) |
||
1031 | elif etree.iselement(xml): |
||
1032 | print(etree.tostring(xml, pretty_print=True).decode('utf-8')) |
||
1033 |