source: examples/python_tests/alumni_rt1/attr.py @ 20eaefe

mei_rt2mei_rt2_fix_1
Last change on this file since 20eaefe was 646e57e, checked in by Mei <mei@…>, 12 years ago

1) add partial proof

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