Passed
Pull Request — master (#187)
by Jan
04:23
created

oval_graph.arf_xml_parser.arf_xml_parser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 98.73%

Importance

Changes 0
Metric Value
eloc 98
dl 0
loc 118
ccs 78
cts 79
cp 0.9873
rs 10
c 0
b 0
f 0
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A ARFXMLParser._get_definition_of_rule() 0 12 3
A ARFXMLParser._get_rules_in_profile() 0 18 4
A ARFXMLParser._get_definitions() 0 4 1
A ARFXMLParser._get_rule_dict() 0 10 2
A ARFXMLParser._get_oval_definitions() 0 6 1
A ARFXMLParser.get_oval_tree() 0 3 1
A ARFXMLParser.validate() 0 5 1
A ARFXMLParser.get_src() 0 4 1
A ARFXMLParser._get_report_data() 0 7 3
A ARFXMLParser.__init__() 0 23 3
1
"""
2
    This file contains a class for creating OVAL graph from ARF XML source
3
"""
4
5 1
import os
6 1
import sys
7
8 1
from lxml import etree as ET
9
10 1
from ..exceptions import NotTestedRule
11 1
from ..oval_tree.builder import Builder
12 1
from ._oval_scan_definitions import _OVALScanDefinitions
13 1
from .global_namespaces import namespaces
14
15
16 1
class ARFXMLParser:
17 1
    def __init__(self, src):
18 1
        self.src = src
19 1
        self.tree = ET.parse(self.src)
20 1
        self.root = self.tree.getroot()
21 1
        self.arf_schemas_src = '../schemas/arf/1.1/asset-reporting-format_1.1.0.xsd'
22 1
        if not self.validate(self.arf_schemas_src):
23 1
            start_red_color = '\033[91m'
24 1
            end_red_color = '\033[0m'
25 1
            message = "{}Warning: This file is not valid arf report.{}".format(
26
                start_red_color, end_red_color)
27 1
            print(message, file=sys.stderr)
28 1
        try:
29 1
            self.used_rules, self.not_tested_rules = self._get_rules_in_profile()
30 1
            self.report_data_href = list(self.used_rules.values())[0]['href']
31 1
            self.report_data = self._get_report_data(self.report_data_href)
32 1
            self.definitions = self._get_definitions()
33 1
            self.oval_definitions = self._get_oval_definitions()
34 1
            self.scan_definitions = _OVALScanDefinitions(
35
                self.definitions, self.oval_definitions, self.report_data).get_scan()
36 1
        except BaseException as error:
37 1
            raise ValueError(
38
                'This file "{}" is not arf report file or there are no results'.format(
39
                    self.src)) from error
40
41 1
    @staticmethod
42
    def get_src(src):
43 1
        dir_path = os.path.dirname(os.path.realpath(__file__))
44 1
        return str(os.path.join(dir_path, src))
45
46 1
    def validate(self, xsd_path):
47 1
        xsd_path = self.get_src(xsd_path)
48 1
        xmlschema_doc = ET.parse(xsd_path)
49 1
        xmlschema = ET.XMLSchema(xmlschema_doc)
50 1
        return xmlschema.validate(self.tree)
51
52 1
    @staticmethod
53
    def _get_rule_dict(rule_result, result, id_def, check_content_ref):
54 1
        message = rule_result.find('.//xccdf:message', namespaces)
55 1
        rule_dict = {}
56 1
        rule_dict['id_def'] = id_def
57 1
        rule_dict['href'] = check_content_ref.attrib.get('href')
58 1
        rule_dict['result'] = result.text
59 1
        if message is not None:
60
            rule_dict['message'] = message.text
61 1
        return rule_dict
62
63 1
    def _get_rules_in_profile(self):
64 1
        rules_results = self.root.findall(
65
            './/xccdf:TestResult/xccdf:rule-result', namespaces)
66 1
        rules = {}
67 1
        not_tested_rules = {}
68 1
        for rule_result in rules_results:
69 1
            result = rule_result.find('.//xccdf:result', namespaces)
70 1
            check_content_ref = rule_result.find(
71
                './/xccdf:check/xccdf:check-content-ref', namespaces)
72 1
            if check_content_ref is not None:
73 1
                id_ = rule_result.get('idref')
74 1
                id_def = check_content_ref.attrib.get('name')
75 1
                if id_def is not None:
76 1
                    rules[id_] = self._get_rule_dict(
77
                        rule_result, result, id_def, check_content_ref)
78 1
                    continue
79 1
            not_tested_rules[rule_result.get('idref')] = result.text
80 1
        return (rules, not_tested_rules)
81
82 1
    def _get_report_data(self, href):
83 1
        report_data = None
84 1
        reports = self.root.find('.//arf:reports', namespaces)
85 1
        for report in reports:
86 1
            if "#" + str(report.get("id")) == href:
87 1
                report_data = report
88 1
        return report_data
89
90 1
    def _get_definitions(self):
91 1
        return self.report_data.find(
92
            ('.//XMLSchema:oval_results/XMLSchema:results/'
93
             'XMLSchema:system/XMLSchema:definitions'), namespaces)
94
95 1
    def _get_oval_definitions(self):
96 1
        return self.root.find(
97
            './/arf:report-requests/arf:report-request/'
98
            'arf:content/scap:data-stream-collection/'
99
            'scap:component/oval-definitions:oval_definitions/'
100
            'oval-definitions:definitions', namespaces)
101
102 1
    def _get_definition_of_rule(self, rule_id):
103 1
        if rule_id in self.used_rules:
104 1
            rule_info = self.used_rules[rule_id]
105 1
            return dict(rule_id=rule_id,
106
                        definition_id=rule_info['id_def'],
107
                        definition=self.scan_definitions[rule_info['id_def']])
108
109 1
        if rule_id in self.not_tested_rules:
110 1
            raise NotTestedRule(
111
                'Rule "{}" is {}, so there are no results.'
112
                .format(rule_id, self.not_tested_rules[rule_id]))
113 1
        raise ValueError('404 rule "{}" not found!'.format(rule_id))
114
115 1
    def get_oval_tree(self, rule_id):
116 1
        return Builder.dict_of_rule_to_oval_tree(
117
            self._get_definition_of_rule(rule_id))
118