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 | import os |
---|
11 | import ABAC |
---|
12 | |
---|
13 | ctxt = ABAC.Context() |
---|
14 | |
---|
15 | # Keystore is the directory containing the principal credentials. |
---|
16 | # Load existing principals and/or policy credentials |
---|
17 | if (os.environ.has_key("keystore")) : |
---|
18 | keystore=os.environ["keystore"] |
---|
19 | ctxt.load_directory(keystore) |
---|
20 | else: |
---|
21 | print("keystore is not set...") |
---|
22 | exit(1) |
---|
23 | |
---|
24 | # extract principal's keyid from local principal cert files |
---|
25 | acmeID=ABAC.ID("Acme_ID.pem") |
---|
26 | acmeID.id_load_privkey_file("Acme_private.pem"); |
---|
27 | acme=acmeID.id_keyid() |
---|
28 | |
---|
29 | coyoteID=ABAC.ID("Coyote_ID.pem") |
---|
30 | coyoteID.id_load_privkey_file("Coyote_private.pem"); |
---|
31 | coyote=coyoteID.id_keyid() |
---|
32 | |
---|
33 | roadrunnerID=ABAC.ID("Roadrunner_ID.pem") |
---|
34 | roadrunnerID.id_load_privkey_file("Roadrunner_private.pem"); |
---|
35 | roadrunner=roadrunnerID.id_keyid() |
---|
36 | |
---|
37 | jackrabbitID=ABAC.ID("Jackrabbit_ID.pem") |
---|
38 | jackrabbitID.id_load_privkey_file("Jackrabbit_private.pem"); |
---|
39 | jackrabbit=jackrabbitID.id_keyid() |
---|
40 | |
---|
41 | ########################################################################## |
---|
42 | # dump the loaded principals/policies |
---|
43 | # |
---|
44 | out = ctxt.context_principals() |
---|
45 | print "\n...final principal set..." |
---|
46 | for x in out[1]: |
---|
47 | print "%s " % x.string() |
---|
48 | out = ctxt.context_credentials() |
---|
49 | print "\n...final policy attribute set..." |
---|
50 | for c in out[1]: |
---|
51 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
52 | |
---|
53 | ########################################################################## |
---|
54 | # is coyote a friend of roadrunner? |
---|
55 | # role =[keyid:Acme].role:friendof([keyid:Roadrunner]) |
---|
56 | # p= [keyid:Coyote] |
---|
57 | param=ABAC.DataTerm(roadrunnerID) |
---|
58 | role = ABAC.Role(acme,"friendof") |
---|
59 | role.role_add_data_term(param) |
---|
60 | p = ABAC.Role(coyote) |
---|
61 | print "\n===bad============ Acme.friendOf(Roadrunner) <- Coyote" |
---|
62 | out = ctxt.query(role, p) |
---|
63 | |
---|
64 | for c in out[1]: |
---|
65 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
66 | |
---|
67 | ########################################################################## |
---|
68 | # is jackrabbit a friend of roadrunner ? |
---|
69 | # role =[keyid:Acme].role:friendof([keyid:Roadrunner]) |
---|
70 | # p= [keyid:Jackrabbit] |
---|
71 | param=ABAC.DataTerm(roadrunnerID) |
---|
72 | role = ABAC.Role(acme,"friendof") |
---|
73 | role.role_add_data_term(param) |
---|
74 | p = ABAC.Role(jackrabbit) |
---|
75 | print "\n===good============ Acme.friendOf(Roadrunner) <- Jackrabbit" |
---|
76 | out = ctxt.query(role, p) |
---|
77 | |
---|
78 | for c in out[1]: |
---|
79 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
80 | |
---|
81 | |
---|
82 | ########################################################################## |
---|
83 | # is jackrabbit a preferred_customer of Acme ? |
---|
84 | # role =[keyid:Acme].role:preferred_customer |
---|
85 | # p =[keyid:Jackrabbit] |
---|
86 | role = ABAC.Role(acme,"preferred_customer") |
---|
87 | p = ABAC.Role(jackrabbit) |
---|
88 | print "\n===good============ Acme.preferred_customer <- Jackrabbit" |
---|
89 | out = ctxt.query(role, p) |
---|
90 | |
---|
91 | for c in out[1]: |
---|
92 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
93 | |
---|
94 | ########################################################################## |
---|
95 | # is coyote a preferred_customer of Acme ? |
---|
96 | # role =[keyid:Acme].role:preferred_customer |
---|
97 | # p =[keyid:Coyote] |
---|
98 | role = ABAC.Role(acme,"preferred_customer") |
---|
99 | p = ABAC.Role(coyote) |
---|
100 | print "\n===good============ Acme.preferred_customer <- Coyote" |
---|
101 | out = ctxt.query(role, p) |
---|
102 | |
---|
103 | for c in out[1]: |
---|
104 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
105 | |
---|
106 | |
---|
107 | ########################################################################## |
---|
108 | # is blah_blah a preferred_customer of Acme ? |
---|
109 | # role =[keyid:Acme].role:preferred_customer |
---|
110 | # p =[keyid:badCoyote] |
---|
111 | role = ABAC.Role(acme,"preferred_customer") |
---|
112 | # some random SHA keyid value |
---|
113 | badcoyote="1fb66793d453226f0b93a48c7f6ae0d51632e628" |
---|
114 | p = ABAC.Role(badcoyote) |
---|
115 | print "\n===bad============ Acme.preferred_customer <- badcoyote" |
---|
116 | out = ctxt.query(role, p) |
---|
117 | |
---|
118 | for c in out[1]: |
---|
119 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
120 | |
---|