X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fmanagers%2Fregistry_manager.py;h=518072b0f938b42785d2c6e7fdd88c1898bbef06;hb=c900832bcc4f4dddceeac1978d56e5f10e0e2bb5;hp=03d8b6d6179b41097e9a66486aa3e51d3dfa7f7d;hpb=d6f7538217350e80c6f6728dcf971bb14f2dab2a;p=sfa.git diff --git a/sfa/managers/registry_manager.py b/sfa/managers/registry_manager.py index 03d8b6d6..518072b0 100644 --- a/sfa/managers/registry_manager.py +++ b/sfa/managers/registry_manager.py @@ -18,7 +18,7 @@ from sfa.trust.credential import Credential from sfa.trust.certificate import Certificate, Keypair, convert_public_key from sfa.trust.gid import create_uuid -from sfa.storage.persistentobjs import make_record,RegRecord +from sfa.storage.model import make_record, RegRecord, RegAuthority, RegUser, RegSlice, RegKey from sfa.storage.alchemy import dbsession class RegistryManager: @@ -37,7 +37,7 @@ class RegistryManager: 'urn':xrn.get_urn(), 'peers':peers}) - def GetCredential(self, api, xrn, type, is_self=False): + def GetCredential(self, api, xrn, type, caller_xrn=None): # convert xrn to hrn if type: hrn = urn_to_hrn(xrn)[0] @@ -63,20 +63,27 @@ class RegistryManager: raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record.email)) # get the callers gid - # if this is a self cred the record's gid is the caller's gid - if is_self: + # if caller_xrn is not specified assume the caller is the record + # object itself. + if not caller_xrn: caller_hrn = hrn caller_gid = record.get_gid_object() else: - caller_gid = api.auth.client_cred.get_gid_caller() - caller_hrn = caller_gid.get_hrn() - + caller_hrn, caller_type = urn_to_hrn(caller_xrn) + if caller_type: + caller_record = dbsession.query(RegRecord).filter_by(hrn=caller_hrn,type=caller_type).first() + else: + caller_record = dbsession.query(RegRecord).filter_by(hrn=caller_hrn).first() + if not caller_record: + raise RecordNotFound("Unable to associated caller (hrn=%s, type=%s) with credential for (hrn: %s, type: %s)"%(caller_hrn, caller_type, hrn, type)) + caller_gid = GID(string=caller_record.gid) + object_hrn = record.get_gid_object().get_hrn() rights = api.auth.determine_user_rights(caller_hrn, record.__dict__) # make sure caller has rights to this object if rights.is_empty(): - raise PermissionError(caller_hrn + " has no rights to " + record.hrn) - + raise PermissionError("%s has no rights to %s (%s)" % \ + (caller_hrn, object_hrn, xrn)) object_gid = GID(string=record.gid) new_cred = Credential(subject = object_gid.get_subject()) new_cred.set_gid_caller(caller_gid) @@ -98,13 +105,13 @@ class RegistryManager: return new_cred.save_to_string(save_parents=True) - def Resolve(self, api, xrns, intype=None, full=True): + def Resolve(self, api, xrns, type=None, full=True): if not isinstance(xrns, types.ListType): - xrns = [xrns] # try to infer type if not set and we get a single input - if not intype: - intype = Xrn(xrns).get_type() + if not type: + type = Xrn(xrns).get_type() + xrns = [xrns] hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] # load all known registry names into a prefix tree and attempt to find @@ -136,28 +143,29 @@ class RegistryManager: credential = api.getCredential() interface = api.registries[registry_hrn] server_proxy = api.server_proxy(interface, credential) - peer_records = server_proxy.Resolve(xrns, credential,intype) + peer_records = server_proxy.Resolve(xrns, credential,type) # pass foreign records as-is + # previous code used to read + # records.extend([SfaRecord(dict=record).as_dict() for record in peer_records]) + # not sure why the records coming through xmlrpc had to be processed at all records.extend(peer_records) # try resolving the remaining unfound records at the local registry local_hrns = list ( set(hrns).difference([record['hrn'] for record in records]) ) - logger.info("Resolve: local_hrns=%s"%local_hrns) # local_records = dbsession.query(RegRecord).filter(RegRecord.hrn.in_(local_hrns)) - if intype: - local_records = local_records.filter_by(type=intype) + if type: + local_records = local_records.filter_by(type=type) local_records=local_records.all() - logger.info("Resolve: local_records=%s (intype=%s)"%(local_records,intype)) + logger.info("Resolve: local_records=%s (type=%s)"%(local_records,type)) local_dicts = [ record.__dict__ for record in local_records ] if full: # in full mode we get as much info as we can, which involves contacting the # testbed for getting implementation details about the record - for record in local_dicts: logger.debug("resolve augment %s"%record) self.driver.augment_records_with_testbed_info(local_dicts) -# # also we fill the 'url' field for known authorities -# # used to be in the driver code, sounds like a poorman thing though + # also we fill the 'url' field for known authorities + # used to be in the driver code, sounds like a poorman thing though def solve_neighbour_url (record): if not record.type.startswith('authority'): return hrn=record.hrn @@ -170,7 +178,7 @@ class RegistryManager: # convert local record objects to dicts for xmlrpc # xxx somehow here calling dict(record) issues a weird error # however record.todict() seems to work fine -# records.extend( [ dict(record) for record in local_records ] ) + # records.extend( [ dict(record) for record in local_records ] ) records.extend( [ record.todict() for record in local_records ] ) if not records: raise RecordNotFound(str(hrns)) @@ -198,6 +206,7 @@ class RegistryManager: interface = api.registries[registry_hrn] server_proxy = api.server_proxy(interface, credential) record_list = server_proxy.List(xrn, credential) + # same as above, no need to process what comes from through xmlrpc # pass foreign records as-is record_dicts = record_list @@ -284,7 +293,7 @@ class RegistryManager: gid = gid_object.save_to_string(save_parents=True) record.gid = gid - if type in ["authority"]: + if isinstance (record, RegAuthority): # update the tree if not api.auth.hierarchy.auth_exists(hrn): api.auth.hierarchy.create_auth(hrn_to_urn(hrn,'authority')) @@ -293,6 +302,25 @@ class RegistryManager: gid = auth_info.get_gid_object() record.gid=gid.save_to_string(save_parents=True) + elif isinstance (record, RegSlice): + # locate objects for relationships + if hasattr (record, 'researcher'): + # we get the list of researcher hrns as + researcher_hrns = record.researcher + # strip that in case we have words + researcher_hrns = [ x.strip() for x in researcher_hrns ] + logger.info ("incoming researchers %s"%researcher_hrns) + request = dbsession.query (RegUser).filter(RegUser.hrn.in_(researcher_hrns)) + logger.info ("%d incoming hrns, %d matches found"%(len(researcher_hrns),request.count())) + researchers = dbsession.query (RegUser).filter(RegUser.hrn.in_(researcher_hrns)).all() + record.reg_researchers = researchers + + elif isinstance (record, RegUser): + # create RegKey objects for incoming keys + if hasattr(record,'keys'): + logger.debug ("creating %d keys for user %s"%(len(record.keys),record.hrn)) + record.reg_keys = [ RegKey (key) for key in record.keys ] + # update testbed-specific data if needed pointer = self.driver.register (record.__dict__, hrn, pub_key) @@ -318,7 +346,6 @@ class RegistryManager: record.just_updated() # validate the type - # xxx might be simpler to just try to commit as this is a constraint in the db if type not in ['authority', 'slice', 'node', 'user']: raise UnknownSfaType(type) @@ -369,6 +396,9 @@ class RegistryManager: raise RecordNotFound(msg) type = record.type + if type not in ['slice', 'user', 'node', 'authority'] : + raise UnknownSfaType(type) + credential = api.getCredential() registries = api.registries