Passed
Pull Request — master (#244)
by
unknown
01:49
created

combine-reports.gmp.check_args()   A

Complexity

Conditions 2

Size

Total Lines 25
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 25
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-2019 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 uuid
20
import time
21
22
from lxml import etree as e
23
24
25
def check_args(args):
26
    len_args = len(args.script) - 1
27
    if len_args < 2:
28
        message = """
29
        This script will combine desired reports into a single report. \
30
    The combined report will then be sent to a desired container task. \
31
    This script will create a container task for the combined report to\
32
    be sent to, however, if you would like the report to be sent to an \
33
    existing task, place the report of the desired task first and add  \
34
    the argument 'first_task'.
35
36
        1. <report_1_uuid> --uuid of report to be combined
37
        2. <report_2_uuid> --uuid of report to be combined
38
        ...
39
        n. <report_n_uuid> --uuid of report to be combined
40
41
        Example for starting up the routine:
42
            $ gvm-script --gmp-username=namessh --gmp-password=pass ssh --hostname=hostname  \
43
     scripts/combine-reports.gmp.py \
44
    "d15a337c-56f3-4208-a462-afeb79eb03b7" \
45
    "303fa0a6-aa9b-43c4-bac0-66ae0b2d1698" 'first_task'
46
47
        """
48
        print(message)
49
        quit(1)
50
51
52
def generate_uuid():
53
    return str(uuid.uuid4())
54
55
56
def gen_combined_report(gmp, args):
57
    id_assign = str(generate_uuid())
58
    report = e.Element(
59
        'report',
60
        {
61
            'id': id_assign,
62
            'format_id': 'd5da9f67-8551-4e51-807b-b6a873d70e34',
63
            'extension': 'xml',
64
            'content_type': 'text/xml',
65
        },
66
    )
67
    report_elem = e.Element('report', {'id': id_assign})
68
    results_elem = e.Element('results', {'start': '1', 'max': '-1'})
69
    report.append(report_elem)
70
    report_elem.append(results_elem)
71
72
    if 'first_task' in args.script:
73
        arg_len = args.script[1:-1]
74
    else:
75
        arg_len = args.script[1:]
76
    for argument in arg_len:
77
        current_report = gmp.get_report(argument)[0]
78
        for result in current_report.xpath('report/results/result'):
79
            results_elem.append(result)
80
81
    send_report(gmp, args, report)
82
83
84
def send_report(gmp, args, report):
85
    if 'first_task' in args.script:
86
        main_report = gmp.get_report(args.script[1])[0]
87
        task_id = main_report.xpath('//task/@id')[0]
88
        task_name = ''
89
    else:
90
        the_time = time.strftime("%Y/%m/%d-%H:%M:%S")
91
        task_id = ''
92
        task_name = "Combined_Report_{}".format(the_time)
93
94
    report = e.tostring(report)
95
96
    gmp.import_report(report, task_id=task_id, task_name=task_name)
97
98
99
def main(gmp, args):
100
    # pylint: disable=undefined-variable
101
102
    check_args(args)
103
104
    gen_combined_report(gmp, args)
105
106
107
if __name__ == '__gmp__':
108
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
109