[5110d42] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | to test with python |
---|
| 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 | # print "ABAC version %s" % ctxt.version() |
---|
| 17 | |
---|
| 18 | keystore=os.environ["keystore"] |
---|
| 19 | |
---|
| 20 | ctxt.load_directory(keystore) |
---|
| 21 | |
---|
| 22 | alphaID=ABAC.ID("Alpha_ID.pem"); |
---|
[5d06689] | 23 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
---|
[5110d42] | 24 | #ctxt.load_id(alphaID) |
---|
[5d06689] | 25 | alpha=alphaID.id_keyid() |
---|
[5110d42] | 26 | |
---|
| 27 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
[5d06689] | 28 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
[5110d42] | 29 | #ctxt.load_id(bobID) |
---|
[5d06689] | 30 | bob=bobID.id_keyid() |
---|
[5110d42] | 31 | |
---|
| 32 | joeID=ABAC.ID("Joe_ID.pem"); |
---|
[5d06689] | 33 | joeID.id_load_privkey_file("Joe_private.pem"); |
---|
| 34 | joe=joeID.id_keyid() |
---|
[5110d42] | 35 | |
---|
| 36 | ########################################################################## |
---|
| 37 | # role =[keyid:alpha].role:access([string:'Read'],[urn:'file//fileA']) |
---|
| 38 | # p = "[keyid:bob]" |
---|
| 39 | param1=ABAC.DataTerm("string", "'Read'") |
---|
| 40 | param2=ABAC.DataTerm("urn","'file//fileA'") |
---|
| 41 | role = ABAC.Role(alpha,"access") |
---|
[5d06689] | 42 | role.role_add_data_term(param1) |
---|
| 43 | role.role_add_data_term(param2) |
---|
[5110d42] | 44 | p = ABAC.Role(bob) |
---|
| 45 | |
---|
| 46 | print "\n===good============ Alpha.access(Read,fileA)<-?-Bob" |
---|
| 47 | out = ctxt.query(role, p) |
---|
| 48 | |
---|
| 49 | for c in out[1]: |
---|
| 50 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 51 | |
---|
| 52 | ########################################################################## |
---|
| 53 | # role =[keyid:alpha].role:access([string:'Read'],[urn:'file//fileA']) |
---|
| 54 | # p = "[keyid:joe]" |
---|
| 55 | param1=ABAC.DataTerm("string", "'Read'") |
---|
| 56 | param2=ABAC.DataTerm("urn","'file//fileA'") |
---|
| 57 | role = ABAC.Role(alpha,"access") |
---|
[5d06689] | 58 | role.role_add_data_term(param1) |
---|
| 59 | role.role_add_data_term(param2) |
---|
[5110d42] | 60 | p = ABAC.Role(joe) |
---|
| 61 | |
---|
| 62 | print "\n===bad============ Alpha.access(Read,fileA)<-?-Joe" |
---|
| 63 | out = ctxt.query(role,p) |
---|
| 64 | |
---|
| 65 | for c in out[1]: |
---|
| 66 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | ########################################################################## |
---|
| 70 | # role =[keyid:alpha].role:team([string:'proj2']) |
---|
| 71 | # p = "[keyid:joe]" |
---|
| 72 | param=ABAC.DataTerm("string", "'proj2'") |
---|
| 73 | role = ABAC.Role(alpha,"team") |
---|
[5d06689] | 74 | role.role_add_data_term(param) |
---|
[5110d42] | 75 | p = ABAC.Role(joe) |
---|
| 76 | print "\n===good============ Alpha.team(proj2)<-?-Joe" |
---|
| 77 | out = ctxt.query(role,p) |
---|
| 78 | |
---|
| 79 | for c in out[1]: |
---|
| 80 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 81 | |
---|
[5d06689] | 82 | |
---|
| 83 | ########################################################################## |
---|
| 84 | # dump the loaded principals/policies |
---|
| 85 | # |
---|
| 86 | out = ctxt.context_principals() |
---|
[7211a95] | 87 | print "\n...final principal set..." |
---|
[5d06689] | 88 | for x in out[1]: |
---|
| 89 | print "%s " % x.string() |
---|
| 90 | print "\n" |
---|
| 91 | out = ctxt.context_credentials() |
---|
[7211a95] | 92 | print "\n...final policy attribute set..." |
---|
[5d06689] | 93 | for c in out[1]: |
---|
| 94 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 95 | print "\n" |
---|
| 96 | |
---|