1
|
1 |
|
import json |
2
|
1 |
|
import os |
3
|
|
|
|
4
|
1 |
|
from ..exceptions import NotTestedRule |
5
|
1 |
|
from ..oval_tree.converter import Converter |
6
|
1 |
|
from .client_arf_input import ClientArfInput |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class ArfToJson(ClientArfInput): |
10
|
1 |
|
def __init__(self, args): |
11
|
1 |
|
super().__init__(args) |
12
|
1 |
|
self.out = self.arg.output |
13
|
|
|
|
14
|
1 |
|
def _get_message(self): |
15
|
1 |
|
return { |
16
|
|
|
'command_name': 'arf-to-json', |
17
|
|
|
'description': 'Client for generating JSON of SCAP rule evaluation results', |
18
|
|
|
'source_filename': 'ARF scan file', |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
def create_dict_of_rule(self, rule_id): |
22
|
1 |
|
oval_tree = self.arf_xml_parser.get_oval_tree(rule_id) |
23
|
1 |
|
converter = Converter(oval_tree) |
24
|
1 |
|
return converter.to_dict() |
25
|
|
|
|
26
|
1 |
|
@staticmethod |
27
|
|
|
def file_is_empty(path): |
28
|
1 |
|
return os.stat(path).st_size == 0 |
29
|
|
|
|
30
|
1 |
|
def save_dict_as_json(self, dict_, src): |
31
|
1 |
|
if os.path.isfile(src) and not self.file_is_empty(src): |
32
|
1 |
|
with open(src, "r", encoding="utf-8") as file_: |
33
|
1 |
|
data = json.load(file_) |
34
|
1 |
|
dict_.update(data) |
35
|
1 |
|
with open(src, "w+", encoding="utf-8") as file_: |
36
|
1 |
|
json.dump(dict_, file_) |
37
|
|
|
|
38
|
1 |
|
def _get_rule_key(self, rule): |
39
|
1 |
|
return rule + self._get_date() |
40
|
|
|
|
41
|
1 |
|
def prepare_data(self, rules): |
42
|
1 |
|
out = [] |
43
|
1 |
|
rule = None |
44
|
1 |
|
out_oval_tree_dict = {} |
45
|
1 |
|
for rule in rules['rules']: |
46
|
1 |
|
try: |
47
|
1 |
|
out_oval_tree_dict[self._get_rule_key(rule)] = self.create_dict_of_rule(rule) |
48
|
1 |
|
except NotTestedRule as error: |
49
|
1 |
|
out_oval_tree_dict[self._get_rule_key(rule)] = str(error) |
50
|
1 |
|
if self.out is not None: |
51
|
1 |
|
self.save_dict_as_json(out_oval_tree_dict, self.out) |
52
|
1 |
|
out.append(self.out) |
53
|
|
|
else: |
54
|
1 |
|
print( |
55
|
|
|
str(json.dumps(out_oval_tree_dict, sort_keys=False, indent=4))) |
56
|
1 |
|
return out |
57
|
|
|
|
58
|
1 |
|
def prepare_parser(self, parser): |
59
|
1 |
|
super().prepare_parser(parser) |
60
|
|
|
self.prepare_args_when_user_can_list_in_rules(parser) |
61
|
|
|
|