1
|
1 |
|
import json |
2
|
1 |
|
import os |
3
|
|
|
|
4
|
1 |
|
from .client import Client |
5
|
1 |
|
from .exceptions import NotChecked |
6
|
1 |
|
from .xml_parser import XmlParser |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class ArfToJson(Client): |
10
|
1 |
|
def _set_attributes(self): |
11
|
1 |
|
self.show_failed_rules = self.arg.show_failed_rules |
12
|
1 |
|
self.show_not_selected_rules = self.arg.show_not_selected_rules |
13
|
1 |
|
self.xml_parser = XmlParser(self.source_filename) |
14
|
|
|
|
15
|
1 |
|
def _get_message(self): |
16
|
1 |
|
MESSAGES = { |
17
|
|
|
'description': 'Client for generating JSON of SCAP rule evaluation results', |
18
|
|
|
'source_filename': 'ARF scan file', |
19
|
|
|
} |
20
|
1 |
|
return MESSAGES |
21
|
|
|
|
22
|
1 |
|
def create_dict_of_rule(self, rule_id): |
23
|
1 |
|
return self.xml_parser.get_oval_tree(rule_id).save_tree_to_dict() |
24
|
|
|
|
25
|
1 |
|
def file_is_empty(self, path): |
26
|
1 |
|
return os.stat(path).st_size == 0 |
27
|
|
|
|
28
|
1 |
|
def save_dict_as_json(self, dict_, src): |
29
|
1 |
|
if os.path.isfile(src) and not self.file_is_empty(src): |
30
|
1 |
|
with open(src, "r") as f: |
31
|
1 |
|
data = json.load(f) |
32
|
1 |
|
for key in data: |
33
|
1 |
|
dict_[key] = data[key] |
34
|
1 |
|
with open(src, "w+") as f: |
35
|
1 |
|
json.dump(dict_, f) |
36
|
|
|
|
37
|
1 |
|
def prepare_data(self, rules): |
38
|
1 |
|
out = [] |
39
|
1 |
|
rule = None |
40
|
1 |
|
out_oval_tree_dict = dict() |
41
|
1 |
|
for rule in rules['rules']: |
42
|
1 |
|
try: |
43
|
1 |
|
out_oval_tree_dict[self.START_OF_FILE_NAME + rule + |
44
|
|
|
self.date] = self.create_dict_of_rule(rule) |
45
|
1 |
|
except NotChecked as error: |
46
|
|
|
out_oval_tree_dict[self.START_OF_FILE_NAME + rule + |
47
|
|
|
self.date] = str(error) |
48
|
1 |
|
if self.out is not None: |
49
|
1 |
|
self.save_dict_as_json(out_oval_tree_dict, self.out) |
50
|
1 |
|
out.append(self.out) |
51
|
|
|
else: |
52
|
1 |
|
print( |
53
|
|
|
str(json.dumps(out_oval_tree_dict, sort_keys=False, indent=4))) |
54
|
1 |
|
return out |
55
|
|
|
|
56
|
1 |
|
def prepare_parser(self): |
57
|
1 |
|
super().prepare_parser() |
58
|
|
|
self.prepare_args_when_user_can_list_in_rules() |
59
|
|
|
|