-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
193 lines (176 loc) · 6.89 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import json
import csv
import re, hashlib
DEFAULT_COMPLETE_ARRAY = [['parent_rule', 'child_rule', 'if_condition', 'boolean', 'then_behaviour']]
complete_array = DEFAULT_COMPLETE_ARRAY.copy()
complete_array1 = [['parent_rule', 'child_rule', 'if_condition', 'boolean', 'then_behaviour']]
def write2csv(data, csv_file):
with open(csv_file, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
def convert_behavior(behaviors):
print("----------------------------------------------------------------------------------------------------------------------")
if behaviors:
a = ''
for x in behaviors:
if x['name'] == 'setVariable' and x['options']:
a += x['name']
a += ':' + x['options']['variableName'] if 'variableName' in x['options'].items() else ""
a += ':' + x['options']['variableValue'] if 'variableValue' in x['options'].items() else ""
a += ':' + x['options']['extractLocation'] if 'extractLocation' in x['options'].items() else ""
elif x['name'] == 'modifyOutgoingRequestHeader' or x['name'] == 'modifyOutgoingResponseHeader':
pattern1 = re.compile(r'HeaderName', re.IGNORECASE)
pattern2 = re.compile(r'value', re.IGNORECASE)
a = a + x['name']
a = a + ':'.join([f"{value}" for key, value in x['options'].items() if re.search(pattern1, key)])
a = a + ':'.join([f"{value}" for key, value in x['options'].items() if re.search(pattern2, key)])
elif x['name'] == 'denyAccess':
a = a + x['name']
a = a + ':' + x['options']['reason'] if 'reason' in x['options'].items() else ""
a = a + ':' + x['options']['enabled'] if 'enabled' in x['options'].items() else ""
else:
a = a + x['name']
a = a + ':' + str(x)
return a
else:
return behaviors
def extract_values(array_of_dicts):
result = ""
for d in array_of_dicts:
name = d.get('name')
options = d.get('options', {})
if name == 'setVariable':
variable_name = options.get('variableName', '')
variable_value = options.get('variableValue', '')
extract_location = options.get('extractLocation', '')
result += variable_name+":"+variable_value+":"+extract_location
result += "\n"
elif name in ['modifyOutgoingRequestHeader', 'modifyOutgoingResponseHeader']:
pattern1 = re.compile(r'HeaderName', re.IGNORECASE)
pattern2 = re.compile(r'value', re.IGNORECASE)
header_name_values = [value for key, value in options.items() if re.search(r'headername', key, re.IGNORECASE)]
header_name = options.get('customHeaderName', '')
new_header_value = options.get('newHeaderValue', '')
result += f"Header Name: {header_name}, New Header Value: {new_header_value}\n"
result += "\n"
elif name == 'denyAccess':
reason = options.get('reason', '')
enabled = options.get('enabled', '')
result += f"Reason: {reason}, Enabled: {enabled}\n"
result += "\n"
else:
result += str(d)
return result
def process_options_in_list(list_of_dicts):
result = ""
for dictionary in list_of_dicts:
options = dictionary.get('options', {})
dictionary.update(options)
dictionary.pop('options', None)
result += str(dictionary) + "\n"
return result
def boolean_decider(a):
if a == 'any':
return "OR"
else:
return "AND"
def empty_children(a,x,name):
if not name:
a.append(x['name'])
a.append(x['children'])
a.append(process_options_in_list(x['criteria']))
a.append(boolean_decider(x['criteriaMustSatisfy']))
a.append(extract_values(x['behaviors']))
complete_array.append(a)
else:
a.append(name)
if name != x['name']:
a.append(x['name'])
else:
a.append('')
a.append(process_options_in_list(x['criteria']))
a.append(boolean_decider(x['criteriaMustSatisfy']))
a.append(extract_values(x['behaviors']))
complete_array.append(a)
def empty_children_1(a,x,name):
if not name:
a.append(x['name'])
a.append('')
if 'criteria' in x and x['criteria']:
a.append(process_options_in_list(x['criteria']))
else:
a.append('')
if 'criteriaMustSatisfy' in x and x['criteriaMustSatisfy']:
a.append(boolean_decider(x['criteriaMustSatisfy']))
else:
a.append('')
if 'behaviors' in x and x['behaviors']:
a.append(extract_values(x['behaviors']))
else:
a.append('')
complete_array.append(a)
else:
a.append(name)
a.append(x['name'])
if 'criteria' in x and x['criteria']:
a.append(process_options_in_list(x['criteria']))
else:
a.append('')
if 'criteriaMustSatisfy' in x and x['criteriaMustSatisfy']:
a.append(boolean_decider(x['criteriaMustSatisfy']))
else:
a.append('')
if 'behaviors' in x and x['behaviors']:
a.append(extract_values(x['behaviors']))
else:
a.append('')
complete_array.append(a)
def finder(x,y):
a = []
b = []
d = ''
if not x['children']:
if not y:
empty_children(a,x,x['name'])
else:
empty_children(a,x,y)
else:
if y:
d = y + '/' + x['name']
else:
d = x['name']
if ('criteria' in x and x['criteria']) or ('behaviors' in x and x['behaviors']):
if y:
empty_children_1(a,x,y)
else:
empty_children_1(a,x,'')
for k in x['children']:
finder(k,d)
def main(json_data):
global complete_array
complete_array = DEFAULT_COMPLETE_ARRAY.copy()
c = []
if 'rules' in json_data:
rules_data = json_data['rules']
if 'children' in rules_data:
first_children = rules_data['children']
for x in first_children:
finder(x,'')
if 'rules' in json_data and 'advancedOverride' in json_data['rules']:
if json_data['rules']['advancedOverride']:
c.append("Advanced Override")
c.append("")
c.append("")
c.append("")
c.append(json_data['rules']['advancedOverride'])
complete_array.append(c)
return complete_array, json_data['propertyVersion']
#commenting this as its used another script
"""file_path = "input.json"
csv_file = "output.csv"
with open(file_path, "r") as file:
json_data = json.load(file)
a = main(json_data)
#a = complete_array
#print(a)
write2csv(complete_array, csv_file)"""