Ejemplo n.º 1
0
 def _labels(self):
     if self.label_file:
         j = utl.LoadJSON(path=self.label_file)
     elif self.label_str:
         j = utl.LoadJSON(str=self.label_str)
     js = j.get_json()
     r = []
     c_l = []
     for ob in js:
         if 'target' in ob and ob['target']:
             r.append((ob['target'], ob['label']))
         elif 'condition' in ob:
             c_l += [(ob['condition'], ob['label'])]
         else:
             pass
     return (r, c_l)
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
def test():
    if len(sys.argv) < 2:
        print "give all arguments"
        exit()
    job = utl.LoadJSON(path=sys.argv[1]).get_json()
    tree = PyObjTree(job)
    pyjsob = PyJSOb()
    to = tree.root
    to.get_prim_mem()
    #print to.print_json()

    print to.pretty_print()
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def test():
    jsonpath = sys.argv[1]
    label = sys.argv[2]
    print "jsonpath:" + jsonpath
    lj = utility.LoadJSON("employee.json")
    j = lj.get_json()
    #print j
    #print x["personalRecord"]["name"]
    la = LexicalAnalyzer(jsonpath)
    token_p = la.token_pair()
    q = Query({"data": j}, token_p)
    print json.dumps(q.execute(), indent=4, sort_keys=True)

    filtered_content = Policy().keep_label(q.execute(), label, [])
    print json.dumps(filtered_content, indent=4, sort_keys=True)
Ejemplo n.º 6
0
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()

    path = sys.argv[2]

    nh = NodeHierarchy()
    nh.insert("private", "public")
    nh.insert("protected", "private")

    oq = ObQuery(obj_tree)

    qry = [path]

    for q in qry:

        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.
        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
Ejemplo n.º 7
0
def test():
    a_p = utility.LoadJSON("path_label_policy.json")
    print a_p.get_json()
Ejemplo n.º 8
0
    def apply(self):

        # ---- Build JSON tree -------#

        print "flg0"
        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 object labels -------#
        if self.policy_file:
            obj_tree = Policy.NodeLabeling(
                obj_tree,
                label_str=utl.File(self.policy_file).read()).appy_labels()
        elif self.policy_str:
            print self.policy_str
            obj_tree = Policy.NodeLabeling(
                obj_tree, label_str=self.policy_str).appy_labels()
        # ---- get uer Hierarchy ------#
        #hierarchy = UserHierarchy().get_hierarchy()
        print "flg1"
        #print self.cbac_policy
        '''cbac_policy was a dictionary, converting it to string by json.dumps then again
		returning it to json format by json.loads'''
        print "1.2"
        print self.cbac_policy
        self.cbac_policy = json.dumps(self.cbac_policy)
        self.cbac_json = json.loads(self.cbac_policy)

        print self.cbac_json

        cbac = CBAC.get(file_in_json=self.cbac_json)

        print "flg1.5"

        # --- Work with Query class for Querying --- #
        oq = ObQuery(obj_tree)

        print "flg2"

        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, cbac, 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