[373bf68] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | Run the queries described in README |
---|
| 5 | |
---|
| 6 | cmd1:env keystore=`pwd` ./query.py |
---|
| 7 | cmd2: env ABAC_CN=1 keystore=`pwd` ./query.py |
---|
| 8 | |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | import os |
---|
| 12 | import ABAC |
---|
| 13 | |
---|
| 14 | ctxt = ABAC.Context() |
---|
| 15 | |
---|
| 16 | # Keystore is the directory containing the principal credentials. |
---|
| 17 | # Load existing principals and/or policy credentials |
---|
| 18 | if (os.environ.has_key("keystore")) : |
---|
| 19 | keystore=os.environ["keystore"] |
---|
| 20 | ctxt.load_directory(keystore) |
---|
| 21 | else: |
---|
| 22 | print("keystore is not set...") |
---|
| 23 | exit(1) |
---|
| 24 | |
---|
| 25 | # retrieve principals' keyid value from local credential files |
---|
| 26 | ralphsID=ABAC.ID("Ralphs_ID.pem"); |
---|
| 27 | ralphs=ralphsID.id_keyid() |
---|
| 28 | |
---|
| 29 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
| 30 | bob=bobID.id_keyid() |
---|
| 31 | |
---|
| 32 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
| 33 | mary=maryID.id_keyid() |
---|
| 34 | |
---|
| 35 | ########################################################################## |
---|
| 36 | # dump the loaded principals/policies |
---|
| 37 | # |
---|
| 38 | |
---|
| 39 | fd=os.open("creds_dump",os.O_WRONLY|os.O_CREAT) |
---|
| 40 | out = ctxt.context_principals() |
---|
| 41 | for x in out[1]: |
---|
| 42 | os.write(fd, x.string()) |
---|
| 43 | os.write(fd,"\n") |
---|
| 44 | out = ctxt.context_credentials() |
---|
| 45 | for c in out[1]: |
---|
| 46 | string="%s <- %s\n" % (c.head_string(), c.tail_string()) |
---|
| 47 | os.write(fd,string) |
---|
| 48 | os.close(fd) |
---|
| 49 | |
---|
| 50 | ########################################################################## |
---|
| 51 | # Would Mary eat navel orange ? |
---|
| 52 | # oset = [keyid:mary].oset:what2eat |
---|
| 53 | # p [string:'navel orange'] |
---|
| 54 | oset = ABAC.Oset(mary,"what2eat") |
---|
| 55 | term=ABAC.DataTerm("string", "'navel orange'") |
---|
| 56 | p = ABAC.Oset(term) |
---|
| 57 | |
---|
| 58 | print "\n===good============ mary.what2eat <- navel orange" |
---|
| 59 | out = ctxt.query(oset, p) |
---|
| 60 | for c in out[1]: |
---|
| 61 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 62 | |
---|
| 63 | ########################################################################## |
---|
| 64 | # Would Mary eat kiwi ? |
---|
| 65 | # oset = [keyid:mary].oset:what2eat |
---|
| 66 | # p [string:'kiwi'] |
---|
| 67 | oset = ABAC.Oset(mary,"what2eat") |
---|
| 68 | term=ABAC.DataTerm("string", "'kiwi'") |
---|
| 69 | p = ABAC.Oset(term) |
---|
| 70 | |
---|
| 71 | print "\n===good============ mary.what2eat <- kiwi" |
---|
| 72 | out = ctxt.query(oset, p) |
---|
| 73 | for c in out[1]: |
---|
| 74 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 75 | |
---|
| 76 | ########################################################################## |
---|
| 77 | # Would Bob eat navel orange ? |
---|
| 78 | # oset = [keyid:bob].oset:what2eat |
---|
| 79 | # p [string:'navel orange'] |
---|
| 80 | oset = ABAC.Oset(bob,"what2eat") |
---|
| 81 | term=ABAC.DataTerm("string", "'navel orange'") |
---|
| 82 | p = ABAC.Oset(term) |
---|
| 83 | |
---|
| 84 | print "\n===bad============ bob.what2eat <- navel orange" |
---|
| 85 | out = ctxt.query(oset, p) |
---|
| 86 | for c in out[1]: |
---|
| 87 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 88 | |
---|
| 89 | ########################################################################## |
---|
| 90 | # Is Apple 1.50 at Ralphs ? |
---|
| 91 | # oset = [keyid:$ralphs].oset:fruitprice([float:1.50]) |
---|
| 92 | # p = [string:'apple'] |
---|
| 93 | param=ABAC.DataTerm("float", "1.50") |
---|
| 94 | oset = ABAC.Oset(ralphs,"fruitprice") |
---|
| 95 | oset.oset_add_data_term(param) |
---|
| 96 | term=ABAC.DataTerm("string", "'apple'") |
---|
| 97 | p = ABAC.Oset(term) |
---|
| 98 | |
---|
| 99 | print "\n===good============ ralphs.fruitprice(1.50) <- apple" |
---|
| 100 | out = ctxt.query(oset, p) |
---|
| 101 | for c in out[1]: |
---|
| 102 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 103 | |
---|
| 104 | ########################################################################## |
---|
| 105 | # Is green apple 1.50 at Ralphs ? |
---|
| 106 | # oset = [keyid:$ralphs].oset:fruitprice([float:1.50]) |
---|
| 107 | # p = [string:'green apple'] |
---|
| 108 | param=ABAC.DataTerm("float", "1.50") |
---|
| 109 | oset = ABAC.Oset(ralphs,"fruitprice") |
---|
| 110 | oset.oset_add_data_term(param) |
---|
| 111 | term=ABAC.DataTerm("string", "'green apple'") |
---|
| 112 | p = ABAC.Oset(term) |
---|
| 113 | |
---|
| 114 | print "\n===bad============ ralphs.fruitprice(1.50) <- green apple" |
---|
| 115 | out = ctxt.query(oset, p) |
---|
| 116 | for c in out[1]: |
---|
| 117 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 118 | |
---|
| 119 | ########################################################################## |
---|
| 120 | # dump the yap dB |
---|
| 121 | # |
---|
| 122 | #ctxt.dump_yap_db() |
---|
| 123 | |
---|