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