source: examples/python_tests/file_read_rt2/attr.py @ 5f551d3

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

1) add more python examples

  • Property mode set to 100755
File size: 2.7 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
31alphaID=ABAC.ID("Alpha_ID.pem")
32alphaID.id_load_privkey_file("Alpha_private.pem");
33alpha=alphaID.id_keyid()
34
35bobID=ABAC.ID("Bob_ID.pem")
36bobID.id_load_privkey_file("Bob_private.pem");
37bob=bobID.id_keyid()
38
39maryannID=ABAC.ID("Maryann_ID.pem")
40maryannID.id_load_privkey_file("Maryann_private.pem")
41maryann=maryannID.id_keyid()
42
43joeID=ABAC.ID("Joe_ID.pem")
44joeID.id_load_privkey_file("Joe_private.pem")
45joe=joeID.id_keyid()
46
47
48################################################
49# [keyid:alpha].role:read([urn:?F])<-
50#    [keyid:alpha].role:managerOf([principal:?E[keyid:alpha].role:ownerOf([urn:?F])]
51param=ABAC.DataTerm("urn", "F")
52head = ABAC.Role(alpha,"read")
53head.role_add_data_term(param)
54param=ABAC.DataTerm("urn", "F")
55condoset=ABAC.Role(alpha,"ownerOf")
56condoset.role_add_data_term(param)
57cond=ABAC.Constraint(condoset)
58param=ABAC.DataTerm("principal", "E", cond)
59tail = ABAC.Role(alpha,"managerOf")
60tail.role_add_data_term(param)
61attr=ABAC.Attribute(head, 1800)
62attr.attribute_add_tail(tail)
63attr.attribute_bake()
64attr.attribute_write_cert("Alpha_read_qF__alpha_managerof_qE_attr.der")
65ctxt.load_attribute_file("Alpha_read_qF__alpha_managerof_qE_attr.der")
66print attr.string() 
67print attr.typed_string()
68print "\n"
69
70#################################################
71# [keyid:Alpha].role:managerOf([Keyid:Joe]) <- [keyid:Bob]
72#
73param=ABAC.DataTerm(joeID)
74role = ABAC.Role(alpha,"managerOf")
75role.role_add_data_term(param)
76tail = ABAC.Role(bob)
77attr=ABAC.Attribute(role, 1800)
78attr.attribute_add_tail(tail)
79attr.attribute_bake()
80attr.attribute_write_cert("Alpha_managerof_Joe__Bob_attr.der")
81ctxt.load_attribute_file("Alpha_managerof_Joe__Bob_attr.der")
82print attr.string() 
83print attr.typed_string()
84print "\n"
85
86#################################################
87#[keyid:Alpha].role:ownerOf([urn:'file://fileA']) <- [keyid:Joe]
88#
89param=ABAC.DataTerm("urn", "'file://fileA'")
90role = ABAC.Role(alpha,"ownerOf")
91role.role_add_data_term(param)
92tail = ABAC.Role(joe)
93attr=ABAC.Attribute(role, 1800)
94attr.attribute_add_tail(tail)
95attr.attribute_bake()
96attr.attribute_write_cert("Alpha_ownerof_fileA__Joe_attr.der")
97ctxt.load_attribute_file("Alpha_ownerof_fileA__Joe_attr.der")
98print attr.string() 
99print attr.typed_string()
100print "\n"
101
102
103ctxt.dump_yap_db()
104##
Note: See TracBrowser for help on using the repository browser.