source: examples/python_tests/access_rt2/attr.py @ 09496b3

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

1) add more doc to python_tests

  • Property mode set to 100755
File size: 4.7 KB
RevLine 
[5110d42]1#!/usr/bin/env python
2
3"""
[f824a9e]4See README in this directory for the semantics of the example.  This file
5creates a principal(Joe) and constructs the credentials described and puts
6copies into this directory
[5110d42]7
8cmd1:env keystore=`pwd` ./attr.py
9"""
10
11import os
12import ABAC
13
14ctxt = ABAC.Context()
15print "ABAC version %s" % ctxt.version()
16
[f824a9e]17# Keystore is the directory containing the principal credentials.
18# Load existing principals and/or policy credentials
19if (os.environ.has_key("keystore")) :
20    keystore=os.environ["keystore"]
21    ctxt.load_directory(keystore)
[5110d42]22
[47d5cf9]23# Print the principals and credentials in the keystore
[5110d42]24out = ctxt.context_principals()
25print "...initial principal set..."
26for x in out[1]:
27    print "%s " % x.string()
28print "\n" 
29
30out = ctxt.context_credentials()
31print "...initial policy attribute set..."
32for c in out[1]:
33    print "%s <- %s" % (c.head_string(), c.tail_string())
34print "\n"
35
[47d5cf9]36# Construct a "Joe" principal and load it into the ABAC context
[5110d42]37joeID=ABAC.ID("Joe", 0)
38ctxt.load_id(joeID)
[47d5cf9]39# Write out the Joe Principal to 2 files - one for the key and one for the
40# identity.  The identity can be shared, but the key must be kept secret.
[5d06689]41joeID.id_write_privkey("Joe_private.pem")
42joeID.id_write_cert("Joe_ID.pem")
[f824a9e]43# Keep a copy of Joe's key identifier as a string.
[5d06689]44joe=joeID.id_keyid()
[5110d42]45
[f824a9e]46# Load alpha and bob from local files (created by ./setup.py)
[5110d42]47alphaID=ABAC.ID("Alpha_ID.pem")
[5d06689]48alphaID.id_load_privkey_file("Alpha_private.pem");
49alpha=alphaID.id_keyid()
[5110d42]50
51bobID=ABAC.ID("Bob_ID.pem")
[5d06689]52bobID.id_load_privkey_file("Bob_private.pem");
53bob=bobID.id_keyid()
[5110d42]54
55################################################
[47d5cf9]56# Create the credential
[5110d42]57# [keyid:alpha].role:access([string:'Read'],[urn:'file//fileB']) <- [keyid:bob]
58param1=ABAC.DataTerm("string", "'Read'")
59param2=ABAC.DataTerm("urn","'file//fileB'")
[669b481]60head = ABAC.Role(alpha,"access")
[47d5cf9]61
62# Attach the parameters to the access role
[669b481]63head.role_add_data_term(param1)
64head.role_add_data_term(param2)
65tail = ABAC.Role(bob)
[47d5cf9]66
67# Hook the head to the tail
[669b481]68attr=ABAC.Attribute(head, 1800)
69attr.attribute_add_tail(tail)
[47d5cf9]70
71# create the credential
[5d06689]72attr.attribute_bake()
[47d5cf9]73
74# Save a copy and add the credential to the context
[5d06689]75attr.attribute_write_cert("Alpha_access_fileB__Bob_attr.der")
[5110d42]76ctxt.load_attribute_file("Alpha_access_fileB__Bob_attr.der")
77print attr.string() 
78print attr.typed_string()
79print "\n"
80
[47d5cf9]81# Constructing the others are similar
82
[5110d42]83#################################################
84## [keyid:alpha].role:team([string:'proj1'])<-[keyid:bob]
85param1=ABAC.DataTerm("string", "'proj1'")
[669b481]86head = ABAC.Role(alpha,"team")
87head.role_add_data_term(param1)
[5110d42]88tail = ABAC.Role(bob)
[669b481]89attr=ABAC.Attribute(head, 1800)
[5d06689]90attr.attribute_add_tail(tail)
91attr.attribute_bake()
92attr.attribute_write_cert("Alpha_team_proj1__Bob_attr.der")
[5110d42]93ctxt.load_attribute_file("Alpha_team_proj1__Bob_attr.der")
94print attr.string() 
95print attr.typed_string()
96print "\n"
97
98#################################################
99## [keyid:alpha].role:team([string:'proj2'])<-[keyid:Joe]
100param1=ABAC.DataTerm("string", "'proj2'")
[669b481]101head = ABAC.Role(alpha,"team")
102head.role_add_data_term(param1)
[5110d42]103tail = ABAC.Role(joe)
[669b481]104attr=ABAC.Attribute(head, 1800)
[5d06689]105attr.attribute_add_tail(tail)
106attr.attribute_bake()
107attr.attribute_write_cert("Alpha_team_proj2__Joe_attr.der")
[5110d42]108ctxt.load_attribute_file("Alpha_team_proj2__Joe_attr.der")
109print attr.string() 
110print attr.typed_string()
111print "\n"
112
113################################################
114# [keyid:alpha].role:access([string:'Read',
115#                [urn:?F[keyid:alpha].oset:documents([string:?P])])
116#                                 <- [keyid:alpha].role:team([string:?P])
117param=ABAC.DataTerm("string", "P")
118oset=ABAC.Oset(alpha,"documents")
[5d06689]119oset.oset_add_data_term(param)
[5110d42]120cond=ABAC.Constraint(oset)
121param2=ABAC.DataTerm("urn", "F", cond)
122param1=ABAC.DataTerm("string", "'Read'")
123head = ABAC.Role(alpha,"access")
[5d06689]124head.role_add_data_term(param1)
125head.role_add_data_term(param2)
[5110d42]126param3=ABAC.DataTerm("string", "P")
127tail = ABAC.Role(alpha,"team")
[5d06689]128tail.role_add_data_term(param3)
[5110d42]129attr=ABAC.Attribute(head, 1800)
[5d06689]130attr.attribute_add_tail(tail)
131attr.attribute_bake()
132attr.attribute_write_cert("Alpha_access_qFqP__alpha_team_qP_attr.der")
[5110d42]133ctxt.load_attribute(attr)
[f824a9e]134print attr.string() 
135print attr.typed_string()
136print "\n"
[5110d42]137
138
139#################################################
140## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileA']
141param1=ABAC.DataTerm("string", "'proj1'")
[669b481]142head = ABAC.Oset(alpha,"documents")
143head.oset_add_data_term(param1)
[5110d42]144obj = ABAC.DataTerm("urn", "'file//fileA'")
145tail= ABAC.Oset(obj)
[669b481]146attr=ABAC.Attribute(head, 1800)
[5d06689]147attr.attribute_add_tail(tail)
148attr.attribute_bake()
149attr.attribute_write_cert("Alpha_team_proj1__fileA_attr.der")
150ctxt.load_attribute(attr)
[5110d42]151print attr.string() 
152print attr.typed_string()
153print "\n"
154
Note: See TracBrowser for help on using the repository browser.