1
|
1 |
|
from ..data_structures.data_structures import Remediation, Rule |
2
|
1 |
|
from ..namespaces import NAMESPACES |
3
|
1 |
|
from .description_parser import DescriptionParser |
4
|
|
|
|
5
|
|
|
|
6
|
1 |
|
class RuleParser(): |
7
|
1 |
|
def __init__(self): |
8
|
1 |
|
self.description_parser = DescriptionParser() |
9
|
|
|
|
10
|
1 |
|
@staticmethod |
11
|
1 |
|
def _get_references(rule): |
12
|
1 |
|
references = [] |
13
|
1 |
|
for referenc in rule.findall(".//xccdf:reference", NAMESPACES): |
14
|
1 |
|
ref = { |
15
|
|
|
"href": referenc.get("href"), |
16
|
|
|
"text": referenc.text, |
17
|
|
|
} |
18
|
1 |
|
references.append(ref) |
19
|
1 |
|
return references |
20
|
|
|
|
21
|
1 |
|
@staticmethod |
22
|
1 |
|
def _get_identifiers(rule): |
23
|
1 |
|
identifiers = [] |
24
|
1 |
|
for identifier in rule.findall(".//xccdf:ident", NAMESPACES): |
25
|
1 |
|
ident = { |
26
|
|
|
"system": identifier.get("system"), |
27
|
|
|
"text": identifier.text, |
28
|
|
|
} |
29
|
1 |
|
identifiers.append(ident) |
30
|
1 |
|
return identifiers |
31
|
|
|
|
32
|
1 |
|
@staticmethod |
33
|
1 |
|
def _get_warnings(rule): |
34
|
1 |
|
warnings = [] |
35
|
1 |
|
for warning in rule.findall(".//xccdf:warning", NAMESPACES): |
36
|
1 |
|
warnings.append(warning.text) |
37
|
1 |
|
return warnings |
38
|
|
|
|
39
|
1 |
|
@staticmethod |
40
|
1 |
|
def _get_remediations(rule): |
41
|
1 |
|
output = [] |
42
|
1 |
|
for fix in rule.findall(".//xccdf:fix", NAMESPACES): |
43
|
1 |
|
fix_dict = { |
44
|
|
|
"remediation_id": fix.get("id"), |
45
|
|
|
"system": fix.get("system"), |
46
|
|
|
"complexity": fix.get("complexity", ""), |
47
|
|
|
"disruption": fix.get("disruption", ""), |
48
|
|
|
"strategy": fix.get("strategy", ""), |
49
|
|
|
"fix": fix.text, |
50
|
|
|
} |
51
|
1 |
|
output.append(Remediation(**fix_dict)) |
52
|
1 |
|
return output |
53
|
|
|
|
54
|
1 |
|
@staticmethod |
55
|
1 |
|
def _get_multi_check(rule): |
56
|
1 |
|
for check in rule.findall(".//xccdf:check", NAMESPACES): |
57
|
1 |
|
if check.get("multi-check") == "true": |
58
|
1 |
|
return True |
59
|
1 |
|
return False |
60
|
|
|
|
61
|
1 |
|
@staticmethod |
62
|
1 |
|
def _get_check_content_refs_dict(rule): |
63
|
1 |
|
check_content_refs = rule.findall(".//xccdf:check-content-ref", NAMESPACES) |
64
|
1 |
|
check_content_refs_dict = {} |
65
|
1 |
|
if check_content_refs is not None: |
66
|
1 |
|
for check_ref in check_content_refs: |
67
|
1 |
|
name = check_ref.get("name", "") |
68
|
1 |
|
id_check = name[:name.find(":")] |
69
|
1 |
|
check_content_refs_dict[id_check] = name |
70
|
1 |
|
return check_content_refs_dict |
71
|
|
|
|
72
|
1 |
|
def process_rule(self, rule): |
73
|
1 |
|
rule_id = rule.get("id") |
74
|
|
|
|
75
|
1 |
|
rule_dict = { |
76
|
|
|
"rule_id": rule_id, |
77
|
|
|
"severity": rule.get("severity", "Unknown"), |
78
|
|
|
"description": self.description_parser.get_full_description(rule), |
79
|
|
|
"references": self._get_references(rule), |
80
|
|
|
"identifiers": self._get_identifiers(rule), |
81
|
|
|
"warnings": self._get_warnings(rule), |
82
|
|
|
"remediations": self._get_remediations(rule), |
83
|
|
|
"multi_check": self._get_multi_check(rule), |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
title = rule.find(".//xccdf:title", NAMESPACES) |
87
|
1 |
|
if title is not None: |
88
|
1 |
|
rule_dict["title"] = title.text |
89
|
|
|
|
90
|
1 |
|
rationale = rule.find(".//xccdf:rationale", NAMESPACES) |
91
|
1 |
|
if rationale is not None: |
92
|
1 |
|
rule_dict["rationale"] = rationale.text |
93
|
|
|
|
94
|
1 |
|
platforms = rule.findall(".//xccdf:platform", NAMESPACES) |
95
|
1 |
|
rule_dict["platforms"] = [] |
96
|
1 |
|
if platforms is not None: |
97
|
1 |
|
for platform in platforms: |
98
|
1 |
|
rule_dict["platforms"].append(platform.get("idref")) |
99
|
|
|
|
100
|
1 |
|
check_content_refs_dict = self._get_check_content_refs_dict(rule) |
101
|
1 |
|
rule_dict["oval_definition_id"] = check_content_refs_dict.get("oval", "") |
102
|
|
|
|
103
|
|
|
return Rule(**rule_dict) |
104
|
|
|
|