[7e3f5e2] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | Run the queries described in README |
---|
| 5 | |
---|
| 6 | cmd: env keystore=`pwd` ./query.py |
---|
| 7 | |
---|
| 8 | """ |
---|
| 9 | |
---|
| 10 | import os |
---|
| 11 | import sys |
---|
| 12 | import ABAC |
---|
| 13 | import time |
---|
| 14 | import datetime |
---|
| 15 | import math |
---|
| 16 | |
---|
| 17 | debug=0 |
---|
| 18 | |
---|
| 19 | ctxt = ABAC.Context() |
---|
| 20 | |
---|
| 21 | cred_count = 2 + 2 * #VAL# |
---|
| 22 | |
---|
| 23 | def get_msec(e_time) : |
---|
| 24 | msec_delta=0 |
---|
| 25 | if( int(e_time.seconds) !=0 ) : |
---|
| 26 | msec_delta= int(e_time.seconds) *1000 |
---|
| 27 | if( int(e_time.microseconds) !=0) : |
---|
| 28 | msec_delta = msec_delta + int(e_time.microseconds)/1000 |
---|
| 29 | return msec_delta |
---|
| 30 | |
---|
| 31 | def get_micro(e_time) : |
---|
| 32 | micro_delta=0 |
---|
| 33 | if( int(e_time.seconds) !=0 ) : |
---|
| 34 | micro_delta= int(e_time.seconds) *1000000 |
---|
| 35 | if( int(e_time.microseconds) !=0) : |
---|
| 36 | micro_delta = micro_delta + int(e_time.microseconds) |
---|
| 37 | return micro_delta |
---|
| 38 | |
---|
| 39 | def extract_delta(starttime, endtime) : |
---|
| 40 | """ given a start time, and a endtime, extract delta """ |
---|
| 41 | elapsed_time = (endtime - starttime) |
---|
| 42 | # Only handle in seconds/microseconds |
---|
| 43 | if ( int(elapsed_time.days) != 0 ) : |
---|
| 44 | sys.stderr,write("%s is longer than a day !!!" % msg) |
---|
| 45 | exit(1) |
---|
| 46 | return elapsed_time |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | # Keystore is the directory containing the principal credentials. |
---|
| 50 | # Load existing principals and/or policy credentials |
---|
| 51 | if (os.environ.has_key("keystore")) : |
---|
| 52 | keystore=os.environ["keystore"] |
---|
| 53 | starttime = datetime.datetime.now() |
---|
| 54 | ctxt.load_directory(keystore) |
---|
| 55 | endtime = datetime.datetime.now() |
---|
| 56 | elapsed_load=extract_delta(starttime, endtime) |
---|
| 57 | elapsed_msec=get_msec(elapsed_load) |
---|
| 58 | sys.stderr.write("%d %d LOAD(msec)\n" % (cred_count,elapsed_msec)) |
---|
| 59 | else: |
---|
| 60 | print("keystore is not set...") |
---|
| 61 | exit(1) |
---|
| 62 | |
---|
| 63 | ########################################################################## |
---|
| 64 | # dump the loaded principals/policies |
---|
| 65 | # |
---|
| 66 | |
---|
| 67 | fd=os.open("creds_dump",os.O_WRONLY|os.O_CREAT) |
---|
| 68 | credentials = ctxt.credentials() |
---|
| 69 | for cred in credentials: |
---|
| 70 | string="%s <- %s" % (cred.head().string(), cred.tail().string()) |
---|
| 71 | os.write(fd,string) |
---|
| 72 | os.write(fd,"\n") |
---|
| 73 | os.close(fd) |
---|
| 74 | |
---|
| 75 | ########################################################################## |
---|
| 76 | # Does JohnX likes John0 ? |
---|
| 77 | # role = [keyid:JohnX].role:after |
---|
| 78 | # p [Keyid:john0] |
---|
| 79 | def goodQuery() : |
---|
| 80 | aid="John%s_ID.pem"% #VAL# |
---|
| 81 | aID=ABAC.ID(aid) |
---|
| 82 | bID=ABAC.ID("John0_ID.pem") |
---|
| 83 | |
---|
| 84 | print "\n===good============ johnN.likes <- john0 " |
---|
| 85 | starttime = datetime.datetime.now() |
---|
| 86 | (success, credentials) = ctxt.query("%s.likes" % aID.keyid(), bID.keyid()) |
---|
| 87 | if success: |
---|
| 88 | print "success" |
---|
| 89 | else: |
---|
| 90 | print "failure" |
---|
| 91 | endtime = datetime.datetime.now() |
---|
| 92 | if(debug): |
---|
| 93 | print "good query start-> %s\n" % starttime |
---|
| 94 | print "good query end -> %s\n" % endtime |
---|
| 95 | for cred in credentials: |
---|
| 96 | print "%s <- %s" % (cred.head().string(), cred.tail().string()) |
---|
| 97 | return extract_delta(starttime, endtime) |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | ########################################################################## |
---|
| 101 | # Does John0 likes JohnX ? |
---|
| 102 | # role = [keyid:JohnX].role:after |
---|
| 103 | # p [Keyid:john0] |
---|
| 104 | def badQuery() : |
---|
| 105 | bid="John%s_ID.pem"% #VAL# |
---|
| 106 | bID=ABAC.ID(bid) |
---|
| 107 | aID=ABAC.ID("John0_ID.pem") |
---|
| 108 | |
---|
| 109 | print "\n===bad============ john0.likes <- johnX " |
---|
| 110 | starttime = datetime.datetime.now() |
---|
| 111 | (success, credentials) = ctxt.query("%s.likes" % aID.keyid(), bID.keyid()) |
---|
| 112 | if success: |
---|
| 113 | print "success" |
---|
| 114 | else: |
---|
| 115 | print "failure" |
---|
| 116 | endtime = datetime.datetime.now() |
---|
| 117 | for cred in credentials: |
---|
| 118 | print "%s <- %s" % (cred.head().string(), cred.tail().string()) |
---|
| 119 | return extract_delta(starttime, endtime) |
---|
| 120 | |
---|
| 121 | ############################################################## |
---|
| 122 | |
---|
| 123 | #skip the first one |
---|
| 124 | e_time=goodQuery() |
---|
| 125 | elapsed_micro=get_micro(e_time) |
---|
| 126 | sys.stderr.write("%d %d GOOD_f(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 127 | |
---|
| 128 | tlist=[] |
---|
| 129 | k=100 |
---|
| 130 | while(k): |
---|
| 131 | e_time=goodQuery() |
---|
| 132 | elapsed_micro=get_micro(e_time) |
---|
| 133 | k=k-1 |
---|
| 134 | tlist.append(elapsed_micro) |
---|
| 135 | if(k==99): |
---|
| 136 | sys.stderr.write("%d %d GOOD_s(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 137 | |
---|
| 138 | if(debug): |
---|
| 139 | sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
| 140 | |
---|
| 141 | sum=0 |
---|
| 142 | for i in tlist: |
---|
| 143 | sum=sum+i |
---|
| 144 | ave=sum/100 |
---|
| 145 | dlist = [(x-ave) for x in tlist ] |
---|
| 146 | slist = [ (x-ave)*(x-ave) for x in tlist] |
---|
| 147 | sum=0 |
---|
| 148 | for i in slist: |
---|
| 149 | sum=sum+i |
---|
| 150 | sd=math.sqrt(sum/99) |
---|
| 151 | sys.stderr.write("%d %d %d GOOD_t(micro)\n" % (cred_count,ave,sd)) |
---|
| 152 | sys.stderr.write("%d 100 %s GOOD_list(micro)\n" % (cred_count,tlist)) |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | ############################################################### |
---|
| 156 | |
---|
| 157 | e_time=badQuery() |
---|
| 158 | elapsed_micro=get_micro(e_time) |
---|
| 159 | sys.stderr.write("%d %d BAD_f(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 160 | |
---|
| 161 | tlist=[] |
---|
| 162 | k=100 |
---|
| 163 | while(k): |
---|
| 164 | e_time=badQuery() |
---|
| 165 | elapsed_micro=get_micro(e_time) |
---|
| 166 | tlist.append(elapsed_micro) |
---|
| 167 | k=k-1 |
---|
| 168 | if(k==99): |
---|
| 169 | sys.stderr.write("%d %d BAD_s(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 170 | |
---|
| 171 | if(debug): |
---|
| 172 | sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
| 173 | |
---|
| 174 | sum=0 |
---|
| 175 | for i in tlist: |
---|
| 176 | sum=sum+i |
---|
| 177 | ave=sum/100 |
---|
| 178 | dlist = [(x-ave) for x in tlist ] |
---|
| 179 | slist = [ (x-ave)*(x-ave) for x in tlist] |
---|
| 180 | sum=0 |
---|
| 181 | for i in slist: |
---|
| 182 | sum=sum+i |
---|
| 183 | sd=math.sqrt(sum/99) |
---|
| 184 | sys.stderr.write("%d %d %d BAD_t(micro)\n" % (cred_count,ave, sd)) |
---|
| 185 | sys.stderr.write("%d 100 %s BAD_list(micro)\n" % (cred_count,tlist)) |
---|