#!/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() markID=ABAC.ID("Mark_ID.pem") markID.id_load_privkey_file("Mark_private.pem") mark=markID.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() janID=ABAC.ID("Jan_ID.pem") janID.id_load_privkey_file("Jan_private.pem") jan=janID.id_keyid() ################################################ # Credential 1, this policy has two range constraints on different parameters # [keyid:stateU].role:foundingAlumni # <- [keyid:stateU].role:diploma([string:?D:['mathmatics','psychology']], # [integer:?Year:[1960,1961,1963]]) head = ABAC.Role(stateU,"foundingAlumni") # initialize a string range constraint cond=ABAC.Constraint("string") # add specific string values to the constraint cond.constraint_add_string_target("'mathmatics'") cond.constraint_add_string_target("'psychology'") # create the parameter with the string range constraint param1=ABAC.DataTerm("string", "D", cond) # initialize another constratnt that is of integer type cond=ABAC.Constraint("integer") # add specific integer values to the constraint cond.constraint_add_integer_target(1960) cond.constraint_add_integer_target(1961) cond.constraint_add_integer_target(1963) # create the parameter with the integer range constraint param2=ABAC.DataTerm("integer", "Year", cond) tail = ABAC.Role(stateU,"diploma") # add the parameter with conditions to a role tail.role_add_data_term(param1) tail.role_add_data_term(param2) # build up the policy rule attr=ABAC.Attribute(head, 1800) attr.attribute_add_tail(tail) # finalize the policy rule attr.attribute_bake() # save it to a credential file attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") 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:1961]) <- [keyid:bob] param1=ABAC.DataTerm("string", "'mathmatics'") param2=ABAC.DataTerm("integer", "1961") 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:'mathmatics'],[integer:1965]) <- [keyid:mark] param1=ABAC.DataTerm("string", "'mathmatics'") param2=ABAC.DataTerm("integer", "1965") head = ABAC.Role(stateU,"diploma") head.role_add_data_term(param1) head.role_add_data_term(param2) tail = ABAC.Role(mark) attr=ABAC.Attribute(head, 1800) attr.attribute_add_tail(tail) attr.attribute_bake() attr.attribute_write_cert("StateU_diploma_m__Mark_attr.der") ctxt.load_attribute_file("StateU_diploma_m__Mark_attr.der") print attr.string() print attr.typed_string() print "\n" ################################################# # Credential 4 # [keyid:stateU].role:diploma([string:'zoology'],[integer:1961]) <- [keyid:joe] param1=ABAC.DataTerm("string", "'zoology'") param2=ABAC.DataTerm("integer", "1961") 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 5 # [keyid:stateU].role:diploma([string:'psychology'],[integer:1962]) # <- [keyid:maryann] param1=ABAC.DataTerm("string", "'psychology'") param2=ABAC.DataTerm("integer", "1962") 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" ################################################# # Credential 6 # [keyid:stateU].role:diploma([string:'psychology'],[integer:1960]) # <- [keyid:jan] param1=ABAC.DataTerm("string", "'psychology'") 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(jan) attr=ABAC.Attribute(head, 1800) attr.attribute_add_tail(tail) attr.attribute_bake() attr.attribute_write_cert("StateU_diploma_p__Jan_attr.der") ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der") print attr.string() print attr.typed_string() print "\n"