source: examples/python_tests/file_read_rt2/attr.py @ 212e99f

mei_rt2mei_rt2_fix_1 libabac-0.2.2apre-partial
Last change on this file since 212e99f was f824a9e, checked in by Mei <mei@…>, 12 years ago

1) add more doc to python_tests

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