[7211a95] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
[f824a9e] | 4 | Run the queries described in README |
---|
[7211a95] | 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 | |
---|
[f824a9e] | 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) |
---|
[7211a95] | 24 | |
---|
[f824a9e] | 25 | # retrieve principals' keyid value from local credential files |
---|
[7211a95] | 26 | ralphsID=ABAC.ID("Ralphs_ID.pem"); |
---|
| 27 | ralphsID.id_load_privkey_file("Ralphs_private.pem"); |
---|
| 28 | ralphs=ralphsID.id_keyid() |
---|
| 29 | |
---|
| 30 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
| 31 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
| 32 | bob=bobID.id_keyid() |
---|
| 33 | |
---|
| 34 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
| 35 | maryID.id_load_privkey_file("Mary_private.pem"); |
---|
| 36 | mary=maryID.id_keyid() |
---|
| 37 | |
---|
| 38 | ########################################################################## |
---|
[f824a9e] | 39 | # dump the loaded principals/policies |
---|
| 40 | # |
---|
| 41 | out = ctxt.context_principals() |
---|
| 42 | print "\n...final principal set..." |
---|
| 43 | for x in out[1]: |
---|
| 44 | print "%s " % x.string() |
---|
| 45 | out = ctxt.context_credentials() |
---|
| 46 | print "\n...final policy attribute set..." |
---|
| 47 | for c in out[1]: |
---|
| 48 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 49 | |
---|
| 50 | ########################################################################## |
---|
| 51 | # Would Mary eat navel orange ? |
---|
[7211a95] | 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 | ########################################################################## |
---|
[f824a9e] | 64 | # Would Mary eat kiwi ? |
---|
[7211a95] | 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 | ########################################################################## |
---|
[f824a9e] | 77 | # Would Bob eat navel orange ? |
---|
[7211a95] | 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 | ########################################################################## |
---|
[f824a9e] | 90 | # Is Apple 1.50 at Ralphs ? |
---|
[7211a95] | 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 | ########################################################################## |
---|
[f824a9e] | 105 | # Is green apple 1.50 at Ralphs ? |
---|
[7211a95] | 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 | |
---|