#!/usr/bin/env python """ See README in this directory for the semantics of the example. This file constructs the credentials described and puts copies into this directory cmd1:env keystore=`pwd` ./attr.py """ import os import ABAC ctxt = ABAC.Context() print "ABAC version %s" % ctxt.version() # Keystore is the directory containing the principal credentials. # Load existing principals and/or policy credentials if (os.environ.has_key("keystore")) : keystore=os.environ["keystore"] ctxt.load_directory(keystore) out = ctxt.context_principals() print "...initial principal set..." for x in out[1]: print "%s " % x.string() print "\n" out = ctxt.context_credentials() print "...initial policy attribute set..." for c in out[1]: print "%s <- %s" % (c.head_string(), c.tail_string()) print "\n" # retrieve principals' keyid value from local credential files stateUID=ABAC.ID("StateU_ID.pem") stateUID.id_load_privkey_file("StateU_private.pem") stateU=stateUID.id_keyid() bobID=ABAC.ID("Bob_ID.pem") bobID.id_load_privkey_file("Bob_private.pem") bob=bobID.id_keyid() joeID=ABAC.ID("Joe_ID.pem") joeID.id_load_privkey_file("Joe_private.pem") joe=joeID.id_keyid() maryannID=ABAC.ID("Maryann_ID.pem") maryannID.id_load_privkey_file("Maryann_private.pem") maryann=maryannID.id_keyid() ################################################ # Credential 1, the year is constrainted by a range constraint with min and max # values # [keyid:stateU].role:foundingAlumni # <- [keyid:stateU].role:diploma([?], [integer:?Year:[1955 .. 1958]]) head = ABAC.Role(stateU,"foundingAlumni") # create an anonymous parameter that will take any type of major param1=ABAC.DataTerm("anonymous", "_") # initialize a constraint with integer type cond=ABAC.Constraint("integer") # set the bounding min and max value for this constraint cond.constraint_add_integer_min(1955) cond.constraint_add_integer_max(1958) # make the parameter with this integer constraint param2=ABAC.DataTerm("integer", "Year", cond) tail = ABAC.Role(stateU,"diploma") tail.role_add_data_term(param1) tail.role_add_data_term(param2) attr=ABAC.Attribute(head, 1800) # build up the policy attr.attribute_add_tail(tail) # finalize the policy attr.attribute_bake() # write the policy out into the file system attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") # load this policy into the context using this external credential file ctxt.load_attribute_file("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") print attr.string() print attr.typed_string() print "\n" ################################################# # Credential 2 # [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1960]) # <- [keyid:bob] param1=ABAC.DataTerm("string", "'mathmatics'") param2=ABAC.DataTerm("integer", "1960") head = ABAC.Role(stateU,"diploma") head.role_add_data_term(param1) head.role_add_data_term(param2) tail = ABAC.Role(bob) attr=ABAC.Attribute(head, 1800) attr.attribute_add_tail(tail) attr.attribute_bake() attr.attribute_write_cert("StateU_diploma_m__Bob_attr.der") ctxt.load_attribute_file("StateU_diploma_m__Bob_attr.der") print attr.string() print attr.typed_string() print "\n" ################################################# # Credential 3 # [keyid:stateU].role:diploma([string:'zoology'],[integer:1955]) # <- [keyid:joe] param1=ABAC.DataTerm("string", "'zoology'") param2=ABAC.DataTerm("integer", "1955") head = ABAC.Role(stateU,"diploma") head.role_add_data_term(param1) head.role_add_data_term(param2) tail = ABAC.Role(joe) attr=ABAC.Attribute(head, 1800) attr.attribute_add_tail(tail) attr.attribute_bake() attr.attribute_write_cert("StateU_diploma_z__Joe_attr.der") ctxt.load_attribute_file("StateU_diploma_z__Joe_attr.der") print attr.string() print attr.typed_string() print "\n" ################################################# # Credential 4 # [keyid:stateU].role:diploma([string:'psychology'],[integer:1956]) # <- [keyid:maryann] param1=ABAC.DataTerm("string", "'psychology'") param2=ABAC.DataTerm("integer", "1956") head = ABAC.Role(stateU,"diploma") head.role_add_data_term(param1) head.role_add_data_term(param2) tail = ABAC.Role(maryann) attr=ABAC.Attribute(head, 1800) attr.attribute_add_tail(tail) attr.attribute_bake() attr.attribute_write_cert("StateU_diploma_p__Maryann_attr.der") ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der") print attr.string() print attr.typed_string() print "\n"