source: examples/python_tests/payraise_rt1/attr.py @ 4b46680

mei_rt2mei_rt2_fix_1
Last change on this file since 4b46680 was f824a9e, checked in by Mei <mei@…>, 12 years ago

1) add more doc to python_tests

  • Property mode set to 100755
File size: 5.5 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
22out = ctxt.context_principals()
23print "...initial principal set..."
24for x in out[1]:
25    print "%s " % x.string()
26print "\n" 
27
28out = ctxt.context_credentials()
29print "...initial policy attribute set..."
30for c in out[1]:
31    print "%s <- %s" % (c.head_string(), c.tail_string())
32print "\n"
33
34# retrieve principals' keyid value from local credential files
35alphaID=ABAC.ID("Alpha_ID.pem");
36alphaID.id_load_privkey_file("Alpha_private.pem");
37alpha=alphaID.id_keyid()
38
39bobID=ABAC.ID("Bob_ID.pem");
40bobID.id_load_privkey_file("Bob_private.pem");
41bob=bobID.id_keyid()
42
43maryannID=ABAC.ID("Maryann_ID.pem");
44maryannID.id_load_privkey_file("Maryann_private.pem");
45maryann=maryannID.id_keyid()
46
47joeID=ABAC.ID("Joe_ID.pem");
48joeID.id_load_privkey_file("Joe_private.pem");
49joe=joeID.id_keyid()
50
51################################################
52# Credential 1, intersecting linking roles with This principal param for the
53#               linked role
54# [keyid:alpha].role:payRaise <-
55#      [keyid:alpha].role:evaluatorOf([principal:?this]).role:goodPerformance &
56#      [keyid:alpha].role:managerOf([principal:?this]).role:niceCoworker
57head=ABAC.Role(alpha,"payRaise")
58
59# build a data term with ?This
60param=ABAC.DataTerm("principal", "This")
61tail1 = ABAC.Role(alpha,"evaluatorOf","goodPerformance")
62
63# add the param to the linked role
64tail1.role_add_linked_data_term(param)
65
66# build another data term with ?This
67param=ABAC.DataTerm("principal", "This")
68tail2 = ABAC.Role(alpha,"managerOf","niceCoworker")
69
70# add the param to the linked role
71tail2.role_add_linked_data_term(param)
72
73# compose the intersecting role attribute policy
74attr=ABAC.Attribute(head, 1800)
75attr.attribute_add_tail(tail1)
76attr.attribute_add_tail(tail2)
77
78# finalize the attribute policy
79attr.attribute_bake()
80
81# write out the attribute credential file
82attr.attribute_write_cert("Alpha_payraise__Alpha_performance_qT_niceguy_qT_attr.der")
83ctxt.load_attribute_file("Alpha_payraise__Alpha_performance_qT_niceguy_qT_attr.der")
84print attr.string() 
85print attr.typed_string()
86print "\n"
87
88#################################################
89# Credential 2,
90# [keyid:alpha].role:managerOf([principal:?Z])<-
91#           [keyid:alpha].role:evaluatorOf([principal:?Z])
92param=ABAC.DataTerm("principal", "Z")
93head=ABAC.Role(alpha,"managerOf")
94head.role_add_data_term(param)
95param=ABAC.DataTerm("principal", "Z")
96tail = ABAC.Role(alpha,"evaluatorOf")
97tail.role_add_data_term(param)
98attr=ABAC.Attribute(head, 1800)
99attr.attribute_add_tail(tail)
100attr.attribute_bake()
101attr.attribute_write_cert("Alpha_manager_qZ__Alpha_evaluator_qZ_attr.der")
102ctxt.load_attribute_file("Alpha_manager_qZ__Alpha_evaluator_qZ_attr.der")
103print attr.string() 
104print attr.typed_string()
105print "\n"
106
107#################################################
108# Credential 3
109# [keyid:alpha].role:evaluatorOf([keyid:Maryann]) <-[keyid:Bob]
110param=ABAC.DataTerm(maryannID)
111head = ABAC.Role(alpha,"evaluatorOf")
112head.role_add_data_term(param)
113tail = ABAC.Role(bob)
114attr=ABAC.Attribute(head, 1800)
115attr.attribute_add_tail(tail)
116attr.attribute_bake()
117attr.attribute_write_cert("Alpha_evaluator_m__Bob_attr.der")
118ctxt.load_attribute_file("Alpha_evaluator_m__Bob_attr.der")
119print attr.string() 
120print attr.typed_string()
121print "\n"
122
123#################################################
124# Credential 4
125# [keyid:Bob].role:goodPerformance <- [keyid:Maryann])
126head = ABAC.Role(bob,"goodPerformance")
127tail = ABAC.Role(maryann)
128attr=ABAC.Attribute(head, 1800)
129attr.attribute_add_tail(tail)
130attr.attribute_bake()
131attr.attribute_write_cert("Bob_goodperformance__Maryann_attr.der")
132ctxt.load_attribute_file("Bob_goodperformance__Maryann_attr.der")
133print attr.string() 
134print attr.typed_string()
135print "\n"
136
137#################################################
138# Credential 5
139# [keyid:Bob].role:niceCoworker <- [keyid:Maryann])
140head = ABAC.Role(bob,"niceCoworker")
141tail = ABAC.Role(maryann)
142attr=ABAC.Attribute(head, 1800)
143attr.attribute_add_tail(tail)
144attr.attribute_bake()
145attr.attribute_write_cert("Bob_nicecoworker__Maryann_attr.der")
146ctxt.load_attribute_file("Bob_nicecoworker__Maryann_attr.der")
147print attr.string() 
148print attr.typed_string()
149print "\n"
150
151#################################################
152# Credential 6
153# [keyid:alpha].role:evaluatorOf([keyid:Joe]) <-[keyid:Bob]
154param=ABAC.DataTerm(joeID)
155head = ABAC.Role(alpha,"evaluatorOf")
156head.role_add_data_term(param)
157tail = ABAC.Role(bob)
158attr=ABAC.Attribute(head, 1800)
159attr.attribute_add_tail(tail)
160attr.attribute_bake()
161attr.attribute_write_cert("Alpha_evaluator_j__Bob_attr.der")
162ctxt.load_attribute_file("Alpha_evaluator_j__Bob_attr.der")
163print attr.string() 
164print attr.typed_string()
165print "\n"
166
167#################################################
168# Credential 7
169# [keyid:Bob].role:goodPerformance <- [keyid:Joe])
170head = ABAC.Role(bob,"goodPerformance")
171tail = ABAC.Role(joe)
172attr=ABAC.Attribute(head, 1800)
173attr.attribute_add_tail(tail)
174attr.attribute_bake()
175attr.attribute_write_cert("Bob_goodperformance__Joe_attr.der")
176ctxt.load_attribute_file("Bob_goodperformance__Joe_attr.der")
177print attr.string() 
178print attr.typed_string()
179print "\n"
180
Note: See TracBrowser for help on using the repository browser.