source: examples/python_tests/balltime_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: 5.1 KB
Line 
1#!/usr/bin/env python
2
3"""
4cmd1:env keystore=`pwd` ./attr.py
5"""
6
7import os
8import ABAC
9
10keystore=os.environ["keystore"]
11
12ctxt = ABAC.Context()
13print "ABAC version %s" % ctxt.version()
14
15ctxt.load_directory(keystore)
16
17out = ctxt.context_principals()
18print "...initial principal set..."
19for x in out[1]:
20    print "%s " % x.string()
21print "\n" 
22
23out = ctxt.context_credentials()
24print "...initial policy attribute set..."
25for c in out[1]:
26    print "%s <- %s" % (c.head_string(), c.tail_string())
27print "\n"
28
29leagueID=ABAC.ID("League_ID.pem");
30leagueID.id_load_privkey_file("League_private.pem");
31league=leagueID.id_keyid()
32
33johnID=ABAC.ID("John_ID.pem");
34johnID.id_load_privkey_file("John_private.pem");
35john=johnID.id_keyid()
36
37markID=ABAC.ID("Mark_ID.pem");
38markID.id_load_privkey_file("Mark_private.pem");
39mark=markID.id_keyid()
40
41################################################
42#[keyid:league].role:stadium([string:'access'],[boolean:true],
43#          [time:?F:[keyid:league].oset.gametime([string:?T])])
44#                <-[keyid:league].role:players([string:?T])
45param1=ABAC.DataTerm("string","'access'")
46param2=ABAC.DataTerm("boolean","true")
47
48param=ABAC.DataTerm("string","T")
49condoset=ABAC.Oset(league,"gametime")
50condoset.oset_add_data_term(param)
51cond=ABAC.Constraint(condoset)
52param3=ABAC.DataTerm("time", "F", cond)
53head = ABAC.Role(league,"stadium")
54head.role_add_data_term(param1)
55head.role_add_data_term(param2)
56head.role_add_data_term(param3)
57
58param=ABAC.DataTerm("string", "T")
59tail = ABAC.Role(league,"players")
60tail.role_add_data_term(param)
61attr=ABAC.Attribute(head, 1800)
62attr.attribute_add_tail(tail)
63attr.attribute_bake()
64attr.attribute_write_cert("League_access_qFqT__League_players_qT_attr.der")
65ctxt.load_attribute_file("League_access_qFqT__League_players_qT_attr.der")
66print attr.string() 
67print attr.typed_string()
68print "\n"
69
70################################################
71# ??? not sure if this is a valid policy
72#[keyid:league].role:stadium([string:'access'],[boolean:?B:[true],
73#              [time:?F:[20120228T080000..20120228T090000]])
74#               <- [keyid:league].role:players(string:?T)
75param1=ABAC.DataTerm("string","'access'")
76cond=ABAC.Constraint("boolean")
77cond.constraint_add_boolean_target("true")
78param2=ABAC.DataTerm("boolean", "B", cond)
79cond=ABAC.Constraint("time")
80cond.constraint_add_time_min("20120228T080000")
81cond.constraint_add_time_max("20120228T090000")
82param3=ABAC.DataTerm("time", "F", cond)
83head = ABAC.Role(league,"stadium")
84head.role_add_data_term(param1)
85head.role_add_data_term(param2)
86head.role_add_data_term(param3)
87
88param=ABAC.DataTerm("string", "T")
89tail = ABAC.Role(league,"players")
90tail.role_add_data_term(param)
91
92attr=ABAC.Attribute(head, 1800)
93attr.attribute_add_tail(tail)
94attr.attribute_bake()
95attr.attribute_write_cert("League_access_qR__League_players_qT_attr.der")
96ctxt.load_attribute_file("League_access_qR__League_players_qT_attr.der")
97print attr.string() 
98print attr.typed_string()
99print "\n"
100
101#################################################
102# [keyid:league].oset:gametime([string:'north'])
103#                    <- [time:20120228T130000]
104param=ABAC.DataTerm("string","'north'")
105head = ABAC.Oset(league,"gametime")
106head.oset_add_data_term(param)
107term=ABAC.DataTerm("time", "20120228T130000")
108tail = ABAC.Oset(term)
109attr=ABAC.Attribute(head, 1800)
110attr.attribute_add_tail(tail)
111attr.attribute_bake()
112attr.attribute_write_cert("League_gametime_north__timeT_attr.der")
113ctxt.load_attribute_file("League_gametime_north__timeT_attr.der")
114print attr.string() 
115print attr.typed_string()
116print "\n"
117
118#################################################
119# [keyid:league].oset:gametime([string:'south'])
120#                    <-[time:20120228T140000]
121param=ABAC.DataTerm("string","'south'")
122head = ABAC.Oset(league,"gametime")
123head.oset_add_data_term(param)
124term=ABAC.DataTerm("time", "20120228T140000")
125tail = ABAC.Oset(term)
126attr=ABAC.Attribute(head, 1800)
127attr.attribute_add_tail(tail)
128attr.attribute_bake()
129attr.attribute_write_cert("League_gametime_south__time2T_attr.der")
130ctxt.load_attribute_file("League_gametime_south__time2T_attr.der")
131print attr.string() 
132print attr.typed_string()
133print "\n"
134
135#################################################
136# [keyid:league].role:players([string:'north'])<-[keyid:John]
137param=ABAC.DataTerm("string", "'north'")
138head = ABAC.Role(league,"players")
139head.role_add_data_term(param)
140tail = ABAC.Role(john)
141attr=ABAC.Attribute(head, 1800)
142attr.attribute_add_tail(tail)
143attr.attribute_bake()
144attr.attribute_write_cert("League_players_north__John_attr.der")
145ctxt.load_attribute_file("League_players_north__John_attr.der")
146print attr.string()
147print attr.typed_string()
148print "\n"
149
150#################################################
151# [keyid:league].role:players([string:'south'])<-[keyid:Mark]
152param=ABAC.DataTerm("string", "'south'")
153head = ABAC.Role(league,"players")
154head.role_add_data_term(param)
155tail = ABAC.Role(mark)
156attr=ABAC.Attribute(head, 1800)
157attr.attribute_add_tail(tail)
158attr.attribute_bake()
159attr.attribute_write_cert("League_players_south__Mark_attr.der")
160ctxt.load_attribute_file("League_players_north__John_attr.der")
161print attr.string()
162print attr.typed_string()
163print "\n"
164
165
166ctxt.dump_yap_db()
167##
Note: See TracBrowser for help on using the repository browser.