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