source: examples/python_tests/fruits_rt2/attr.py @ 47d5cf9

mei_rt2mei_rt2_fix_1
Last change on this file since 47d5cf9 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.4 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
29ralphsID=ABAC.ID("Ralphs_ID.pem");
30ralphsID.id_load_privkey_file("Ralphs_private.pem");
31ralphs=ralphsID.id_keyid()
32
33bobID=ABAC.ID("Bob_ID.pem");
34bobID.id_load_privkey_file("Bob_private.pem");
35bob=bobID.id_keyid()
36
37maryID=ABAC.ID("Mary_ID.pem");
38maryID.id_load_privkey_file("Mary_private.pem");
39mary=maryID.id_keyid()
40
41################################################
42# [keyid:mary].oset:what2eat
43#      <- [keyid:ralphs].oset:fruitprice([float:?P:[..2.00]])
44# [keyid:alpha].role:access([string:'Read'],[urn:'file//fileB']) <- [keyid:bob]
45head = ABAC.Oset(mary,"what2eat")
46cond=ABAC.Constraint("float")
47cond.constraint_add_float_max(2.00)
48param=ABAC.DataTerm("float", "P", cond)
49tail = ABAC.Oset(ralphs,"fruitprice")
50tail.oset_add_data_term(param)
51attr=ABAC.Attribute(head, 1800)
52attr.attribute_add_tail(tail)
53attr.attribute_bake()
54attr.attribute_write_cert("mary_what2eat__ralphs_fruitprice_qP_attr.der")
55ctxt.load_attribute_file("mary_what2eat__ralphs_fruitprice_qP_attr.der")
56print attr.string() 
57print attr.typed_string()
58print "\n"
59
60################################################
61# [keyid:bob].oset:what2eat
62#      <- [keyid:ralphs].oset:fruitprice([float:?P:[1.00..5.00]])
63head = ABAC.Oset(bob,"what2eat")
64cond=ABAC.Constraint("float")
65cond.constraint_add_float_min(1.00)
66cond.constraint_add_float_max(5.00)
67print cond.typed_string()
68param=ABAC.DataTerm("float", "P", cond)
69tail = ABAC.Oset(ralphs,"fruitprice")
70tail.oset_add_data_term(param)
71attr=ABAC.Attribute(head, 1800)
72attr.attribute_add_tail(tail)
73attr.attribute_bake()
74attr.attribute_write_cert("bob_what2eat__ralphs_fruitprice_qP_attr.der")
75ctxt.load_attribute_file("bob_what2eat__ralphs_fruitprice_qP_attr.der")
76print attr.string() 
77print attr.typed_string()
78print "\n"
79
80#################################################
81# [keyid:ralphs].oset:fruitprice([float:1.50]) <- [string:'apple']
82param=ABAC.DataTerm("float", "1.50")
83head = ABAC.Oset(ralphs,"fruitprice")
84head.oset_add_data_term(param)
85param=ABAC.DataTerm("string", "'apple'")
86tail = ABAC.Oset(param)
87attr=ABAC.Attribute(head, 1800)
88attr.attribute_add_tail(tail)
89attr.attribute_bake()
90attr.attribute_write_cert("Ralphs_fruitprice__apple_attr.der")
91ctxt.load_attribute_file("Ralphs_fruitprice__apple_attr.der")
92print attr.string() 
93print attr.typed_string()
94print "\n"
95
96#################################################
97# [keyid:ralphs].oset:fruitprice([float:1.50]) <- [string:'kiwi']
98param=ABAC.DataTerm("float", "1.50")
99head = ABAC.Oset(ralphs,"fruitprice")
100head.oset_add_data_term(param)
101param=ABAC.DataTerm("string", "'kiwi'")
102tail = ABAC.Oset(param)
103attr=ABAC.Attribute(head, 1800)
104attr.attribute_add_tail(tail)
105attr.attribute_bake()
106attr.attribute_write_cert("Ralphs_fruitprice__kiwi_attr.der")
107ctxt.load_attribute_file("Ralphs_fruitprice__kiwi_attr.der")
108print attr.string() 
109print attr.typed_string()
110print "\n"
111
112#################################################
113# [keyid:ralphs].oset:fruitprice([float:2.50]) <- [string:'black berry']
114param=ABAC.DataTerm("float", "2.50")
115head = ABAC.Oset(ralphs,"fruitprice")
116head.oset_add_data_term(param)
117param=ABAC.DataTerm("string", "'black berry'")
118tail = ABAC.Oset(param)
119attr=ABAC.Attribute(head, 1800)
120attr.attribute_add_tail(tail)
121attr.attribute_bake()
122attr.attribute_write_cert("Ralphs_fruitprice__blackberry_attr.der")
123ctxt.load_attribute_file("Ralphs_fruitprice__blackberry_attr.der")
124print attr.string() 
125print attr.typed_string()
126print "\n"
127
128#################################################
129# [keyid:ralphs].oset:fruitprice([float:0.50]) <- [string:'navel orange']
130param=ABAC.DataTerm("float", "0.50")
131head = ABAC.Oset(ralphs,"fruitprice")
132head.oset_add_data_term(param)
133param=ABAC.DataTerm("string", "'navel orange'")
134tail = ABAC.Oset(param)
135attr=ABAC.Attribute(head, 1800)
136attr.attribute_add_tail(tail)
137attr.attribute_bake()
138attr.attribute_write_cert("Ralphs_fruitprice__navelorange_attr.der")
139ctxt.load_attribute_file("Ralphs_fruitprice__navelorange_attr.der")
140print attr.string() 
141print attr.typed_string()
142print "\n"
143
144##
145ctxt.dump_yap_db()
146##
Note: See TracBrowser for help on using the repository browser.