1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | Run the queries described in README |
---|
5 | |
---|
6 | cmd: env ABAC_CN=1 keystore=`pwd` ./query.py |
---|
7 | |
---|
8 | number of credentials, |
---|
9 | 3 + 6 + 3 x #VAL# |
---|
10 | |
---|
11 | """ |
---|
12 | |
---|
13 | import os |
---|
14 | import sys |
---|
15 | import ABAC |
---|
16 | import time |
---|
17 | import datetime |
---|
18 | from datetime import timedelta |
---|
19 | |
---|
20 | debug=0 |
---|
21 | |
---|
22 | ctxt = ABAC.Context() |
---|
23 | |
---|
24 | cred_count = 3 + 6 + 3 * #VAL# |
---|
25 | |
---|
26 | def get_msec(e_time) : |
---|
27 | msec_delta=0 |
---|
28 | if( int(e_time.seconds) !=0 ) : |
---|
29 | msec_delta= int(e_time.seconds) *1000 |
---|
30 | if( int(e_time.microseconds) !=0) : |
---|
31 | msec_delta = msec_delta + int(e_time.microseconds)/1000 |
---|
32 | return msec_delta |
---|
33 | |
---|
34 | def get_micro(e_time) : |
---|
35 | micro_delta=0 |
---|
36 | if( int(e_time.seconds) !=0 ) : |
---|
37 | micro_delta= int(e_time.seconds) *1000000 |
---|
38 | if( int(e_time.microseconds) !=0) : |
---|
39 | micro_delta = micro_delta + int(e_time.microseconds) |
---|
40 | return micro_delta |
---|
41 | |
---|
42 | def extract_delta(starttime, endtime) : |
---|
43 | """ given a start time, and a endtime, extract delta """ |
---|
44 | elapsed_time = (endtime - starttime) |
---|
45 | # Only handle in seconds/microseconds |
---|
46 | if ( int(elapsed_time.days) != 0 ) : |
---|
47 | sys.stderr.write("%s is longer than a day !!!" % msg) |
---|
48 | exit(1) |
---|
49 | return elapsed_time |
---|
50 | |
---|
51 | # Keystore is the directory containing the principal credentials. |
---|
52 | # Load existing principals and/or policy credentials |
---|
53 | if (os.environ.has_key("keystore")) : |
---|
54 | keystore=os.environ["keystore"] |
---|
55 | starttime = datetime.datetime.now() |
---|
56 | ctxt.load_directory(keystore) |
---|
57 | endtime = datetime.datetime.now() |
---|
58 | elapsed_load=extract_delta(starttime, endtime) |
---|
59 | elapsed_msec=get_msec(elapsed_load) |
---|
60 | sys.stderr.write("%d %d LOAD(msec)\n" % (cred_count,elapsed_msec)) |
---|
61 | else: |
---|
62 | print("keystore is not set...") |
---|
63 | exit(1) |
---|
64 | |
---|
65 | # retrieve principals' keyid value from local credential files |
---|
66 | ralphsID=ABAC.ID("Ralphs_ID.pem"); |
---|
67 | ralphs=ralphsID.id_keyid() |
---|
68 | |
---|
69 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
70 | bob=bobID.id_keyid() |
---|
71 | |
---|
72 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
73 | mary=maryID.id_keyid() |
---|
74 | |
---|
75 | ########################################################################## |
---|
76 | # dump the loaded principals/policies |
---|
77 | # |
---|
78 | |
---|
79 | fd=os.open("creds_dump",os.O_WRONLY|os.O_CREAT) |
---|
80 | out = ctxt.context_principals() |
---|
81 | for x in out[1]: |
---|
82 | os.write(fd, x.string()) |
---|
83 | os.write(fd,"\n") |
---|
84 | out = ctxt.context_credentials() |
---|
85 | for c in out[1]: |
---|
86 | string="%s <- %s\n" % (c.head_string(), c.tail_string()) |
---|
87 | os.write(fd,string) |
---|
88 | os.close(fd) |
---|
89 | |
---|
90 | ########################################################################## |
---|
91 | # Would Mary eat navel orange ? |
---|
92 | # oset = [keyid:mary].oset:what2eat |
---|
93 | # p [string:'navel orange'] |
---|
94 | def maryQuery(pr) : |
---|
95 | oset = ABAC.Oset(mary,"what2eat") |
---|
96 | term=ABAC.DataTerm("string", "'navel orange'") |
---|
97 | p = ABAC.Oset(term) |
---|
98 | |
---|
99 | if(pr) : |
---|
100 | print "\n===good============ mary.what2eat <- navel orange" |
---|
101 | starttime = datetime.datetime.now() |
---|
102 | out = ctxt.query(oset, p) |
---|
103 | endtime = datetime.datetime.now() |
---|
104 | if(pr) : |
---|
105 | for c in out[1]: |
---|
106 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
107 | return extract_delta(starttime, endtime) |
---|
108 | |
---|
109 | # skip the first one |
---|
110 | e_time=maryQuery(1) |
---|
111 | elapsed_micro=get_micro(e_time) |
---|
112 | sys.stderr.write("%d %d GOOD_1(micro)\n" % (cred_count,elapsed_micro)) |
---|
113 | |
---|
114 | elapsed_good=timedelta() |
---|
115 | k=20 |
---|
116 | while(k): |
---|
117 | e_time=maryQuery(0) |
---|
118 | elapsed_good=elapsed_good+e_time |
---|
119 | k=k-1 |
---|
120 | if(debug): |
---|
121 | elapsed_micro=get_micro(e_time) |
---|
122 | sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
123 | elapsed_micro=get_micro(elapsed_good) |
---|
124 | sys.stderr.write("%d %d GOOD_t(micro)\n" % (cred_count,elapsed_micro/20)) |
---|
125 | |
---|
126 | ########################################################################## |
---|
127 | # Would Mary eat kiwi ? |
---|
128 | # oset = [keyid:mary].oset:what2eat |
---|
129 | # p [string:'kiwi'] |
---|
130 | oset = ABAC.Oset(mary,"what2eat") |
---|
131 | term=ABAC.DataTerm("string", "'kiwi'") |
---|
132 | p = ABAC.Oset(term) |
---|
133 | |
---|
134 | print "\n===good============ mary.what2eat <- kiwi" |
---|
135 | out = ctxt.query(oset, p) |
---|
136 | for c in out[1]: |
---|
137 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
138 | |
---|
139 | ########################################################################## |
---|
140 | # Would Bob eat navel orange ? |
---|
141 | # oset = [keyid:bob].oset:what2eat |
---|
142 | # p [string:'navel orange'] |
---|
143 | def bobQuery(pr) : |
---|
144 | oset = ABAC.Oset(bob,"what2eat") |
---|
145 | term=ABAC.DataTerm("string", "'navel orange'") |
---|
146 | p = ABAC.Oset(term) |
---|
147 | |
---|
148 | if(pr) : |
---|
149 | print "\n===bad============ bob.what2eat <- navel orange" |
---|
150 | starttime = datetime.datetime.now() |
---|
151 | out = ctxt.query(oset, p) |
---|
152 | endtime = datetime.datetime.now() |
---|
153 | if(pr) : |
---|
154 | for c in out[1]: |
---|
155 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
156 | return extract_delta(starttime, endtime) |
---|
157 | |
---|
158 | # skip the first one |
---|
159 | e_time=bobQuery(1) |
---|
160 | elapsed_micro=get_micro(e_time) |
---|
161 | sys.stderr.write("%d %d BAD_1(micro)\n" % (cred_count,elapsed_micro)) |
---|
162 | |
---|
163 | elapsed_bad=timedelta() |
---|
164 | k=20 |
---|
165 | while(k): |
---|
166 | e_time=bobQuery(0) |
---|
167 | elapsed_bad=elapsed_bad+e_time |
---|
168 | k=k-1 |
---|
169 | if(debug): |
---|
170 | elapsed_micro=get_micro(e_time) |
---|
171 | sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
172 | elapsed_micro=get_micro(elapsed_bad) |
---|
173 | sys.stderr.write("%d %d BAD_t(micro)\n" % (cred_count,elapsed_micro/20)) |
---|
174 | |
---|
175 | ########################################################################## |
---|
176 | # Is Apple 1.50 at Ralphs ? |
---|
177 | # oset = [keyid:$ralphs].oset:fruitprice([float:1.50]) |
---|
178 | # p = [string:'apple'] |
---|
179 | param=ABAC.DataTerm("float", "1.50") |
---|
180 | oset = ABAC.Oset(ralphs,"fruitprice") |
---|
181 | oset.oset_add_data_term(param) |
---|
182 | term=ABAC.DataTerm("string", "'apple'") |
---|
183 | p = ABAC.Oset(term) |
---|
184 | |
---|
185 | print "\n===good============ ralphs.fruitprice(1.50) <- apple" |
---|
186 | out = ctxt.query(oset, p) |
---|
187 | for c in out[1]: |
---|
188 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
189 | |
---|
190 | ########################################################################## |
---|
191 | # Is green apple 1.50 at Ralphs ? |
---|
192 | # oset = [keyid:$ralphs].oset:fruitprice([float:1.50]) |
---|
193 | # p = [string:'green apple'] |
---|
194 | param=ABAC.DataTerm("float", "1.50") |
---|
195 | oset = ABAC.Oset(ralphs,"fruitprice") |
---|
196 | oset.oset_add_data_term(param) |
---|
197 | term=ABAC.DataTerm("string", "'green apple'") |
---|
198 | p = ABAC.Oset(term) |
---|
199 | |
---|
200 | print "\n===bad============ ralphs.fruitprice(1.50) <- green apple" |
---|
201 | out = ctxt.query(oset, p) |
---|
202 | for c in out[1]: |
---|
203 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
204 | |
---|
205 | ########################################################################## |
---|
206 | # dump the yap dB |
---|
207 | # |
---|
208 | #ctxt.dump_yap_db() |
---|
209 | |
---|