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

mei_rt2mei_rt2_fix_1
Last change on this file since f3421a0 was f824a9e, checked in by Mei <mei@…>, 12 years ago

1) add more doc to python_tests

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