Completed
Push — master ( d6b6e9...ddb09d )
by Jaspar
15s queued 12s
created

combine-reports.gmp.combine_reports()   A

Complexity

Conditions 4

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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