source: examples/python_tests/alice_rt1/attr.py @ 340bd01

mei_rt2mei_rt2_fix_1
Last change on this file since 340bd01 was f824a9e, checked in by Mei <mei@…>, 13 years ago

1) add more doc to python_tests

  • Property mode set to 100755
File size: 13.2 KB
Line 
1#!/usr/bin/env python
2
3"""
4See README in this directory for the semantics of the example.  This file
5constructs the credentials described and puts copies into this directory
6
7cmd1:env keystore=`pwd` ./attr.py
8"""
9
10import os
11import ABAC
12
13ctxt = ABAC.Context()
14print "ABAC version %s" % ctxt.version()
15
16# Keystore is the directory containing the principal credentials.
17# Load existing principals and/or policy credentials
18if (os.environ.has_key("keystore")) :
19    keystore=os.environ["keystore"]
20    ctxt.load_directory(keystore)
21
22# retrieve principals' keyid value from local credential files
23out = ctxt.context_principals()
24print "...initial principal set..."
25for x in out[1]:
26    print "%s " % x.string()
27print "\n" 
28
29out = ctxt.context_credentials()
30print "...initial policy attribute set..."
31for c in out[1]:
32    print "%s <- %s" % (c.head_string(), c.tail_string())
33print "\n"
34
35aliceID=ABAC.ID("Alice_ID.pem")
36aliceID.id_load_privkey_file("Alice_private.pem")
37alice=aliceID.id_keyid()
38
39partyID=ABAC.ID("Party_ID.pem")
40partyID.id_load_privkey_file("Party_private.pem")
41party=partyID.id_keyid()
42
43teaID=ABAC.ID("Tea_ID.pem")
44teaID.id_load_privkey_file("Tea_private.pem")
45tea=teaID.id_keyid()
46
47hatterID=ABAC.ID("Hatter_ID.pem")
48hatterID.id_load_privkey_file("Hatter_private.pem")
49hatter=hatterID.id_keyid()
50
51marchhareID=ABAC.ID("Marchhare_ID.pem")
52marchhareID.id_load_privkey_file("Marchhare_private.pem")
53marchhare=marchhareID.id_keyid()
54
55dormouseID=ABAC.ID("Dormouse_ID.pem")
56dormouseID.id_load_privkey_file("Dormouse_private.pem")
57dormouse=dormouseID.id_keyid()
58
59
60# can not have attribute string more than 382 characters or so
61# that would cause encoded attribute string more than 511 in length
62# and could not push/pull out of ietf_attribute_t properly
63###############################################
64# NOTE: space and '~' in string value
65#       time value's optional ending
66# [keyid:Party].role: about([keyid:tea],[time:20101010T],[boolean:true],
67#                           [integer:4],[float:-200.0],[float:8],[time:20120205T182930],
68#                           [string:'a list'],[urn:'file://usr/party/~teaparty'])
69#                <- [keyid:Party].role:guestOf([keyid:MarchHare])
70head=ABAC.Role(party,"about")
71param=ABAC.DataTerm(teaID)
72head.role_add_data_term(param)
73param=ABAC.DataTerm("time","20101010T")
74head.role_add_data_term(param)
75param=ABAC.DataTerm("boolean","true")
76head.role_add_data_term(param)
77param=ABAC.DataTerm("integer","4")
78head.role_add_data_term(param)
79param=ABAC.DataTerm("float","-200.0")
80head.role_add_data_term(param)
81param=ABAC.DataTerm("float","8")
82head.role_add_data_term(param)
83param=ABAC.DataTerm("time","20120205T182930")
84head.role_add_data_term(param)
85param=ABAC.DataTerm("string","'a list'")
86head.role_add_data_term(param)
87param=ABAC.DataTerm("urn","'file://usr/party/~teaparty'")
88head.role_add_data_term(param)
89tail=ABAC.Role(party,"guestOf")
90param=ABAC.DataTerm(marchhareID)
91tail.role_add_data_term(param)
92attr=ABAC.Attribute(head, 1800)
93attr.attribute_add_tail(tail)
94attr.attribute_bake()
95attr.attribute_write_cert("party_about_party__guestof_marchhare_attr.der")
96ctxt.load_attribute_file("party_about_party__guestof_marchhare_attr.der")
97print attr.string() 
98print attr.typed_string()
99print "\n"
100
101###############################################
102# NOTE: anonymous parameter
103#       Variable value for different parameter types
104# [keyid:Party].role:about_other_party([keyid:tea],[?],
105#                         [integer:99],[time:?AAA],[integer:?BBB],
106#                         [boolean:true],[integer:?])
107#                 <- [keyid:MarchHare]
108head=ABAC.Role(party,"about_other_party")
109param=ABAC.DataTerm(teaID)
110head.role_add_data_term(param)
111param=ABAC.DataTerm("anonymous","_")
112head.role_add_data_term(param)
113param=ABAC.DataTerm("integer","99")
114head.role_add_data_term(param)
115param=ABAC.DataTerm("time","AAA")
116head.role_add_data_term(param)
117param=ABAC.DataTerm("integer","BBB")
118head.role_add_data_term(param)
119param=ABAC.DataTerm("boolean","true")
120head.role_add_data_term(param)
121param=ABAC.DataTerm("integer","?")
122head.role_add_data_term(param)
123tail=ABAC.Role(marchhare)
124attr=ABAC.Attribute(head, 1800)
125attr.attribute_add_tail(tail)
126attr.attribute_bake()
127attr.attribute_write_cert("party_about_other_party__marchhare_attr.der")
128ctxt.load_attribute_file("party_about_other_party__marchhare_attr.der")
129print attr.string()
130print attr.typed_string()
131print "\n"
132
133###############################################
134# [keyid:Party].role:about_string([string:'a confused party'])
135#                 <- [keyid:MarchHare]
136head=ABAC.Role(party,"about_string")
137param=ABAC.DataTerm("string","'a confused party'")
138head.role_add_data_term(param)
139tail=ABAC.Role(marchhare)
140attr=ABAC.Attribute(head, 1800)
141attr.attribute_add_tail(tail)
142attr.attribute_bake()
143attr.attribute_write_cert("party_about_string__marchhare_attr.der")
144ctxt.load_attribute_file("party_about_string__marchhare_attr.der")
145print attr.string()
146print attr.typed_string()
147print "\n"
148
149###############################################
150# NOTE: how to escape within a string value
151# [keyid:Party].role:about_string2([string:'a mangled \\'string\\''])
152#                 <- [keyid:MarchHare]
153head=ABAC.Role(party,"about_string2")
154param=ABAC.DataTerm("string","'a mangled \\'string\\''")
155head.role_add_data_term(param)
156tail=ABAC.Role(marchhare)
157attr=ABAC.Attribute(head, 1800)
158attr.attribute_add_tail(tail)
159attr.attribute_bake()
160attr.attribute_write_cert("party_about_string2__marchhare_attr.der")
161ctxt.load_attribute_file("party_about_string2__marchhare_attr.der")
162print attr.string()
163print attr.typed_string()
164print "\n"
165
166###############################################
167# [keyid:Party].role:about_urn([urn:'file://user/local/party/~sillyparty'])
168#                 <- [keyid:MarchHare]
169head=ABAC.Role(party,"about_urn")
170param=ABAC.DataTerm("urn","'file://user/local/party/~sillyparty'")
171head.role_add_data_term(param)
172tail=ABAC.Role(marchhare)
173attr=ABAC.Attribute(head, 1800)
174attr.attribute_add_tail(tail)
175attr.attribute_bake()
176attr.attribute_write_cert("party_about_urn__marchhare_attr.der")
177ctxt.load_attribute_file("party_about_urn__marchhare_attr.der")
178print attr.string()
179print attr.typed_string()
180print "\n"
181
182###############################################
183# [keyid:Party].role:about_another_float([float:0.22])
184#                 <- [keyid:MarchHare]
185head=ABAC.Role(party,"about_another_float")
186param=ABAC.DataTerm("float","0.22")
187head.role_add_data_term(param)
188tail=ABAC.Role(marchhare)
189attr=ABAC.Attribute(head, 1800)
190attr.attribute_add_tail(tail)
191attr.attribute_bake()
192attr.attribute_write_cert("party_about_third_float__marchhare_attr.der")
193ctxt.load_attribute_file("party_about_third_float__marchhare_attr.der")
194print attr.string()
195print attr.typed_string()
196print "\n"
197
198###############################################
199# [keyid:Party].role:about_another_float([float:8])
200#                 <- [keyid:MarchHare]
201head=ABAC.Role(party,"about_another_float")
202param=ABAC.DataTerm("float","8")
203head.role_add_data_term(param)
204tail=ABAC.Role(marchhare)
205attr=ABAC.Attribute(head, 1800)
206attr.attribute_add_tail(tail)
207attr.attribute_bake()
208attr.attribute_write_cert("party_about_another_float__marchhare_attr.der")
209ctxt.load_attribute_file("party_about_another_float__marchhare_attr.der")
210print attr.string()
211print attr.typed_string()
212print "\n"
213
214###############################################
215# [keyid:Party].role:about_float([float:-200.0])
216#                 <- [keyid:MarchHare]
217head=ABAC.Role(party,"about_float")
218param=ABAC.DataTerm("float","-200.0")
219head.role_add_data_term(param)
220tail=ABAC.Role(marchhare)
221attr=ABAC.Attribute(head, 1800)
222attr.attribute_add_tail(tail)
223attr.attribute_bake()
224attr.attribute_write_cert("party_about_float__marchhare_attr.der")
225ctxt.load_attribute_file("party_about_float__marchhare_attr.der")
226print attr.string()
227print attr.typed_string()
228print "\n"
229
230###############################################
231# [keyid:Party].role:about_another_integer([integer:-7])
232#                 <- [keyid:MarchHare]
233head=ABAC.Role(party,"about_another_integer")
234param=ABAC.DataTerm("integer","-7")
235head.role_add_data_term(param)
236tail=ABAC.Role(marchhare)
237attr=ABAC.Attribute(head, 1800)
238attr.attribute_add_tail(tail)
239attr.attribute_bake()
240attr.attribute_write_cert("party_about_another_integer__marchhare_attr.der")
241ctxt.load_attribute_file("party_about_another_integer__marchhare_attr.der")
242print attr.string()
243print attr.typed_string()
244print "\n"
245
246###############################################
247# [keyid:Party].role:about_integer([integer:4])
248#                 <- [keyid:MarchHare]
249head=ABAC.Role(party,"about_integer")
250param=ABAC.DataTerm("integer","4")
251head.role_add_data_term(param)
252tail=ABAC.Role(marchhare)
253attr=ABAC.Attribute(head, 1800)
254attr.attribute_add_tail(tail)
255attr.attribute_bake()
256attr.attribute_write_cert("party_about_integer__marchhare_attr.der")
257ctxt.load_attribute_file("party_about_integer__marchhare_attr.der")
258print attr.string()
259print attr.typed_string()
260print "\n"
261
262###############################################
263# [keyid:Party].role:about_boolean([boolean:true])
264#                 <- [keyid:MarchHare]
265head=ABAC.Role(party,"about_boolean")
266param=ABAC.DataTerm("boolean","true")
267head.role_add_data_term(param)
268tail=ABAC.Role(marchhare)
269attr=ABAC.Attribute(head, 1800)
270attr.attribute_add_tail(tail)
271attr.attribute_bake()
272attr.attribute_write_cert("party_about_boolean__marchhare_attr.der")
273ctxt.load_attribute_file("party_about_boolean__marchhare_attr.der")
274print attr.string()
275print attr.typed_string()
276print "\n"
277
278###############################################
279# [keyid:Party].role:about_key([keyid:tea])
280#                 <- [keyid:MarchHare]
281head=ABAC.Role(party,"about_key")
282param=ABAC.DataTerm(teaID)
283head.role_add_data_term(param)
284tail=ABAC.Role(marchhare)
285attr=ABAC.Attribute(head, 1800)
286attr.attribute_add_tail(tail)
287attr.attribute_bake()
288attr.attribute_write_cert("party_about_key__marchhare_attr.der")
289ctxt.load_attribute_file("party_about_key__marchhare_attr.der")
290print attr.string()
291print attr.typed_string()
292print "\n"
293
294###############################################
295# [keyid:Party].role:about_another_time([time:20201101T182930])
296#                 <- [keyid:MarchHare]
297head=ABAC.Role(party,"about_another_time")
298param=ABAC.DataTerm("time","20201101T182930")
299head.role_add_data_term(param)
300tail=ABAC.Role(marchhare)
301attr=ABAC.Attribute(head, 1800)
302attr.attribute_add_tail(tail)
303attr.attribute_bake()
304attr.attribute_write_cert("party_about_another_time__marchhare_attr.der")
305ctxt.load_attribute_file("party_about_another_time__marchhare_attr.der")
306print attr.string()
307print attr.typed_string()
308print "\n"
309
310###############################################
311# [keyid:Party].role:about_time([time:20201101T])
312#                 <- [keyid:MarchHare]
313head=ABAC.Role(party,"about_time")
314param=ABAC.DataTerm("time","20201101T")
315head.role_add_data_term(param)
316tail=ABAC.Role(marchhare)
317attr=ABAC.Attribute(head, 1800)
318attr.attribute_add_tail(tail)
319attr.attribute_bake()
320attr.attribute_write_cert("party_about_time__marchhare_attr.der")
321ctxt.load_attribute_file("party_about_time__marchhare_attr.der")
322print attr.string()
323print attr.typed_string()
324print "\n"
325
326###############################################
327# NOTE: principal parameter type
328# [keyid:Party].role:guestof([principal:?V])
329#           <- [keyid:Party].role:friendOf([principal:?V])
330#
331head=ABAC.Role(party,"guestOf")
332param=ABAC.DataTerm("principal","V")
333head.role_add_data_term(param)
334tail=ABAC.Role(party,"friendOf")
335param=ABAC.DataTerm("principal","V")
336tail.role_add_data_term(param)
337attr=ABAC.Attribute(head, 1800)
338attr.attribute_add_tail(tail)
339attr.attribute_bake()
340attr.attribute_write_cert("party_guestof_qV__party_friendof_qV_attr.der")
341ctxt.load_attribute_file("party_guestof_qV__party_friendof_qV_attr.der")
342print attr.string()
343print attr.typed_string()
344print "\n"
345
346###############################################
347# [keyid:Party].role:guests
348#        <- [keyid:Party].role:friendOf([keyid:MarchHare])
349#
350head=ABAC.Role(party,"guests")
351tail=ABAC.Role(party,"friendOf")
352param=ABAC.DataTerm(marchhareID)
353tail.role_add_data_term(param)
354attr=ABAC.Attribute(head, 1800)
355attr.attribute_add_tail(tail)
356attr.attribute_bake()
357attr.attribute_write_cert("party_guests__party_friendof_marchhare_attr.der")
358ctxt.load_attribute_file("party_guests__party_friendof_marchhare_attr.der")
359print attr.string()
360print attr.typed_string()
361print "\n"
362
363###############################################
364# [keyid:Party].role:friendOf([keyid:MarchHare])
365#                               <- [keyid:Dormouse]
366#
367head=ABAC.Role(party,"friendOf")
368param=ABAC.DataTerm(marchhareID)
369head.role_add_data_term(param)
370tail=ABAC.Role(dormouse)
371attr=ABAC.Attribute(head, 1800)
372attr.attribute_add_tail(tail)
373attr.attribute_bake()
374attr.attribute_write_cert("party_friendof_marchhare__dormouse_attr.der")
375ctxt.load_attribute_file("party_friendof_marchhare__dormouse_attr.der")
376print attr.string()
377print attr.typed_string()
378print "\n"
379
380###############################################
381# NOTE: named principal tail
382# [keyid:Party].role:friendOf([keyid:Alice])
383#                               <- [keyid:Hatter]
384#
385head=ABAC.Role(party,"friendOf")
386param=ABAC.DataTerm(aliceID)
387head.role_add_data_term(param)
388tail=ABAC.Role(hatter)
389attr=ABAC.Attribute(head, 1800)
390attr.attribute_add_tail(tail)
391attr.attribute_bake()
392attr.attribute_write_cert("party_friendof_alice__hatter_attr.der")
393ctxt.load_attribute_file("party_friendof_alice__hatter_attr.der")
394print attr.string()
395print attr.typed_string()
396print "\n"
397
Note: See TracBrowser for help on using the repository browser.