| 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"); |
|---|
| 23 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
|---|
| 24 | #ctxt.load_id(alphaID) |
|---|
| 25 | alpha=alphaID.id_keyid() |
|---|
| 26 | |
|---|
| 27 | bobID=ABAC.ID("Bob_ID.pem"); |
|---|
| 28 | bobID.id_load_privkey_file("Bob_private.pem"); |
|---|
| 29 | #ctxt.load_id(bobID) |
|---|
| 30 | bob=bobID.id_keyid() |
|---|
| 31 | |
|---|
| 32 | joeID=ABAC.ID("Joe_ID.pem"); |
|---|
| 33 | joeID.id_load_privkey_file("Joe_private.pem"); |
|---|
| 34 | joe=joeID.id_keyid() |
|---|
| 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") |
|---|
| 42 | role.role_add_data_term(param1) |
|---|
| 43 | role.role_add_data_term(param2) |
|---|
| 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") |
|---|
| 58 | role.role_add_data_term(param1) |
|---|
| 59 | role.role_add_data_term(param2) |
|---|
| 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") |
|---|
| 74 | role.role_add_data_term(param) |
|---|
| 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 | |
|---|
| 82 | |
|---|
| 83 | ########################################################################## |
|---|
| 84 | # dump the yap dB |
|---|
| 85 | # |
|---|
| 86 | #ctxt.dump_yap_db() |
|---|
| 87 | |
|---|
| 88 | ########################################################################## |
|---|
| 89 | # dump the loaded principals/policies |
|---|
| 90 | # |
|---|
| 91 | out = ctxt.context_principals() |
|---|
| 92 | print "...initial principal set..." |
|---|
| 93 | for x in out[1]: |
|---|
| 94 | print "%s " % x.string() |
|---|
| 95 | print "\n" |
|---|
| 96 | out = ctxt.context_credentials() |
|---|
| 97 | print "...initial policy attribute set..." |
|---|
| 98 | for c in out[1]: |
|---|
| 99 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
|---|
| 100 | print "\n" |
|---|
| 101 | |
|---|