source: examples/python_tests/alumni2_rt1/attr.py @ f824a9e

mei_rt2mei_rt2_fix_1
Last change on this file since f824a9e 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.4 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, Any alumni of stateU is a member of foundingAlumni if
54# they have diploma from those specific years
55# [keyid:stateU].role:foundingAlumni
56#       <- [keyid:stateU].role:diploma([?], [integer:?Year:[1960,1961,1963]])
57head = ABAC.Role(stateU,"foundingAlumni")
58
59# constructing an anonymous parameter
60param1=ABAC.DataTerm("anonymous", "_")
61
62# initialize a integer range constraint
63cond=ABAC.Constraint("integer")
64
65# add specific target value for the integer range constraint
66cond.constraint_add_integer_target(1960)
67cond.constraint_add_integer_target(1961)
68cond.constraint_add_integer_target(1963)
69
70# create a paramter term that has a range constraint on it,
71# the variable name supplied has to start with a capitalized alphabet character
72param2=ABAC.DataTerm("integer", "Year", cond)
73tail = ABAC.Role(stateU,"diploma")
74tail.role_add_data_term(param1)
75tail.role_add_data_term(param2)
76attr=ABAC.Attribute(head, 1800)
77
78# construct the attribute
79attr.attribute_add_tail(tail)
80
81# finalize the attribute
82attr.attribute_bake()
83attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der")
84ctxt.load_attribute_file("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der")
85print attr.string() 
86print attr.typed_string()
87print "\n"
88
89#################################################
90# Credential 2
91# [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1961]) <- [keyid:bob]
92param1=ABAC.DataTerm("string", "'mathmatics'")
93param2=ABAC.DataTerm("integer", "1961")
94head = ABAC.Role(stateU,"diploma")
95head.role_add_data_term(param1)
96head.role_add_data_term(param2)
97tail = ABAC.Role(bob)
98attr=ABAC.Attribute(head, 1800)
99attr.attribute_add_tail(tail)
100attr.attribute_bake()
101attr.attribute_write_cert("StateU_diploma_m__Bob_attr.der")
102ctxt.load_attribute_file("StateU_diploma_m__Bob_attr.der")
103print attr.string() 
104print attr.typed_string()
105print "\n"
106
107#################################################
108# Credential 3
109# [keyid:stateU].role:diploma([string:'zoology'],[integer:1955]) <- [keyid:joe]
110param1=ABAC.DataTerm("string", "'zoology'")
111param2=ABAC.DataTerm("integer", "1955")
112head = ABAC.Role(stateU,"diploma")
113head.role_add_data_term(param1)
114head.role_add_data_term(param2)
115tail = ABAC.Role(joe)
116attr=ABAC.Attribute(head, 1800)
117attr.attribute_add_tail(tail)
118attr.attribute_bake()
119attr.attribute_write_cert("StateU_diploma_z__Joe_attr.der")
120ctxt.load_attribute_file("StateU_diploma_z__Joe_attr.der")
121print attr.string() 
122print attr.typed_string()
123print "\n"
124
125#################################################
126# Credential 4
127# [keyid:stateU].role:diploma([string:'psychology'],[integer:1962]) <- [keyid:maryann]
128param1=ABAC.DataTerm("string", "'psychology'")
129param2=ABAC.DataTerm("integer", "1962")
130head = ABAC.Role(stateU,"diploma")
131head.role_add_data_term(param1)
132head.role_add_data_term(param2)
133tail = ABAC.Role(maryann)
134attr=ABAC.Attribute(head, 1800)
135attr.attribute_add_tail(tail)
136attr.attribute_bake()
137attr.attribute_write_cert("StateU_diploma_p__Maryann_attr.der")
138ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der")
139print attr.string() 
140print attr.typed_string()
141print "\n"
142
Note: See TracBrowser for help on using the repository browser.