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