source: examples/python_tests/file_read_rt2/attr.py @ accd63d

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

1) add partial proof

  • Property mode set to 100755
File size: 3.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)
21else:
22    print("keystore is not set...")
23    exit(1)
24
25
26out = ctxt.context_principals()
27print "...initial principal set..."
28for x in out[1]:
29    print "%s " % x.string()
30print "\n" 
31
32out = ctxt.context_credentials()
33print "...initial policy attribute set..."
34for c in out[1]:
35    print "%s <- %s" % (c.head_string(), c.tail_string())
36print "\n"
37
38# retrieve principals' keyid value from local credential files
39alphaID=ABAC.ID("Alpha_ID.pem")
40alphaID.id_load_privkey_file("Alpha_private.pem");
41alpha=alphaID.id_keyid()
42
43bobID=ABAC.ID("Bob_ID.pem")
44bobID.id_load_privkey_file("Bob_private.pem");
45bob=bobID.id_keyid()
46
47maryannID=ABAC.ID("Maryann_ID.pem")
48maryannID.id_load_privkey_file("Maryann_private.pem")
49maryann=maryannID.id_keyid()
50
51joeID=ABAC.ID("Joe_ID.pem")
52joeID.id_load_privkey_file("Joe_private.pem")
53joe=joeID.id_keyid()
54
55
56################################################
57# Credential 1, demostrates role constraint on a file,
58#               manager of the owner of a file can also read that file
59# [keyid:alpha].role:read([urn:?F])<-
60#    [keyid:alpha].role:managerOf([principal:?E[keyid:alpha].role:ownerOf([urn:?F])]
61param=ABAC.DataTerm("urn", "F")
62head = ABAC.Role(alpha,"read")
63head.role_add_data_term(param)
64
65# create variable data term
66param=ABAC.DataTerm("urn", "F")
67
68# create the constraining role structure
69condrole=ABAC.Role(alpha,"ownerOf")
70condrole.role_add_data_term(param)
71
72# create the constraint
73cond=ABAC.Constraint(condrole)
74
75# build the parameter with constraint
76param=ABAC.DataTerm("principal", "E", cond)
77tail = ABAC.Role(alpha,"managerOf")
78tail.role_add_data_term(param)
79
80# compose attribute policy
81attr=ABAC.Attribute(head, 1800)
82attr.attribute_add_tail(tail)
83
84# finalize the policy
85attr.attribute_bake()
86
87# write out to external file
88attr.attribute_write_cert("Alpha_read_qF__alpha_managerof_qE_attr.der")
89ctxt.load_attribute_file("Alpha_read_qF__alpha_managerof_qE_attr.der")
90print attr.string() 
91print attr.typed_string()
92print "\n"
93
94#################################################
95# Credential 2, Bob is Joe's manager
96# [keyid:Alpha].role:managerOf([Keyid:Joe]) <- [keyid:Bob]
97#
98param=ABAC.DataTerm(joeID)
99role = ABAC.Role(alpha,"managerOf")
100role.role_add_data_term(param)
101tail = ABAC.Role(bob)
102attr=ABAC.Attribute(role, 1800)
103attr.attribute_add_tail(tail)
104attr.attribute_bake()
105attr.attribute_write_cert("Alpha_managerof_Joe__Bob_attr.der")
106ctxt.load_attribute_file("Alpha_managerof_Joe__Bob_attr.der")
107print attr.string() 
108print attr.typed_string()
109print "\n"
110
111#################################################
112# Credential 3, Joe is file's owner
113#[keyid:Alpha].role:ownerOf([urn:'file://fileA']) <- [keyid:Joe]
114#
115param=ABAC.DataTerm("urn", "'file://fileA'")
116role = ABAC.Role(alpha,"ownerOf")
117role.role_add_data_term(param)
118tail = ABAC.Role(joe)
119attr=ABAC.Attribute(role, 1800)
120attr.attribute_add_tail(tail)
121attr.attribute_bake()
122attr.attribute_write_cert("Alpha_ownerof_fileA__Joe_attr.der")
123ctxt.load_attribute_file("Alpha_ownerof_fileA__Joe_attr.der")
124print attr.string() 
125print attr.typed_string()
126print "\n"
127
Note: See TracBrowser for help on using the repository browser.