source: examples/python_tests/access_rt2/attr.py @ 5110d42

mei_rt2mei_rt2_fix_1
Last change on this file since 5110d42 was 5110d42, checked in by Mei <mei@…>, 12 years ago

1) reorganized the test directory to include python tests
2) attribute via api and principal via api from python scripts is

working (although there is a annoying seg fault at the very end
that must be related to something not been dup()ed.. need to wait
for c example to debug it)

3) able to query via api
4) replicated access_rt2 example in python and the query result matches
5) expanded api to make it easier to generate rt2 structure

  • Property mode set to 100755
File size: 4.0 KB
Line 
1#!/usr/bin/env python
2
3"""
4  to test with python
5
6cmd1:env keystore=`pwd` ./attr.py
7expect this to fail,
8cmd2: env ABAC_CN=1 keystore=`pwd` ./attr.py
9
10"""
11
12import os
13import ABAC
14
15keystore=os.environ["keystore"]
16
17ctxt = ABAC.Context()
18print "ABAC version %s" % ctxt.version()
19
20ctxt.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
34joeID=ABAC.ID("Joe", 0)
35ctxt.load_id(joeID)
36#joeID.write_privkey("Joe_IDKEY.pem")
37#joeID.write_cert("Joe_IDKEY.pem")
38joeID.write_privkey("Joe_private.pem")
39joeID.write_cert("Joe_ID.pem")
40joe=joeID.keyid()
41
42#ctxt.load_id_file("Alpha_ID.pem","Alpha_private.pem")
43alphaID=ABAC.ID("Alpha_ID.pem")
44alphaID.load_privkey("Alpha_private.pem");
45alpha=alphaID.keyid()
46
47#ctxt.load_id_file("Bob_ID.pem","Bob_private.pem")
48bobID=ABAC.ID("Bob_ID.pem")
49bobID.load_privkey("Bob_private.pem");
50bob=bobID.keyid()
51
52################################################
53# [keyid:alpha].role:access([string:'Read'],[urn:'file//fileB']) <- [keyid:bob]
54param1=ABAC.DataTerm("string", "'Read'")
55param2=ABAC.DataTerm("urn","'file//fileB'")
56role = ABAC.Role(alpha,"access")
57role.add_data_term(param1)
58role.add_data_term(param2)
59p = ABAC.Role(bob)
60attr=ABAC.Attribute(role, 1800)
61attr.add_tail(p)
62attr.bake()
63attr.write_cert("Alpha_access_fileB__Bob_attr.der")
64ctxt.load_attribute_file("Alpha_access_fileB__Bob_attr.der")
65print attr.string() 
66print attr.typed_string()
67print "\n"
68
69#################################################
70## [keyid:alpha].role:team([string:'proj1'])<-[keyid:bob]
71param1=ABAC.DataTerm("string", "'proj1'")
72role = ABAC.Role(alpha,"team")
73role.add_data_term(param1)
74tail = ABAC.Role(bob)
75attr=ABAC.Attribute(role, 1800)
76attr.add_tail(tail)
77attr.bake()
78attr.write_cert("Alpha_team_proj1__Bob_attr.der")
79ctxt.load_attribute_file("Alpha_team_proj1__Bob_attr.der")
80print attr.string() 
81print attr.typed_string()
82print "\n"
83
84#################################################
85## [keyid:alpha].role:team([string:'proj2'])<-[keyid:Joe]
86param1=ABAC.DataTerm("string", "'proj2'")
87role = ABAC.Role(alpha,"team")
88role.add_data_term(param1)
89tail = ABAC.Role(joe)
90attr=ABAC.Attribute(role, 1800)
91attr.add_tail(tail)
92attr.bake()
93attr.write_cert("Alpha_team_proj2__Joe_attr.der")
94ctxt.load_attribute_file("Alpha_team_proj2__Joe_attr.der")
95print attr.string() 
96print attr.typed_string()
97print "\n"
98
99## bad beause of that constraint..
100################################################
101# [keyid:alpha].role:access([string:'Read',
102#                [urn:?F[keyid:alpha].oset:documents([string:?P])])
103#                                 <- [keyid:alpha].role:team([string:?P])
104param=ABAC.DataTerm("string", "P")
105oset=ABAC.Oset(alpha,"documents")
106oset.add_data_term(param)
107cond=ABAC.Constraint(oset)
108param2=ABAC.DataTerm("urn", "F", cond)
109param1=ABAC.DataTerm("string", "'Read'")
110head = ABAC.Role(alpha,"access")
111head.add_data_term(param1)
112head.add_data_term(param2)
113param3=ABAC.DataTerm("string", "P")
114tail = ABAC.Role(alpha,"team")
115tail.add_data_term(param3)
116
117attr=ABAC.Attribute(head, 1800)
118attr.add_tail(tail)
119attr.bake()
120attr.write_cert("Alpha_access_qFqP__alpha_team_qP_attr.der")
121#ctxt.load_attribute_file("Alpha_access_qFqP__alpha_team_qP_attr.der")
122ctxt.load_attribute(attr)
123#print attr.string()
124#print attr.typed_string()
125#print "\n"
126
127
128#################################################
129## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileA']
130param1=ABAC.DataTerm("string", "'proj1'")
131oset = ABAC.Oset(alpha,"documents")
132oset.add_data_term(param1)
133obj = ABAC.DataTerm("urn", "'file//fileA'")
134tail= ABAC.Oset(obj)
135attr=ABAC.Attribute(oset, 1800)
136attr.add_tail(tail)
137attr.bake()
138attr.write_cert("Alpha_team_proj1__fileA_attr.der")
139ctxt.load_attribute_file("Alpha_team_proj1__fileA_attr.der")
140print attr.string() 
141print attr.typed_string()
142print "\n"
143
144#ctxt.dump_yap()
145##
Note: See TracBrowser for help on using the repository browser.