[be6cb41] | 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 ABAC |
---|
| 12 | |
---|
| 13 | ctxt = ABAC.Context() |
---|
| 14 | |
---|
| 15 | # Keystore is the directory containing the principal credentials. |
---|
| 16 | # Load existing principals and/or policy credentials |
---|
| 17 | if (os.environ.has_key("keystore")) : |
---|
| 18 | keystore=os.environ["keystore"] |
---|
| 19 | ctxt.load_directory(keystore) |
---|
| 20 | else: |
---|
| 21 | print("keystore is not set...") |
---|
| 22 | exit(1) |
---|
| 23 | |
---|
| 24 | # retrieve principals' keyid value from local credential files |
---|
| 25 | acmeID=ABAC.ID("Acme_ID.pem"); |
---|
| 26 | acmeID.load_privkey("Acme_private.pem"); |
---|
| 27 | acme=acmeID.keyid() |
---|
| 28 | |
---|
| 29 | coyoteID=ABAC.ID("Coyote_ID.pem"); |
---|
| 30 | coyoteID.load_privkey("Coyote_private.pem"); |
---|
| 31 | coyote=coyoteID.keyid() |
---|
| 32 | |
---|
| 33 | bigbirdID=ABAC.ID("Bigbird_ID.pem"); |
---|
| 34 | bigbirdID.load_privkey("Bigbird_private.pem"); |
---|
| 35 | bigbird=bigbirdID.keyid() |
---|
| 36 | |
---|
| 37 | ########################################################################## |
---|
| 38 | # dump the loaded attribute policies |
---|
| 39 | # |
---|
| 40 | print "\n...policy attribute set..." |
---|
| 41 | credentials = ctxt.credentials() |
---|
| 42 | for credential in credentials: |
---|
| 43 | print "context: %s <- %s" % (credential.head().string(), credential.tail().string()) |
---|
| 44 | |
---|
| 45 | ########################################################################## |
---|
| 46 | # is coyote a preferred_customer of Acme ? |
---|
| 47 | # role=[keyid:Acme].role:preferred_customer |
---|
| 48 | # p =[keyid:coyote] |
---|
| 49 | |
---|
| 50 | print "\n===good============ Acme.preferred_customer <- Coyote" |
---|
| 51 | (success, credentials) = ctxt.query("%s.preferred_customer" % acme, coyote) |
---|
| 52 | if success: |
---|
| 53 | print "success!" |
---|
| 54 | else: |
---|
| 55 | print "failure!" |
---|
| 56 | for credential in credentials: |
---|
| 57 | print "credential %s <- %s" % (credential.head().string(), credential.tail().string()) |
---|
| 58 | |
---|
| 59 | ########################################################################## |
---|
| 60 | # can coyote buy rockets from Acme ? |
---|
| 61 | # role=[keyid:Acme].role:buy_rockets |
---|
| 62 | # p =[keyid:coyote] |
---|
| 63 | |
---|
| 64 | print "\n===good============ Acme.buy_rockets <- Coyote" |
---|
| 65 | (success, credentials) = ctxt.query("%s.buy_rockets" % acme, coyote) |
---|
| 66 | if success: |
---|
| 67 | print "success!" |
---|
| 68 | else: |
---|
| 69 | print "failure!" |
---|
| 70 | for credential in credentials: |
---|
| 71 | print "credential %s <- %s" % (credential.head().string(), credential.tail().string()) |
---|
| 72 | |
---|
| 73 | ########################################################################## |
---|
| 74 | # is Acme a friend of coyote ? |
---|
| 75 | # role=[keyid:Coyote].role:friend |
---|
| 76 | # p=[keyid:Acme] |
---|
| 77 | |
---|
| 78 | print "\n===bad=============== Coyote.friend <- Acme" |
---|
| 79 | (success, credentials) = ctxt.query("%s.friend" % coyote, acme) |
---|
| 80 | if success: |
---|
| 81 | print "success!" |
---|
| 82 | else: |
---|
| 83 | print "failure!" |
---|
| 84 | for credential in credentials: |
---|
| 85 | print "credential %s <- %s" % (credential.head().string(), credential.tail().string()) |
---|
| 86 | |
---|
| 87 | ########################################################################## |
---|
| 88 | # using complex role to ask a question.. expecting to fail |
---|
| 89 | # role=[keyid:Acme].role:buy_rockets |
---|
| 90 | # p=[keyid:Acme].role:preferred_customer |
---|
| 91 | |
---|
| 92 | print "\n===bad?=============== Acme.buy_rockets <- Acme.preferred_customer" |
---|
| 93 | (success, credentials) = ctxt.query("%s.buy_rockets" % acme, "%s.preferred_customer" % acme) |
---|
| 94 | if success: |
---|
| 95 | print "success!" |
---|
| 96 | else: |
---|
| 97 | print "failure!" |
---|
| 98 | for credential in credentials: |
---|
| 99 | print "credential %s <- %s" % (credential.head().string(), credential.tail().string()) |
---|