source: examples/python_tests/leader_rt1/attr.py @ 928b1b7

mei_rt2mei_rt2_fix_1
Last change on this file since 928b1b7 was f824a9e, checked in by Mei <mei@…>, 13 years ago

1) add more doc to python_tests

  • Property mode set to 100755
File size: 3.5 KB
RevLine 
[7211a95]1#!/usr/bin/env python
2
3"""
[f824a9e]4See README in this directory for the semantics of the example.  This file
5constructs the credentials described and puts copies into this directory
[7211a95]6
7cmd1:env keystore=`pwd` ./attr.py
8"""
9
10import os
11import ABAC
12
13ctxt = ABAC.Context()
14print "ABAC version %s" % ctxt.version()
15
[f824a9e]16# Keystore is the directory containing the principal credentials.
17# Load existing principals and/or policy credentials
18if (os.environ.has_key("keystore")) :
19    keystore=os.environ["keystore"]
20    ctxt.load_directory(keystore)
[7211a95]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
[f824a9e]34# retrieve principals' keyid value from local credential files
[7211a95]35geniID=ABAC.ID("Geni_ID.pem");
36geniID.id_load_privkey_file("Geni_private.pem");
37geni=geniID.id_keyid()
38
39bobID=ABAC.ID("Bob_ID.pem");
40bobID.id_load_privkey_file("Bob_private.pem");
41bob=bobID.id_keyid()
42
43jackID=ABAC.ID("Jack_ID.pem");
44jackID.id_load_privkey_file("Jack_private.pem");
45jack=jackID.id_keyid()
46
47joeID=ABAC.ID("Joe_ID.pem");
48joeID.id_load_privkey_file("Joe_private.pem");
49joe=joeID.id_keyid()
50
51################################################
[f824a9e]52# Credential 1,
[7211a95]53# [keyid:geni].role:leader
54#         <- [keyid:geni].role:equivalent([principal:?P[keyid:geni].role:leader])
55head=ABAC.Role(geni,"leader")
[f824a9e]56
57# initialize the role constraint on a principlal
58condrole=ABAC.Role(geni,"leader")
59cond=ABAC.Constraint(condrole)
60
61# make the data term with the role constraint
[7211a95]62param=ABAC.DataTerm("principal","P", cond)
63tail = ABAC.Role(geni,"equivalent")
64tail.role_add_data_term(param)
[f824a9e]65
66# build he attribute policy
[7211a95]67attr=ABAC.Attribute(head, 1800)
68attr.attribute_add_tail(tail)
[f824a9e]69
70# finalize the policy
[7211a95]71attr.attribute_bake()
[f824a9e]72
73# write the policy out to a credential file
[7211a95]74attr.attribute_write_cert("geni_leader__geni_leader_qP_attr.der")
[f824a9e]75
76# load the policy into the context using the credential file
[7211a95]77ctxt.load_attribute_file("geni_leader__geni_leader_qP_attr.der")
78print attr.string() 
79print attr.typed_string()
80print "\n"
81
82#################################################
[f824a9e]83# Credential 2
[7211a95]84# [keyid:geni].role:leader <- [keyid:bob]
85head=ABAC.Role(geni,"leader")
86tail = ABAC.Role(bob)
87attr=ABAC.Attribute(head, 1800)
88attr.attribute_add_tail(tail)
89attr.attribute_bake()
90attr.attribute_write_cert("geni_leader__Bob_attr.der")
91ctxt.load_attribute_file("geni_leader__Bob_attr.der")
92print attr.string() 
93print attr.typed_string()
94print "\n"
95
96#################################################
[f824a9e]97# Credential 3
[7211a95]98# [keyid:geni].role:equivalent([keyid:bob]) <- [keyid:Joe]
99param=ABAC.DataTerm(bobID)
100head = ABAC.Role(geni,"equivalent")
101head.role_add_data_term(param)
102tail = ABAC.Role(joe)
103attr=ABAC.Attribute(head, 1800)
104attr.attribute_add_tail(tail)
105attr.attribute_bake()
106attr.attribute_write_cert("geni_equivalent_Bob__Joe_attr.der")
107ctxt.load_attribute_file("geni_equivalent_Bob__Joe_attr.der")
108print attr.string() 
109print attr.typed_string()
110print "\n"
111
112#################################################
[f824a9e]113# Credential 4
[7211a95]114# [keyid:geni].role:equivalent([keyid:Joe]) <- [keyid:Bob]
115param=ABAC.DataTerm(joeID)
116head = ABAC.Role(geni,"equivalent")
117head.role_add_data_term(param)
118tail = ABAC.Role(bob)
119attr=ABAC.Attribute(head, 1800)
120attr.attribute_add_tail(tail)
121attr.attribute_bake()
122attr.attribute_write_cert("geni_equivalent_Joe__Bob_attr.der")
123ctxt.load_attribute_file("geni_equivalent_Joe__Bob_attr.der")
124print attr.string() 
125print attr.typed_string()
126print "\n"
Note: See TracBrowser for help on using the repository browser.