Total Complexity | 327 |
Total Lines | 1604 |
Duplicated Lines | 2.81 % |
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 | node = secET.fromstring(xml_text) |
||
62 | self._element.append(node) |
||
63 | |||
64 | def to_string(self): |
||
65 | return etree.tostring(self._element).decode('utf-8') |
||
66 | |||
67 | def __str__(self): |
||
68 | return self.to_string() |
||
69 | |||
70 | |||
71 | class XmlCommand(XmlCommandElement): |
||
72 | |||
73 | def __init__(self, name): |
||
74 | super().__init__(etree.Element(name)) |
||
75 | |||
76 | |||
77 | class _GmpCommandFactory: |
||
78 | |||
79 | """Factory to create gmp - Greenbone Manangement Protocol - commands |
||
80 | """ |
||
81 | |||
82 | def create_agent_command(self, installer, signature, name, comment='', |
||
83 | copy='', howto_install='', howto_use=''): |
||
84 | |||
85 | cmd = XmlCommand('create_agent') |
||
86 | cmd.add_element('installer', installer) |
||
87 | cmd.add_element('signature', signature) |
||
88 | cmd.add_element('name', name) |
||
89 | |||
90 | if comment: |
||
91 | cmd.add_element('comment', comment) |
||
92 | |||
93 | if copy: |
||
94 | cmd.add_element('copy', copy) |
||
95 | |||
96 | if howto_install: |
||
97 | cmd.add_element('howto_install', howto_install) |
||
98 | |||
99 | if howto_use: |
||
100 | cmd.add_element('howto_use', howto_use) |
||
101 | |||
102 | return cmd.to_string() |
||
103 | |||
104 | def create_alert_command(self, name, condition, event, method, filter_id='', |
||
105 | copy='', comment=''): |
||
106 | |||
107 | cmd = XmlCommand('create_alert') |
||
108 | cmd.add_element('name', name) |
||
109 | |||
110 | if len(condition) > 1: |
||
111 | conditions = cmd.add_element('condition', condition[0]) |
||
112 | for value, key in condition[1].items(): |
||
113 | _data = conditions.add_element('data', value) |
||
114 | _data.add_element('name', key) |
||
115 | |||
116 | elif condition[0] == "Always": |
||
117 | conditions = cmd.add_element('condition', condition[0]) |
||
118 | |||
119 | if len(event) > 1: |
||
120 | events = cmd.add_element('event', event[0]) |
||
121 | for value, key in event[1].items(): |
||
122 | _data = events.add_element('data', value) |
||
123 | _data.add_element('name', key) |
||
124 | |||
125 | if len(method) > 1: |
||
126 | methods = cmd.add_element('method', method[0]) |
||
127 | for value, key in method[1].items(): |
||
128 | _data = methods.add_element('data', value) |
||
129 | _data.add_element('name', key) |
||
130 | |||
131 | if filter_id: |
||
132 | cmd.add_element('filter', attrs={'id': filter_id}) |
||
133 | |||
134 | if copy: |
||
135 | cmd.add_element('copy', copy) |
||
136 | |||
137 | if comment: |
||
138 | cmd.add_element('comment', comment) |
||
139 | |||
140 | return cmd.to_string() |
||
141 | |||
142 | def create_asset_command(self, name, asset_type, comment=''): |
||
143 | if asset_type not in ('host', 'os'): |
||
144 | raise ValueError('create_asset requires asset_type to be either ' |
||
145 | 'host or os') |
||
146 | cmd = XmlCommand('create_asset') |
||
147 | asset = cmd.add_element('asset') |
||
148 | asset.add_element('type', asset_type) |
||
149 | asset.add_element('name', name) |
||
150 | |||
151 | if comment: |
||
152 | asset.add_element('comment', comment) |
||
153 | |||
154 | return cmd.to_string() |
||
155 | |||
156 | def create_authenticate_command(self, username, password): |
||
157 | """Generates string for authentification on gvmd |
||
158 | |||
159 | Creates the gmp authentication xml string. |
||
160 | Inserts the username and password into it. |
||
161 | |||
162 | Keyword Arguments: |
||
163 | username {str} -- Username for GVM User |
||
164 | password {str} -- Password for GVM User |
||
165 | """ |
||
166 | cmd = XmlCommand('authenticate') |
||
167 | |||
168 | credentials = cmd.add_element('credentials') |
||
169 | credentials.add_element('username', username) |
||
170 | credentials.add_element('password', password) |
||
171 | |||
172 | return cmd.to_string() |
||
173 | |||
174 | def create_config_command(self, copy_id, name): |
||
175 | """Generates xml string for create config on gvmd.""" |
||
176 | cmd = XmlCommand('create_config') |
||
177 | cmd.add_element('copy', copy_id) |
||
178 | cmd.add_element('name', name) |
||
179 | |||
180 | return cmd.to_string() |
||
181 | |||
182 | def create_credential_command(self, name, kwargs): |
||
183 | """Generates xml string for create credential on gvmd.""" |
||
184 | cmd = XmlCommand('create_credential') |
||
185 | cmd.add_element('name', name) |
||
186 | |||
187 | comment = kwargs.get('comment', '') |
||
188 | if comment: |
||
189 | cmd.add_element('comment', comment) |
||
190 | |||
191 | copy = kwargs.get('copy', '') |
||
192 | if copy: |
||
193 | cmd.add_element('copy', copy) |
||
194 | |||
195 | allow_insecure = kwargs.get('allow_insecure', '') |
||
196 | if allow_insecure: |
||
197 | cmd.add_element('allow_insecure', allow_insecure) |
||
198 | |||
199 | certificate = kwargs.get('certificate', '') |
||
200 | if certificate: |
||
201 | cmd.add_element('certificate', certificate) |
||
202 | |||
203 | key = kwargs.get('key', '') |
||
204 | if key: |
||
205 | phrase = key['phrase'] |
||
206 | private = key['private'] |
||
207 | if not phrase: |
||
208 | raise ValueError('create_credential requires a phrase element') |
||
209 | if not private: |
||
210 | raise ValueError('create_credential requires a ' |
||
211 | 'private element') |
||
212 | |||
213 | _xmlKey = cmd.add_element('key') |
||
214 | _xmlKey.add_element('phrase', phrase) |
||
215 | _xmlKey.add_element('private', private) |
||
216 | |||
217 | login = kwargs.get('login', '') |
||
218 | if login: |
||
219 | cmd.add_element('login', login) |
||
220 | |||
221 | password = kwargs.get('password', '') |
||
222 | if password: |
||
223 | cmd.add_element('password', password) |
||
224 | |||
225 | auth_algorithm = kwargs.get('auth_algorithm', '') |
||
226 | if auth_algorithm: |
||
227 | if auth_algorithm not in ('md5', 'sha1'): |
||
228 | raise ValueError('create_credential requires auth_algorithm ' |
||
229 | 'to be either md5 or sha1') |
||
230 | cmd.add_element('auth_algorithm', auth_algorithm) |
||
231 | |||
232 | community = kwargs.get('community', '') |
||
233 | if community: |
||
234 | cmd.add_element('community', community) |
||
235 | |||
236 | privacy = kwargs.get('privacy', '') |
||
237 | if privacy: |
||
238 | algorithm = privacy.algorithm |
||
239 | if algorithm not in ('aes', 'des'): |
||
240 | raise ValueError('create_credential requires algorithm ' |
||
241 | 'to be either aes or des') |
||
242 | p_password = privacy.password |
||
243 | _xmlPrivacy = cmd.add_element('privacy') |
||
244 | _xmlPrivacy.add_element('algorithm', algorithm) |
||
245 | _xmlPrivacy.add_element('password', p_password) |
||
246 | |||
247 | cred_type = kwargs.get('type', '') |
||
248 | if cred_type: |
||
249 | if cred_type not in ('cc', 'snmp', 'up', 'usk'): |
||
250 | raise ValueError('create_credential requires type ' |
||
251 | 'to be either cc, snmp, up or usk') |
||
252 | cmd.add_element('type', cred_type) |
||
253 | |||
254 | return cmd.to_string() |
||
255 | |||
256 | def create_filter_command(self, name, make_unique, kwargs): |
||
257 | """Generates xml string for create filter on gvmd.""" |
||
258 | |||
259 | cmd = XmlCommand('create_filter') |
||
260 | _xmlName = cmd.add_element('name', name) |
||
261 | _xmlName.add_element('make_unique', '') |
||
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.add_element('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 len(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 | 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 | _xmlName.add_element('make_unique', '') |
||
658 | if make_unique: |
||
659 | _xmlName.add_element('make_unique', '1') |
||
660 | else: |
||
661 | _xmlName.add_element('make_unique', '0') |
||
662 | |||
663 | if 'asset_hosts' in kwargs: |
||
664 | hosts = kwargs.get('asset_hosts') |
||
665 | filter = hosts['filter'] |
||
666 | cmd.add_element('asset_hosts', attrs={'filter': str(filter)}) |
||
667 | elif 'hosts' in kwargs: |
||
668 | hosts = kwargs.get('hosts') |
||
669 | cmd.add_element('hosts', hosts) |
||
670 | else: |
||
671 | raise ValueError('create_target requires either a hosts or ' |
||
672 | 'an asset_hosts element') |
||
673 | |||
674 | if 'comment' in kwargs: |
||
675 | cmd.add_element('comment', kwargs.get('comment')) |
||
676 | |||
677 | if 'copy' in kwargs: |
||
678 | # NOTE: It seems that hosts/asset_hosts is silently ignored by the |
||
679 | # server when copy is supplied. But for specification conformance |
||
680 | # we raise the ValueError above and consider copy optional. |
||
681 | cmd.add_element('copy', kwargs.get('copy')) |
||
682 | |||
683 | if 'exclude_hosts' in kwargs: |
||
684 | cmd.add_element('exclude_hosts', kwargs.get('exclude_hosts')) |
||
685 | |||
686 | if 'ssh_credential' in kwargs: |
||
687 | ssh_credential = kwargs.get('ssh_credential') |
||
688 | if 'id' in ssh_credential: |
||
689 | |||
690 | _xmlSSH = exclude_hosts('ssh_credential', '', |
||
691 | attrs={'id': ssh_credential['id']}) |
||
692 | |||
693 | if 'port' in ssh_credential: |
||
694 | _xmlSSH.add_element('port', ssh_credential['port']) |
||
695 | else: |
||
696 | raise ValueError('ssh_credential requires an id attribute') |
||
697 | |||
698 | if 'smb_credential' in kwargs: |
||
699 | smb_credential = kwargs.get('smb_credential') |
||
700 | if 'id' in smb_credential: |
||
701 | cmd.add_element('smb_credential', |
||
702 | attrs={'id': smb_credential['id']}) |
||
703 | else: |
||
704 | raise ValueError('smb_credential requires an id attribute') |
||
705 | |||
706 | if 'esxi_credential' in kwargs: |
||
707 | esxi_credential = kwargs.get('esxi_credential') |
||
708 | if 'id' in esxi_credential: |
||
709 | cmd.add_element('esxi_credential', |
||
710 | attrs={'id': esxi_credential['id']}) |
||
711 | else: |
||
712 | raise ValueError('esxi_credential requires an id attribute') |
||
713 | |||
714 | if 'snmp_credential' in kwargs: |
||
715 | snmp_credential = kwargs.get('snmp_credential') |
||
716 | if 'id' in snmp_credential: |
||
717 | cmd.add_element('snmp_credential', |
||
718 | attrs={'id': snmp_credential['id']}) |
||
719 | else: |
||
720 | raise ValueError('snmp_credential requires an id attribute') |
||
721 | |||
722 | if 'alive_tests' in kwargs: |
||
723 | # NOTE: As the alive_tests are referenced by their name and some |
||
724 | # names contain ampersand ('&') characters it should be considered |
||
725 | # replacing any characters special to XML in the variable with |
||
726 | # their corresponding entities. |
||
727 | cmd.add_element('alive_tests', kwargs.get('alive_tests')) |
||
728 | |||
729 | if 'reverse_lookup_only' in kwargs: |
||
730 | reverse_lookup_only = kwargs.get('reverse_lookup_only') |
||
731 | if reverse_lookup_only: |
||
732 | cmd.add_element('reverse_lookup_only', '1') |
||
733 | else: |
||
734 | cmd.add_element('reverse_lookup_only', '0') |
||
735 | |||
736 | if 'reverse_lookup_unify' in kwargs: |
||
737 | reverse_lookup_unify = kwargs.get('reverse_lookup_unify') |
||
738 | if reverse_lookup_unify: |
||
739 | cmd.add_element('reverse_lookup_unify', '1') |
||
740 | else: |
||
741 | cmd.add_element('reverse_lookup_unify', '0') |
||
742 | |||
743 | if 'port_range' in kwargs: |
||
744 | cmd.add_element('port_range', kwargs.get('port_range')) |
||
745 | |||
746 | if 'port_list' in kwargs: |
||
747 | port_list = kwargs.get('port_list') |
||
748 | if 'id' in port_list: |
||
749 | cmd.add_element('port_list', |
||
750 | attrs={'id': str(port_list['id'])}) |
||
751 | else: |
||
752 | raise ValueError('port_list requires an id attribute') |
||
753 | |||
754 | return cmd.to_string() |
||
755 | |||
756 | def create_task_command(self, name, config_id, target_id, scanner_id, |
||
757 | alert_ids=None, comment=''): |
||
758 | """Generates xml string for create task on gvmd.""" |
||
759 | |||
760 | if alert_ids is None: |
||
761 | alert_ids = [] |
||
762 | cmd = XmlCommand('create_task') |
||
763 | cmd.add_element('name', name) |
||
764 | cmd.add_element('comment', comment) |
||
765 | cmd.add_element('config', attrs={'id': config_id}) |
||
766 | cmd.add_element('target', attrs={'id': target_id}) |
||
767 | cmd.add_element('scanner', attrs={'id': scanner_id}) |
||
768 | |||
769 | #if given the alert_id is wrapped and integrated suitably as xml |
||
770 | if len(alert_ids) > 0: |
||
771 | if isinstance(alert_ids, str): |
||
772 | #if a single id is given as a string wrap it into a list |
||
773 | alert_ids = [alert_ids] |
||
774 | if isinstance(alert_ids, list): |
||
775 | #parse all given alert id's |
||
776 | for alert in alert_ids: |
||
777 | cmd.add_element('alert', attrs={'id': str(alert)}) |
||
778 | |||
779 | return cmd.to_string() |
||
780 | |||
781 | def create_user_command(self, name, password, copy='', hosts_allow='0', |
||
782 | ifaces_allow='0', role_ids=(), hosts=None, |
||
783 | ifaces=None): |
||
784 | """Generates xml string for create user on gvmd.""" |
||
785 | cmd = XmlCommand('create_user') |
||
786 | cmd.add_element('name', name) |
||
787 | |||
788 | if copy: |
||
789 | cmd.add_element('copy', copy) |
||
790 | |||
791 | if password: |
||
792 | cmd.add_element('password', password) |
||
793 | |||
794 | if hosts is not None: |
||
795 | cmd.add_element('hosts', hosts, attrs={'allow': str(hosts_allow)}) |
||
796 | |||
797 | if ifaces is not None: |
||
798 | cmd.add_element('ifaces', ifaces, |
||
799 | attrs={'allow': str(ifaces_allow)}) |
||
800 | |||
801 | if len(role_ids) > 0: |
||
802 | for role in role_ids: |
||
803 | cmd.add_element('role', attrs={'allow': str(role)}) |
||
804 | |||
805 | return cmd.to_string() |
||
806 | |||
807 | def modify_agent_command(self, agent_id, name='', comment=''): |
||
808 | if not agent_id: |
||
809 | raise ValueError('modify_agent requires an agent_id element') |
||
810 | |||
811 | xmlRoot = etree.Element('modify_agent', agent_id=str(agent_id)) |
||
812 | if name: |
||
813 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
814 | _xmlName.text = name |
||
815 | |||
816 | if comment: |
||
817 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
818 | _xmlComment.text = comment |
||
819 | |||
820 | return etree.tostring(xmlRoot).decode('utf-8') |
||
821 | |||
822 | def modify_alert_command(self, alert_id, kwargs): |
||
823 | if not alert_id: |
||
824 | raise ValueError('modify_alert requires an agent_id element') |
||
825 | |||
826 | xmlRoot = etree.Element('modify_alert', alert_id=str(alert_id)) |
||
827 | |||
828 | name = kwargs.get('name', '') |
||
829 | if name: |
||
830 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
831 | _xmlName.text = name |
||
832 | |||
833 | comment = kwargs.get('comment', '') |
||
834 | if comment: |
||
835 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
836 | _xmlComment.text = comment |
||
837 | |||
838 | filter_id = kwargs.get('filter_id', '') |
||
839 | if filter_id: |
||
840 | _xmlFilter = etree.SubElement(xmlRoot, 'filter', id=filter_id) |
||
841 | |||
842 | event = kwargs.get('event', '') |
||
843 | if len(event) > 1: |
||
844 | _xmlEvent = etree.SubElement(xmlRoot, 'event') |
||
845 | _xmlEvent.text = event[0] |
||
846 | for value, key in event[1].items(): |
||
847 | _xmlData = etree.SubElement(_xmlEvent, 'data') |
||
848 | _xmlData.text = value |
||
849 | _xmlDName = etree.SubElement(_xmlData, 'name') |
||
850 | _xmlDName.text = key |
||
851 | |||
852 | condition = kwargs.get('condition', '') |
||
853 | if len(condition) > 1: |
||
854 | _xmlCond = etree.SubElement(xmlRoot, 'condition') |
||
855 | _xmlCond.text = condition[0] |
||
856 | for value, key in condition[1].items(): |
||
857 | _xmlData = etree.SubElement(_xmlCond, 'data') |
||
858 | _xmlData.text = value |
||
859 | _xmlDName = etree.SubElement(_xmlData, 'name') |
||
860 | _xmlDName.text = key |
||
861 | |||
862 | method = kwargs.get('method', '') |
||
863 | if len(method) > 1: |
||
864 | _xmlMethod = etree.SubElement(xmlRoot, 'method') |
||
865 | _xmlMethod.text = method[0] |
||
866 | for value, key in method[1].items(): |
||
867 | _xmlData = etree.SubElement(_xmlMethod, 'data') |
||
868 | _xmlData.text = value |
||
869 | _xmlDName = etree.SubElement(_xmlData, 'name') |
||
870 | _xmlDName.text = key |
||
871 | |||
872 | return etree.tostring(xmlRoot).decode('utf-8') |
||
873 | |||
874 | def modify_auth_command(self, group_name, auth_conf_settings): |
||
875 | if not group_name: |
||
876 | raise ValueError('modify_auth requires a group element ' |
||
877 | 'with a name attribute') |
||
878 | if not auth_conf_settings: |
||
879 | raise ValueError('modify_auth requires ' |
||
880 | 'an auth_conf_settings element') |
||
881 | |||
882 | xmlRoot = etree.Element('modify_auth') |
||
883 | _xmlGroup = etree.SubElement(xmlRoot, 'group', name=str(group_name)) |
||
884 | |||
885 | for key, value in auth_conf_settings.items(): |
||
886 | _xmlAuthConf = etree.SubElement(_xmlGroup, 'auth_conf_setting') |
||
887 | _xmlKey = etree.SubElement(_xmlAuthConf, 'key') |
||
888 | _xmlKey.text = key |
||
889 | _xmlValue = etree.SubElement(_xmlAuthConf, 'value') |
||
890 | _xmlValue.text = value |
||
891 | |||
892 | return etree.tostring(xmlRoot).decode('utf-8') |
||
893 | |||
894 | def modify_config_command(self, selection, kwargs): |
||
895 | if selection not in ('nvt_pref', 'sca_pref', |
||
896 | 'family_selection', 'nvt_selection'): |
||
897 | raise ValueError('selection must be one of nvt_pref, sca_pref, ' |
||
898 | 'family_selection or nvt_selection') |
||
899 | config_id = kwargs.get('config_id') |
||
900 | |||
901 | xmlRoot = etree.Element('modify_config', config_id=str(config_id)) |
||
902 | |||
903 | if selection in 'nvt_pref': |
||
904 | nvt_oid = kwargs.get('nvt_oid') |
||
905 | name = kwargs.get('name') |
||
906 | value = kwargs.get('value') |
||
907 | _xmlPref = etree.SubElement(xmlRoot, 'preference') |
||
908 | _xmlNvt = etree.SubElement(_xmlPref, 'nvt', oid=nvt_oid) |
||
909 | _xmlName = etree.SubElement(_xmlPref, 'name') |
||
910 | _xmlName.text = name |
||
911 | _xmlValue = etree.SubElement(_xmlPref, 'value') |
||
912 | _xmlValue.text = value |
||
913 | |||
914 | elif selection in 'nvt_selection': |
||
915 | nvt_oid = kwargs.get('nvt_oid') |
||
916 | family = kwargs.get('family') |
||
917 | _xmlNvtSel = etree.SubElement(xmlRoot, 'nvt_selection') |
||
918 | _xmlFamily = etree.SubElement(_xmlNvtSel, 'family') |
||
919 | _xmlFamily.text = family |
||
920 | |||
921 | if isinstance(nvt_oid, list): |
||
922 | for nvt in nvt_oid: |
||
923 | _xmlNvt = etree.SubElement(_xmlNvtSel, 'nvt', oid=nvt) |
||
924 | else: |
||
925 | _xmlNvt = etree.SubElement(_xmlNvtSel, 'nvt', oid=nvt) |
||
926 | |||
927 | elif selection in 'family_selection': |
||
928 | family = kwargs.get('family') |
||
929 | _xmlFamSel = etree.SubElement(xmlRoot, 'family_selection') |
||
930 | _xmlGrow = etree.SubElement(_xmlFamSel, 'growing') |
||
931 | _xmlGrow.text = '1' |
||
932 | _xmlFamily = etree.SubElement(_xmlFamSel, 'family') |
||
933 | _xmlName = etree.SubElement(_xmlFamily, 'name') |
||
934 | _xmlName.text = family |
||
935 | _xmlAll = etree.SubElement(_xmlFamily, 'all') |
||
936 | _xmlAll.text = '1' |
||
937 | _xmlGrowI = etree.SubElement(_xmlFamily, 'growing') |
||
938 | _xmlGrowI.text = '1' |
||
939 | else: |
||
940 | raise NotImplementedError |
||
941 | |||
942 | return etree.tostring(xmlRoot).decode('utf-8') |
||
943 | |||
944 | def modify_credential_command(self, credential_id, kwargs): |
||
945 | if not credential_id: |
||
946 | raise ValueError('modify_credential requires ' |
||
947 | 'a credential_id attribute') |
||
948 | |||
949 | xmlRoot = etree.Element('modify_credential', |
||
950 | credential_id=credential_id) |
||
951 | |||
952 | comment = kwargs.get('comment', '') |
||
953 | if comment: |
||
954 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
955 | _xmlComment.text = comment |
||
956 | |||
957 | name = kwargs.get('name', '') |
||
958 | if name: |
||
959 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
960 | _xmlName.text = name |
||
961 | |||
962 | allow_insecure = kwargs.get('allow_insecure', '') |
||
963 | if allow_insecure: |
||
964 | _xmlAllowinsecure = etree.SubElement(xmlRoot, 'allow_insecure') |
||
965 | _xmlAllowinsecure.text = allow_insecure |
||
966 | |||
967 | certificate = kwargs.get('certificate', '') |
||
968 | if certificate: |
||
969 | _xmlCertificate = etree.SubElement(xmlRoot, 'certificate') |
||
970 | _xmlCertificate.text = certificate |
||
971 | |||
972 | key = kwargs.get('key', '') |
||
973 | if key: |
||
974 | phrase = key['phrase'] |
||
975 | private = key['private'] |
||
976 | if not phrase: |
||
977 | raise ValueError('modify_credential requires a phrase element') |
||
978 | if not private: |
||
979 | raise ValueError('modify_credential requires ' |
||
980 | 'a private element') |
||
981 | _xmlKey = etree.SubElement(xmlRoot, 'key') |
||
982 | _xmlKeyphrase = etree.SubElement(_xmlKey, 'phrase') |
||
983 | _xmlKeyphrase.text = phrase |
||
984 | _xmlKeyprivate = etree.SubElement(_xmlKey, 'private') |
||
985 | _xmlKeyprivate.text = private |
||
986 | |||
987 | login = kwargs.get('login', '') |
||
988 | if login: |
||
989 | _xmlLogin = etree.SubElement(xmlRoot, 'login') |
||
990 | _xmlLogin.text = login |
||
991 | |||
992 | password = kwargs.get('password', '') |
||
993 | if password: |
||
994 | _xmlPass = etree.SubElement(xmlRoot, 'password') |
||
995 | _xmlPass.text = password |
||
996 | |||
997 | auth_algorithm = kwargs.get('auth_algorithm', '') |
||
998 | if auth_algorithm: |
||
999 | if auth_algorithm not in ('md5', 'sha1'): |
||
1000 | raise ValueError('modify_credential requires auth_algorithm ' |
||
1001 | 'to be either md5 or sha1') |
||
1002 | _xmlAuthalg = etree.SubElement(xmlRoot, 'auth_algorithm') |
||
1003 | _xmlAuthalg.text = auth_algorithm |
||
1004 | |||
1005 | community = kwargs.get('community', '') |
||
1006 | if community: |
||
1007 | _xmlCommunity = etree.SubElement(xmlRoot, 'community') |
||
1008 | _xmlCommunity.text = community |
||
1009 | |||
1010 | privacy = kwargs.get('privacy', '') |
||
1011 | if privacy: |
||
1012 | algorithm = privacy.algorithm |
||
1013 | if algorithm not in ('aes', 'des'): |
||
1014 | raise ValueError('modify_credential requires algorithm ' |
||
1015 | 'to be either aes or des') |
||
1016 | p_password = privacy.password |
||
1017 | _xmlPrivacy = etree.SubElement(xmlRoot, 'privacy') |
||
1018 | _xmlAlgorithm = etree.SubElement(_xmlPrivacy, 'algorithm') |
||
1019 | _xmlAlgorithm.text = algorithm |
||
1020 | _xmlPpass = etree.SubElement(_xmlPrivacy, 'password') |
||
1021 | _xmlPpass.text = p_password |
||
1022 | |||
1023 | cred_type = kwargs.get('type', '') |
||
1024 | if cred_type: |
||
1025 | if cred_type not in ('cc', 'snmp', 'up', 'usk'): |
||
1026 | raise ValueError('modify_credential requires type ' |
||
1027 | 'to be either cc, snmp, up or usk') |
||
1028 | _xmlCredtype = etree.SubElement(xmlRoot, 'type') |
||
1029 | _xmlCredtype.text = cred_type |
||
1030 | |||
1031 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1032 | |||
1033 | def modify_filter_command(self, filter_id, kwargs): |
||
1034 | if not filter_id: |
||
1035 | raise ValueError('modify_filter requires a filter_id attribute') |
||
1036 | |||
1037 | xmlRoot = etree.Element('modify_filter', filter_id=filter_id) |
||
1038 | |||
1039 | comment = kwargs.get('comment', '') |
||
1040 | if comment: |
||
1041 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1042 | _xmlComment.text = comment |
||
1043 | |||
1044 | name = kwargs.get('name', '') |
||
1045 | if name: |
||
1046 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1047 | _xmlName.text = name |
||
1048 | |||
1049 | copy = kwargs.get('copy', '') |
||
1050 | if copy: |
||
1051 | _xmlCopy = etree.SubElement(xmlRoot, 'copy') |
||
1052 | _xmlCopy.text = copy |
||
1053 | |||
1054 | term = kwargs.get('term', '') |
||
1055 | if term: |
||
1056 | _xmlTerm = etree.SubElement(xmlRoot, 'term') |
||
1057 | _xmlTerm.text = term |
||
1058 | |||
1059 | filter_type = kwargs.get('type', '') |
||
1060 | if filter_type: |
||
1061 | if filter_type not in ('cc', 'snmp', 'up', 'usk'): |
||
1062 | raise ValueError('modify_filter requires type ' |
||
1063 | 'to be either cc, snmp, up or usk') |
||
1064 | _xmlFiltertype = etree.SubElement(xmlRoot, 'type') |
||
1065 | _xmlFiltertype.text = filter_type |
||
1066 | |||
1067 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1068 | |||
1069 | View Code Duplication | def modify_group_command(self, group_id, kwargs): |
|
1070 | if not group_id: |
||
1071 | raise ValueError('modify_group requires a group_id attribute') |
||
1072 | |||
1073 | xmlRoot = etree.Element('modify_group', group_id=group_id) |
||
1074 | |||
1075 | comment = kwargs.get('comment', '') |
||
1076 | if comment: |
||
1077 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1078 | _xmlComment.text = comment |
||
1079 | |||
1080 | name = kwargs.get('name', '') |
||
1081 | if name: |
||
1082 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1083 | _xmlName.text = name |
||
1084 | |||
1085 | users = kwargs.get('users', '') |
||
1086 | if users: |
||
1087 | _xmlUser = etree.SubElement(xmlRoot, 'users') |
||
1088 | _xmlUser.text = users |
||
1089 | |||
1090 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1091 | |||
1092 | def modify_note_command(self, note_id, text, kwargs): |
||
1093 | if not note_id: |
||
1094 | raise ValueError('modify_note requires a note_id attribute') |
||
1095 | if not text: |
||
1096 | raise ValueError('modify_note requires a text element') |
||
1097 | |||
1098 | xmlRoot = etree.Element('modify_note', note_id=note_id) |
||
1099 | _xmlText = etree.SubElement(xmlRoot, 'text') |
||
1100 | _xmlText.text = text |
||
1101 | |||
1102 | active = kwargs.get('active', '') |
||
1103 | if active: |
||
1104 | _xmlActive = etree.SubElement(xmlRoot, 'active') |
||
1105 | _xmlActive.text = active |
||
1106 | |||
1107 | hosts = kwargs.get('hosts', '') |
||
1108 | if hosts: |
||
1109 | _xmlHosts = etree.SubElement(xmlRoot, 'hosts') |
||
1110 | _xmlHosts.text = hosts |
||
1111 | |||
1112 | port = kwargs.get('port', '') |
||
1113 | if port: |
||
1114 | _xmlPort = etree.SubElement(xmlRoot, 'port') |
||
1115 | _xmlPort.text = port |
||
1116 | |||
1117 | result_id = kwargs.get('result_id', '') |
||
1118 | if result_id: |
||
1119 | _xmlResultid = etree.SubElement(xmlRoot, 'result', id=result_id) |
||
1120 | |||
1121 | severity = kwargs.get('severity', '') |
||
1122 | if severity: |
||
1123 | _xmlSeverity = etree.SubElement(xmlRoot, 'severity') |
||
1124 | _xmlSeverity.text = severity |
||
1125 | |||
1126 | task_id = kwargs.get('task_id', '') |
||
1127 | if task_id: |
||
1128 | _xmlTaskid = etree.SubElement(xmlRoot, 'task', id=task_id) |
||
1129 | |||
1130 | threat = kwargs.get('threat', '') |
||
1131 | if threat: |
||
1132 | _xmlThreat = etree.SubElement(xmlRoot, 'threat') |
||
1133 | _xmlThreat.text = threat |
||
1134 | |||
1135 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1136 | |||
1137 | def modify_override_command(self, override_id, text, kwargs): |
||
1138 | xmlRoot = etree.Element('modify_override', |
||
1139 | override_id=override_id) |
||
1140 | _xmlText = etree.SubElement(xmlRoot, 'text') |
||
1141 | _xmlText.text = text |
||
1142 | |||
1143 | active = kwargs.get('active', '') |
||
1144 | if active: |
||
1145 | _xmlActive = etree.SubElement(xmlRoot, 'active') |
||
1146 | _xmlActive.text = active |
||
1147 | |||
1148 | hosts = kwargs.get('hosts', '') |
||
1149 | if hosts: |
||
1150 | _xmlHosts = etree.SubElement(xmlRoot, 'hosts') |
||
1151 | _xmlHosts.text = hosts |
||
1152 | |||
1153 | port = kwargs.get('port', '') |
||
1154 | if port: |
||
1155 | _xmlPort = etree.SubElement(xmlRoot, 'port') |
||
1156 | _xmlPort.text = port |
||
1157 | |||
1158 | result_id = kwargs.get('result_id', '') |
||
1159 | if result_id: |
||
1160 | _xmlResultid = etree.SubElement(xmlRoot, 'result', id=result_id) |
||
1161 | |||
1162 | severity = kwargs.get('severity', '') |
||
1163 | if severity: |
||
1164 | _xmlSeverity = etree.SubElement(xmlRoot, 'severity') |
||
1165 | _xmlSeverity.text = severity |
||
1166 | |||
1167 | new_severity = kwargs.get('new_severity', '') |
||
1168 | if new_severity: |
||
1169 | _xmlNSeverity = etree.SubElement(xmlRoot, 'new_severity') |
||
1170 | _xmlNSeverity.text = new_severity |
||
1171 | |||
1172 | task_id = kwargs.get('task_id', '') |
||
1173 | if task_id: |
||
1174 | _xmlTaskid = etree.SubElement(xmlRoot, 'task', id=task_id) |
||
1175 | |||
1176 | threat = kwargs.get('threat', '') |
||
1177 | if threat: |
||
1178 | _xmlThreat = etree.SubElement(xmlRoot, 'threat') |
||
1179 | _xmlThreat.text = threat |
||
1180 | |||
1181 | new_threat = kwargs.get('new_threat', '') |
||
1182 | if new_threat: |
||
1183 | _xmlNThreat = etree.SubElement(xmlRoot, 'new_threat') |
||
1184 | _xmlNThreat.text = new_threat |
||
1185 | |||
1186 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1187 | |||
1188 | def modify_permission_command(self, permission_id, kwargs): |
||
1189 | if not permission_id: |
||
1190 | raise ValueError('modify_permission requires ' |
||
1191 | 'a permission_id element') |
||
1192 | |||
1193 | xmlRoot = etree.Element('modify_permission', |
||
1194 | permission_id=permission_id) |
||
1195 | |||
1196 | comment = kwargs.get('comment', '') |
||
1197 | if comment: |
||
1198 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1199 | _xmlComment.text = comment |
||
1200 | |||
1201 | name = kwargs.get('name', '') |
||
1202 | if name: |
||
1203 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1204 | _xmlName.text = name |
||
1205 | |||
1206 | resource = kwargs.get('resource', '') |
||
1207 | if resource: |
||
1208 | resource_id = resource['id'] |
||
1209 | resource_type = resource['type'] |
||
1210 | _xmlResource = etree.SubElement(xmlRoot, 'resource', |
||
1211 | id=resource_id) |
||
1212 | _xmlRType = etree.SubElement(_xmlResource, 'type') |
||
1213 | _xmlRType.text = resource_type |
||
1214 | |||
1215 | subject = kwargs.get('subject', '') |
||
1216 | if subject: |
||
1217 | subject_id = subject['id'] |
||
1218 | subject_type = subject['type'] |
||
1219 | _xmlSubject = etree.SubElement(xmlRoot, 'subject', id=subject_id) |
||
1220 | _xmlType = etree.SubElement(_xmlSubject, 'type') |
||
1221 | _xmlType.text = subject_type |
||
1222 | |||
1223 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1224 | |||
1225 | def modify_port_list_command(self, port_list_id, kwargs): |
||
1226 | if not port_list_id: |
||
1227 | raise ValueError('modify_port_list requires ' |
||
1228 | 'a port_list_id attribute') |
||
1229 | xmlRoot = etree.Element('modify_port_list', |
||
1230 | port_list_id=port_list_id) |
||
1231 | |||
1232 | comment = kwargs.get('comment', '') |
||
1233 | if comment: |
||
1234 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1235 | _xmlComment.text = comment |
||
1236 | |||
1237 | name = kwargs.get('name', '') |
||
1238 | if name: |
||
1239 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1240 | _xmlName.text = name |
||
1241 | |||
1242 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1243 | |||
1244 | def modify_report_format_command(self, report_format_id, kwargs): |
||
1245 | if len(kwargs) < 1: |
||
1246 | raise Exception('modify_report_format: Missing parameter') |
||
1247 | |||
1248 | xmlRoot = etree.Element('modify_report_format', |
||
1249 | report_format_id=report_format_id) |
||
1250 | |||
1251 | active = kwargs.get('active', '') |
||
1252 | if active: |
||
1253 | _xmlActive = etree.SubElement(xmlRoot, 'active') |
||
1254 | _xmlActive.text = active |
||
1255 | |||
1256 | name = kwargs.get('name', '') |
||
1257 | if name: |
||
1258 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1259 | _xmlName.text = name |
||
1260 | |||
1261 | summary = kwargs.get('summary', '') |
||
1262 | if summary: |
||
1263 | _xmlSummary = etree.SubElement(xmlRoot, 'summary') |
||
1264 | _xmlSummary.text = summary |
||
1265 | |||
1266 | param = kwargs.get('param', '') |
||
1267 | if param: |
||
1268 | p_name = param[0] |
||
1269 | p_value = param[1] |
||
1270 | _xmlParam = etree.SubElement(xmlRoot, 'param') |
||
1271 | _xmlPname = etree.SubElement(_xmlParam, 'name') |
||
1272 | _xmlPname.text = p_name |
||
1273 | _xmlValue = etree.SubElement(_xmlParam, 'value') |
||
1274 | _xmlValue.text = p_value |
||
1275 | |||
1276 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1277 | |||
1278 | View Code Duplication | def modify_role_command(self, role_id, kwargs): |
|
1279 | if not role_id: |
||
1280 | raise ValueError('modify_role requires a role_id element') |
||
1281 | |||
1282 | xmlRoot = etree.Element('modify_role', |
||
1283 | role_id=role_id) |
||
1284 | |||
1285 | comment = kwargs.get('comment', '') |
||
1286 | if comment: |
||
1287 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1288 | _xmlComment.text = comment |
||
1289 | |||
1290 | name = kwargs.get('name', '') |
||
1291 | if name: |
||
1292 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1293 | _xmlName.text = name |
||
1294 | |||
1295 | users = kwargs.get('users', '') |
||
1296 | if users: |
||
1297 | _xmlUser = etree.SubElement(xmlRoot, 'users') |
||
1298 | _xmlUser.text = users |
||
1299 | |||
1300 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1301 | |||
1302 | def modify_scanner_command(self, scanner_id, host, port, scanner_type, |
||
1303 | kwargs): |
||
1304 | if not scanner_id: |
||
1305 | raise ValueError('modify_scanner requires a scanner_id element') |
||
1306 | if not host: |
||
1307 | raise ValueError('modify_scanner requires a host element') |
||
1308 | if not port: |
||
1309 | raise ValueError('modify_scanner requires a port element') |
||
1310 | if not scanner_type: |
||
1311 | raise ValueError('modify_scanner requires a type element') |
||
1312 | |||
1313 | xmlRoot = etree.Element('modify_scanner', scanner_id=scanner_id) |
||
1314 | _xmlHost = etree.SubElement(xmlRoot, 'host') |
||
1315 | _xmlHost.text = host |
||
1316 | _xmlPort = etree.SubElement(xmlRoot, 'port') |
||
1317 | _xmlPort.text = port |
||
1318 | _xmlType = etree.SubElement(xmlRoot, 'type') |
||
1319 | _xmlType.text = scanner_type |
||
1320 | |||
1321 | comment = kwargs.get('comment', '') |
||
1322 | if comment: |
||
1323 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1324 | _xmlComment.text = comment |
||
1325 | |||
1326 | name = kwargs.get('name', '') |
||
1327 | if name: |
||
1328 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1329 | _xmlName.text = name |
||
1330 | |||
1331 | ca_pub = kwargs.get('ca_pub', '') |
||
1332 | if ca_pub: |
||
1333 | _xmlCAPub = etree.SubElement(xmlRoot, 'ca_pub') |
||
1334 | _xmlCAPub.text = ca_pub |
||
1335 | |||
1336 | credential_id = kwargs.get('credential_id', '') |
||
1337 | if credential_id: |
||
1338 | _xmlCred = etree.SubElement(xmlRoot, 'credential', |
||
1339 | id=str(credential_id)) |
||
1340 | |||
1341 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1342 | |||
1343 | def modify_schedule_command(self, schedule_id, kwargs): |
||
1344 | if not schedule_id: |
||
1345 | raise ValueError('modify_schedule requires a schedule_id element') |
||
1346 | |||
1347 | xmlRoot = etree.Element('modify_schedule', schedule_id=schedule_id) |
||
1348 | comment = kwargs.get('comment', '') |
||
1349 | if comment: |
||
1350 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1351 | _xmlComment.text = comment |
||
1352 | |||
1353 | name = kwargs.get('name', '') |
||
1354 | if name: |
||
1355 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1356 | _xmlName.text = name |
||
1357 | |||
1358 | first_time = kwargs.get('first_time', '') |
||
1359 | if first_time: |
||
1360 | first_time_minute = first_time['minute'] |
||
1361 | first_time_hour = first_time['hour'] |
||
1362 | first_time_day_of_month = first_time['day_of_month'] |
||
1363 | first_time_month = first_time['month'] |
||
1364 | first_time_year = first_time['year'] |
||
1365 | |||
1366 | _xmlFtime = etree.SubElement(xmlRoot, 'first_time') |
||
1367 | _xmlMinute = etree.SubElement(_xmlFtime, 'minute') |
||
1368 | _xmlMinute.text = str(first_time_minute) |
||
1369 | _xmlHour = etree.SubElement(_xmlFtime, 'hour') |
||
1370 | _xmlHour.text = str(first_time_hour) |
||
1371 | _xmlDay = etree.SubElement(_xmlFtime, 'day_of_month') |
||
1372 | _xmlDay.text = str(first_time_day_of_month) |
||
1373 | _xmlMonth = etree.SubElement(_xmlFtime, 'month') |
||
1374 | _xmlMonth.text = str(first_time_month) |
||
1375 | _xmlYear = etree.SubElement(_xmlFtime, 'year') |
||
1376 | _xmlYear.text = str(first_time_year) |
||
1377 | |||
1378 | duration = kwargs.get('duration', '') |
||
1379 | if len(duration) > 1: |
||
1380 | _xmlDuration = etree.SubElement(xmlRoot, 'duration') |
||
1381 | _xmlDuration.text = str(duration[0]) |
||
1382 | _xmlUnit = etree.SubElement(_xmlDuration, 'unit') |
||
1383 | _xmlUnit.text = str(duration[1]) |
||
1384 | |||
1385 | period = kwargs.get('period', '') |
||
1386 | if len(period) > 1: |
||
1387 | _xmlPeriod = etree.SubElement(xmlRoot, 'period') |
||
1388 | _xmlPeriod.text = str(period[0]) |
||
1389 | _xmlPUnit = etree.SubElement(_xmlPeriod, 'unit') |
||
1390 | _xmlPUnit.text = str(period[1]) |
||
1391 | |||
1392 | timezone = kwargs.get('timezone', '') |
||
1393 | if timezone: |
||
1394 | _xmlTimezone = etree.SubElement(xmlRoot, 'timezone') |
||
1395 | _xmlTimezone.text = str(timezone) |
||
1396 | |||
1397 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1398 | |||
1399 | def modify_tag_command(self, tag_id, kwargs): |
||
1400 | if not tag_id: |
||
1401 | raise ValueError('modify_tag requires a tag_id element') |
||
1402 | |||
1403 | xmlRoot = etree.Element('modify_tag', tag_id=str(tag_id)) |
||
1404 | |||
1405 | comment = kwargs.get('comment', '') |
||
1406 | if comment: |
||
1407 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1408 | _xmlComment.text = comment |
||
1409 | |||
1410 | name = kwargs.get('name', '') |
||
1411 | if name: |
||
1412 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1413 | _xmlName.text = name |
||
1414 | |||
1415 | value = kwargs.get('value', '') |
||
1416 | if value: |
||
1417 | _xmlValue = etree.SubElement(xmlRoot, 'value') |
||
1418 | _xmlValue.text = value |
||
1419 | |||
1420 | active = kwargs.get('active', '') |
||
1421 | if active: |
||
1422 | _xmlActive = etree.SubElement(xmlRoot, 'active') |
||
1423 | _xmlActive.text = value |
||
1424 | |||
1425 | resource = kwargs.get('resource', '') |
||
1426 | if resource: |
||
1427 | resource_id = resource['id'] |
||
1428 | resource_type = resource['type'] |
||
1429 | _xmlResource = etree.SubElement(xmlRoot, 'resource', |
||
1430 | resource_id=resource_id) |
||
1431 | _xmlRType = etree.SubElement(_xmlResource, 'type') |
||
1432 | _xmlRType.text = resource_type |
||
1433 | |||
1434 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1435 | |||
1436 | def modify_target_command(self, target_id, kwargs): |
||
1437 | if not target_id: |
||
1438 | raise ValueError('modify_target requires a target_id element') |
||
1439 | |||
1440 | xmlRoot = etree.Element('modify_target', target_id=target_id) |
||
1441 | |||
1442 | comment = kwargs.get('comment', '') |
||
1443 | if comment: |
||
1444 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1445 | _xmlComment.text = comment |
||
1446 | |||
1447 | name = kwargs.get('name', '') |
||
1448 | if name: |
||
1449 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1450 | _xmlName.text = name |
||
1451 | |||
1452 | hosts = kwargs.get('hosts', '') |
||
1453 | if hosts: |
||
1454 | _xmlHosts = etree.SubElement(xmlRoot, 'hosts') |
||
1455 | _xmlHosts.text = hosts |
||
1456 | |||
1457 | copy = kwargs.get('copy', '') |
||
1458 | if copy: |
||
1459 | _xmlCopy = etree.SubElement(xmlRoot, 'copy') |
||
1460 | _xmlCopy.text = kwargs.get('copy') |
||
1461 | |||
1462 | exclude_hosts = kwargs.get('exclude_hosts', '') |
||
1463 | if exclude_hosts: |
||
1464 | _xmlExHosts = etree.SubElement(xmlRoot, 'exclude_hosts') |
||
1465 | _xmlExHosts.text = kwargs.get('exclude_hosts') |
||
1466 | |||
1467 | alive_tests = kwargs.get('alive_tests', '') |
||
1468 | if alive_tests: |
||
1469 | _xmlAlive = etree.SubElement(xmlRoot, 'alive_tests') |
||
1470 | _xmlAlive.text = kwargs.get('alive_tests') |
||
1471 | |||
1472 | reverse_lookup_only = kwargs.get('reverse_lookup_only', '') |
||
1473 | if reverse_lookup_only: |
||
1474 | _xmlLookup = etree.SubElement(xmlRoot, 'reverse_lookup_only') |
||
1475 | _xmlLookup.text = reverse_lookup_only |
||
1476 | |||
1477 | reverse_lookup_unify = kwargs.get('reverse_lookup_unify', '') |
||
1478 | if reverse_lookup_unify: |
||
1479 | _xmlLookupU = etree.SubElement(xmlRoot, 'reverse_lookup_unify') |
||
1480 | _xmlLookupU.text = reverse_lookup_unify |
||
1481 | |||
1482 | port_range = kwargs.get('port_range', '') |
||
1483 | if port_range: |
||
1484 | _xmlPortR = etree.SubElement(xmlRoot, 'port_range') |
||
1485 | _xmlPortR.text = kwargs.get('port_range') |
||
1486 | |||
1487 | port_list = kwargs.get('port_list', '') |
||
1488 | if port_list: |
||
1489 | _xmlPortL = etree.SubElement(xmlRoot, 'port_list', |
||
1490 | id=str(port_list)) |
||
1491 | |||
1492 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1493 | |||
1494 | def modify_task_command(self, task_id, kwargs): |
||
1495 | if not task_id: |
||
1496 | raise ValueError('modify_task requires a task_id element') |
||
1497 | |||
1498 | xmlRoot = etree.Element('modify_task', task_id=task_id) |
||
1499 | |||
1500 | name = kwargs.get('name', '') |
||
1501 | if name: |
||
1502 | _xmlName = etree.SubElement(xmlRoot, 'name') |
||
1503 | _xmlName.text = name |
||
1504 | |||
1505 | comment = kwargs.get('comment', '') |
||
1506 | if comment: |
||
1507 | _xmlComment = etree.SubElement(xmlRoot, 'comment') |
||
1508 | _xmlComment.text = comment |
||
1509 | |||
1510 | target_id = kwargs.get('target_id', '') |
||
1511 | if target_id: |
||
1512 | _xmlTarget = etree.SubElement(xmlRoot, 'target', id=target_id) |
||
1513 | |||
1514 | scanner = kwargs.get('scanner', '') |
||
1515 | if scanner: |
||
1516 | _xmlScanner = etree.SubElement(xmlRoot, 'scanner', id=scanner) |
||
1517 | |||
1518 | schedule_periods = kwargs.get('schedule_periods', '') |
||
1519 | if schedule_periods: |
||
1520 | _xmlPeriod = etree.SubElement(xmlRoot, 'schedule_periods') |
||
1521 | _xmlPeriod.text = str(schedule_periods) |
||
1522 | |||
1523 | schedule = kwargs.get('schedule', '') |
||
1524 | if schedule: |
||
1525 | _xmlSched = etree.SubElement(xmlRoot, 'schedule', id=str(schedule)) |
||
1526 | |||
1527 | alert = kwargs.get('alert', '') |
||
1528 | if alert: |
||
1529 | _xmlAlert = etree.SubElement(xmlRoot, 'alert', id=str(alert)) |
||
1530 | |||
1531 | observers = kwargs.get('observers', '') |
||
1532 | if observers: |
||
1533 | _xmlObserver = etree.SubElement(xmlRoot, 'observers') |
||
1534 | _xmlObserver.text = str(observers) |
||
1535 | |||
1536 | preferences = kwargs.get('preferences', '') |
||
1537 | if preferences: |
||
1538 | _xmlPrefs = etree.SubElement(xmlRoot, 'preferences') |
||
1539 | for n in range(len(preferences["scanner_name"])): |
||
1540 | preferences_scanner_name = preferences["scanner_name"][n] |
||
1541 | preferences_value = preferences["value"][n] |
||
1542 | _xmlPref = etree.SubElement(_xmlPrefs, 'preference') |
||
1543 | _xmlScan = etree.SubElement(_xmlPref, 'scanner_name') |
||
1544 | _xmlScan.text = preferences_scanner_name |
||
1545 | _xmlVal = etree.SubElement(_xmlPref, 'value') |
||
1546 | _xmlVal.text = preferences_value |
||
1547 | |||
1548 | file = kwargs.get('file', '') |
||
1549 | if file: |
||
1550 | file_name = file['name'] |
||
1551 | file_action = file['action'] |
||
1552 | if file_action != "update" and file_action != "remove": |
||
1553 | raise ValueError('action can only be "update" or "remove"!') |
||
1554 | _xmlFile = etree.SubElement(xmlRoot, 'file', name=file_name, |
||
1555 | action=file_action) |
||
1556 | |||
1557 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1558 | |||
1559 | def modify_user_command(self, kwargs): |
||
1560 | user_id = kwargs.get('user_id', '') |
||
1561 | name = kwargs.get('name', '') |
||
1562 | |||
1563 | if not user_id and not name: |
||
1564 | raise ValueError('modify_user requires ' |
||
1565 | 'either a user_id or a name element') |
||
1566 | |||
1567 | xmlRoot = etree.Element('modify_user', user_id=str(user_id)) |
||
1568 | |||
1569 | new_name = kwargs.get('new_name', '') |
||
1570 | if new_name: |
||
1571 | _xmlName = etree.SubElement(xmlRoot, 'new_name') |
||
1572 | _xmlName.text = new_name |
||
1573 | |||
1574 | password = kwargs.get('password', '') |
||
1575 | if password: |
||
1576 | _xmlPass = etree.SubElement(xmlRoot, 'password') |
||
1577 | _xmlPass.text = password |
||
1578 | |||
1579 | role_ids = kwargs.get('role_ids', '') |
||
1580 | if len(role_ids) > 0: |
||
1581 | for role in role_ids: |
||
1582 | _xmlRole = etree.SubElement(xmlRoot, 'role', |
||
1583 | id=str(role)) |
||
1584 | hosts = kwargs.get('hosts', '') |
||
1585 | hosts_allow = kwargs.get('hosts_allow', '') |
||
1586 | if hosts or hosts_allow: |
||
1587 | _xmlHosts = etree.SubElement(xmlRoot, 'hosts', |
||
1588 | allow=str(hosts_allow)) |
||
1589 | _xmlHosts.text = hosts |
||
1590 | |||
1591 | ifaces = kwargs.get('ifaces', '') |
||
1592 | ifaces_allow = kwargs.get('ifaces_allow', '') |
||
1593 | if ifaces or ifaces_allow: |
||
1594 | _xmlIFaces = etree.SubElement(xmlRoot, 'ifaces', |
||
1595 | allow=str(ifaces_allow)) |
||
1596 | _xmlIFaces.text = ifaces |
||
1597 | |||
1598 | sources = kwargs.get('sources', '') |
||
1599 | if sources: |
||
1600 | _xmlSource = etree.SubElement(xmlRoot, 'sources') |
||
1601 | _xmlSource.text = sources |
||
1602 | |||
1603 | return etree.tostring(xmlRoot).decode('utf-8') |
||
1604 |
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.