source: examples/python_tests/alumni_rt1/attr.py @ a0c1772

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

1) add more doc to python_tests

  • Property mode set to 100755
File size: 4.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
35stateUID=ABAC.ID("StateU_ID.pem")
36stateUID.id_load_privkey_file("StateU_private.pem")
37stateU=stateUID.id_keyid()
38
39bobID=ABAC.ID("Bob_ID.pem")
40bobID.id_load_privkey_file("Bob_private.pem")
41bob=bobID.id_keyid()
42
43joeID=ABAC.ID("Joe_ID.pem")
44joeID.id_load_privkey_file("Joe_private.pem")
45joe=joeID.id_keyid()
46
47maryannID=ABAC.ID("Maryann_ID.pem")
48maryannID.id_load_privkey_file("Maryann_private.pem")
49maryann=maryannID.id_keyid()
50
51
52################################################
53# Credential 1, the year is constrainted by a range constraint with min and max
54#               values
55# [keyid:stateU].role:foundingAlumni
56#          <- [keyid:stateU].role:diploma([?], [integer:?Year:[1955 .. 1958]])
57head = ABAC.Role(stateU,"foundingAlumni")
58
59# create an anonymous parameter that will take any type of major
60param1=ABAC.DataTerm("anonymous", "_")
61
62# initialize a constraint with integer type
63cond=ABAC.Constraint("integer")
64
65# set the bounding min and max value for this constraint
66cond.constraint_add_integer_min(1955)
67cond.constraint_add_integer_max(1958)
68
69# make the parameter with this integer constraint
70param2=ABAC.DataTerm("integer", "Year", cond)
71tail = ABAC.Role(stateU,"diploma")
72tail.role_add_data_term(param1)
73tail.role_add_data_term(param2)
74attr=ABAC.Attribute(head, 1800)
75
76# build up the policy
77attr.attribute_add_tail(tail)
78
79# finalize the policy
80attr.attribute_bake()
81
82# write the policy out into the file system
83attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der")
84
85# load this policy into the context using this external credential file
86ctxt.load_attribute_file("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der")
87print attr.string() 
88print attr.typed_string()
89print "\n"
90
91#################################################
92# Credential 2
93# [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1960])
94#                                                            <- [keyid:bob]
95param1=ABAC.DataTerm("string", "'mathmatics'")
96param2=ABAC.DataTerm("integer", "1960")
97head = ABAC.Role(stateU,"diploma")
98head.role_add_data_term(param1)
99head.role_add_data_term(param2)
100tail = ABAC.Role(bob)
101attr=ABAC.Attribute(head, 1800)
102attr.attribute_add_tail(tail)
103attr.attribute_bake()
104attr.attribute_write_cert("StateU_diploma_m__Bob_attr.der")
105ctxt.load_attribute_file("StateU_diploma_m__Bob_attr.der")
106print attr.string() 
107print attr.typed_string()
108print "\n"
109
110#################################################
111# Credential 3
112# [keyid:stateU].role:diploma([string:'zoology'],[integer:1955])
113#                                                     <- [keyid:joe]
114param1=ABAC.DataTerm("string", "'zoology'")
115param2=ABAC.DataTerm("integer", "1955")
116head = ABAC.Role(stateU,"diploma")
117head.role_add_data_term(param1)
118head.role_add_data_term(param2)
119tail = ABAC.Role(joe)
120attr=ABAC.Attribute(head, 1800)
121attr.attribute_add_tail(tail)
122attr.attribute_bake()
123attr.attribute_write_cert("StateU_diploma_z__Joe_attr.der")
124ctxt.load_attribute_file("StateU_diploma_z__Joe_attr.der")
125print attr.string() 
126print attr.typed_string()
127print "\n"
128
129#################################################
130# Credential 4
131# [keyid:stateU].role:diploma([string:'psychology'],[integer:1956])
132#                                                   <- [keyid:maryann]
133param1=ABAC.DataTerm("string", "'psychology'")
134param2=ABAC.DataTerm("integer", "1956")
135head = ABAC.Role(stateU,"diploma")
136head.role_add_data_term(param1)
137head.role_add_data_term(param2)
138tail = ABAC.Role(maryann)
139attr=ABAC.Attribute(head, 1800)
140attr.attribute_add_tail(tail)
141attr.attribute_bake()
142attr.attribute_write_cert("StateU_diploma_p__Maryann_attr.der")
143ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der")
144print attr.string() 
145print attr.typed_string()
146print "\n"
147
Note: See TracBrowser for help on using the repository browser.