1
|
|
|
''' |
2
|
|
|
Modules form my lib and for create ID |
3
|
|
|
''' |
4
|
1 |
|
import graph.xml_parser |
5
|
1 |
|
import graph.evaluate |
6
|
1 |
|
import uuid |
7
|
1 |
|
import collections |
8
|
1 |
|
import re |
9
|
|
|
|
10
|
|
|
''' |
11
|
|
|
This module contains methods and classes for |
12
|
|
|
constructing and controlling an oval tree. |
13
|
|
|
''' |
14
|
|
|
|
15
|
|
|
|
16
|
1 |
|
class OvalNode(): |
17
|
|
|
''' |
18
|
|
|
The OvalNode object is one node of oval graph. |
19
|
|
|
|
20
|
|
|
Args: |
21
|
|
|
node_id (str|int): identifies node |
22
|
|
|
input_node_type (str): type of node (value or operator) |
23
|
|
|
input_value (str): value of node |
24
|
|
|
children ([OvalNode]): array of children of node |
25
|
|
|
|
26
|
|
|
Attributes: |
27
|
|
|
node_id (str): id of node |
28
|
|
|
node_type (str): type node |
29
|
|
|
value (str): value of node for operator and, |
30
|
|
|
or, one etc... and for value true, false, error etc... |
31
|
|
|
children ([OvalNode]): children of node |
32
|
|
|
''' |
33
|
|
|
|
34
|
1 |
|
def __init__( |
35
|
|
|
self, |
36
|
|
|
node_id, |
37
|
|
|
input_node_type, |
38
|
|
|
input_value, |
39
|
|
|
input_negation, |
40
|
|
|
children=None): |
41
|
1 |
|
self.node_id = node_id |
42
|
1 |
|
if isinstance(input_negation, bool): |
43
|
1 |
|
self.negation = input_negation |
44
|
|
|
else: |
45
|
1 |
|
raise ValueError("err- negation is bool (only True or False)") |
46
|
1 |
|
value = input_value.lower() |
47
|
1 |
|
node_type = input_node_type.lower() |
48
|
1 |
|
if node_type == "value" or node_type == "operator": |
49
|
1 |
|
self.node_type = node_type |
50
|
|
|
else: |
51
|
|
|
raise ValueError("err- unknown type") |
52
|
1 |
|
allowed_operators = [ |
53
|
|
|
"or", |
54
|
|
|
"and", |
55
|
|
|
"one", |
56
|
|
|
"xor"] |
57
|
1 |
|
allowed_values = [ |
58
|
|
|
"true", |
59
|
|
|
"false", |
60
|
|
|
"error", |
61
|
|
|
"unknown", |
62
|
|
|
"noteval", |
63
|
|
|
"notappl"] |
64
|
1 |
|
if self.node_type == "value": |
65
|
1 |
|
if value in allowed_values: |
66
|
1 |
|
self.value = value |
67
|
|
|
else: |
68
|
1 |
|
raise ValueError("err- unknown value") |
69
|
1 |
|
if self.node_type == "operator": |
70
|
1 |
|
if value in allowed_operators: |
71
|
1 |
|
self.value = value |
72
|
|
|
else: |
73
|
1 |
|
raise ValueError("err- unknown operator") |
74
|
1 |
|
self.children = [] |
75
|
1 |
|
if children is not None: |
76
|
1 |
|
for child in children: |
77
|
1 |
|
self.add_child(child) |
78
|
|
|
else: |
79
|
1 |
|
if self.node_type == "operator": |
80
|
1 |
|
raise ValueError('err- Operator node has child!') |
81
|
|
|
|
82
|
1 |
|
def __repr__(self): |
83
|
1 |
|
return self.value |
84
|
|
|
|
85
|
1 |
|
def add_child(self, node): |
86
|
1 |
|
if self.node_type == "operator": |
87
|
1 |
|
assert isinstance(node, OvalNode) |
88
|
1 |
|
self.children.append(node) |
89
|
|
|
else: |
90
|
1 |
|
self.children = None |
91
|
1 |
|
raise ValueError( |
92
|
|
|
"err- Value node don't has any child!") |
93
|
|
|
|
94
|
1 |
|
def _get_result_counts(self): |
95
|
1 |
|
result = { |
96
|
|
|
'true_cnt': 0, |
97
|
|
|
'false_cnt': 0, |
98
|
|
|
'error_cnt': 0, |
99
|
|
|
'unknown_cnt': 0, |
100
|
|
|
'noteval_cnt': 0, |
101
|
|
|
'notappl_cnt': 0 |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
for child in self.children: |
105
|
1 |
|
if child.value == 'true': |
106
|
1 |
|
if child.negation: |
107
|
1 |
|
result['false_cnt'] += 1 |
108
|
|
|
else: |
109
|
1 |
|
result['true_cnt'] += 1 |
110
|
1 |
|
elif child.value == 'false': |
111
|
1 |
|
if child.negation: |
112
|
1 |
|
result['true_cnt'] += 1 |
113
|
|
|
else: |
114
|
1 |
|
result['false_cnt'] += 1 |
115
|
1 |
|
elif child.value == 'error': |
116
|
1 |
|
result['error_cnt'] += 1 |
117
|
1 |
|
elif child.value == 'unknown': |
118
|
1 |
|
result['unknown_cnt'] += 1 |
119
|
1 |
|
elif child.value == 'noteval': |
120
|
1 |
|
result['noteval_cnt'] += 1 |
121
|
1 |
|
elif child.value == 'notappl': |
122
|
1 |
|
result['notappl_cnt'] += 1 |
123
|
|
|
else: |
124
|
1 |
|
if self.node_type == "operator": |
125
|
1 |
|
result[child.evaluate_tree() + "_cnt"] += 1 |
126
|
1 |
|
return result |
127
|
|
|
|
128
|
1 |
|
def evaluate_tree(self): |
129
|
1 |
|
result = self._get_result_counts() |
130
|
1 |
|
out_result = None |
131
|
1 |
|
if graph.evaluate.is_notapp_result(result): |
132
|
1 |
|
out_result = "notappl" |
133
|
|
|
else: |
134
|
1 |
|
if self.value == "or": |
135
|
1 |
|
out_result = graph.evaluate.oval_operator_or(result) |
136
|
1 |
|
elif self.value == "and": |
137
|
1 |
|
out_result = graph.evaluate.oval_operator_and(result) |
138
|
1 |
|
elif self.value == "one": |
139
|
1 |
|
out_result = graph.evaluate.oval_operator_one(result) |
140
|
1 |
|
elif self.value == "xor": |
141
|
1 |
|
out_result = graph.evaluate.oval_operator_xor(result) |
142
|
|
|
|
143
|
1 |
|
if out_result == 'true' and self.negation: |
144
|
1 |
|
out_result = 'false' |
145
|
1 |
|
elif out_result == 'false' and self.negation: |
146
|
1 |
|
out_result = 'true' |
147
|
|
|
|
148
|
1 |
|
return out_result |
149
|
|
|
|
150
|
1 |
|
def save_tree_to_dict(self): |
151
|
1 |
|
if not self.children: |
152
|
1 |
|
return { |
153
|
|
|
'node_id': self.node_id, |
154
|
|
|
'type': self.node_type, |
155
|
|
|
'value': self.value, |
156
|
|
|
'negation': self.negation, |
157
|
|
|
'child': None |
158
|
|
|
} |
159
|
1 |
|
return { |
160
|
|
|
'node_id': self.node_id, |
161
|
|
|
'type': self.node_type, |
162
|
|
|
'value': self.value, |
163
|
|
|
'negation': self.negation, |
164
|
|
|
'child': [child.save_tree_to_dict() for child in self.children] |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
def find_node_with_ID(self, node_id): |
168
|
1 |
|
if self.node_id == node_id: |
169
|
1 |
|
return self |
170
|
|
|
else: |
171
|
1 |
|
for child in self.children: |
172
|
1 |
|
if child.node_id == node_id: |
173
|
1 |
|
return child |
174
|
1 |
|
for child in self.children: |
175
|
1 |
|
if child.children != []: |
176
|
1 |
|
return child.find_node_with_ID(node_id) |
177
|
|
|
|
178
|
1 |
|
def add_to_tree(self, node_id, newNode): |
179
|
1 |
|
self.find_node_with_ID(node_id).add_child(newNode) |
180
|
|
|
|
181
|
1 |
|
def change_tree_value(self, node_id, value): |
182
|
1 |
|
self.find_node_with_ID(node_id).value = value |
183
|
|
|
|
184
|
|
|
# Methods for interpreting oval tree with SigmaJS |
185
|
|
|
|
186
|
1 |
|
def _get_label(self): |
187
|
1 |
|
if self.node_type == 'value': |
188
|
1 |
|
return re.sub( |
189
|
|
|
'(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.node_id)) |
190
|
|
|
else: |
191
|
1 |
|
if str(self.node_id).startswith('xccdf_org'): |
192
|
1 |
|
return re.sub( |
193
|
|
|
'(xccdf_org.ssgproject.content_)', '', str( |
194
|
|
|
self.node_id)) |
195
|
1 |
|
return self.value |
196
|
|
|
|
197
|
1 |
|
def _is_negated_boolean(self, boolean, value): |
198
|
1 |
|
if value == boolean and self.negation: |
199
|
1 |
|
return True |
200
|
1 |
|
return False |
201
|
|
|
|
202
|
1 |
|
def _get_node_colors(self): |
203
|
1 |
|
value = self.evaluate_tree() |
204
|
1 |
|
borderValue = None |
205
|
1 |
|
if value is None: |
206
|
1 |
|
if self._is_negated_boolean('true', self.value): |
207
|
1 |
|
borderValue = 'false' |
208
|
1 |
|
elif self._is_negated_boolean('false', self.value): |
209
|
1 |
|
borderValue = 'true' |
210
|
|
|
else: |
211
|
1 |
|
borderValue = self.value |
212
|
1 |
|
borderValue, value = self.value, borderValue |
213
|
|
|
else: |
214
|
1 |
|
if self._is_negated_boolean('true', value): |
215
|
1 |
|
borderValue = 'false' |
216
|
1 |
|
elif self._is_negated_boolean('false', value): |
217
|
1 |
|
borderValue = 'true' |
218
|
|
|
else: |
219
|
1 |
|
borderValue = value |
220
|
1 |
|
VALUE_TO_COLOR = { |
221
|
|
|
"true": "#00ff00", |
222
|
|
|
"false": "#ff0000", |
223
|
|
|
"error": "#000000", |
224
|
|
|
"unknown": "#000000", |
225
|
|
|
"noteval": "#000000", |
226
|
|
|
"notappl": "#000000" |
227
|
|
|
} |
228
|
1 |
|
return dict( |
229
|
|
|
color=VALUE_TO_COLOR[value], |
230
|
|
|
borderColor=VALUE_TO_COLOR[borderValue]) |
231
|
|
|
|
232
|
1 |
|
def _get_node_title(self): |
233
|
1 |
|
value = self.evaluate_tree() |
234
|
1 |
|
if value is None: |
235
|
1 |
|
value = self.value |
236
|
1 |
|
if value == 'true' or value == 'false': |
237
|
1 |
|
return self.node_id |
238
|
1 |
|
return str(self.node_id) + ' ' + self.value |
239
|
|
|
|
240
|
1 |
|
def _create_node(self, x, y): |
241
|
|
|
# print(self.evaluate_tree(),self.value) |
242
|
1 |
|
colors = self._get_node_colors() |
243
|
1 |
|
return { |
244
|
|
|
'id': self.node_id, |
245
|
|
|
'label': self._get_label(), |
246
|
|
|
'url': 'null', |
247
|
|
|
'text': 'null', |
248
|
|
|
'title': self._get_node_title(), |
249
|
|
|
"x": x, |
250
|
|
|
"y": y, |
251
|
|
|
"size": 3, |
252
|
|
|
"color": colors['color'], |
253
|
|
|
"type": "circle", |
254
|
|
|
"borderColor": colors['borderColor']} |
255
|
|
|
|
256
|
1 |
|
def _create_edge(self, id_source, id_target, target_node): |
257
|
1 |
|
return { |
258
|
|
|
"id": str(uuid.uuid4()), |
259
|
|
|
"source": id_source, |
260
|
|
|
"target": id_target, |
261
|
|
|
"color": self._get_color_edge(target_node) |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
def _get_color_edge(self, target_node): |
265
|
1 |
|
return target_node['color'] |
266
|
|
|
|
267
|
1 |
|
def create_list_of_id(self, array_of_ids=None): |
268
|
1 |
|
if array_of_ids is None: |
269
|
1 |
|
array_of_ids = [] |
270
|
1 |
|
array_of_ids.append(self.node_id) |
271
|
1 |
|
for child in self.children: |
272
|
1 |
|
if child.node_type != "operator": |
273
|
1 |
|
array_of_ids.append(child.node_id) |
274
|
|
|
else: |
275
|
1 |
|
array_of_ids.append(child.node_id) |
276
|
1 |
|
child.create_list_of_id(array_of_ids) |
277
|
1 |
|
return array_of_ids |
278
|
|
|
|
279
|
1 |
|
def _remove_Duplication(self, graph_data): |
280
|
1 |
|
array_of_ids = self.create_list_of_id() |
281
|
1 |
|
out = dict(nodes=[], edges=graph_data['edges']) |
282
|
1 |
|
duplicate_ids = [item for item, count in collections.Counter( |
283
|
|
|
array_of_ids).items() if count > 1] |
284
|
|
|
|
285
|
1 |
|
for node in graph_data['nodes']: |
286
|
1 |
|
if node['id'] not in duplicate_ids: |
287
|
1 |
|
out['nodes'].append(node) |
288
|
|
|
|
289
|
1 |
|
for id in duplicate_ids: |
290
|
1 |
|
for node in graph_data['nodes']: |
291
|
1 |
|
if node['id'] == id: |
292
|
1 |
|
out['nodes'].append(node) |
293
|
1 |
|
break |
294
|
1 |
|
return out |
295
|
|
|
|
296
|
1 |
|
def _fix_graph(self, preprocessed_graph_data): |
297
|
1 |
|
for node in preprocessed_graph_data['nodes']: |
298
|
1 |
|
for node1 in preprocessed_graph_data['nodes']: |
299
|
1 |
|
if node['x'] == node1['x'] and node['y'] == node1['y']: |
300
|
1 |
|
node['x'] = node['x'] - 1 |
301
|
1 |
|
return preprocessed_graph_data |
302
|
|
|
|
303
|
1 |
|
def _help_to_sigma_dict(self, x, y, preprocessed_graph_data=None): |
304
|
1 |
|
if preprocessed_graph_data is None: |
305
|
1 |
|
preprocessed_graph_data = dict(nodes=[], edges=[]) |
306
|
1 |
|
preprocessed_graph_data['nodes'].append(self._create_node(x, y)) |
307
|
1 |
|
y_row = y + 1 |
308
|
1 |
|
x_row = x |
309
|
1 |
|
for node in self.children: |
310
|
1 |
|
preprocessed_graph_data['nodes'].append( |
311
|
|
|
node._create_node(x_row, y_row)) |
312
|
1 |
|
preprocessed_graph_data['edges'].append(node._create_edge( |
313
|
|
|
self.node_id, node.node_id, preprocessed_graph_data['nodes'][-1])) |
314
|
1 |
|
x_row = x_row + 1 |
315
|
1 |
|
if node.children is not None: |
316
|
1 |
|
preprocessed_graph_data = node._help_to_sigma_dict( |
317
|
|
|
x_row + 1, y_row + 1, preprocessed_graph_data) |
318
|
1 |
|
return self._fix_graph(preprocessed_graph_data) |
319
|
|
|
|
320
|
1 |
|
def _count_max_y(self, out): |
321
|
1 |
|
max_y = 0 |
322
|
|
|
|
323
|
1 |
|
for node in out['nodes']: |
324
|
1 |
|
if max_y < node['y']: |
325
|
1 |
|
max_y = node['y'] |
326
|
1 |
|
return max_y |
327
|
|
|
|
328
|
1 |
|
def _create_nodes_in_rows(self, rows): |
329
|
1 |
|
nodes_in_rows = dict() |
330
|
|
|
|
331
|
1 |
|
for i in range(rows + 1): |
332
|
1 |
|
nodes_in_rows[i] = [] |
333
|
1 |
|
return nodes_in_rows |
334
|
|
|
|
335
|
1 |
|
def _push_nodes_to_nodes_in_row(self, out, nodes_in_rows): |
336
|
1 |
|
for node in out['nodes']: |
337
|
1 |
|
nodes_in_rows[node['y']].append(node) |
338
|
|
|
|
339
|
1 |
|
def _remove_empty_rows(self, nodes_in_rows, max_y): |
340
|
1 |
|
for row in range(max_y + 1): |
341
|
1 |
|
if not nodes_in_rows[row]: |
342
|
1 |
|
del nodes_in_rows[row] |
343
|
|
|
|
344
|
1 |
|
def _move_rows(self, nodes_in_rows): |
345
|
1 |
|
count = 0 |
346
|
1 |
|
nodes_in_rows1 = dict() |
347
|
|
|
|
348
|
1 |
|
for row in nodes_in_rows: |
349
|
1 |
|
nodes_in_rows1[count] = nodes_in_rows[row] |
350
|
1 |
|
for node in nodes_in_rows1[count]: |
351
|
1 |
|
node['y'] = count |
352
|
1 |
|
count += 1 |
353
|
1 |
|
return nodes_in_rows1 |
354
|
|
|
|
355
|
1 |
|
def _create_positions(self, nodes_in_rows): |
356
|
1 |
|
positions = [] |
357
|
1 |
|
for row in nodes_in_rows: |
358
|
1 |
|
len_of_row = len(nodes_in_rows[row]) |
359
|
1 |
|
if len_of_row > 1: |
360
|
1 |
|
if (len_of_row % 2) == 1: |
361
|
1 |
|
len_of_row += 1 |
362
|
|
|
|
363
|
1 |
|
for i in range((int(-(len_of_row / 2))) * 2, |
364
|
|
|
(int(+(len_of_row / 2)) + 1) * 2, 2): |
365
|
1 |
|
positions.append(i) |
366
|
|
|
|
367
|
1 |
|
if len_of_row == 2: |
368
|
1 |
|
positions.remove(0) |
369
|
|
|
|
370
|
1 |
|
if len(nodes_in_rows[row]) < len(positions): |
371
|
1 |
|
positions.pop() |
372
|
1 |
|
if len(nodes_in_rows[row]) < len(positions): |
373
|
1 |
|
positions.pop(0) |
374
|
|
|
|
375
|
1 |
|
count = 0 |
376
|
|
|
|
377
|
1 |
|
for pos in positions: |
378
|
1 |
|
nodes_in_rows[row][count]['x'] = pos |
379
|
1 |
|
count += 1 |
380
|
1 |
|
positions = [] |
381
|
|
|
else: |
382
|
1 |
|
nodes_in_rows[row][0]['x'] = 0 |
383
|
|
|
|
384
|
1 |
|
return positions |
385
|
|
|
|
386
|
1 |
|
def _convert_nodes_in_rows_to_nodes(self, nodes_in_rows): |
387
|
1 |
|
nodes = [] |
388
|
1 |
|
for row in nodes_in_rows: |
389
|
1 |
|
for node in nodes_in_rows[row]: |
390
|
1 |
|
nodes.append(node) |
391
|
1 |
|
return nodes |
392
|
|
|
|
393
|
1 |
|
def _change_position(self, positions, nodes_in_rows): |
394
|
1 |
|
x = 0.6 |
395
|
1 |
|
up_and_down = True |
396
|
1 |
|
down = False |
397
|
1 |
|
down_row = False |
398
|
1 |
|
save_x = 0 |
399
|
1 |
|
continue_move = False |
400
|
|
|
|
401
|
1 |
|
for row in nodes_in_rows: |
402
|
1 |
|
for node in nodes_in_rows[row]: |
403
|
1 |
|
if (len(node['label']) > 6 |
404
|
|
|
and len(node['label']) < 40 |
405
|
|
|
or continue_move): |
406
|
1 |
|
if up_and_down: |
407
|
1 |
|
node['y'] = node['y'] + (0.6 * x) |
408
|
1 |
|
up_and_down = False |
409
|
|
|
else: |
410
|
1 |
|
up_and_down = True |
411
|
1 |
|
continue_move = True |
412
|
1 |
|
elif len(node['label']) > 30: |
413
|
1 |
|
node['y'] = node['y'] + (0.6 * x) |
414
|
1 |
|
x += 0.6 |
415
|
1 |
|
save_x = x |
416
|
1 |
|
down = True |
417
|
|
|
else: |
418
|
1 |
|
if down: |
419
|
1 |
|
node['y'] = node['y'] + (0.6 * save_x) |
420
|
|
|
|
421
|
1 |
|
if down_row: |
422
|
1 |
|
node['y'] = node['y'] + (0.6 * save_x) - 0.7 |
423
|
1 |
|
if down: |
424
|
1 |
|
down = False |
425
|
1 |
|
down_row = True |
426
|
1 |
|
continue_move = False |
427
|
1 |
|
x = 0.6 |
428
|
|
|
|
429
|
1 |
|
def _sort(self, array): |
430
|
1 |
|
less = [] |
431
|
1 |
|
equal = [] |
432
|
1 |
|
greater = [] |
433
|
|
|
|
434
|
1 |
|
if len(array) > 1: |
435
|
1 |
|
pivot = array[0]['x'] |
436
|
1 |
|
for node in array: |
437
|
1 |
|
if node['x'] < pivot: |
438
|
|
|
less.append(node) |
439
|
1 |
|
if node['x'] == pivot: |
440
|
1 |
|
equal.append(node) |
441
|
1 |
|
if node['x'] > pivot: |
442
|
1 |
|
greater.append(node) |
443
|
1 |
|
return self._sort(less) + equal + self._sort(greater) |
444
|
|
|
else: |
445
|
1 |
|
return array |
446
|
|
|
|
447
|
1 |
|
def _sort_nodes(self, nodes_in_rows): |
448
|
1 |
|
for row in nodes_in_rows: |
449
|
1 |
|
nodes_in_rows[row] = self._sort(nodes_in_rows[row]) |
450
|
|
|
|
451
|
1 |
|
def _center_graph(self, out): |
452
|
1 |
|
max_y = self._count_max_y(out) |
453
|
1 |
|
nodes_in_rows = self._create_nodes_in_rows(max_y) |
454
|
1 |
|
self._push_nodes_to_nodes_in_row(out, nodes_in_rows) |
455
|
1 |
|
self._remove_empty_rows(nodes_in_rows, max_y) |
456
|
1 |
|
nodes_in_rows = self._move_rows(nodes_in_rows) |
457
|
1 |
|
self._sort_nodes(nodes_in_rows) |
458
|
1 |
|
positions = self._create_positions(nodes_in_rows) |
459
|
1 |
|
self._change_position(positions, nodes_in_rows) |
460
|
1 |
|
out['nodes'] = self._convert_nodes_in_rows_to_nodes(nodes_in_rows) |
461
|
1 |
|
return out |
462
|
|
|
|
463
|
1 |
|
def to_sigma_dict(self, x, y): |
464
|
1 |
|
return self._center_graph( |
465
|
|
|
self._remove_Duplication( |
466
|
|
|
self._help_to_sigma_dict( |
467
|
|
|
x, y))) |
468
|
|
|
|
469
|
|
|
|
470
|
1 |
|
def build_nodes_form_xml(xml_src, rule_id): |
471
|
1 |
|
parser = graph.xml_parser.xml_parser(xml_src) |
472
|
1 |
|
return parser.get_oval_graph(rule_id) |
473
|
|
|
|
474
|
|
|
|
475
|
1 |
|
def restore_dict_to_tree(dict_of_tree): |
476
|
1 |
|
if dict_of_tree["child"] is None: |
477
|
1 |
|
return OvalNode( |
478
|
|
|
dict_of_tree["node_id"], |
479
|
|
|
dict_of_tree["type"], |
480
|
|
|
dict_of_tree["value"], |
481
|
|
|
dict_of_tree["negation"]) |
482
|
1 |
|
return OvalNode( |
483
|
|
|
dict_of_tree["node_id"], |
484
|
|
|
dict_of_tree["type"], |
485
|
|
|
dict_of_tree["value"], |
486
|
|
|
dict_of_tree["negation"], |
487
|
|
|
[restore_dict_to_tree(i) for i in dict_of_tree["child"]]) |
488
|
|
|
|