2 # Geniwrapper XML-RPC and SOAP interfaces
11 from geni.util.auth import Auth
12 from geni.util.config import *
13 from geni.util.faults import *
14 from geni.util.debug import *
15 from geni.util.rights import *
16 from geni.util.credential import *
18 # See "2.2 Characters" in the XML specification:
20 # #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
22 # [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF]
24 invalid_xml_ascii = map(chr, range(0x0, 0x8) + [0xB, 0xC] + range(0xE, 0x1F))
25 xml_escape_table = string.maketrans("".join(invalid_xml_ascii), "?" * len(invalid_xml_ascii))
27 def xmlrpclib_escape(s, replace = string.replace):
29 xmlrpclib does not handle invalid 7-bit control characters. This
30 function augments xmlrpclib.escape, which by default only replaces
31 '&', '<', and '>' with entities.
34 # This is the standard xmlrpclib.escape function
35 s = replace(s, "&", "&")
36 s = replace(s, "<", "<")
37 s = replace(s, ">", ">",)
39 # Replace invalid 7-bit control characters with '?'
40 return s.translate(xml_escape_table)
42 def xmlrpclib_dump(self, value, write):
44 xmlrpclib cannot marshal instances of subclasses of built-in
45 types. This function overrides xmlrpclib.Marshaller.__dump so that
46 any value that is an instance of one of its acceptable types is
47 marshalled as that type.
49 xmlrpclib also cannot handle invalid 7-bit control characters. See
53 # Use our escape function
54 args = [self, value, write]
55 if isinstance(value, (str, unicode)):
56 args.append(xmlrpclib_escape)
59 # Try for an exact match first
60 f = self.dispatch[type(value)]
63 # Try for an isinstance() match
64 for Type, f in self.dispatch.iteritems():
65 if isinstance(value, Type):
68 raise TypeError, "cannot marshal %s objects" % type(value)
72 # You can't hide from me!
73 xmlrpclib.Marshaller._Marshaller__dump = xmlrpclib_dump
75 # SOAP support is optional
78 from SOAPpy.Parser import parseSOAPRPC
79 from SOAPpy.Types import faultType
80 from SOAPpy.NS import NS
81 from SOAPpy.SOAPBuilder import buildSOAP
87 def import_deep(name):
88 mod = __import__(name)
89 components = name.split('.')
90 for comp in components[1:]:
91 mod = getattr(mod, comp)
96 # flat list of method names
97 methods = geni.methods.methods
99 def __init__(self, config = "/usr/share/geniwrapper/geni/util/geni_config", encoding = "utf-8", peer_cert = None, interface = None, key_file = None, cert_file = None):
100 self.encoding = encoding
102 # Better just be documenting the API
107 self.config = Config(config)
108 self.auth = Auth(peer_cert)
109 self.interface = interface
110 self.key_file = key_file
111 self.cert_file = cert_file
112 self.credential = None
113 self.plshell = self.getPLCShell()
114 self.basedir = self.config.GENI_BASE_DIR + os.sep
115 self.server_basedir = self.basedir + os.sep + "geni" + os.sep
116 self.hrn = self.config.GENI_INTERFACE_HRN
117 self.time_format = "%Y-%m-%d %H:%M:%S"
120 def getPLCShell(self):
121 self.plauth = {'Username': self.config.GENI_PLC_USER,
122 'AuthMethod': 'password',
123 'AuthString': self.config.GENI_PLC_PASSWORD}
126 shell = PLC.Shell.Shell(globals = globals())
127 shell.AuthCheck(self.plauth)
131 plc_host = self.config.GENI_PLC_HOST
132 plc_port = self.config.GENI_PLC_PORT
133 plc_api_path = self.config.GENI_PLC_API_PATH
134 url = "https://%(plc_host)s:%(plc_port)s/%(plc_api_path)s/" % \
137 shell = xmlrpclib.Server(url, verbose = 0, allow_none = True)
138 shell.AuthCheck(self.plauth)
141 def getCredential(self):
142 if self.interface in ['registry']:
143 return self.getCredentialFromLocalRegistry()
145 return self.getCredentialFromRegistry()
148 def getCredentialFromRegistry(self):
150 Get our credential from a remote registry using a geniclient connection
153 cred_filename = ".".join([self.server_basedir, self.interface, self.hrn, type, "cred"])
155 credential = Credential(filename = cred_filename)
158 from geni.util.registry import Registries
159 registries = Registries()
160 registry = registries[self.hrn]
161 self_cred = registry.get_credential(None, type, self.hrn)
162 cred = registry.get_credential(self_cred, type, self.hrn)
163 cred.save_to_file(cred_filename, save_parents=True)
165 def getCredentialFromLocalRegistry(self):
167 Get our current credential directly from the local registry.
171 auth_hrn = self.auth.get_authority(hrn)
174 auth_info = self.auth.get_auth_info(auth_hrn)
175 table = self.auth.get_auth_table(auth_hrn)
176 records = table.resolve('*', hrn)
180 type = record.get_type()
181 object_gid = record.get_gid_object()
182 new_cred = Credential(subject = object_gid.get_subject())
183 new_cred.set_gid_caller(object_gid)
184 new_cred.set_gid_object(object_gid)
185 new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
186 new_cred.set_pubkey(object_gid.get_pubkey())
187 r1 = determine_rights(type, hrn)
188 new_cred.set_privileges(r1)
190 # determine the type of credential that we want to use as a parent for
193 if (type == "ma") or (type == "node"):
194 auth_kind = "authority,ma"
195 else: # user, slice, sa
196 auth_kind = "authority,sa"
198 new_cred.set_parent(self.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
206 def loadCredential(self):
208 Attempt to load credential from file if it exists. If it doesnt get
209 credential from registry.
212 # see if this file exists
213 # XX This is really the aggregate's credential. Using this is easier than getting
214 # the registry's credential from iteslf (ssl errors).
215 ma_cred_filename = self.server_basedir + os.sep + self.interface + self.hrn + ".ma.cred"
217 self.credential = Credential(filename = ma_cred_filename)
219 self.credential = self.getCredentialFromRegistry()
222 # Convert geni fields to PLC fields for use when registering up updating
223 # registry record in the PLC database
225 # @param type type of record (user, slice, ...)
226 # @param hrn human readable name
227 # @param geni_fields dictionary of geni fields
228 # @param pl_fields dictionary of PLC fields (output)
230 def geni_fields_to_pl_fields(self, type, hrn, geni_fields, pl_fields):
232 if not "email" in pl_fields:
233 if not "email" in geni_fields:
234 raise MissingGeniInfo("email")
235 pl_fields["email"] = geni_fields["email"]
237 if not "first_name" in pl_fields:
238 pl_fields["first_name"] = "geni"
240 if not "last_name" in pl_fields:
241 pl_fields["last_name"] = hrn
243 elif type == "slice":
244 if not "instantiation" in pl_fields:
245 pl_fields["instantiation"] = "delegated" # "plc-instantiated"
246 if not "name" in pl_fields:
247 pl_fields["name"] = hrn_to_pl_slicename(hrn)
248 if not "max_nodes" in pl_fields:
249 pl_fields["max_nodes"] = 10
252 if not "hostname" in pl_fields:
253 if not "dns" in geni_fields:
254 raise MissingGeniInfo("dns")
255 pl_fields["hostname"] = geni_fields["dns"]
256 if not "model" in pl_fields:
257 pl_fields["model"] = "geni"
260 pl_fields["login_base"] = hrn_to_pl_login_base(hrn)
262 if not "name" in pl_fields:
263 pl_fields["name"] = hrn
265 if not "abbreviated_name" in pl_fields:
266 pl_fields["abbreviated_name"] = hrn
268 if not "enabled" in pl_fields:
269 pl_fields["enabled"] = True
271 if not "is_public" in pl_fields:
272 pl_fields["is_public"] = True
276 def fill_record_pl_info(self, record):
278 Fill in the planetlab specific fields of a Geni record. This
279 involves calling the appropraite PLC method to retrie the
280 dtabase record for the object.
282 PLC data is filled into the pl_fino field of the record.
284 @param record record to fill in field (in/out param)
286 type = record.get_type()
287 pointer = record.get_pointer()
289 # records with pointer==-1 do not have plc info associated with them.
290 # for example, the top level authority records which are
291 # authorities, but not PL "sites"
293 record.set_pl_info({})
296 if (type == "sa") or (type == "ma"):
297 pl_res = self.plshell.GetSites(self.plauth, [pointer])
298 elif (type == "slice"):
299 pl_res = self.plshell.GetSlices(self.plauth, [pointer])
300 elif (type == "user"):
301 pl_res = self.plshell.GetPersons(self.plauth, [pointer])
302 key_ids = pl_res[0]['key_ids']
303 keys = self.plshell.GetKeys(self.plauth, key_ids)
306 pubkeys = [key['key'] for key in keys]
307 pl_res[0]['keys'] = pubkeys
308 elif (type == "node"):
309 pl_res = self.plshell.GetNodes(self.plauth, [pointer])
311 raise UnknownGeniType(type)
314 # the planetlab record no longer exists
315 # TODO: delete the geni record ?
316 raise PlanetLabRecordDoesNotExist(record.get_name())
318 record.set_pl_info(pl_res[0])
321 def lookup_users(self, auth_table, user_id_list, role="*"):
323 for person_id in user_id_list:
324 user_records = auth_table.find("user", person_id, "pointer")
325 for user_record in user_records:
326 self.fill_record_info(user_record)
328 user_roles = user_record.get_pl_info().get("roles")
329 if (role=="*") or (role in user_roles):
330 record_list.append(user_record.get_name())
333 def fill_record_geni_info(self, record):
335 type = record.get_type()
337 if (type == "slice"):
338 auth_table = self.auth.get_auth_table(self.auth.get_authority(record.get_name()))
339 person_ids = record.pl_info.get("person_ids", [])
340 researchers = self.lookup_users(auth_table, person_ids)
341 geni_info['researcher'] = researchers
344 auth_table = self.auth.get_auth_table(record.get_name())
345 person_ids = record.pl_info.get("person_ids", [])
346 pis = self.lookup_users(auth_table, person_ids, "pi")
347 geni_info['pi'] = pis
348 # TODO: OrganizationName
351 auth_table = self.auth.get_auth_table(record.get_name())
352 person_ids = record.pl_info.get("person_ids", [])
353 operators = self.lookup_users(auth_table, person_ids, "tech")
354 geni_info['operator'] = operators
355 # TODO: OrganizationName
357 auth_table = self.auth.get_auth_table(record.get_name())
358 person_ids = record.pl_info.get("person_ids", [])
359 owners = self.lookup_users(auth_table, person_ids, "admin")
360 geni_info['owner'] = owners
362 elif (type == "node"):
363 geni_info['dns'] = record.pl_info.get("hostname", "")
364 # TODO: URI, LatLong, IP, DNS
366 elif (type == "user"):
367 geni_info['email'] = record.pl_info.get("email", "")
368 # TODO: PostalAddress, Phone
370 record.set_geni_info(geni_info)
372 def fill_record_info(self, record):
374 Given a geni record, fill in the PLC specific and Geni specific
375 fields in the record.
377 self.fill_record_pl_info(record)
378 self.fill_record_geni_info(record)
380 def update_membership_list(self, oldRecord, record, listName, addFunc, delFunc):
381 # get a list of the HRNs tht are members of the old and new records^M
383 if oldRecord.pl_info == None:
384 oldRecord.pl_info = {}
385 oldList = oldRecord.get_geni_info().get(listName, [])
388 newList = record.get_geni_info().get(listName, [])
390 # if the lists are the same, then we don't have to update anything
391 if (oldList == newList):
394 # build a list of the new person ids, by looking up each person to get
398 userRecord = self.resolve_raw("user", hrn)[0]
399 newIdList.append(userRecord.get_pointer())
401 # build a list of the old person ids from the person_ids field of the
404 oldIdList = oldRecord.plinfo.get("person_ids", [])
405 containerId = oldRecord.get_pointer()
407 # if oldRecord==None, then we are doing a Register, instead of an
410 containerId = record.get_pointer()
412 # add people who are in the new list, but not the oldList
413 for personId in newIdList:
414 if not (personId in oldIdList):
415 print "adding id", personId, "to", record.get_name()
416 addFunc(self.plauth, personId, containerId)
418 # remove people who are in the old list, but not the new list
419 for personId in oldIdList:
420 if not (personId in newIdList):
421 print "removing id", personId, "from", record.get_name()
422 delFunc(self.plauth, personId, containerId)
424 def update_membership(self, oldRecord, record):
425 if record.type == "slice":
426 self.update_membership_list(oldRecord, record, 'researcher',
427 self.plshell.AddPersonToSlice,
428 self.plshell.DeletePersonFromSlice)
429 elif record.type == "sa":
432 elif record.type == "ma":
437 def callable(self, method):
439 Return a new instance of the specified method.
442 if method not in self.methods:
443 raise GeniInvalidAPIMethod, method
445 # Get new instance of method
447 classname = method.split(".")[-1]
448 module = __import__("geni.methods." + method, globals(), locals(), [classname])
449 callablemethod = getattr(module, classname)(self)
450 return getattr(module, classname)(self)
451 except ImportError, AttributeError:
453 raise GeniInvalidAPIMethod, method
455 def call(self, source, method, *args):
457 Call the named method from the specified source with the
460 function = self.callable(method)
461 function.source = source
462 return function(*args)
464 def handle(self, source, data):
466 Handle an XML-RPC or SOAP request from the specified source.
469 # Parse request into method name and arguments
471 interface = xmlrpclib
472 (args, method) = xmlrpclib.loads(data)
473 methodresponse = True
475 if SOAPpy is not None:
477 (r, header, body, attrs) = parseSOAPRPC(data, header = 1, body = 1, attrs = 1)
480 # XXX Support named arguments
485 result = self.call(source, method, *args)
486 except Exception, fault:
487 traceback.print_exc(file = log)
488 # Handle expected faults
489 if interface == xmlrpclib:
491 methodresponse = None
492 elif interface == SOAPpy:
493 result = faultParameter(NS.ENV_T + ":Server", "Method Failed", method)
494 result._setDetail("Fault %d: %s" % (fault.faultCode, fault.faultString))
497 if interface == xmlrpclib:
498 if not isinstance(result, GeniFault):
501 data = xmlrpclib.dumps(result, methodresponse = True, encoding = self.encoding, allow_none = 1)
502 elif interface == SOAPpy:
503 data = buildSOAP(kw = {'%sResponse' % method: {'Result': result}}, encoding = self.encoding)