[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() |
---|
| 98 | for c in out[1]: |
---|
| 99 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 100 | return extract_delta(starttime, endtime) |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | ########################################################################## |
---|
| 104 | # Does John0 likes JohnX ? |
---|
| 105 | # role = [keyid:JohnX].role:after |
---|
| 106 | # p [Keyid:john0] |
---|
| 107 | def badQuery() : |
---|
| 108 | bid="John%s_ID.pem"% #VAL# |
---|
| 109 | bID=ABAC.ID(bid) |
---|
| 110 | b=bID.id_keyid() |
---|
| 111 | |
---|
| 112 | aID=ABAC.ID("John0_ID.pem") |
---|
| 113 | a=aID.id_keyid() |
---|
| 114 | |
---|
| 115 | role = ABAC.Role(a,"likes") |
---|
| 116 | p = ABAC.Role(b) |
---|
| 117 | |
---|
| 118 | print "\n===bad============ john0.likes <- johnX " |
---|
| 119 | starttime = datetime.datetime.now() |
---|
| 120 | out = ctxt.query(role, p) |
---|
| 121 | endtime = datetime.datetime.now() |
---|
| 122 | for c in out[1]: |
---|
| 123 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 124 | return extract_delta(starttime, endtime) |
---|
| 125 | |
---|
| 126 | ############################################################## |
---|
| 127 | |
---|
| 128 | #skip the first one |
---|
| 129 | e_time=goodQuery() |
---|
| 130 | elapsed_micro=get_micro(e_time) |
---|
| 131 | sys.stderr.write("%d %d GOOD_1(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 132 | |
---|
| 133 | elapsed_good=timedelta() |
---|
| 134 | k=10 |
---|
| 135 | while(k): |
---|
| 136 | e_time=goodQuery() |
---|
| 137 | elapsed_good=elapsed_good+e_time |
---|
| 138 | k=k-1 |
---|
| 139 | if(k==9): |
---|
| 140 | elapsed_micro=get_micro(e_time) |
---|
| 141 | sys.stderr.write("%d %d GOOD_2(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 142 | |
---|
| 143 | if(debug): |
---|
| 144 | elapsed_micro=get_micro(e_time) |
---|
| 145 | sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
| 146 | elapsed_micro=get_micro(elapsed_good) |
---|
| 147 | sys.stderr.write("%d %d GOOD_t(micro)\n" % (cred_count,elapsed_micro/10)) |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | ############################################################### |
---|
| 151 | |
---|
| 152 | e_time=badQuery() |
---|
| 153 | elapsed_micro=get_micro(e_time) |
---|
| 154 | sys.stderr.write("%d %d BAD_1(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 155 | |
---|
| 156 | elapsed_bad=timedelta() |
---|
| 157 | k=10 |
---|
| 158 | while(k): |
---|
| 159 | e_time=badQuery() |
---|
| 160 | elapsed_bad=elapsed_bad+e_time |
---|
| 161 | k=k-1 |
---|
| 162 | if(k==9): |
---|
| 163 | elapsed_micro=get_micro(e_time) |
---|
| 164 | sys.stderr.write("%d %d BAD_2(micro)\n" % (cred_count,elapsed_micro)) |
---|
| 165 | |
---|
| 166 | if(debug): |
---|
| 167 | elapsed_micro=get_micro(e_time) |
---|
| 168 | sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
[373bf68] | 169 | |
---|
[08b8da7] | 170 | elapsed_micro=get_micro(elapsed_bad) |
---|
| 171 | sys.stderr.write("%d %d BAD_t(micro)\n" % (cred_count,elapsed_micro/10)) |
---|