[da73657] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | cmd1:env keystore=`pwd` ./query.py |
---|
| 5 | cmd2: env ABAC_CN=1 keystore=`pwd` ./query.py |
---|
| 6 | |
---|
| 7 | """ |
---|
| 8 | |
---|
| 9 | import os |
---|
| 10 | import ABAC |
---|
| 11 | |
---|
| 12 | ctxt = ABAC.Context() |
---|
| 13 | |
---|
| 14 | # Keystore is the directory containing the principal credentials. |
---|
| 15 | # Load existing principals and/or policy credentials |
---|
| 16 | if (os.environ.has_key("keystore")) : |
---|
| 17 | keystore=os.environ["keystore"] |
---|
| 18 | ctxt.load_directory(keystore) |
---|
| 19 | else: |
---|
| 20 | print("keystore is not set...") |
---|
| 21 | exit(1) |
---|
| 22 | |
---|
| 23 | def get_next(ctxt) : |
---|
| 24 | while( 1 ) : |
---|
| 25 | print ("\nnext proof:") |
---|
| 26 | (success, out) = ctxt.next_proof() |
---|
| 27 | if(success) : |
---|
| 28 | for c in out: |
---|
| 29 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 30 | else: |
---|
| 31 | print("no more..\n") |
---|
| 32 | return |
---|
| 33 | |
---|
| 34 | # retrieve principals' keyid value from local credential files |
---|
| 35 | acmeID=ABAC.ID("Acme_ID.pem"); |
---|
| 36 | acme=acmeID.id_keyid() |
---|
| 37 | |
---|
| 38 | oshID=ABAC.ID("Osh_ID.pem"); |
---|
| 39 | osh=oshID.id_keyid() |
---|
| 40 | |
---|
| 41 | coyoteID=ABAC.ID("Coyote_ID.pem"); |
---|
| 42 | coyote=coyoteID.id_keyid() |
---|
| 43 | |
---|
| 44 | ladybugID=ABAC.ID("Ladybug_ID.pem"); |
---|
| 45 | ladybug=ladybugID.id_keyid() |
---|
| 46 | |
---|
| 47 | grannyID=ABAC.ID("Granny_ID.pem"); |
---|
| 48 | granny=grannyID.id_keyid() |
---|
| 49 | |
---|
| 50 | poohID=ABAC.ID("Pooh_ID.pem"); |
---|
| 51 | pooh=poohID.id_keyid() |
---|
| 52 | |
---|
| 53 | burpeeID=ABAC.ID("Burpee_ID.pem"); |
---|
| 54 | burpee=burpeeID.id_keyid() |
---|
| 55 | |
---|
| 56 | ########################################################################## |
---|
| 57 | # dump the loaded principals/policies |
---|
| 58 | # |
---|
| 59 | out = ctxt.context_principals() |
---|
| 60 | print "\n...final principal set..." |
---|
| 61 | for x in out[1]: |
---|
| 62 | print "%s " % x.string() |
---|
| 63 | out = ctxt.context_credentials() |
---|
| 64 | print "\n...final policy attribute set..." |
---|
| 65 | for c in out[1]: |
---|
| 66 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 67 | |
---|
| 68 | #ctxt.set_no_partial_proof() |
---|
| 69 | |
---|
| 70 | ########################################################################## |
---|
| 71 | # can coyote buy rockets from Acme ? |
---|
| 72 | # role=[keyid:Acme].role:buy_rockets |
---|
| 73 | # p =[keyid:coyote] |
---|
| 74 | role = ABAC.Role(acme,"buy_rockets") |
---|
| 75 | p = ABAC.Role(coyote) |
---|
| 76 | print "\n===good============ Acme.buy_rockets <- Coyote" |
---|
| 77 | out = ctxt.query(role, p) |
---|
| 78 | for c in out[1]: |
---|
| 79 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 80 | |
---|
| 81 | ########################################################################## |
---|
| 82 | # can ladybug buy rockets from Acme ? |
---|
| 83 | # role=[keyid:Acme].role:buy_rockets |
---|
| 84 | # p =[keyid:ladybug] |
---|
| 85 | role = ABAC.Role(acme,"buy_rockets") |
---|
| 86 | p = ABAC.Role(ladybug) |
---|
| 87 | print "\n===bad============ Acme.buy_rockets <- Ladybug" |
---|
| 88 | out = ctxt.query(role, p) |
---|
| 89 | for c in out[1]: |
---|
| 90 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 91 | get_next(ctxt) |
---|
| 92 | |
---|
| 93 | ########################################################################## |
---|
| 94 | # can ladybug buy rockets from Osh ? |
---|
| 95 | # role=[keyid:Osh].role:buy_rockets |
---|
| 96 | # p =[keyid:ladybug] |
---|
| 97 | role = ABAC.Role(osh,"buy_rockets") |
---|
| 98 | p = ABAC.Role(ladybug) |
---|
| 99 | print "\n===good============ Osh.buy_rockets <- Ladybug" |
---|
| 100 | out = ctxt.query(role, p) |
---|
| 101 | for c in out[1]: |
---|
| 102 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 103 | |
---|
| 104 | ########################################################################## |
---|
| 105 | # can ladybug buy lumbers from Acme ? |
---|
| 106 | # role=[keyid:Acme].role:buy_lumbers |
---|
| 107 | # p =[keyid:ladybug] |
---|
| 108 | role = ABAC.Role(acme,"buy_lumbers") |
---|
| 109 | p = ABAC.Role(ladybug) |
---|
| 110 | print "\n===bad============ Acme.buy_lumbers <- Ladybug" |
---|
| 111 | out = ctxt.query(role, p) |
---|
| 112 | for c in out[1]: |
---|
| 113 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 114 | get_next(ctxt) |
---|
| 115 | |
---|
| 116 | ########################################################################## |
---|
| 117 | # can pooh buy rockets from Osh ? |
---|
| 118 | # role=[keyid:Osh].role:buy_rockets |
---|
| 119 | # p =[keyid:pooh] |
---|
| 120 | role = ABAC.Role(osh,"buy_rockets") |
---|
| 121 | p = ABAC.Role(pooh) |
---|
| 122 | print "\n===bad============ Osh.buy_rockets <- Pooh" |
---|
| 123 | out = ctxt.query(role, p) |
---|
| 124 | for c in out[1]: |
---|
| 125 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 126 | |
---|
| 127 | ########################################################################## |
---|
| 128 | # can pooh buy rockets from Osh ? |
---|
| 129 | # role=[keyid:Osh].role:buy_rockets |
---|
| 130 | # p =[keyid:pooh] |
---|
| 131 | role = ABAC.Role(osh,"buy_rockets") |
---|
| 132 | p = ABAC.Role(pooh) |
---|
| 133 | print "\n===bad============ Osh.buy_rockets <- Pooh" |
---|
| 134 | out = ctxt.query(role, p) |
---|
| 135 | for c in out[1]: |
---|
| 136 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 137 | get_next(ctxt) |
---|
| 138 | |
---|
| 139 | ########################################################################## |
---|
| 140 | # can ladybug buy flowers from Acme ? |
---|
| 141 | # role=[keyid:Acme].role:buy_flowers |
---|
| 142 | # p =[keyid:ladybug] |
---|
| 143 | role = ABAC.Role(acme,"buy_flowers") |
---|
| 144 | param=ABAC.DataTerm(burpeeID) |
---|
| 145 | role.role_add_data_term(param) |
---|
| 146 | p = ABAC.Role(ladybug) |
---|
| 147 | print "\n===good============ Acme.buy_flowers(Burpee) <- Ladybug" |
---|
| 148 | out = ctxt.query(role, p) |
---|
| 149 | for c in out[1]: |
---|
| 150 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 151 | |
---|
| 152 | ########################################################################## |
---|
| 153 | # can pooh buy flowers from Acme ? |
---|
| 154 | # role=[keyid:Acme].role:buy_flowers |
---|
| 155 | # p =[keyid:pooh] |
---|
| 156 | role = ABAC.Role(acme,"buy_flowers") |
---|
| 157 | param=ABAC.DataTerm(burpeeID) |
---|
| 158 | role.role_add_data_term(param) |
---|
| 159 | p = ABAC.Role(pooh) |
---|
| 160 | print "\n===bad============ Acme.buy_flowers(Burpee) <- Pooh" |
---|
| 161 | out = ctxt.query(role, p) |
---|
| 162 | for c in out[1]: |
---|
| 163 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 164 | get_next(ctxt) |
---|
| 165 | |
---|
| 166 | |
---|