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 | ctxt.set_no_partial_proof() |
---|
16 | |
---|
17 | # Keystore is the directory containing the principal credentials. |
---|
18 | # Load existing principals and/or policy credentials |
---|
19 | if (os.environ.has_key("keystore")) : |
---|
20 | keystore=os.environ["keystore"] |
---|
21 | ctxt.load_directory(keystore) |
---|
22 | else: |
---|
23 | print("keystore is not set...") |
---|
24 | exit(1) |
---|
25 | |
---|
26 | # Load the principals created in ./attr.py and ./setup.py. Each has an |
---|
27 | # identity and private key. |
---|
28 | alphaID=ABAC.ID("Alpha_ID.pem"); |
---|
29 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
---|
30 | alpha=alphaID.id_keyid() |
---|
31 | |
---|
32 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
33 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
34 | bob=bobID.id_keyid() |
---|
35 | |
---|
36 | joeID=ABAC.ID("Joe_ID.pem"); |
---|
37 | joeID.id_load_privkey_file("Joe_private.pem"); |
---|
38 | joe=joeID.id_keyid() |
---|
39 | |
---|
40 | ########################################################################## |
---|
41 | # dump the loaded principals/policies |
---|
42 | # |
---|
43 | out = ctxt.context_principals() |
---|
44 | print "\n...final principal set..." |
---|
45 | for x in out[1]: |
---|
46 | print "%s " % x.string() |
---|
47 | out = ctxt.context_credentials() |
---|
48 | print "\n...final policy attribute set..." |
---|
49 | for c in out[1]: |
---|
50 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
51 | |
---|
52 | ########################################################################## |
---|
53 | # Construct and run the queries. In each case we create a role object and a |
---|
54 | # principal and call the query method on the context. The contents of the |
---|
55 | # proof are printed for successful queries. |
---|
56 | # role is the role to look for |
---|
57 | # p is the principal to check. |
---|
58 | ########################################################################## |
---|
59 | # role =[keyid:alpha].role:access([string:'Read'],[urn:'file//fileA']) |
---|
60 | # p = "[keyid:bob]" |
---|
61 | param1=ABAC.DataTerm("string", "'Read'") |
---|
62 | param2=ABAC.DataTerm("urn","'file//fileA'") |
---|
63 | role = ABAC.Role(alpha,"access") |
---|
64 | role.role_add_data_term(param1) |
---|
65 | role.role_add_data_term(param2) |
---|
66 | |
---|
67 | p = ABAC.Role(bob) |
---|
68 | print "\n===good============ Alpha.access(Read,fileA)<-?-Bob" |
---|
69 | out = ctxt.query(role, p) |
---|
70 | |
---|
71 | for c in out[1]: |
---|
72 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
73 | |
---|
74 | ########################################################################## |
---|
75 | # role =[keyid:alpha].role:access([string:'Read'],[urn:'file//fileA']) |
---|
76 | # p = "[keyid:joe]" |
---|
77 | param1=ABAC.DataTerm("string", "'Read'") |
---|
78 | param2=ABAC.DataTerm("urn","'file//fileA'") |
---|
79 | role = ABAC.Role(alpha,"access") |
---|
80 | role.role_add_data_term(param1) |
---|
81 | role.role_add_data_term(param2) |
---|
82 | p = ABAC.Role(joe) |
---|
83 | |
---|
84 | print "\n===bad============ Alpha.access(Read,fileA)<-?-Joe" |
---|
85 | out = ctxt.query(role,p) |
---|
86 | |
---|
87 | for c in out[1]: |
---|
88 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
89 | |
---|
90 | |
---|
91 | ########################################################################## |
---|
92 | # role =[keyid:alpha].role:team([string:'proj2']) |
---|
93 | # p = "[keyid:joe]" |
---|
94 | param=ABAC.DataTerm("string", "'proj2'") |
---|
95 | role = ABAC.Role(alpha,"team") |
---|
96 | role.role_add_data_term(param) |
---|
97 | p = ABAC.Role(joe) |
---|
98 | print "\n===good============ Alpha.team(proj2)<-?-Joe" |
---|
99 | out = ctxt.query(role,p) |
---|
100 | |
---|
101 | for c in out[1]: |
---|
102 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
103 | |
---|
104 | |
---|
105 | ## ctxt.dump_yap_db() |
---|