|
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 getpass |
|
20
|
|
|
|
|
21
|
|
|
from lxml import etree |
|
22
|
|
|
|
|
23
|
|
|
from gmp.error import GmpError |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def authenticate(gmp, args): |
|
27
|
|
|
"""Authentication helper |
|
28
|
|
|
|
|
29
|
|
|
Tries to get authentication username and password from args and if not |
|
30
|
|
|
present asks the username and/or password from the terminal. |
|
31
|
|
|
|
|
32
|
|
|
Attributes: |
|
33
|
|
|
gmp: A protocol instance |
|
34
|
|
|
args: The parsed arguments |
|
35
|
|
|
|
|
36
|
|
|
Raises: |
|
37
|
|
|
GmpError: Raises GmpError if authentication fails. |
|
38
|
|
|
""" |
|
39
|
|
|
if gmp.is_authenticated(): |
|
40
|
|
|
return |
|
41
|
|
|
|
|
42
|
|
|
username = args.gmp_username |
|
43
|
|
|
password = args.gmp_password |
|
44
|
|
|
|
|
45
|
|
|
# Ask for login credentials if none are given. |
|
46
|
|
|
if not username: |
|
47
|
|
|
while len(username) == 0: |
|
|
|
|
|
|
48
|
|
|
username = input('Enter username: ') |
|
49
|
|
|
|
|
50
|
|
|
if not password: |
|
51
|
|
|
password = getpass.getpass( |
|
52
|
|
|
'Enter password for {0}: '.format(username)) |
|
53
|
|
|
|
|
54
|
|
|
try: |
|
55
|
|
|
gmp.authenticate(username, password) |
|
56
|
|
|
except GmpError as e: |
|
|
|
|
|
|
57
|
|
|
print('Could not authenticate. Please check your credentials.') |
|
58
|
|
|
raise e |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
def pretty_print(xml): |
|
62
|
|
|
"""Prints beautiful XML-Code |
|
63
|
|
|
|
|
64
|
|
|
This function gets an object of list<lxml.etree._Element> |
|
65
|
|
|
or directly a lxml element. |
|
66
|
|
|
Print it with good readable format. |
|
67
|
|
|
|
|
68
|
|
|
Arguments: |
|
69
|
|
|
xml: List<lxml.etree.Element> or directly a lxml element |
|
70
|
|
|
""" |
|
71
|
|
|
if isinstance(xml, list): |
|
72
|
|
|
for item in xml: |
|
73
|
|
|
if etree.iselement(item): |
|
|
|
|
|
|
74
|
|
|
print(etree.tostring(item, pretty_print=True).decode('utf-8')) |
|
|
|
|
|
|
75
|
|
|
else: |
|
76
|
|
|
print(item) |
|
77
|
|
|
elif etree.iselement(xml): |
|
|
|
|
|
|
78
|
|
|
print(etree.tostring(xml, pretty_print=True).decode('utf-8')) |
|
|
|
|
|
|
79
|
|
|
|
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.