Test Failed
Pull Request — master (#203)
by Jan
03:38
created

ARFXMLParser.validate()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
14 1
ns = {
15
    'XMLSchema': 'http://oval.mitre.org/XMLSchema/oval-results-5',
16
    'xccdf': 'http://checklists.nist.gov/xccdf/1.2',
17
    'arf': 'http://scap.nist.gov/schema/asset-reporting-format/1.1',
18
    'oval-definitions': 'http://oval.mitre.org/XMLSchema/oval-definitions-5',
19
    'scap': 'http://scap.nist.gov/schema/scap/source/1.2',
20
    'oval-characteristics': 'http://oval.mitre.org/XMLSchema/oval-system-characteristics-5',
21
}
22
23
24 1
class ARFXMLParser:
25 1
    def __init__(self, src):
26 1
        self.src = src
27 1
        self.tree = ET.parse(self.src)
28 1
        self.root = self.tree.getroot()
29 1
        self.arf_schemas_src = '../schemas/arf/1.1/asset-reporting-format_1.1.0.xsd'
30 1
        if not self.validate(self.arf_schemas_src):
31 1
            start_red_color = '\033[91m'
32 1
            end_red_color = '\033[0m'
33 1
            message = "{}Warning: This file is not valid arf report.{}".format(
34
                start_red_color, end_red_color)
35 1
            print(message, file=sys.stderr)
36 1
        try:
37 1
            self.used_rules, self.not_tested_rules = self._get_used_rules()
38 1
            self.report_data_href = list(self.used_rules.values())[0]['href']
39 1
            self.report_data = self._get_report_data(self.report_data_href)
40 1
            self.definitions = self._get_definitions()
41 1
            self.oval_definitions = self._get_oval_definitions()
42 1
            self.scan_definitions = _OVALScanDefinitions(
43
                self.definitions, self.oval_definitions, self.report_data).get_scan()
44 1
        except BaseException as error:
45 1
            raise ValueError(
46
                'This file "{}" is not arf report file or there are no results'.format(
47
                    self.src)) from error
48
49 1
    @staticmethod
50
    def get_src(src):
51 1
        dir_path = os.path.dirname(os.path.realpath(__file__))
52 1
        return str(os.path.join(dir_path, src))
53
54 1
    def validate(self, xsd_path):
55 1
        xsd_path = self.get_src(xsd_path)
56 1
        xmlschema_doc = ET.parse(xsd_path)
57 1
        xmlschema = ET.XMLSchema(xmlschema_doc)
58
59 1
        xml_doc = self.tree
60 1
        return xmlschema.validate(xml_doc)
61
62 1
    @staticmethod
63
    def _get_rule_dict(rule_result, result, id_def, check_content_ref):
64 1
        message = rule_result.find('.//xccdf:message', ns)
65 1
        rule_dict = {}
66 1
        rule_dict['id_def'] = id_def
67 1
        rule_dict['href'] = check_content_ref.attrib.get('href')
68 1
        rule_dict['result'] = result.text
69 1
        if message is not None:
70
            rule_dict['message'] = message.text
71 1
        return rule_dict
72
73 1
    def _get_used_rules(self):
74 1
        rules_results = self.root.findall(
75
            './/xccdf:TestResult/xccdf:rule-result', ns)
76 1
        rules = {}
77 1
        not_tested_rules = {}
78 1
        for rule_result in rules_results:
79 1
            result = rule_result.find('.//xccdf:result', ns)
80 1
            check_content_ref = rule_result.find(
81
                './/xccdf:check/xccdf:check-content-ref', ns)
82 1
            if check_content_ref is not None:
83 1
                id_ = rule_result.get('idref')
84 1
                id_def = check_content_ref.attrib.get('name')
85 1
                if id_def is not None:
86 1
                    rules[id_] = self._get_rule_dict(
87
                        rule_result, result, id_def, check_content_ref)
88 1
                    continue
89 1
            not_tested_rules[rule_result.get('idref')] = result.text
90 1
        return (rules, not_tested_rules)
91
92 1
    def _get_report_data(self, href):
93 1
        report_data = None
94 1
        reports = self.root.find('.//arf:reports', ns)
95 1
        for report in reports:
96 1
            if "#" + str(report.get("id")) == href:
97 1
                report_data = report
98 1
        return report_data
99
100 1
    def _get_definitions(self):
101 1
        data = self.report_data.find(
102
            ('.//XMLSchema:oval_results/XMLSchema:results/'
103
             'XMLSchema:system/XMLSchema:definitions'), ns)
104 1
        return data
105
106 1
    def _get_oval_definitions(self):
107 1
        return self.root.find(
108
            './/arf:report-requests/arf:report-request/'
109
            'arf:content/scap:data-stream-collection/'
110
            'scap:component/oval-definitions:oval_definitions/'
111
            'oval-definitions:definitions', ns)
112
113 1
    def _get_definition_of_rule(self, rule_id):
114 1
        if rule_id in self.used_rules:
115 1
            rule_info = self.used_rules[rule_id]
116 1
            return dict(rule_id=rule_id,
117
                        definition_id=rule_info['id_def'],
118
                        definition=self.scan_definitions[rule_info['id_def']])
119
120 1
        if rule_id in self.not_tested_rules:
121 1
            raise NotTestedRule(
122
                'Rule "{}" is {}, so there are no results.'
123
                .format(rule_id, self.not_tested_rules[rule_id]))
124 1
        raise ValueError('404 rule "{}" not found!'.format(rule_id))
125
126 1
    def get_oval_tree(self, rule_id):
127 1
        return Builder.dict_of_rule_to_oval_tree(
128
            self._get_definition_of_rule(rule_id))
129