merging with geni-api branch
[sfa.git] / sfa / managers / registry_manager_pl.py
index 43c2865..50776c0 100644 (file)
@@ -1,6 +1,5 @@
 import types
 import time 
-from sfa.server.registry import Registries
 from sfa.util.prefixTree import prefixTree
 from sfa.util.record import SfaRecord
 from sfa.util.table import SfaTable
@@ -11,12 +10,15 @@ from sfa.trust.credential import *
 from sfa.trust.certificate import *
 from sfa.util.faults import *
 
+    
+
 def get_credential(api, xrn, type, is_self=False):
     # convert xrn to hrn     
     if type:
         hrn = urn_to_hrn(xrn)[0]
     else:
         hrn, type = urn_to_hrn(xrn)
+        
     # Is this a root or sub authority
     auth_hrn = api.auth.get_authority(hrn)
     if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
@@ -32,6 +34,9 @@ def get_credential(api, xrn, type, is_self=False):
     # verify_cancreate_credential requires that the member lists
     # (researchers, pis, etc) be filled in
     api.fill_record_info(record)
+    if record['type']=='user':
+       if not record['enabled']:
+          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
@@ -52,18 +57,43 @@ def get_credential(api, xrn, type, is_self=False):
     new_cred = Credential(subject = object_gid.get_subject())
     new_cred.set_gid_caller(caller_gid)
     new_cred.set_gid_object(object_gid)
-    new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
-    new_cred.set_pubkey(object_gid.get_pubkey())
+    new_cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
+    #new_cred.set_pubkey(object_gid.get_pubkey())
     new_cred.set_privileges(rights)
-    new_cred.set_delegate(True)
+    new_cred.get_privileges().delegate_all_privileges(True)
     auth_kind = "authority,ma,sa"
-    new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
+    # Parent not necessary, verify with certs
+    #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
     new_cred.encode()
     new_cred.sign()
 
     return new_cred.save_to_string(save_parents=True)
 
-def resolve(api, xrns, type=None, origin_hrn=None):
+
+# The GENI GetVersion call
+def GetVersion():
+    version = {}
+    version['geni_api'] = 1
+    return version
+
+
+
+# The GENI resolve call
+def Resolve(api, xrn, creds):
+    records = resolve(api, xrn)
+    
+    if len(records) == 0:
+        return {}
+    
+    record = records[0]
+    if record.type == 'slice':
+        return {'geni_urn': xrn, 'geni_creator': " ".join(record.PI)}
+    if record.type == 'user':
+        return {'geni_urn': xrn, 'geni_certificate': record.gid}
+    
+    
+
+def resolve(api, xrns, type=None, origin_hrn=None, full=True):
 
     # load all know registry names into a prefix tree and attempt to find
     # the longest matching prefix
@@ -73,7 +103,7 @@ def resolve(api, xrns, type=None, origin_hrn=None):
     # create a dict whre key is an registry hrn and its value is a
     # hrns at that registry (determined by the known prefix tree).  
     xrn_dict = {}
-    registries = Registries(api)
+    registries = api.registries
     tree = prefixTree()
     registry_hrns = registries.keys()
     tree.load(registry_hrns)
@@ -103,19 +133,12 @@ def resolve(api, xrns, type=None, origin_hrn=None):
     # convert set to list
     remaining_hrns = [hrn for hrn in remaining_hrns] 
     table = SfaTable()
-
-    print remaining_hrns
     local_records = table.findObjects({'hrn': remaining_hrns})
-    for record in local_records:
-        try:
-            api.fill_record_info(record)
-            records.append(dict(record))
-        except PlanetLabRecordDoesNotExist:
-            # silently drop the ones that are missing in PL
-            print >> log, "ignoring SFA record ", record['hrn'], \
-                              " because pl record does not exist"    
-            table.remove(record)
-
+    if full:
+        api.fill_record_info(local_records)
+    
+    # convert local record objects to dicts
+    records.extend([dict(record) for record in local_records])
     if not records:
         raise RecordNotFound(str(hrns))
 
@@ -124,17 +147,17 @@ def resolve(api, xrns, type=None, origin_hrn=None):
 
     return records
 
-def list(api, xrn):
+def list(api, xrn, origin_hrn=None):
     hrn, type = urn_to_hrn(xrn)
     # load all know registry names into a prefix tree and attempt to find
     # the longest matching prefix
     records = []
-    registries = Registries(api)
+    registries = api.registries
     registry_hrns = registries.keys()
     tree = prefixTree()
     tree.load(registry_hrns)
     registry_hrn = tree.best_match(hrn)
-    
+   
     #if there was no match then this record belongs to an unknow registry
     if not registry_hrn:
         raise MissingAuthority(xrn)
@@ -161,7 +184,7 @@ def list(api, xrn):
 def register(api, record):
 
     hrn, type = record['hrn'], record['type']
-
+    urn = hrn_to_urn(hrn,type)
     # validate the type
     if type not in ['authority', 'slice', 'node', 'user']:
         raise UnknownSfaType(type) 
@@ -190,7 +213,7 @@ def register(api, record):
                 pub_key = record['key']
             pkey = convert_public_key(pub_key)
 
-        gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
+        gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
         gid = gid_object.save_to_string(save_parents=True)
         record['gid'] = gid
         record.set_gid(gid)
@@ -198,7 +221,7 @@ def register(api, record):
     if type in ["authority"]:
         # update the tree
         if not api.auth.hierarchy.auth_exists(hrn):
-            api.auth.hierarchy.create_auth(hrn)
+            api.auth.hierarchy.create_auth(hrn_to_urn(hrn,'authority'))
 
         # get the GID from the newly created authority
         gid = auth_info.get_gid_object()
@@ -219,13 +242,13 @@ def register(api, record):
         for key in pl_record.keys():
             if key not in acceptable_fields:
                 pl_record.pop(key)
-            slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
-            if not slices:
-                pointer = api.plshell.AddSlice(api.plauth, pl_record)
-            else:
-                pointer = slices[0]['slice_id']
-            record.set_pointer(pointer)
-            record['pointer'] = pointer
+        slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
+        if not slices:
+             pointer = api.plshell.AddSlice(api.plauth, pl_record)
+        else:
+             pointer = slices[0]['slice_id']
+        record.set_pointer(pointer)
+        record['pointer'] = pointer
 
     elif  (type == "user"):
         persons = api.plshell.GetPersons(api.plauth, [record['email']])
@@ -271,6 +294,7 @@ def update(api, record_dict):
     new_record = SfaRecord(dict = record_dict)
     type = new_record['type']
     hrn = new_record['hrn']
+    urn = hrn_to_urn(hrn,type)
     api.auth.verify_object_permission(hrn)
     table = SfaTable()
     # make sure the record exists
@@ -335,7 +359,7 @@ def update(api, record_dict):
             # update the openssl key and gid
             pkey = convert_public_key(new_key)
             uuid = create_uuid()
-            gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
+            gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
             gid = gid_object.save_to_string(save_parents=True)
             record['gid'] = gid
             record = SfaRecord(dict=record)
@@ -370,7 +394,7 @@ def remove(api, xrn, type, origin_hrn=None):
     type = record['type']
 
     credential = api.getCredential()
-    registries = Registries(api)
+    registries = api.registries
 
     # Try to remove the object from the PLCDB of federated agg.
     # This is attempted before removing the object from the local agg's PLCDB and sfa table