Passed
Pull Request — master (#317)
by Björn
01:28
created

gvmtools.helper.create_xml_tree()   A

Complexity

Conditions 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nop 1
dl 0
loc 18
rs 9.85
c 0
b 0
f 0
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
import os
21
import sys
22
23
from lxml import etree
24
25
from gvm.errors import GvmError
26
from gvm.xml import pretty_print
27
28
29
__all__ = ['authenticate', 'pretty_print', 'run_script']
30
31
32
class Table:
33
    def __init__(self, heading=None, rows=None, divider=' | '):
34
        self.heading = heading or []
35
        self.rows = rows or []
36
        self.divider = divider
37
38
    def _calculate_dimensions(self):
39
        column_sizes = []
40
41
        for column in self.heading:
42
            column_sizes.append(len(column))
43
44
        for row in self.rows:
45
            for i, column in enumerate(row):
46
                dim = column_sizes[i]
47
                column_size = len(column)
48
49
                if dim < column_size:
50
                    column_sizes[i] = column_size
51
52
        return column_sizes
53
54
    def _create_column(self, column, size):
55
        return '{}{}'.format(column, ' ' * (size - len(column)))
56
57
    def _create_row(self, columns):
58
        return self.divider.join(columns)
59
60
    def __str__(self):
61
        column_sizes = self._calculate_dimensions()
62
63
        row_strings = []
64
65
        heading_columns = []
66
        heading_divider_columns = []
67
68
        for i, column in enumerate(self.heading):
69
            column_size = column_sizes[i]
70
71
            heading_columns.append(self._create_column(column, column_size))
72
            heading_divider_columns.append(
73
                self._create_column('-' * column_size, column_size)
74
            )
75
76
        row_strings.append(self._create_row(heading_columns))
77
        row_strings.append(self._create_row(heading_divider_columns))
78
79
        for row in self.rows:
80
            row_columns = []
81
82
            for i, column in enumerate(row):
83
                column_size = column_sizes[i]
84
                row_columns.append(self._create_column(column, column_size))
85
86
            row_strings.append(self._create_row(row_columns))
87
88
        return "\n".join(row_strings)
89
90
91
def yes_or_no(question):
92
    """Asks the user to proceed or not in a gvmtools script
93
94
    Arguments:
95
        question (str): The condition the user should answer
96
    """
97
    reply = str(input(question + ' (y/n): ')).lower().strip()
98
    if reply[0] == ('y'):
99
        return True
100
    if reply[0] == ('n'):
101
        return False
102
    else:
103
        return yes_or_no("Please enter 'y' or 'n'")
104
105
106
def error_and_exit(msg):
107
    """Prints an error message and quits the gvmtools script
108
109
    Arguments:
110
        msg (str): The error message, that will be printed
111
    """
112
    print("\nError: {}\n".format(msg), file=sys.stderr)
113
    sys.exit(1)
114
115
116
def create_xml_tree(xml_doc):
117
    """Creates an XML tree that can be read by an gvmtools script
118
119
    Arguments:
120
        xml_doc (str): Path to the xml document
121
    """
122
    try:
123
        xml_tree = etree.parse(xml_doc)
124
        xml_tree = xml_tree.getroot()
125
    except IOError as err:
126
        error_and_exit("Failed to read xml_file: {} (exit)".format(str(err)))
127
    except etree.Error as err:
128
        error_and_exit("Failed to parse xml_file: {} (exit)".format(str(err)))
129
130
    if len(xml_tree) == 0:
131
        error_and_exit("XML file is empty (exit)")
132
133
    return xml_tree
134
135
136
def do_not_run_as_root():
137
    if hasattr(os, 'geteuid') and os.geteuid() == 0:
138
        raise RuntimeError('This tool MUST NOT be run as root user.')
139
140
141
def authenticate(gmp, username=None, password=None):
142
    """Authentication helper
143
144
    Tries to get authentication username and password from arguments and if not
145
    present asks the username and/or password from the terminal.
146
147
    Arguments:
148
        gmp: A protocol instance
149
        username (:obj:`str`, optional): Username to authenticate with. If None,
150
            username will be read from terminal.
151
        password (:obj:`str`, optional): Password to authenticate with. If None,
152
            password will be read from the terminal.
153
154
    Returns:
155
        tuple: (username, password) tuple
156
157
    Raises:
158
        GmpError: Raises GmpError if authentication fails.
159
    """
160
    if gmp.is_authenticated():
161
        return
162
163
    # Ask for login credentials if none are given.
164
    if not username:
165
        while username is None or len(username) == 0:
166
            username = input('Enter username: ')
167
168
    if not password:
169
        password = getpass.getpass('Enter password for {0}: '.format(username))
170
171
    try:
172
        gmp.authenticate(username, password)
173
        return (username, password)
174
    except GvmError as e:
175
        print('Could not authenticate. Please check your credentials.')
176
        raise e
177
178
179
def run_script(path, global_vars):
180
    """Loads and executes a file as a python script
181
182
    Arguments:
183
        path (str): Path to the script file
184
        vars (dict): Variables passed as globals to the script
185
    """
186
    try:
187
        file = open(path, 'r', newline='').read()
188
    except FileNotFoundError:
189
        print('Script {path} does not exist'.format(path=path), file=sys.stderr)
190
        sys.exit(2)
191
192
    exec(file, global_vars)  # pylint: disable=exec-used
193