1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | Run the queries described in README |
---|
5 | |
---|
6 | cmd1:env keystore=`pwd` ./query.py |
---|
7 | cmd2: env ABAC_CN=1 keystore=`pwd` ./query.py |
---|
8 | |
---|
9 | """ |
---|
10 | |
---|
11 | import os |
---|
12 | import ABAC |
---|
13 | |
---|
14 | ctxt = ABAC.Context() |
---|
15 | ctxt.set_no_partial_proof() |
---|
16 | |
---|
17 | # Keystore is the directory containing the principal credentials. |
---|
18 | # Load existing principals and/or policy credentials |
---|
19 | if (os.environ.has_key("keystore")) : |
---|
20 | keystore=os.environ["keystore"] |
---|
21 | ctxt.load_directory(keystore) |
---|
22 | else: |
---|
23 | print("keystore is not set...") |
---|
24 | exit(1) |
---|
25 | |
---|
26 | # retrieve principals' keyid value from local credential files |
---|
27 | ralphsID=ABAC.ID("Ralphs_ID.pem"); |
---|
28 | ralphsID.id_load_privkey_file("Ralphs_private.pem"); |
---|
29 | ralphs=ralphsID.id_keyid() |
---|
30 | |
---|
31 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
32 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
33 | bob=bobID.id_keyid() |
---|
34 | |
---|
35 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
36 | maryID.id_load_privkey_file("Mary_private.pem"); |
---|
37 | mary=maryID.id_keyid() |
---|
38 | |
---|
39 | ########################################################################## |
---|
40 | # dump the loaded principals/policies |
---|
41 | # |
---|
42 | out = ctxt.context_principals() |
---|
43 | print "\n...final principal set..." |
---|
44 | for x in out[1]: |
---|
45 | print "%s " % x.string() |
---|
46 | out = ctxt.context_credentials() |
---|
47 | print "\n...final policy attribute set..." |
---|
48 | for 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'] |
---|
55 | oset = ABAC.Oset(mary,"what2eat") |
---|
56 | term=ABAC.DataTerm("string", "'navel orange'") |
---|
57 | p = ABAC.Oset(term) |
---|
58 | |
---|
59 | print "\n===good============ mary.what2eat <- navel orange" |
---|
60 | out = ctxt.query(oset, p) |
---|
61 | for 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'] |
---|
68 | oset = ABAC.Oset(mary,"what2eat") |
---|
69 | term=ABAC.DataTerm("string", "'kiwi'") |
---|
70 | p = ABAC.Oset(term) |
---|
71 | |
---|
72 | print "\n===good============ mary.what2eat <- kiwi" |
---|
73 | out = ctxt.query(oset, p) |
---|
74 | for 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'] |
---|
81 | oset = ABAC.Oset(bob,"what2eat") |
---|
82 | term=ABAC.DataTerm("string", "'navel orange'") |
---|
83 | p = ABAC.Oset(term) |
---|
84 | |
---|
85 | print "\n===bad============ bob.what2eat <- navel orange" |
---|
86 | out = ctxt.query(oset, p) |
---|
87 | for 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'] |
---|
94 | param=ABAC.DataTerm("float", "1.50") |
---|
95 | oset = ABAC.Oset(ralphs,"fruitprice") |
---|
96 | oset.oset_add_data_term(param) |
---|
97 | term=ABAC.DataTerm("string", "'apple'") |
---|
98 | p = ABAC.Oset(term) |
---|
99 | |
---|
100 | print "\n===good============ ralphs.fruitprice(1.50) <- apple" |
---|
101 | out = ctxt.query(oset, p) |
---|
102 | for 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'] |
---|
109 | param=ABAC.DataTerm("float", "1.50") |
---|
110 | oset = ABAC.Oset(ralphs,"fruitprice") |
---|
111 | oset.oset_add_data_term(param) |
---|
112 | term=ABAC.DataTerm("string", "'green apple'") |
---|
113 | p = ABAC.Oset(term) |
---|
114 | |
---|
115 | print "\n===bad============ ralphs.fruitprice(1.50) <- green apple" |
---|
116 | out = ctxt.query(oset, p) |
---|
117 | for 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 | |
---|