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 | # retrieve principals' keyid value from local credential files |
---|
27 | leagueID=ABAC.ID("League_ID.pem"); |
---|
28 | leagueID.id_load_privkey_file("League_private.pem"); |
---|
29 | league=leagueID.id_keyid() |
---|
30 | |
---|
31 | johnID=ABAC.ID("John_ID.pem"); |
---|
32 | johnID.id_load_privkey_file("John_private.pem"); |
---|
33 | john=johnID.id_keyid() |
---|
34 | |
---|
35 | markID=ABAC.ID("Mark_ID.pem"); |
---|
36 | markID.id_load_privkey_file("Mark_private.pem"); |
---|
37 | mark=markID.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 | # can john go to stadium at 1pm? |
---|
53 | # role=[keyid:league].role:stadium([string:'access'],[boolean:true],[time:20120228T130000]) |
---|
54 | # p=[keyid:john] |
---|
55 | param1=ABAC.DataTerm("string", "'access'") |
---|
56 | param2=ABAC.DataTerm("boolean", "true") |
---|
57 | param3=ABAC.DataTerm("time", "20120228T130000") |
---|
58 | role = ABAC.Role(league,"stadium") |
---|
59 | role.role_add_data_term(param1) |
---|
60 | role.role_add_data_term(param2) |
---|
61 | role.role_add_data_term(param3) |
---|
62 | p=ABAC.Role(john) |
---|
63 | print "\n===good============ league.stadium(access,true,20120128T130000)<-?-john" |
---|
64 | out = ctxt.query(role, p) |
---|
65 | for c in out[1]: |
---|
66 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
67 | |
---|
68 | ########################################################################## |
---|
69 | # can mark go to stadium at 11am? |
---|
70 | # role=[keyid:league].role:stadium([string:'access'],[boolean:true],[time:20120228T110000]) |
---|
71 | # p=[keyid:mark] |
---|
72 | param1=ABAC.DataTerm("string", "'access'") |
---|
73 | param2=ABAC.DataTerm("boolean", "true") |
---|
74 | param3=ABAC.DataTerm("time", "20120228T110000") |
---|
75 | role = ABAC.Role(league,"stadium") |
---|
76 | role.role_add_data_term(param1) |
---|
77 | role.role_add_data_term(param2) |
---|
78 | role.role_add_data_term(param3) |
---|
79 | p=ABAC.Role(mark) |
---|
80 | print "\n===bad============ league.stadium(access,true,20120128T110000)<-?-mark" |
---|
81 | out = ctxt.query(role, p) |
---|
82 | for c in out[1]: |
---|
83 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
84 | |
---|
85 | ########################################################################## |
---|
86 | # can mark go to stadium at 8am then? |
---|
87 | # role=[keyid:league].role:stadium([string:'access'],[boolean:true],[time:20120228T080000]) |
---|
88 | # p=[keyid:mark] |
---|
89 | param1=ABAC.DataTerm("string", "'access'") |
---|
90 | param2=ABAC.DataTerm("boolean", "true") |
---|
91 | param3=ABAC.DataTerm("time", "20120228T080000") |
---|
92 | role = ABAC.Role(league,"stadium") |
---|
93 | role.role_add_data_term(param1) |
---|
94 | role.role_add_data_term(param2) |
---|
95 | role.role_add_data_term(param3) |
---|
96 | p=ABAC.Role(mark) |
---|
97 | print "\n===good============ league.stadium(access,true,20120128T080000)<-?-mark" |
---|
98 | out = ctxt.query(role, p) |
---|
99 | for c in out[1]: |
---|
100 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|