source: examples/python_tests/fruits_rt2/query.py @ 7751094

mei_rt2
Last change on this file since 7751094 was 646e57e, checked in by Mei <mei@…>, 12 years ago

1) add partial proof

  • Property mode set to 100755
File size: 3.6 KB
Line 
1#!/usr/bin/env python
2
3"""
4Run the queries described in README
5
6cmd1:env keystore=`pwd` ./query.py
7cmd2: env ABAC_CN=1 keystore=`pwd` ./query.py
8
9"""
10
11import os
12import ABAC
13
14ctxt = ABAC.Context()
15ctxt.set_no_partial_proof()
16
17# Keystore is the directory containing the principal credentials.
18# Load existing principals and/or policy credentials
19if (os.environ.has_key("keystore")) :
20    keystore=os.environ["keystore"]
21    ctxt.load_directory(keystore)
22else:
23    print("keystore is not set...")
24    exit(1)
25
26# retrieve principals' keyid value from local credential files
27ralphsID=ABAC.ID("Ralphs_ID.pem");
28ralphsID.id_load_privkey_file("Ralphs_private.pem");
29ralphs=ralphsID.id_keyid()
30
31bobID=ABAC.ID("Bob_ID.pem");
32bobID.id_load_privkey_file("Bob_private.pem");
33bob=bobID.id_keyid()
34
35maryID=ABAC.ID("Mary_ID.pem");
36maryID.id_load_privkey_file("Mary_private.pem");
37mary=maryID.id_keyid()
38
39##########################################################################
40# dump the loaded principals/policies
41#
42out = ctxt.context_principals()
43print "\n...final principal set..."
44for x in out[1]:
45    print "%s " % x.string()
46out = ctxt.context_credentials()
47print "\n...final policy attribute set..."
48for c in out[1]:
49    print "%s <- %s" % (c.head_string(), c.tail_string())
50
51##########################################################################
52# Would Mary eat navel orange ?
53# oset = [keyid:mary].oset:what2eat
54# p [string:'navel orange']
55oset = ABAC.Oset(mary,"what2eat")
56term=ABAC.DataTerm("string", "'navel orange'")
57p = ABAC.Oset(term)
58
59print "\n===good============ mary.what2eat <- navel orange"
60out = ctxt.query(oset, p)
61for c in out[1]:
62    print "%s <- %s" % (c.head_string(), c.tail_string())
63
64##########################################################################
65# Would Mary eat kiwi ?
66# oset = [keyid:mary].oset:what2eat
67# p [string:'kiwi']
68oset = ABAC.Oset(mary,"what2eat")
69term=ABAC.DataTerm("string", "'kiwi'")
70p = ABAC.Oset(term)
71
72print "\n===good============ mary.what2eat <- kiwi"
73out = ctxt.query(oset, p)
74for c in out[1]:
75    print "%s <- %s" % (c.head_string(), c.tail_string())
76
77##########################################################################
78# Would Bob eat navel orange ?
79# oset = [keyid:bob].oset:what2eat
80# p [string:'navel orange']
81oset = ABAC.Oset(bob,"what2eat")
82term=ABAC.DataTerm("string", "'navel orange'")
83p = ABAC.Oset(term)
84
85print "\n===bad============ bob.what2eat <- navel orange"
86out = ctxt.query(oset, p)
87for c in out[1]:
88    print "%s <- %s" % (c.head_string(), c.tail_string())
89
90##########################################################################
91# Is Apple 1.50 at Ralphs ?
92# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
93# p = [string:'apple']
94param=ABAC.DataTerm("float", "1.50")
95oset = ABAC.Oset(ralphs,"fruitprice")
96oset.oset_add_data_term(param)
97term=ABAC.DataTerm("string", "'apple'")
98p = ABAC.Oset(term)
99
100print "\n===good============ ralphs.fruitprice(1.50) <- apple"
101out = ctxt.query(oset, p)
102for c in out[1]:
103    print "%s <- %s" % (c.head_string(), c.tail_string())
104
105##########################################################################
106# Is green apple 1.50 at Ralphs ?
107# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
108# p = [string:'green apple']
109param=ABAC.DataTerm("float", "1.50")
110oset = ABAC.Oset(ralphs,"fruitprice")
111oset.oset_add_data_term(param)
112term=ABAC.DataTerm("string", "'green apple'")
113p = ABAC.Oset(term)
114
115print "\n===bad============ ralphs.fruitprice(1.50) <- green apple"
116out = ctxt.query(oset, p)
117for c in out[1]:
118    print "%s <- %s" % (c.head_string(), c.tail_string())
119
120##########################################################################
121# dump the yap dB
122#
123#ctxt.dump_yap_db()
124
Note: See TracBrowser for help on using the repository browser.