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