X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Ftrust%2Fhierarchy.py;h=239240e21688178991160b3be40f7906d6b5dba3;hb=66af88fe4acfdf333930643ffc8184ad948ecd5f;hp=6ab509e19565276ba8b55c99ba5ae7c076d2205d;hpb=731ec9fdd043e15f1f1da3b4a42f0cb7a941f835;p=sfa.git diff --git a/sfa/trust/hierarchy.py b/sfa/trust/hierarchy.py index 6ab509e1..239240e2 100644 --- a/sfa/trust/hierarchy.py +++ b/sfa/trust/hierarchy.py @@ -12,18 +12,16 @@ # *.DBINFO - database info ## -### $Id$ -### $URL$ - import os -from sfa.util.report import * +from sfa.util.faults import MissingAuthority +from sfa.util.sfalogging import logger +from sfa.util.xrn import get_leaf, get_authority, hrn_to_urn, urn_to_hrn from sfa.trust.certificate import Keypair -from sfa.trust.credential import * +from sfa.trust.credential import Credential from sfa.trust.gid import GID, create_uuid -from sfa.util.namespace import * from sfa.util.config import Config -from sfa.util.sfaticket import SfaTicket +from sfa.trust.sfaticket import SfaTicket ## # The AuthInfo class contains the information for an authority. This information @@ -35,7 +33,6 @@ class AuthInfo: gid_filename = None privkey_filename = None dbinfo_filename = None - ## # Initialize and authority object. # @@ -115,8 +112,8 @@ class Hierarchy: # @param basedir the base directory to store the hierarchy in def __init__(self, basedir = None): + self.config = Config() if not basedir: - self.config = Config() basedir = os.path.join(self.config.SFA_DATA_DIR, "authorities") self.basedir = basedir ## @@ -161,14 +158,13 @@ class Hierarchy: def create_auth(self, xrn, create_parents=False): hrn, type = urn_to_hrn(xrn) - trace("Hierarchy: creating authority: " + hrn) + logger.debug("Hierarchy: creating authority: %s"% hrn) # create the parent authority if necessary parent_hrn = get_authority(hrn) parent_urn = hrn_to_urn(parent_hrn, 'authority') if (parent_hrn) and (not self.auth_exists(parent_urn)) and (create_parents): self.create_auth(parent_urn, create_parents) - (directory, gid_filename, privkey_filename, dbinfo_filename) = \ self.get_auth_filenames(hrn) @@ -181,7 +177,7 @@ class Hierarchy: pass if os.path.exists(privkey_filename): - print "using existing key", privkey_filename, "for authority", hrn + logger.debug("using existing key %r for authority %r"%(privkey_filename,hrn)) pkey = Keypair(filename = privkey_filename) else: pkey = Keypair(create = True) @@ -197,6 +193,32 @@ class Hierarchy: dbinfo_file.write(str(dbinfo)) dbinfo_file.close() + def create_top_level_auth(self, hrn=None): + """ + Create top level records (includes root and sub authorities (local/remote) + """ + if not hrn: + hrn = self.config.SFA_INTERFACE_HRN + # make sure parent exists + parent_hrn = get_authority(hrn) + if not parent_hrn: + parent_hrn = hrn + if not parent_hrn == hrn: + self.create_top_level_auth(parent_hrn) + + # create the authority if it doesnt alrady exist + if not self.auth_exists(hrn): + self.create_auth(hrn) + + + def get_interface_auth_info(self, create=True): + hrn = self.config.SFA_INTERFACE_HRN + if not self.auth_exists(hrn): + if create==True: + self.create_top_level_auth(hrn) + else: + raise MissingAuthority(hrn) + return self.get_auth_info(hrn) ## # Return the AuthInfo object for the specified authority. If the authority # does not exist, then an exception is thrown. As a side effect, disk files @@ -205,10 +227,9 @@ class Hierarchy: # @param xrn the human readable name of the authority to create (urn will be converted to hrn). def get_auth_info(self, xrn): - - #trace("Hierarchy: getting authority: " + hrn) hrn, type = urn_to_hrn(xrn) if not self.auth_exists(hrn): + logger.warning("Hierarchy: missing authority - xrn=%s, hrn=%s"%(xrn,hrn)) raise MissingAuthority(hrn) (directory, gid_filename, privkey_filename, dbinfo_filename) = \ @@ -233,15 +254,29 @@ class Hierarchy: # @param uuid the unique identifier to store in the GID # @param pkey the public key to store in the GID - def create_gid(self, xrn, uuid, pkey): + def create_gid(self, xrn, uuid, pkey, CA=False): hrn, type = urn_to_hrn(xrn) + if not type: + type = 'authority' + parent_hrn = get_authority(hrn) # Using hrn_to_urn() here to make sure the urn is in the right format # If xrn was a hrn instead of a urn, then the gid's urn will be # of type None urn = hrn_to_urn(hrn, type) gid = GID(subject=hrn, uuid=uuid, hrn=hrn, urn=urn) + # is this a CA cert + if hrn == self.config.SFA_INTERFACE_HRN or not parent_hrn: + # root or sub authority + gid.set_intermediate_ca(True) + elif type and 'authority' in type: + # authority type + gid.set_intermediate_ca(True) + elif CA: + gid.set_intermediate_ca(True) + else: + gid.set_intermediate_ca(False) - parent_hrn = get_authority(hrn) + # set issuer if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN: # if there is no parent hrn, then it must be self-signed. this # is where we terminate the recursion @@ -251,7 +286,6 @@ class Hierarchy: parent_auth_info = self.get_auth_info(parent_hrn) gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn) gid.set_parent(parent_auth_info.get_gid_object()) - gid.set_intermediate_ca(True) gid.set_pubkey(pkey) gid.encode()