[373bf68] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | Run the queries described in README |
---|
| 5 | |
---|
[08b8da7] | 6 | cmd: env ABAC_CN=1 keystore=`pwd` ./query.py |
---|
[373bf68] | 7 | |
---|
| 8 | """ |
---|
| 9 | |
---|
| 10 | import os |
---|
[08b8da7] | 11 | import sys |
---|
[373bf68] | 12 | import ABAC |
---|
[08b8da7] | 13 | import time |
---|
| 14 | import datetime |
---|
| 15 | from datetime import timedelta |
---|
| 16 | |
---|
| 17 | debug=0 |
---|
[373bf68] | 18 | |
---|
| 19 | ctxt = ABAC.Context() |
---|
| 20 | |
---|
[08b8da7] | 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 | |
---|
[373bf68] | 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"] |
---|
[08b8da7] | 53 | starttime = datetime.datetime.now() |
---|
[373bf68] | 54 | ctxt.load_directory(keystore) |
---|
[08b8da7] | 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)) |
---|
[373bf68] | 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 | out = ctxt.context_principals() |
---|
| 69 | for x in out[1]: |
---|
| 70 | os.write(fd, x.string()) |
---|
| 71 | os.write(fd,"\n") |
---|
| 72 | out = ctxt.context_credentials() |
---|
| 73 | for c in out[1]: |
---|
| 74 | string="%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 75 | os.write(fd,string) |
---|
| 76 | os.write(fd,"\n") |
---|
| 77 | os.close(fd) |
---|
| 78 | |
---|
| 79 | ########################################################################## |
---|
| 80 | # Does JohnX likes John0 ? |
---|
| 81 | # role = [keyid:JohnX].role:after |
---|
| 82 | # p [Keyid:john0] |
---|
[08b8da7] | 83 | def goodQuery() : |
---|
| 84 | aid="John%s_ID.pem"% #VAL# |
---|
| 85 | aID=ABAC.ID(aid) |
---|
| 86 | a=aID.id_keyid() |
---|
[373bf68] | 87 | |
---|
[08b8da7] | 88 | bID=ABAC.ID("John0_ID.pem") |
---|
| 89 | b=bID.id_keyid() |
---|
[373bf68] | 90 | |
---|
[08b8da7] | 91 | role = ABAC.Role(a,"likes") |
---|
| 92 | p = ABAC.Role(b) |
---|
[373bf68] | 93 | |
---|
[08b8da7] | 94 | print "\n===good============ johnN.likes <- john0 " |
---|
| 95 | starttime = datetime.datetime.now() |
---|
| 96 | out = ctxt.query(role, p) |
---|
| 97 | endtime = datetime.datetime.now() |
---|
[2b4e740] | 98 | if(debug): |
---|
| 99 | print "good query start-> %s\n" % starttime |
---|
| 100 | print "good query end -> %s\n" % endtime |
---|
[08b8da7] | 101 | for c in out[1]: |
---|
| 102 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 103 | return extract_delta(starttime, endtime) |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | ########################################################################## |
---|
| 107 | # Does John0 likes JohnX ? |
---|
| 108 | # role = [keyid:JohnX].role:after |
---|
| 109 | # p [Keyid:john0] |
---|
| 110 | def badQuery() : |
---|
| 111 | bid="John%s_ID.pem"% #VAL# |
---|
| 112 | bID=ABAC.ID(bid) |
---|
| 113 | b=bID.id_keyid() |
---|
| 114 | |
---|
| 115 | aID=ABAC.ID("John0_ID.pem") |
---|
| 116 | a=aID.id_keyid() |
---|
| 117 | |
---|
| 118 | role = ABAC.Role(a,"likes") |
---|
| 119 | p = ABAC.Role(b) |
---|
| 120 | |
---|
| 121 | print "\n===bad============ john0.likes <- johnX " |
---|
| 122 | starttime = datetime.datetime.now() |
---|
| 123 | out = ctxt.query(role, p) |
---|
| 124 | endtime = datetime.datetime.now() |
---|
| 125 | for c in out[1]: |
---|
| 126 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 127 | return extract_delta(starttime, endtime) |
---|
| 128 | |
---|
| 129 | ############################################################## |
---|
| 130 | |
---|
| 131 | #skip the first one |
---|
| 132 | e_time=goodQuery() |
---|
| 133 | elapsed_micro=get_micro(e_time) |
---|
| 134 | sys.stderr.write("%d %d GOOD_1(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 135 | |
---|
| 136 | elapsed_good=timedelta() |
---|
| 137 | k=10 |
---|
| 138 | while(k): |
---|
| 139 | e_time=goodQuery() |
---|
| 140 | elapsed_good=elapsed_good+e_time |
---|
| 141 | k=k-1 |
---|
| 142 | if(k==9): |
---|
| 143 | elapsed_micro=get_micro(e_time) |
---|
| 144 | sys.stderr.write("%d %d GOOD_2(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 145 | |
---|
| 146 | if(debug): |
---|
| 147 | elapsed_micro=get_micro(e_time) |
---|
| 148 | sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
| 149 | elapsed_micro=get_micro(elapsed_good) |
---|
| 150 | sys.stderr.write("%d %d GOOD_t(micro)\n" % (cred_count,elapsed_micro/10)) |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | ############################################################### |
---|
| 154 | |
---|
| 155 | e_time=badQuery() |
---|
| 156 | elapsed_micro=get_micro(e_time) |
---|
| 157 | sys.stderr.write("%d %d BAD_1(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 158 | |
---|
| 159 | elapsed_bad=timedelta() |
---|
| 160 | k=10 |
---|
| 161 | while(k): |
---|
| 162 | e_time=badQuery() |
---|
| 163 | elapsed_bad=elapsed_bad+e_time |
---|
| 164 | k=k-1 |
---|
| 165 | if(k==9): |
---|
| 166 | elapsed_micro=get_micro(e_time) |
---|
| 167 | sys.stderr.write("%d %d BAD_2(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 168 | |
---|
| 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)) |
---|
[373bf68] | 172 | |
---|
[08b8da7] | 173 | elapsed_micro=get_micro(elapsed_bad) |
---|
| 174 | sys.stderr.write("%d %d BAD_t(micro)\n" % (cred_count,elapsed_micro/10)) |
---|