source: examples/python_tests/access_rt2/attr.py @ 7211a95

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

1) add more python examples
2) add the missing linking role and linking oset api calls
3) fix the output of time typed data term/oset obj in typed_string format

(transform back from yap time format to our ddddddddTdddddd format

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