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

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

1) add partial proof

  • Property mode set to 100755
File size: 4.6 KB
RevLine 
[669b481]1#!/usr/bin/env python
2
3"""
[f824a9e]4See README in this directory for the semantics of the example.  This file
5constructs the credentials described and puts copies into this directory
[669b481]6
7cmd1:env keystore=`pwd` ./attr.py
8"""
9
10import os
11import ABAC
12
13ctxt = ABAC.Context()
14print "ABAC version %s" % ctxt.version()
15
[f824a9e]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)
[646e57e]21else:
22    print("keystore is not set...")
23    exit(1)
[669b481]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
[f824a9e]37# retrieve principals' keyid value from local credential files
[669b481]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################################################
[f824a9e]56# Credential 1, the year is constrainted by a range constraint with min and max
57#               values
[669b481]58# [keyid:stateU].role:foundingAlumni
[f824a9e]59#          <- [keyid:stateU].role:diploma([?], [integer:?Year:[1955 .. 1958]])
[669b481]60head = ABAC.Role(stateU,"foundingAlumni")
[f824a9e]61
62# create an anonymous parameter that will take any type of major
[669b481]63param1=ABAC.DataTerm("anonymous", "_")
[f824a9e]64
65# initialize a constraint with integer type
[669b481]66cond=ABAC.Constraint("integer")
[f824a9e]67
68# set the bounding min and max value for this constraint
[669b481]69cond.constraint_add_integer_min(1955)
70cond.constraint_add_integer_max(1958)
[f824a9e]71
72# make the parameter with this integer constraint
[669b481]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)
[f824a9e]78
79# build up the policy
[669b481]80attr.attribute_add_tail(tail)
[f824a9e]81
82# finalize the policy
[669b481]83attr.attribute_bake()
[f824a9e]84
85# write the policy out into the file system
[669b481]86attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der")
[f824a9e]87
88# load this policy into the context using this external credential file
[669b481]89ctxt.load_attribute_file("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der")
90print attr.string() 
91print attr.typed_string()
92print "\n"
93
94#################################################
[f824a9e]95# Credential 2
96# [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1960])
97#                                                            <- [keyid:bob]
[669b481]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#################################################
[f824a9e]114# Credential 3
115# [keyid:stateU].role:diploma([string:'zoology'],[integer:1955])
116#                                                     <- [keyid:joe]
[669b481]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#################################################
[f824a9e]133# Credential 4
134# [keyid:stateU].role:diploma([string:'psychology'],[integer:1956])
135#                                                   <- [keyid:maryann]
[669b481]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.