/
ObQuery.py
277 lines (226 loc) · 7.24 KB
/
ObQuery.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from lexical_analyzer import LexicalAnalyzer
from PythonJsonObj import PyObjTree, PyJSOb
import constant as constant
import utility as utl
import sys
import policy as Policy
from access_control import NodeHierarchy
class ObQuery:
def __init__(self, root_ob):
self.tree_root = root_ob
def _gapvalue(self,gap_key, obj):
r = []
'''only object type has primitve member of {k:v} format. In Array object, primitive members are only string, or number.
so, need not to check prim_mem of array object '''
if obj.type == "OBJECT":
for o in obj.prim_mem:
(k,v) = o.items()[0]
if k == gap_key:
r.append(v)
for o in obj.obj_mem:
for (k,v) in o.items():
if k == gap_key:
r.append(v)
else:
r = r + self._gapvalue(gap_key,v)
for o in obj.array_mem:
if isinstance(o,PyJSOb):
r = r + self._gapvalue(gap_key,o)
return r
pass
def query(self,path):
if path == "/":
#print "returing root obj----------------"
#print id( self.tree_root)
return [self.tree_root]
path_token = LexicalAnalyzer(path).token_pair()
ini_nodes = [self.tree_root]
final_nodes=[]
token_pair = path_token
for tp in token_pair:
(t1,t2) = tp
final_nodes = []
if t1 == "child":
for root in ini_nodes:
# looking into the object
for n in root.obj_mem:
for (k,v) in n.items():
if k == t2:
final_nodes.append(v)
for n in root.prim_mem:
(k,v) = n.items()[0]
if k == t2:
final_nodes.append(v)
pass
elif t1 == "index":
for root in ini_nodes:
t2 = int(t2)
try:
n = root.array_mem[t2]
except:
pass
final_nodes.append(n)
pass
elif t1 == "gap":
for root in ini_nodes:
final_nodes = final_nodes + self._gapvalue(t2,root)
pass
ini_nodes = final_nodes
return final_nodes
def _authorized_only(self, job, label_hierarchy, user_clearance):
# check descendant nodes for clearance
#array_mem = []
#obj_mem = []
#prim_mem = []
r_obj = []
#print job
if job.type == "OBJECT":
# check for obj_members
for ob in job.obj_mem:
(key,value) = ob.items()[0]
value = self._authorized_only(value,label_hierarchy, user_clearance)
if value:
r_obj = r_obj + [{key:value}] # if obj, pass key,value dictionary
pass
elif job.type == "ARRAY":
for ar in job.array_mem:
value = self._authorized_only(ar, label_hierarchy, user_clearance)
if value:
r_obj = r_obj + [value]
pass
else:
pass
# check if job is has clearance, a function shold be called here
if job.label != constant.DEFAULT_LABEL and label_hierarchy.check(user_clearance,job.label) :
#remove all existing array, ob members
job.array_mem = []
job.obj_mem = []
# return None when a json obj has no member
if len(r_obj) == 0 and len (job.prim_mem) == 0:
return None
for o in r_obj:
if type(o) is dict:
(k,v) = o.items()[0]
job.add_obj_mem(key=k, value=v)
else:
job.add_array_mem(o)
pass
return job
else:
ob = PyJSOb()
if len(r_obj) == 0:
return None
for mem in r_obj:
if type(mem) == dict:
(k,v) = mem.items()[0]
ob.add_obj_mem(key=k, value = v)
elif isinstance(mem,PyJSOb) and mem.type == constant.ARRAY:
ob.add_array_mem(mem)
return ob
# this method do query without ac, then apply access control on the result
# not considering clearance lattice.
def ac_query(self, path, label_hierarchy, user_clearance):
q_res = self.query(path)
authorized_list = []
for ob in q_res:
authorized_list.append( self._authorized_only(ob,label_hierarchy, user_clearance))
pass
return authorized_list
pass
class UserHierarchy:
def __init__(self):
pass
def get_hierarchy(self):
nh = NodeHierarchy()
nh.insert("private","public")
nh.insert("protected","private")
return nh
class ContentFilter:
def __init__(self, content_file=None, content_str=None, policy_file=None, policy_str=None, query=None, user_clearance=None):
self.content_file = content_file
self.content_str = content_str
self.policy_file = policy_file
self.policy_str = policy_str
self.JSONPath = query
self.user_clearance = user_clearance
#print "debug------"
#print locals()
def apply(self):
# ---- Build JSON tree -------#
if self.content_file:
obj_tree = PyObjTree(utl.LoadJSON(path=self.content_file).get_json()).get_root()
elif self.content_str:
obj_tree = PyObjTree(utl.LoadJSON(str=self.content_str).get_json()).get_root()
# ------- Label JSON tree with Policy -------#
if self.policy_file:
obj_tree = Policy.NodeLabeling(obj_tree,label_str=utl.File(self.policy_file).read()).appy_labels()
elif self.policy_str:
obj_tree = Policy.NodeLabeling(obj_tree,label_str=self.policy_str).appy_labels()
# ---- get uer Hierarchy ------#
hierarchy = UserHierarchy().get_hierarchy()
# --- Work with Query class for Querying --- #
oq = ObQuery(obj_tree)
self.JSONPath = self.JSONPath if self.JSONPath else '/'
qry = [self.JSONPath]
for q in qry:
if self.user_clearance : # if user_clearance is given, user hiearchy is used.
res = oq.ac_query(q,hierarchy,self.user_clearance)
else: # if user clearance is not given just use the JSONPath to query.
res = oq.query(q)
# we need to iterate through the res. there can be more than one result.
for r in res:
if type(r) is dict:
(k,v) = r.items()[0]
return utl.pretty_print ( v.print_json() )
pass
elif isinstance(r,PyJSOb):
#print r.print_json()
return utl.pretty_print ( r.print_json() )
else:
return r
#print utl.pretty_print ( obj_tree.print_json() )
def test1():
print ContentFilter(content_file="employee.json", policy_file="path_label_policy.json",query="/personalRecord",user_clearance="protected").apply()
def test():
#run with this command : python ObQuery.py employee.json /personalRecord
#build the Json Obj tree here
#obj_tree = PyObjTree(utl.LoadJSON(path=sys.argv[1]).get_json()).get_root()
obj_tree = PyObjTree(utl.LoadJSON(str=utl.File(sys.argv[1]).read()).get_json()).get_root()
#apply labels to obj tree
#obj_tree = Policy.NodeLabeling(obj_tree,label_file="path_label_policy.json").appy_labels()
obj_tree = Policy.NodeLabeling(obj_tree,label_str=utl.File("path_label_policy.json").read()).appy_labels()
#print utl.pretty_print (obj_tree.print_json())
#return
#querying against give path
#print id(obj_tree)
path = sys.argv[2]
nh = NodeHierarchy()
nh.insert("private","public")
nh.insert("protected","private")
oq = ObQuery(obj_tree)
#print oq.query_on_condition( {'path':'name', 'op':'=', 'value':'Alice'}, obj_tree)
#return
#qry = ["/","/personalRecord","/","/personalRecord/identification"]
qry = [path]
for q in qry:
#print "path is {}".format(q)
if len(sys.argv) >=4 :
res = oq.ac_query(q,nh,sys.argv[3])
else:
res = oq.query(q)
# we need to iterate through the res. there can be more than one result.
#print (res)
for r in res:
#print r
if type(r) is dict:
(k,v) = r.items()[0]
print utl.pretty_print ( v.print_json() )
pass
elif isinstance(r,PyJSOb):
#print r.print_json()
print utl.pretty_print ( r.print_json() )
else:
print r
#print utl.pretty_print ( obj_tree.print_json() )
if __name__ == "__main__":
test1()