From bd3ec5aaae7b09abe45dd4d94c5fba606f81d44c Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Fri, 4 Nov 2011 11:38:37 +0100 Subject: [PATCH] first batch of changes to reconcile with new driver-based paradigm --- sfa/managers/registry_manager_slab.py | 202 ++++++++++++-------------- sfa/senslab/slabdriver.py | 20 +-- 2 files changed, 106 insertions(+), 116 deletions(-) diff --git a/sfa/managers/registry_manager_slab.py b/sfa/managers/registry_manager_slab.py index a13eb8a5..f3957a2b 100644 --- a/sfa/managers/registry_manager_slab.py +++ b/sfa/managers/registry_manager_slab.py @@ -2,27 +2,24 @@ import types import time import sys -from sfa.util.faults import * +from sfa.util.faults import RecordNotFound, AccountNotEnabled, PermissionError, MissingAuthority, \ + UnknownSfaType, ExistingRecord from sfa.util.prefixTree import prefixTree from sfa.util.record import SfaRecord from sfa.senslab.table_slab import SfaTable -from sfa.util.record import SfaRecord -from sfa.trust.gid import GID from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn, urn_to_hrn -from sfa.util.plxrn import hrn_to_pl_login_base +from sfa.util.version import version_core + +from sfa.trust.gid import GID from sfa.trust.credential import Credential -from sfa.trust.certificate import Certificate, Keypair +from sfa.trust.certificate import Certificate, Keypair, convert_public_key from sfa.trust.gid import create_uuid -from sfa.util.version import version_core -from sfa.senslab.api import * -myapi=SfaAPI(interface='registry') # The GENI GetVersion call def GetVersion(api): - - peers =dict ([ (peername,v._ServerProxy__host) for (peername,v) in myapi.registries.iteritems() - if peername != myapi.hrn]) - xrn=Xrn(myapi.hrn) + peers =dict ([ (peername,v._ServerProxy__host) for (peername,v) in api.registries.iteritems() + if peername != api.hrn]) + xrn=Xrn(api.hrn) return version_core({'interface':'registry', 'hrn':xrn.get_hrn(), 'urn':xrn.get_urn(), @@ -34,14 +31,14 @@ def get_credential(api, xrn, type, is_self=False): hrn = urn_to_hrn(xrn)[0] else: hrn, type = urn_to_hrn(xrn) - + # Is this a root or sub authority - auth_hrn = myapi.auth.get_authority(hrn) + auth_hrn = api.auth.get_authority(hrn) print>> sys.stderr , " \r\n REGISTRY get_credential auth_hrn:" , auth_hrn,"hrn : ", hrn, " Type : ", type, "is self : " , is_self,"<<" - if not auth_hrn or hrn == myapi.config.SFA_INTERFACE_HRN: + if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN: auth_hrn = hrn # get record info - auth_info = myapi.auth.get_auth_info(auth_hrn) + auth_info = api.auth.get_auth_info(auth_hrn) table = SfaTable() print >> sys.stderr , " findObject ", type, hrn records = table.findObjects({'type': type, 'hrn': hrn}) @@ -52,7 +49,7 @@ def get_credential(api, xrn, type, is_self=False): # verify_cancreate_credential requires that the member lists # (researchers, pis, etc) be filled in - myapi.fill_record_info(record) + api.driver.fill_record_info(record, api.aggregates) record['enabled'] = True print>> sys.stderr , " \r\n ++ REGISTRY get_credential hrn %s record['enabled'] %s is_self %s" %(hrn, record['enabled'], is_self) if record['type']=='user': @@ -68,7 +65,7 @@ def get_credential(api, xrn, type, is_self=False): print>>sys.stderr, " \r\n REGISTRY IS SELF OK caller_hrn %s--- \r\n caller_gid %s---------" %(caller_hrn,caller_gid) else: print>> sys.stderr , " \r\n ++ ELSE " - caller_gid = myapi.auth.client_cred.get_gid_caller() + caller_gid = api.auth.client_cred.get_gid_caller() print>> sys.stderr , " \r\n ++ ELSE caller_gid %s record %s" %(caller_gid, record) caller_hrn = caller_gid.get_hrn() print>> sys.stderr , " \r\n ++ ELSE caller_hrn %s " %(caller_hrn) @@ -76,7 +73,7 @@ def get_credential(api, xrn, type, is_self=False): object_hrn = record.get_gid_object().get_hrn() print>> sys.stderr , " \r\n ++ ELSE object_hrn %s " %(object_hrn) - rights = myapi.auth.determine_user_rights(caller_hrn, record) + rights = api.auth.determine_user_rights(caller_hrn, record) print>> sys.stderr , " \r\n ++ After rights record: %s \r\n ====RIGHTS %s " %(record , rights) # make sure caller has rights to this object @@ -95,7 +92,7 @@ def get_credential(api, xrn, type, is_self=False): new_cred.set_expiration(int(record['expires'])) auth_kind = "authority,ma,sa" # Parent not necessary, verify with certs - #new_cred.set_parent(myapi.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind)) + #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind)) new_cred.encode() new_cred.sign() @@ -105,7 +102,7 @@ def get_credential(api, xrn, type, is_self=False): def resolve(api, xrns, type=None, full=True): # load all known registry names into a prefix tree and attempt to find - # the longest matching prefix + # the longest matching prefix print >>sys.stderr , '\t\t REGISTRY MANAGER : resolve=========xrns ', xrns if not isinstance(xrns, types.ListType): if not type: @@ -117,7 +114,7 @@ def resolve(api, xrns, type=None, full=True): # hrns at that registry (determined by the known prefix tree). xrn_dict = {} print >>sys.stderr, '\r\n REGISTRY MANAGER : resolve xrns ' , xrns #api.__dict__.keys() - registries = myapi.registries + registries = api.registries tree = prefixTree() registry_hrns = registries.keys() print >>sys.stderr, '\r\n \t\t REGISTRY MANAGER registry_hrns' , registry_hrns @@ -130,10 +127,9 @@ def resolve(api, xrns, type=None, full=True): xrn_dict[registry_hrn].append(xrn) print >>sys.stderr, '\t\tREGISTRY MANAGER *****xrn_dict[registry_hrn] ',xrn_dict[registry_hrn] records = [] - for registry_hrn in xrn_dict: # skip the hrn without a registry hrn - # XX should we let the user know the authority is unknown? + # XX should we let the user know the authority is unknown? print >>sys.stderr, '\t\t registry_hrn in xrn_dict ', registry_hrn if not registry_hrn: continue @@ -141,16 +137,17 @@ def resolve(api, xrns, type=None, full=True): # if the best match (longest matching hrn) is not the local registry, # forward the request xrns = xrn_dict[registry_hrn] - if registry_hrn != myapi.hrn: - credential = myapi.getCredential() - peer_records = registries[registry_hrn].Resolve(xrns, credential) + if registry_hrn != api.hrn: + credential = api.getCredential() + interface = api.registries[registry_hrn] + server = api.server_proxy(interface, credential) + peer_records = server.Resolve(xrns, credential) print >>sys.stderr , '\t\t peer_records ', peer_records records.extend([SfaRecord(dict=record).as_dict() for record in peer_records]) print >>sys.stderr,'\t\t hrns ' , hrns # try resolving the remaining unfound records at the local registry remaining_hrns = set(hrns).difference([record['hrn'] for record in records]) - # convert set to list remaining_hrns = [hrn for hrn in remaining_hrns] print >>sys.stderr, '\t\t remaining_hrns', remaining_hrns @@ -162,10 +159,9 @@ def resolve(api, xrns, type=None, full=True): print >>sys.stderr, '\t\t resolve regmanager : rec ', rec if full: - print >>sys.stderr, '\r\n \r\n REGISTRY:_FULL', myapi - myapi.fill_record_info(local_records) - - + print >>sys.stderr, '\r\n \r\n REGISTRY:_FULL', api + api.driver.fill_record_info(local_records) + # convert local record objects to dicts records.extend([dict(record) for record in local_records]) print >>sys.stderr, "\r\n \t\t records extends %s" %(records) @@ -182,7 +178,7 @@ def list(api, xrn, origin_hrn=None): # load all know registry names into a prefix tree and attempt to find # the longest matching prefix records = [] - registries = myapi.registries + registries = api.registries registry_hrns = registries.keys() tree = prefixTree() tree.load(registry_hrns) @@ -191,24 +187,20 @@ def list(api, xrn, origin_hrn=None): #if there was no match then this record belongs to an unknow registry if not registry_hrn: raise MissingAuthority(xrn) - # if the best match (longest matching hrn) is not the local registry, # forward the request records = [] - if registry_hrn != myapi.hrn: - credential = myapi.getCredential() + if registry_hrn != api.hrn: + credential = api.getCredential() print>>sys.stderr, "Registries : ", registries - #record_list = registries[registry_hrn].List(xrn, credential) - - interface=registries[registry_hrn] - server=api.get_server(interface,credential) - records= server.List(xrn,credential) - + interface = api.registries[registry_hrn] + server = api.server_proxy(interface, credential) + record_list = server.List(xrn, credential) records = [SfaRecord(dict=record).as_dict() for record in record_list] # if we still have not found the record yet, try the local registry if not records: - if not myapi.auth.hierarchy.auth_exists(hrn): + if not api.auth.hierarchy.auth_exists(hrn): raise MissingAuthority(hrn) table = SfaTable() @@ -219,19 +211,17 @@ def list(api, xrn, origin_hrn=None): def register(api, record): + #hrn, type = record['hrn'], record['type'] hrn = str(record['hrn']).strip("['']") - type_of_rec = str( record['type']).strip("['']") - - - urn = hrn_to_urn(hrn,type_of_rec) + type = str( record['type']).strip("['']") + urn = hrn_to_urn(hrn,type) # validate the type - - if type_of_rec not in ['authority', 'slice', 'node', 'user']: - raise UnknownSfaType(type_of_rec) + if type not in ['authority', 'slice', 'node', 'user']: + raise UnknownSfaType(type) # check if record already exists table = SfaTable() - existing_records = table.find({'type': type_of_rec, 'hrn': hrn}) + existing_records = table.find({'type': type, 'hrn': hrn}) if existing_records: raise ExistingRecord(hrn) @@ -245,11 +235,8 @@ def register(api, record): #hrn = record['hrn'] #api.auth.verify_object_permission(hrn) - myapi.auth.verify_object_permission( record['hrn']) - auth_info = myapi.auth.get_auth_info(record['authority']) - - - + api.auth.verify_object_permission( record['hrn']) + auth_info = api.auth.get_auth_info(record['authority']) pub_key = None # make sure record has a gid if 'gid' not in record: @@ -262,80 +249,83 @@ def register(api, record): pub_key = record['key'] pkey = convert_public_key(pub_key) - gid_object = myapi.auth.hierarchy.create_gid(urn, 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) print>>sys.stderr, " \r\n \r\n ----------- REGISTRY_MANAGER_SLAB.PY record['gid'] %s" %(record['gid']) - print>>sys.stderr, " \r\n \r\n ----------- REGISTRY_MANAGER_SLAB.PY register type_of_rec %s"%(type_of_rec) - - if type_of_rec in ["authority"]: + print>>sys.stderr, " \r\n \r\n ----------- REGISTRY_MANAGER_SLAB.PY register type %s"%(type) + if type in ["authority"]: # update the tree - if not myapi.auth.hierarchy.auth_exists(hrn): - myapi.auth.hierarchy.create_auth(hrn_to_urn(hrn,'authority')) + if not api.auth.hierarchy.auth_exists(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() record.set_gid(gid.save_to_string(save_parents=True)) - #pl_record = myapi.sfa_fields_to_pl_fields(type, hrn, record) - print>>sys.stderr, " \r\n \r\n ----------- REGISTRY_MANAGER_SLAB.PY register : type_of_rec in [authority ] sfa_fields_to_pl_fields FIELDS A CHANGER" + #pl_record = api.driver.sfa_fields_to_pl_fields(type, hrn, record) + print>>sys.stderr, " \r\n \r\n ----------- REGISTRY_MANAGER_SLAB.PY register : type in [authority ] sfa_fields_to_pl_fields FIELDS A CHANGER" - sites = myapi.oar.GetSites( [pl_record['login_base']]) + # thierry: ideally we'd like to be able to write api.driver.GetSites + # in which case the code would become mostly the same as for pl + sites = api.driver.GetSites([pl_record['login_base']]) if not sites: - pointer = myapi.oar.AddSite( pl_record) + # thierry + # Error (E0601, register): Using variable 'pl_record' before assignment + pointer = api.driver.AddSite( pl_record) else: pointer = sites[0]['site_id'] record.set_pointer(pointer) record['pointer'] = pointer - elif (type_of_rec == "slice"): + elif (type == "slice"): acceptable_fields=['url', 'instantiation', 'name', 'description'] - pl_record = myapi.sfa_fields_to_pl_fields(type_of_rec, hrn, record) + pl_record = api.driver.sfa_fields_to_pl_fields(type, hrn, record) print>>sys.stderr, " \r\n \r\n ----------- REGISTRY_MANAGER_SLAB.PY register slice pl_record %s"%(pl_record) for key in pl_record.keys(): if key not in acceptable_fields: pl_record.pop(key) - slices = myapi.users.GetSlices( [pl_record['name']]) + slices = api.driver.GetSlices([pl_record['name']]) if not slices: - pointer = myapi.users.AddSlice(pl_record) + pointer = api.driver.AddSlice(pl_record) else: pointer = slices[0]['slice_id'] record.set_pointer(pointer) record['pointer'] = pointer - elif (type_of_rec == "user"): - persons = myapi.users.GetPersons( [record['email']]) + elif (type == "user"): + persons = api.driver.GetPersons([record['email']]) if not persons: print>>sys.stderr, " \r\n \r\n ----------- registry_manager_slab register NO PERSON ADD TO LDAP?" #if not persons: - #pointer = myapi.users.AddPerson( dict(record)) + #pointer = api.driver.AddPerson( dict(record)) #else: #pointer = persons[0]['person_id'] if 'enabled' in record and record['enabled']: - myapi.users.UpdatePerson( pointer, {'enabled': record['enabled']}) + api.driver.UpdatePerson(pointer, {'enabled': record['enabled']}) # add this persons to the site only if he is being added for the first # time by sfa and doesont already exist in plc if not persons or not persons[0]['site_ids']: login_base = get_leaf(record['authority']) - myapi.users.AddPersonToSite( pointer, login_base) + api.driver.AddPersonToSite(pointer, login_base) # What roles should this user have? - myapi.users.AddRoleToPerson( 'user', pointer) + api.driver.AddRoleToPerson('user', pointer) # Add the user's key if pub_key: - myapi.users.AddPersonKey( pointer, {'key_type' : 'ssh', 'key' : pub_key}) + api.driver.AddPersonKey(pointer, {'key_type' : 'ssh', 'key' : pub_key}) #elif (type == "node"): - #pl_record = myapi.sfa_fields_to_pl_fields(type, hrn, record) + #pl_record = api.driver.sfa_fields_to_pl_fields(type, hrn, record) #login_base = hrn_to_pl_login_base(record['authority']) - #nodes = myapi.oar.GetNodes( [pl_record['hostname']]) + #nodes = api.driver.GetNodes([pl_record['hostname']]) #if not nodes: - #pointer = myapi.oar.AddNode(login_base, pl_record) + #pointer = api.driver.AddNode(login_base, pl_record) #else: #pointer = nodes[0]['node_id'] @@ -345,7 +335,7 @@ def register(api, record): #record['record_id'] = record_id # update membership for researchers, pis, owners, operators - myapi.update_membership(None, record) + api.driver.update_membership(None, record) return record.get_gid_object().save_to_string(save_parents=True) @@ -354,7 +344,7 @@ def update(api, record_dict): type = new_record['type'] hrn = new_record['hrn'] urn = hrn_to_urn(hrn,type) - myapi.auth.verify_object_permission(hrn) + api.auth.verify_object_permission(hrn) table = SfaTable() # make sure the record exists records = table.findObjects({'type': type, 'hrn': hrn}) @@ -365,7 +355,7 @@ def update(api, record_dict): # Update_membership needs the membership lists in the existing record # filled in, so it can see if members were added or removed - myapi.fill_record_info(record) + api.driver.fill_record_info(record) # Use the pointer from the existing record, not the one that the user # gave us. This prevents the user from inserting a forged pointer @@ -373,13 +363,13 @@ def update(api, record_dict): # update the PLC information that was specified with the record if (type == "authority"): - myapi.oar.UpdateSite( pointer, new_record) + api.driver.UpdateSite(pointer, new_record) elif type == "slice": - pl_record=myapi.sfa_fields_to_pl_fields(type, hrn, new_record) + pl_record=api.driver.sfa_fields_to_pl_fields(type, hrn, new_record) if 'name' in pl_record: pl_record.pop('name') - myapi.users.UpdateSlice( pointer, pl_record) + api.driver.UpdateSlice(pointer, pl_record) elif type == "user": # SMBAKER: UpdatePerson only allows a limited set of fields to be @@ -392,14 +382,14 @@ def update(api, record_dict): 'password', 'phone', 'url', 'bio', 'accepted_aup', 'enabled']: update_fields[key] = all_fields[key] - myapi.users.UpdatePerson( pointer, update_fields) + api.driver.UpdatePerson(pointer, update_fields) if 'key' in new_record and new_record['key']: # must check this key against the previous one if it exists - persons = myapi.users.GetPersons( [pointer], ['key_ids']) + persons = api.driver.GetPersons([pointer], ['key_ids']) person = persons[0] keys = person['key_ids'] - keys = myapi.users.GetKeys( person['key_ids']) + keys = api.driver.GetKeys(person['key_ids']) key_exists = False if isinstance(new_record['key'], types.ListType): new_key = new_record['key'][0] @@ -409,29 +399,29 @@ def update(api, record_dict): # Delete all stale keys for key in keys: if new_record['key'] != key['key']: - myapi.users.DeleteKey( key['key_id']) + api.driver.DeleteKey(key['key_id']) else: key_exists = True if not key_exists: - myapi.users.AddPersonKey( pointer, {'key_type': 'ssh', 'key': new_key}) + api.driver.AddPersonKey(pointer, {'key_type': 'ssh', 'key': new_key}) # update the openssl key and gid pkey = convert_public_key(new_key) uuid = create_uuid() - gid_object = myapi.auth.hierarchy.create_gid(urn, 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) table.update(record) elif type == "node": - myapi.oar.UpdateNode( pointer, new_record) + api.driver.UpdateNode(pointer, new_record) else: raise UnknownSfaType(type) # update membership for researchers, pis, owners, operators - myapi.update_membership(record, new_record) + api.driver.update_membership(record, new_record) return 1 @@ -450,33 +440,33 @@ def remove(api, xrn, origin_hrn=None): record = records[0] type = record['type'] - credential = myapi.getCredential() - registries = myapi.registries + credential = api.getCredential() + 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 - if hrn.startswith(myapi.hrn) and type in ['user', 'slice', 'authority']: + if hrn.startswith(api.hrn) and type in ['user', 'slice', 'authority']: for registry in registries: - if registry not in [myapi.hrn]: + if registry not in [api.hrn]: try: result=registries[registry].remove_peer_object(credential, record, origin_hrn) except: pass if type == "user": - persons = myapi.users.GetPersons(record['pointer']) + persons = api.driver.GetPersons(record['pointer']) # only delete this person if he has site ids. if he doesnt, it probably means # he was just removed from a site, not actually deleted if persons and persons[0]['site_ids']: - myapi.users.DeletePerson(record['pointer']) + api.driver.DeletePerson(record['pointer']) elif type == "slice": - if myapi.users.GetSlices( record['pointer']): - myapi.users.DeleteSlice( record['pointer']) + if api.driver.GetSlices(record['pointer']): + api.driver.DeleteSlice(record['pointer']) elif type == "node": - if myapi.oar.GetNodes( record['pointer']): - myapi.oar.DeleteNode( record['pointer']) + if api.driver.GetNodes(record['pointer']): + api.driver.DeleteNode(record['pointer']) elif type == "authority": - if myapi.oar.GetSites( record['pointer']): - myapi.oar.DeleteSite( record['pointer']) + if api.driver.GetSites(record['pointer']): + api.driver.DeleteSite(record['pointer']) else: raise UnknownSfaType(type) diff --git a/sfa/senslab/slabdriver.py b/sfa/senslab/slabdriver.py index 89d2bc97..724f2934 100644 --- a/sfa/senslab/slabdriver.py +++ b/sfa/senslab/slabdriver.py @@ -24,22 +24,22 @@ def list_to_dict(recs, key): #print>>sys.stderr, " \r\n \t\t list_to_dict : rec %s \r\n \t\t list_to_dict keys %s" %(recs,keys) return dict(zip(keys, recs)) -class SlabDriver: +# thierry : note +# this inheritance scheme is so that the driver object can receive +# GetNodes or GetSites sorts of calls directly +# and thus minimize the differences in the managers with the pl version +class SlabDriver (OARapi, SenslabImportUsers): def __init__(self, config): self.config=config self.hrn = config.SFA_INTERFACE_HRN print >>sys.stderr, "\r\n_____________ SFA SENSLAB DRIVER \r\n" - # thierry : note - # this class chould be able to somehow call this API - # in the pl case see the PlShell class that does exactly that - # I mean, the rest of the code is going to make calls like - # api.driver.GetNodes - # which will result in this class (SlabDriver) receiving the GetNodes call - # you might wish for example to have SlabDriver inherit the OARapi class instead - self.oar = OARapi() - self.users = SenslabImportUsers() + # thierry - just to not break the rest of this code + #self.oar = OARapi() + #self.users = SenslabImportUsers() + self.oar = self + self.users = self self.time_format = "%Y-%m-%d %H:%M:%S" #self.logger=sfa_logger() print >>sys.stderr, "\r\n \t\t___________PSFA SENSLAN /API.PY __init__ STOP ",self.interface #dir(self) -- 2.43.0