1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | See README in this directory for the semantics of the example. This file |
---|
5 | constructs the credentials described and puts copies into this directory |
---|
6 | |
---|
7 | cmd1:env keystore=`pwd` ./attr.py |
---|
8 | """ |
---|
9 | |
---|
10 | import os |
---|
11 | import ABAC |
---|
12 | |
---|
13 | ctxt = ABAC.Context() |
---|
14 | print "ABAC version %s" % ctxt.version() |
---|
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 | out = ctxt.context_principals() |
---|
26 | print "...initial principal set..." |
---|
27 | for x in out[1]: |
---|
28 | print "%s " % x.string() |
---|
29 | print "\n" |
---|
30 | |
---|
31 | out = ctxt.context_credentials() |
---|
32 | print "...initial policy attribute set..." |
---|
33 | for c in out[1]: |
---|
34 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
35 | print "\n" |
---|
36 | |
---|
37 | # retrieve principals' keyid value from local credential files |
---|
38 | stateUID=ABAC.ID("StateU_ID.pem") |
---|
39 | stateUID.id_load_privkey_file("StateU_private.pem") |
---|
40 | stateU=stateUID.id_keyid() |
---|
41 | |
---|
42 | bobID=ABAC.ID("Bob_ID.pem") |
---|
43 | bobID.id_load_privkey_file("Bob_private.pem") |
---|
44 | bob=bobID.id_keyid() |
---|
45 | |
---|
46 | markID=ABAC.ID("Mark_ID.pem") |
---|
47 | markID.id_load_privkey_file("Mark_private.pem") |
---|
48 | mark=markID.id_keyid() |
---|
49 | |
---|
50 | joeID=ABAC.ID("Joe_ID.pem") |
---|
51 | joeID.id_load_privkey_file("Joe_private.pem") |
---|
52 | joe=joeID.id_keyid() |
---|
53 | |
---|
54 | maryannID=ABAC.ID("Maryann_ID.pem") |
---|
55 | maryannID.id_load_privkey_file("Maryann_private.pem") |
---|
56 | maryann=maryannID.id_keyid() |
---|
57 | |
---|
58 | janID=ABAC.ID("Jan_ID.pem") |
---|
59 | janID.id_load_privkey_file("Jan_private.pem") |
---|
60 | jan=janID.id_keyid() |
---|
61 | |
---|
62 | |
---|
63 | ################################################ |
---|
64 | # Credential 1, this policy has two range constraints on different parameters |
---|
65 | # [keyid:stateU].role:foundingAlumni |
---|
66 | # <- [keyid:stateU].role:diploma([string:?D:['mathmatics','psychology']], |
---|
67 | # [integer:?Year:[1960,1961,1963]]) |
---|
68 | head = ABAC.Role(stateU,"foundingAlumni") |
---|
69 | |
---|
70 | # initialize a string range constraint |
---|
71 | cond=ABAC.Constraint("string") |
---|
72 | |
---|
73 | # add specific string values to the constraint |
---|
74 | cond.constraint_add_string_target("'mathmatics'") |
---|
75 | cond.constraint_add_string_target("'psychology'") |
---|
76 | |
---|
77 | # create the parameter with the string range constraint |
---|
78 | param1=ABAC.DataTerm("string", "D", cond) |
---|
79 | |
---|
80 | # initialize another constratnt that is of integer type |
---|
81 | cond=ABAC.Constraint("integer") |
---|
82 | |
---|
83 | # add specific integer values to the constraint |
---|
84 | cond.constraint_add_integer_target(1960) |
---|
85 | cond.constraint_add_integer_target(1961) |
---|
86 | cond.constraint_add_integer_target(1963) |
---|
87 | |
---|
88 | # create the parameter with the integer range constraint |
---|
89 | param2=ABAC.DataTerm("integer", "Year", cond) |
---|
90 | tail = ABAC.Role(stateU,"diploma") |
---|
91 | |
---|
92 | # add the parameter with conditions to a role |
---|
93 | tail.role_add_data_term(param1) |
---|
94 | tail.role_add_data_term(param2) |
---|
95 | |
---|
96 | # build up the policy rule |
---|
97 | attr=ABAC.Attribute(head, 1800) |
---|
98 | attr.attribute_add_tail(tail) |
---|
99 | |
---|
100 | # finalize the policy rule |
---|
101 | attr.attribute_bake() |
---|
102 | |
---|
103 | # save it to a credential file |
---|
104 | attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") |
---|
105 | ctxt.load_attribute_file("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") |
---|
106 | print attr.string() |
---|
107 | print attr.typed_string() |
---|
108 | print "\n" |
---|
109 | |
---|
110 | ################################################# |
---|
111 | # Credential 2 |
---|
112 | # [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1961]) <- [keyid:bob] |
---|
113 | param1=ABAC.DataTerm("string", "'mathmatics'") |
---|
114 | param2=ABAC.DataTerm("integer", "1961") |
---|
115 | head = ABAC.Role(stateU,"diploma") |
---|
116 | head.role_add_data_term(param1) |
---|
117 | head.role_add_data_term(param2) |
---|
118 | tail = ABAC.Role(bob) |
---|
119 | attr=ABAC.Attribute(head, 1800) |
---|
120 | attr.attribute_add_tail(tail) |
---|
121 | attr.attribute_bake() |
---|
122 | attr.attribute_write_cert("StateU_diploma_m__Bob_attr.der") |
---|
123 | ctxt.load_attribute_file("StateU_diploma_m__Bob_attr.der") |
---|
124 | print attr.string() |
---|
125 | print attr.typed_string() |
---|
126 | print "\n" |
---|
127 | |
---|
128 | ################################################# |
---|
129 | # Credential 3 |
---|
130 | # [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1965]) <- [keyid:mark] |
---|
131 | param1=ABAC.DataTerm("string", "'mathmatics'") |
---|
132 | param2=ABAC.DataTerm("integer", "1965") |
---|
133 | head = ABAC.Role(stateU,"diploma") |
---|
134 | head.role_add_data_term(param1) |
---|
135 | head.role_add_data_term(param2) |
---|
136 | tail = ABAC.Role(mark) |
---|
137 | attr=ABAC.Attribute(head, 1800) |
---|
138 | attr.attribute_add_tail(tail) |
---|
139 | attr.attribute_bake() |
---|
140 | attr.attribute_write_cert("StateU_diploma_m__Mark_attr.der") |
---|
141 | ctxt.load_attribute_file("StateU_diploma_m__Mark_attr.der") |
---|
142 | print attr.string() |
---|
143 | print attr.typed_string() |
---|
144 | print "\n" |
---|
145 | |
---|
146 | ################################################# |
---|
147 | # Credential 4 |
---|
148 | # [keyid:stateU].role:diploma([string:'zoology'],[integer:1961]) <- [keyid:joe] |
---|
149 | param1=ABAC.DataTerm("string", "'zoology'") |
---|
150 | param2=ABAC.DataTerm("integer", "1961") |
---|
151 | head = ABAC.Role(stateU,"diploma") |
---|
152 | head.role_add_data_term(param1) |
---|
153 | head.role_add_data_term(param2) |
---|
154 | tail = ABAC.Role(joe) |
---|
155 | attr=ABAC.Attribute(head, 1800) |
---|
156 | attr.attribute_add_tail(tail) |
---|
157 | attr.attribute_bake() |
---|
158 | attr.attribute_write_cert("StateU_diploma_z__Joe_attr.der") |
---|
159 | ctxt.load_attribute_file("StateU_diploma_z__Joe_attr.der") |
---|
160 | print attr.string() |
---|
161 | print attr.typed_string() |
---|
162 | print "\n" |
---|
163 | |
---|
164 | ################################################# |
---|
165 | # Credential 5 |
---|
166 | # [keyid:stateU].role:diploma([string:'psychology'],[integer:1962]) |
---|
167 | # <- [keyid:maryann] |
---|
168 | param1=ABAC.DataTerm("string", "'psychology'") |
---|
169 | param2=ABAC.DataTerm("integer", "1962") |
---|
170 | head = ABAC.Role(stateU,"diploma") |
---|
171 | head.role_add_data_term(param1) |
---|
172 | head.role_add_data_term(param2) |
---|
173 | tail = ABAC.Role(maryann) |
---|
174 | attr=ABAC.Attribute(head, 1800) |
---|
175 | attr.attribute_add_tail(tail) |
---|
176 | attr.attribute_bake() |
---|
177 | attr.attribute_write_cert("StateU_diploma_p__Maryann_attr.der") |
---|
178 | ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der") |
---|
179 | print attr.string() |
---|
180 | print attr.typed_string() |
---|
181 | print "\n" |
---|
182 | |
---|
183 | |
---|
184 | ################################################# |
---|
185 | # Credential 6 |
---|
186 | # [keyid:stateU].role:diploma([string:'psychology'],[integer:1960]) |
---|
187 | # <- [keyid:jan] |
---|
188 | param1=ABAC.DataTerm("string", "'psychology'") |
---|
189 | param2=ABAC.DataTerm("integer", "1960") |
---|
190 | head = ABAC.Role(stateU,"diploma") |
---|
191 | head.role_add_data_term(param1) |
---|
192 | head.role_add_data_term(param2) |
---|
193 | tail = ABAC.Role(jan) |
---|
194 | attr=ABAC.Attribute(head, 1800) |
---|
195 | attr.attribute_add_tail(tail) |
---|
196 | attr.attribute_bake() |
---|
197 | attr.attribute_write_cert("StateU_diploma_p__Jan_attr.der") |
---|
198 | ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der") |
---|
199 | print attr.string() |
---|
200 | print attr.typed_string() |
---|
201 | print "\n" |
---|
202 | |
---|