Merge branch 'master' into senslab2
[sfa.git] / sfa / managers / registry_manager.py
index 79ac91c..db4348d 100644 (file)
@@ -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.model 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,26 @@ 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)
+            caller_record = dbsession.query(RegRecord).filter_by(hrn=caller_hrn).first()
+            if caller_type:
+                caller_record = caller_record.filter_by(type=caller_type)
+            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)
@@ -101,10 +107,10 @@ class RegistryManager:
     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 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
@@ -286,7 +292,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'))
@@ -295,6 +301,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 <researcher> words </researcher>
+                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)